use of net.citizensnpcs.api.ai.Navigator in project Denizen-For-Bukkit by DenizenScript.
the class WalkCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Fetch required objects
dLocation loc = (dLocation) scriptEntry.getObject("location");
Element speed = scriptEntry.getElement("speed");
Element auto_range = scriptEntry.getElement("auto_range");
Element radius = scriptEntry.getElement("radius");
Element stop = scriptEntry.getElement("stop");
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
final dLocation lookat = scriptEntry.getdObject("lookat");
// Debug the execution
dB.report(scriptEntry, getName(), (loc != null ? loc.debug() : "") + (speed != null ? speed.debug() : "") + (auto_range != null ? auto_range.debug() : "") + (radius != null ? radius.debug() : "") + (lookat != null ? lookat.debug() : "") + stop.debug() + (aH.debugObj("entities", entities)));
// Do the execution
boolean shouldStop = stop.asBoolean();
List<dNPC> npcs = new ArrayList<dNPC>();
final List<dEntity> waitForEntities = new ArrayList<dEntity>();
for (final dEntity entity : entities) {
if (entity.isCitizensNPC()) {
dNPC npc = entity.getDenizenNPC();
npcs.add(npc);
if (!npc.isSpawned()) {
dB.echoError(scriptEntry.getResidingQueue(), "NPC " + npc.identify() + " is not spawned!");
continue;
}
if (shouldStop) {
npc.getNavigator().cancelNavigation();
continue;
}
if (auto_range != null && auto_range == Element.TRUE) {
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(new Function<Navigator, Location>() {
@Override
public Location apply(Navigator nav) {
return 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.getInstance().getEntityHelper().stopWalking(entity.getBukkitEntity());
} else {
waitForEntities.add(entity);
NMSHandler.getInstance().getEntityHelper().walkTo(entity.getBukkitEntity(), loc, speed != null ? speed.asDouble() : 0.2, new Runnable() {
@Override
public void run() {
checkHeld(entity);
}
});
}
}
if (scriptEntry.shouldWaitFor()) {
held.add(scriptEntry);
if (!npcs.isEmpty()) {
scriptEntry.addObject("tally", npcs);
}
if (!waitForEntities.isEmpty()) {
scriptEntry.addObject("entities", waitForEntities);
}
}
}
use of net.citizensnpcs.api.ai.Navigator in project Denizen-For-Bukkit by DenizenScript.
the class AttackCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
dEntity target = (dEntity) scriptEntry.getObject("target");
boolean cancel = scriptEntry.hasObject("cancel");
// Report to dB
dB.report(scriptEntry, getName(), (cancel ? aH.debugObj("cancel", "true") : "") + aH.debugObj("entities", entities.toString()) + (target != null ? aH.debugObj("target", target) : ""));
for (dEntity entity : entities) {
if (entity.isCitizensNPC()) {
Navigator nav = entity.getDenizenNPC().getCitizen().getNavigator();
if (!cancel) {
nav.setTarget(target.getBukkitEntity(), true);
} else {
// Only cancel navigation if the NPC is attacking something
if (nav.isNavigating() && nav.getTargetType().equals(TargetType.ENTITY) && nav.getEntityTarget().isAggressive()) {
nav.cancelNavigation();
}
}
} else {
if (!cancel) {
entity.target(target.getLivingEntity());
} else {
entity.target(null);
}
}
}
}
Aggregations