use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.
the class AnchorCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
Action action = (Action) scriptEntry.getObject("action");
LocationTag location = scriptEntry.getObjectTag("location");
ElementTag range = scriptEntry.getElement("range");
ElementTag id = scriptEntry.getElement("id");
NPCTag npc = Utilities.getEntryNPC(scriptEntry);
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), npc, db("action", action.name()), id, location, range);
}
Anchors anchors = npc.getCitizen().getOrAddTrait(Anchors.class);
switch(action) {
case ADD:
{
if (location == null) {
Debug.echoError("Must specify a location!");
return;
}
Anchor existing = anchors.getAnchor(id.asString());
if (existing != null) {
anchors.removeAnchor(existing);
}
anchors.addAnchor(id.asString(), location);
break;
}
case REMOVE:
{
Anchor n = anchors.getAnchor(id.asString());
if (n == null) {
Debug.echoError(scriptEntry, "Invalid anchor name '" + id.asString() + "'");
} else {
anchors.removeAnchor(n);
}
break;
}
}
}
use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.
the class WalkCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
LocationTag loc = scriptEntry.getObjectTag("location");
ElementTag speed = scriptEntry.getElement("speed");
ElementTag auto_range = scriptEntry.getElement("auto_range");
ElementTag radius = scriptEntry.getElement("radius");
ElementTag stop = scriptEntry.getElement("stop");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
final LocationTag lookat = scriptEntry.getObjectTag("lookat");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), loc, speed, auto_range, radius, lookat, stop, (db("entities", entities)));
}
boolean shouldStop = stop.asBoolean();
List<NPCTag> npcs = new ArrayList<>();
final List<EntityTag> waitForEntities = new ArrayList<>();
for (final EntityTag entity : entities) {
if (entity.isCitizensNPC()) {
NPCTag npc = entity.getDenizenNPC();
npcs.add(npc);
if (!npc.isSpawned()) {
Debug.echoError(scriptEntry, "NPC " + npc.identify() + " is not spawned!");
continue;
}
if (shouldStop) {
npc.getNavigator().cancelNavigation();
continue;
}
if (auto_range != null && auto_range.asBoolean()) {
double distance = npc.getLocation().distance(loc);
if (npc.getNavigator().getLocalParameters().range() < distance + 10) {
npc.getNavigator().getLocalParameters().range((float) distance + 10);
}
}
npc.getNavigator().setTarget(loc);
if (lookat != null) {
npc.getNavigator().getLocalParameters().lookAtFunction(nav -> lookat);
}
if (speed != null) {
npc.getNavigator().getLocalParameters().speedModifier(speed.asFloat());
}
if (radius != null) {
npc.getNavigator().getLocalParameters().addRunCallback(WalkCommandCitizensEvents.generateNewFlocker(npc.getCitizen(), radius.asDouble()));
}
} else if (shouldStop) {
NMSHandler.getEntityHelper().stopWalking(entity.getBukkitEntity());
} else {
waitForEntities.add(entity);
NMSHandler.getEntityHelper().walkTo(entity.getLivingEntity(), loc, speed != null ? speed.asDouble() : null, () -> checkHeld(entity));
}
}
if (scriptEntry.shouldWaitFor()) {
held.add(scriptEntry);
if (!npcs.isEmpty()) {
scriptEntry.addObject("tally", npcs);
}
if (!waitForEntities.isEmpty()) {
scriptEntry.addObject("entities", waitForEntities);
}
}
}
use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.
the class WalkCommand method checkHeld.
public void checkHeld(EntityTag entity) {
for (int i = 0; i < held.size(); i++) {
ScriptEntry entry = held.get(i);
List<EntityTag> waitForEntities = (List<EntityTag>) entry.getObject("entities");
if (waitForEntities == null) {
continue;
}
waitForEntities.remove(entity);
if (waitForEntities.isEmpty()) {
if (!entry.hasObject("tally") || ((List<NPCTag>) entry.getObject("tally")).isEmpty()) {
entry.setFinished(true);
held.remove(i);
i--;
}
}
}
}
use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.
the class AssignmentCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ScriptTag script = scriptEntry.getObjectTag("script");
Action action = (Action) scriptEntry.getObject("action");
List<NPCTag> npcs = (List<NPCTag>) scriptEntry.getObject("npcs");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("action", action), script, db("npc", npcs));
}
PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
for (NPCTag npc : npcs) {
switch(action) {
case SET:
{
if (script == null) {
throw new InvalidArgumentsRuntimeException("Missing script!");
}
AssignmentTrait assignment = npc.getCitizen().getOrAddTrait(AssignmentTrait.class);
assignment.clearAssignments(player);
assignment.addAssignmentScript((AssignmentScriptContainer) script.getContainer(), player);
break;
}
case ADD:
if (script == null) {
throw new InvalidArgumentsRuntimeException("Missing script!");
}
npc.getCitizen().getOrAddTrait(AssignmentTrait.class).addAssignmentScript((AssignmentScriptContainer) script.getContainer(), player);
break;
case REMOVE:
if (script == null) {
Deprecations.assignmentRemove.warn(scriptEntry);
if (npc.getCitizen().hasTrait(AssignmentTrait.class)) {
npc.getCitizen().getOrAddTrait(AssignmentTrait.class).clearAssignments(player);
npc.getCitizen().removeTrait(AssignmentTrait.class);
}
} else {
if (npc.getCitizen().hasTrait(AssignmentTrait.class)) {
AssignmentTrait trait = npc.getCitizen().getOrAddTrait(AssignmentTrait.class);
trait.removeAssignmentScript(script.getName(), player);
trait.checkAutoRemove();
}
}
break;
case CLEAR:
if (npc.getCitizen().hasTrait(AssignmentTrait.class)) {
npc.getCitizen().getOrAddTrait(AssignmentTrait.class).clearAssignments(player);
npc.getCitizen().removeTrait(AssignmentTrait.class);
}
break;
}
}
}
use of com.denizenscript.denizen.objects.NPCTag in project Denizen-For-Bukkit by DenizenScript.
the class CreateCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
ElementTag name = scriptEntry.getElement("name");
EntityTag type = scriptEntry.getObjectTag("entity_type");
LocationTag loc = scriptEntry.getObjectTag("spawn_location");
ListTag traits = scriptEntry.getObjectTag("traits");
ElementTag registry = scriptEntry.getElement("registry");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), name, type, loc, traits, registry);
}
NPCTag created;
if (!type.isGeneric() && type.isCitizensNPC()) {
created = new NPCTag(type.getDenizenNPC().getCitizen().clone());
created.getCitizen().setName(name.asString());
} else {
NPCRegistry actualRegistry = CitizensAPI.getNPCRegistry();
if (registry != null) {
actualRegistry = NPCTag.getRegistryByName(registry.asString());
if (actualRegistry == null) {
actualRegistry = CitizensAPI.createNamedNPCRegistry(registry.asString(), new MemoryNPCDataStore());
}
}
created = new NPCTag(actualRegistry.createNPC(type.getBukkitEntityType(), name.asString()));
}
// Add the created NPC into the script entry so it can be utilized if need be.
scriptEntry.addObject("created_npc", created);
if (created.isSpawned()) {
if (loc != null) {
created.getCitizen().teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
} else {
created.getCitizen().despawn();
}
} else {
if (loc != null) {
created.getCitizen().spawn(loc);
}
}
if (traits != null) {
for (String trait_name : traits) {
Trait trait = CitizensAPI.getTraitFactory().getTrait(trait_name);
if (trait != null) {
created.getCitizen().addTrait(trait);
} else {
Debug.echoError(scriptEntry, "Could not add trait to NPC: " + trait_name);
}
}
}
for (Mechanism mechanism : type.getWaitingMechanisms()) {
created.safeAdjust(mechanism);
}
}
Aggregations