use of net.citizensnpcs.api.astar.pathfinder.FlyingBlockExaminer in project Citizens2 by CitizensDev.
the class FlyingAStarNavigationStrategy method update.
@Override
public boolean update() {
if (!planned) {
Location location = npc.getEntity().getLocation();
VectorGoal goal = new VectorGoal(target, (float) parameters.pathDistanceMargin());
boolean found = false;
for (BlockExaminer examiner : parameters.examiners()) {
if (examiner instanceof FlyingBlockExaminer) {
found = true;
break;
}
}
if (!found) {
parameters.examiner(new FlyingBlockExaminer());
}
setPlan(ASTAR.runFully(goal, new VectorNode(goal, location, new ChunkBlockSource(location, parameters.range()), parameters.examiners()), Setting.MAXIMUM_ASTAR_ITERATIONS.asInt()));
}
if (getCancelReason() != null || plan == null || plan.isComplete()) {
return true;
}
Location current = npc.getEntity().getLocation(NPC_LOCATION);
if (current.toVector().distanceSquared(vector) <= parameters.distanceMargin()) {
plan.update(npc);
if (plan.isComplete()) {
return true;
}
vector = plan.getCurrentVector();
}
if (parameters.debug()) {
npc.getEntity().getWorld().playEffect(vector.toLocation(npc.getEntity().getWorld()), Effect.ENDER_SIGNAL, 0);
}
double d0 = vector.getX() + 0.5D - current.getX();
double d1 = vector.getY() + 0.1D - current.getY();
double d2 = vector.getZ() + 0.5D - current.getZ();
Vector velocity = npc.getEntity().getVelocity();
double motX = velocity.getX(), motY = velocity.getY(), motZ = velocity.getZ();
motX += (Math.signum(d0) * 0.5D - motX) * 0.1;
motY += (Math.signum(d1) - motY) * 0.1;
motZ += (Math.signum(d2) * 0.5D - motZ) * 0.1;
float targetYaw = (float) (Math.atan2(motZ, motX) * 180.0D / Math.PI) - 90.0F;
float normalisedTargetYaw = (targetYaw - current.getYaw()) % 360;
if (normalisedTargetYaw >= 180.0F) {
normalisedTargetYaw -= 360.0F;
}
if (normalisedTargetYaw < -180.0F) {
normalisedTargetYaw += 360.0F;
}
velocity.setX(motX).setY(motY).setZ(motZ).multiply(parameters.speed());
npc.getEntity().setVelocity(velocity);
if (npc.getEntity().getType() != EntityType.ENDER_DRAGON) {
NMS.setVerticalMovement(npc.getEntity(), 0.5);
float newYaw = current.getYaw() + normalisedTargetYaw;
current.setYaw(newYaw);
NMS.setHeadYaw(npc.getEntity(), newYaw);
npc.teleport(current, TeleportCause.PLUGIN);
}
parameters.run();
plan.run(npc);
return false;
}
use of net.citizensnpcs.api.astar.pathfinder.FlyingBlockExaminer in project Denizen-For-Bukkit by DenizenScript.
the class dNPC method adjust.
@Override
public void adjust(Mechanism mechanism) {
Element value = mechanism.getValue();
// -->
if (mechanism.matches("set_assignment") && mechanism.requireObject(dScript.class)) {
getAssignmentTrait().setAssignment(value.asType(dScript.class).getName(), null);
}
// -->
if (mechanism.matches("remove_assignment")) {
getAssignmentTrait().removeAssignment(null);
}
// -->
if (mechanism.matches("set_nickname")) {
getNicknameTrait().setNickname(value.asString());
}
// -->
if (mechanism.matches("remove_nickname")) {
getNicknameTrait().removeNickname();
}
// -->
if (mechanism.matches("set_entity_type") && mechanism.requireObject(dEntity.class)) {
getCitizen().setBukkitEntityType(value.asType(dEntity.class).getBukkitEntityType());
}
// -->
if (mechanism.matches("name") || mechanism.matches("set_name")) {
getCitizen().setName(value.asString().length() > 64 ? value.asString().substring(0, 64) : value.asString());
}
// -->
if (mechanism.matches("owner")) {
getCitizen().getTrait(Owner.class).setOwner(value.asString());
}
// -->
if (mechanism.matches("skin_blob")) {
if (!mechanism.hasValue()) {
getCitizen().data().remove(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_METADATA);
getCitizen().data().remove(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_SIGN_METADATA);
} else {
String[] dat = mechanism.getValue().asString().split(";");
getCitizen().data().setPersistent(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_METADATA, dat[0]);
getCitizen().data().setPersistent(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_SIGN_METADATA, dat.length > 0 ? dat[1] : null);
}
if (getCitizen().isSpawned()) {
getCitizen().despawn(DespawnReason.PENDING_RESPAWN);
getCitizen().spawn(getCitizen().getStoredLocation());
}
}
// -->
if (mechanism.matches("skin")) {
if (!mechanism.hasValue()) {
getCitizen().data().remove(NPC.PLAYER_SKIN_UUID_METADATA);
} else {
getCitizen().data().setPersistent(NPC.PLAYER_SKIN_UUID_METADATA, mechanism.getValue().asString());
}
if (getCitizen().isSpawned()) {
getCitizen().despawn(DespawnReason.PENDING_RESPAWN);
getCitizen().spawn(getCitizen().getStoredLocation());
}
}
// -->
if (mechanism.matches("item_type") && mechanism.requireObject(dItem.class)) {
dItem item = mechanism.getValue().asType(dItem.class);
Material mat = item.getMaterial().getMaterial();
int data = item.getMaterial().getData((byte) 0);
switch(getEntity().getType()) {
case DROPPED_ITEM:
((org.bukkit.entity.Item) getEntity()).getItemStack().setType(mat);
//((ItemController.ItemNPC) getEntity()).setType(mat, data);
break;
case ITEM_FRAME:
((ItemFrame) getEntity()).getItem().setType(mat);
//((ItemFrameController.ItemFrameNPC) getEntity()).setType(mat, data);
break;
case FALLING_BLOCK:
//((FallingBlockController.FallingBlockNPC) getEntity()).setType(mat, data);
break;
default:
dB.echoError("NPC is the not an item type!");
break;
}
if (getCitizen().isSpawned()) {
getCitizen().despawn();
getCitizen().spawn(getCitizen().getStoredLocation());
}
}
// -->
if (mechanism.matches("spawn")) {
if (mechanism.requireObject("Invalid dLocation specified. Assuming last known NPC location.", dLocation.class)) {
getCitizen().spawn(value.asType(dLocation.class));
} else {
getCitizen().spawn(getCitizen().getStoredLocation());
}
}
// -->
if (mechanism.matches("range") && mechanism.requireFloat()) {
getCitizen().getNavigator().getDefaultParameters().range(mechanism.getValue().asFloat());
}
// -->
if (mechanism.matches("attack_range") && mechanism.requireFloat()) {
getCitizen().getNavigator().getDefaultParameters().attackRange(mechanism.getValue().asFloat());
}
// -->
if (mechanism.matches("speed") && mechanism.requireFloat()) {
getCitizen().getNavigator().getDefaultParameters().speedModifier(mechanism.getValue().asFloat());
}
// -->
if (mechanism.matches("despawn")) {
getCitizen().despawn(DespawnReason.PLUGIN);
}
// -->
if (mechanism.matches("set_protected") && mechanism.requireBoolean()) {
getCitizen().setProtected(value.asBoolean());
}
// -->
if (mechanism.matches("lookclose") && mechanism.requireBoolean()) {
getLookCloseTrait().lookClose(value.asBoolean());
}
// -->
if (mechanism.matches("set_examiner")) {
if (mechanism.getValue().toString().equalsIgnoreCase("default")) {
getNavigator().getLocalParameters().clearExaminers();
getNavigator().getLocalParameters().examiner(new MinecraftBlockExaminer());
} else if (mechanism.getValue().toString().equalsIgnoreCase("fly")) {
getNavigator().getLocalParameters().clearExaminers();
getNavigator().getLocalParameters().examiner(new FlyingBlockExaminer());
} else if (mechanism.getValue().toString().equalsIgnoreCase("path")) {
getNavigator().getLocalParameters().clearExaminers();
getNavigator().getLocalParameters().examiner(new PathBlockExaminer(this, null));
}
}
// -->
if (mechanism.matches("teleport_on_stuck") && mechanism.requireBoolean()) {
if (value.asBoolean()) {
getNavigator().getDefaultParameters().stuckAction(TeleportStuckAction.INSTANCE);
} else {
getNavigator().getDefaultParameters().stuckAction(null);
}
}
// -->
if (mechanism.matches("set_distance") && mechanism.requireDouble()) {
getNavigator().getDefaultParameters().distanceMargin(mechanism.getValue().asDouble());
}
// -->
if (mechanism.matches("add_waypoint") && mechanism.requireObject(dLocation.class)) {
if (!getCitizen().hasTrait(Waypoints.class)) {
getCitizen().addTrait(Waypoints.class);
}
Waypoints wp = getCitizen().getTrait(Waypoints.class);
if ((wp.getCurrentProvider() instanceof WaypointProvider.EnumerableWaypointProvider)) {
((List<Waypoint>) ((WaypointProvider.EnumerableWaypointProvider) wp.getCurrentProvider()).waypoints()).add(new Waypoint(value.asType(dLocation.class)));
}
}
// Iterate through this object's properties' mechanisms
for (Property property : PropertyParser.getProperties(this)) {
property.adjust(mechanism);
if (mechanism.fulfilled()) {
break;
}
}
// Pass along to dEntity mechanism handler if not already handled.
if (!mechanism.fulfilled()) {
if (isSpawned()) {
new dEntity(getEntity()).adjust(mechanism);
} else {
mechanism.reportInvalid();
}
}
}
Aggregations