Add an effect

This commit is contained in:
fullwall 2012-12-09 00:39:07 +08:00
parent e998d2a78c
commit 338662936f
8 changed files with 56 additions and 54 deletions

View File

@ -83,14 +83,14 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent event) { public void onChunkUnload(ChunkUnloadEvent event) {
ChunkCoord coord = toCoord(event.getChunk()); ChunkCoord coord = toCoord(event.getChunk());
Location cachedLocation = new Location(null, 0, 0, 0); Location location = new Location(null, 0, 0, 0);
for (NPC npc : npcRegistry) { for (NPC npc : npcRegistry) {
if (!npc.isSpawned()) if (!npc.isSpawned())
continue; continue;
cachedLocation = npc.getBukkitEntity().getLocation(cachedLocation); location = npc.getBukkitEntity().getLocation(location);
boolean sameChunkCoordinates = coord.z == cachedLocation.getBlockZ() >> 4 boolean sameChunkCoordinates = coord.z == location.getBlockZ() >> 4
&& coord.x == cachedLocation.getBlockX() >> 4; && coord.x == location.getBlockX() >> 4;
if (sameChunkCoordinates && event.getWorld().equals(cachedLocation.getWorld())) { if (sameChunkCoordinates && event.getWorld().equals(location.getWorld())) {
npc.despawn(DespawnReason.CHUNK_UNLOAD); npc.despawn(DespawnReason.CHUNK_UNLOAD);
toRespawn.put(coord, npc.getId()); toRespawn.put(coord, npc.getId());
Messaging.debug("Despawned", npc.getId(), "due to chunk unload at [" + coord.x + "," Messaging.debug("Despawned", npc.getId(), "due to chunk unload at [" + coord.x + ","

View File

@ -8,36 +8,38 @@ import net.citizensnpcs.api.astar.pathfinder.ChunkBlockSource;
import net.citizensnpcs.api.astar.pathfinder.Path; import net.citizensnpcs.api.astar.pathfinder.Path;
import net.citizensnpcs.api.astar.pathfinder.VectorGoal; import net.citizensnpcs.api.astar.pathfinder.VectorGoal;
import net.citizensnpcs.api.astar.pathfinder.VectorNode; import net.citizensnpcs.api.astar.pathfinder.VectorNode;
import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.NMS;
import org.bukkit.Effect;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
public class AStarNavigationStrategy extends AbstractPathStrategy { public class AStarNavigationStrategy extends AbstractPathStrategy {
private final Location dest; private final Location destination;
private final CitizensNPC npc; private final NPC npc;
private final NavigatorParameters params; private final NavigatorParameters params;
private Path plan; private Path plan;
private Vector vector; private Vector vector;
AStarNavigationStrategy(CitizensNPC npc, Location dest, NavigatorParameters params) { AStarNavigationStrategy(NPC npc, Location dest, NavigatorParameters params) {
super(TargetType.LOCATION); super(TargetType.LOCATION);
this.params = params; this.params = params;
this.dest = dest; this.destination = dest;
this.npc = npc; this.npc = npc;
Location location = npc.getBukkitEntity().getEyeLocation(); Location location = npc.getBukkitEntity().getEyeLocation();
plan = (Path) ASTAR.runFully(new VectorGoal(dest), new VectorNode(location, new ChunkBlockSource( plan = (Path) ASTAR.runFully(new VectorGoal(dest), new VectorNode(location, new ChunkBlockSource(
location, params.range()), params.examiners()), (int) (params.range() * 10)); location, params.range()), params.examiners()), (int) (params.range() * 10));
if (plan == null || plan.isComplete()) if (plan == null || plan.isComplete()) {
setCancelReason(CancelReason.STUCK); setCancelReason(CancelReason.STUCK);
else } else {
vector = plan.getCurrentVector(); vector = plan.getCurrentVector();
}
} }
@Override @Override
public Location getTargetAsLocation() { public Location getTargetAsLocation() {
return dest; return destination;
} }
@Override @Override
@ -47,19 +49,24 @@ public class AStarNavigationStrategy extends AbstractPathStrategy {
@Override @Override
public boolean update() { public boolean update() {
if (getCancelReason() != null) if (getCancelReason() != null || plan == null || plan.isComplete())
return true; return true;
if (plan == null || plan.isComplete()) if (npc.getBukkitEntity().getLocation(NPC_LOCATION).toVector().distanceSquared(vector) <= params
return true; .distanceMargin()) {
if (npc.getBukkitEntity().getVelocity().distanceSquared(vector) <= params.distanceMargin()) {
plan.update(npc); plan.update(npc);
if (plan.isComplete()) if (plan.isComplete())
return true; return true;
vector = plan.getCurrentVector(); vector = plan.getCurrentVector();
npc.getBukkitEntity()
.getWorld()
.playEffect(vector.toLocation(npc.getBukkitEntity().getWorld()), Effect.STEP_SOUND,
org.bukkit.Material.STONE.getId());
} }
NMS.setDestination(npc.getBukkitEntity(), vector.getX(), vector.getY(), vector.getZ(), params.speed()); NMS.setDestination(npc.getBukkitEntity(), vector.getX(), vector.getY(), vector.getZ(), params.speed());
return false; return false;
} }
private static final AStarMachine ASTAR = AStarMachine.createWithDefaultStorage(); private static final AStarMachine ASTAR = AStarMachine.createWithDefaultStorage();
}
private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
}

View File

@ -29,4 +29,4 @@ public abstract class AbstractPathStrategy implements PathStrategy {
protected void setCancelReason(CancelReason reason) { protected void setCancelReason(CancelReason reason) {
cancelReason = reason; cancelReason = reason;
} }
} }

View File

@ -15,7 +15,6 @@ import net.citizensnpcs.api.ai.event.NavigationReplaceEvent;
import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer; import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.NMS;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -31,12 +30,11 @@ public class CitizensNavigator implements Navigator, Runnable {
private PathStrategy executing; private PathStrategy executing;
private int lastX, lastY, lastZ; private int lastX, lastY, lastZ;
private NavigatorParameters localParams = defaultParams; private NavigatorParameters localParams = defaultParams;
private final CitizensNPC npc; private final NPC npc;
private final Location stationaryLocation = new Location(null, 0, 0, 0);
private int stationaryTicks; private int stationaryTicks;
public CitizensNavigator(CitizensNPC npc) { public CitizensNavigator(NPC npc) {
this.npc = npc; this.npc = npc;
} }
@ -213,19 +211,21 @@ public class CitizensNavigator implements Navigator, Runnable {
private boolean updateStationaryStatus() { private boolean updateStationaryStatus() {
if (localParams.stationaryTicks() < 0) if (localParams.stationaryTicks() < 0)
return false; return false;
npc.getBukkitEntity().getLocation(stationaryLocation); Location current = npc.getBukkitEntity().getLocation(STATIONARY_LOCATION);
if (lastX == stationaryLocation.getBlockX() && lastY == stationaryLocation.getBlockY() if (lastX == current.getBlockX() && lastY == current.getBlockY() && lastZ == current.getBlockZ()) {
&& lastZ == stationaryLocation.getBlockZ()) {
if (++stationaryTicks >= localParams.stationaryTicks()) { if (++stationaryTicks >= localParams.stationaryTicks()) {
stopNavigating(CancelReason.STUCK); stopNavigating(CancelReason.STUCK);
return true; return true;
} }
} else } else
stationaryTicks = 0; stationaryTicks = 0;
lastX = stationaryLocation.getBlockX(); lastX = current.getBlockX();
lastY = stationaryLocation.getBlockY(); lastY = current.getBlockY();
lastZ = stationaryLocation.getBlockZ(); lastZ = current.getBlockZ();
return false; return false;
} }
private static final Location STATIONARY_LOCATION = new Location(null, 0, 0, 0);
private static int UNINITIALISED_SPEED = Integer.MIN_VALUE; private static int UNINITIALISED_SPEED = Integer.MIN_VALUE;
} }

View File

@ -22,12 +22,8 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
private CancelReason cancelReason; private CancelReason cancelReason;
private final EntityLiving handle, target; private final EntityLiving handle, target;
private final Navigation navigation; private final Navigation navigation;
private final Location npcLocation = new Location(null, 0, 0, 0);
private final NavigatorParameters parameters; private final NavigatorParameters parameters;
private final Location targetLocation = new Location(null, 0, 0, 0);
public MCTargetStrategy(NPC handle, LivingEntity target, boolean aggro, NavigatorParameters params) { public MCTargetStrategy(NPC handle, LivingEntity target, boolean aggro, NavigatorParameters params) {
this.handle = ((CraftLivingEntity) handle.getBukkitEntity()).getHandle(); this.handle = ((CraftLivingEntity) handle.getBukkitEntity()).getHandle();
this.target = ((CraftLivingEntity) target).getHandle(); this.target = ((CraftLivingEntity) target).getHandle();
@ -42,16 +38,16 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
&& (handle.boundingBox.e > target.boundingBox.b && handle.boundingBox.b < target.boundingBox.e) && (handle.boundingBox.e > target.boundingBox.b && handle.boundingBox.b < target.boundingBox.e)
&& distanceSquared() <= ATTACK_DISTANCE && hasLineOfSight(); && distanceSquared() <= ATTACK_DISTANCE && hasLineOfSight();
} }
@Override @Override
public void clearCancelReason() { public void clearCancelReason() {
cancelReason = null; cancelReason = null;
} }
private double distanceSquared() { private double distanceSquared() {
return handle.getBukkitEntity().getLocation(npcLocation) return handle.getBukkitEntity().getLocation(HANDLE_LOCATION)
.distanceSquared(target.getBukkitEntity().getLocation(targetLocation)); .distanceSquared(target.getBukkitEntity().getLocation(TARGET_LOCATION));
} }
@Override @Override
public CancelReason getCancelReason() { public CancelReason getCancelReason() {
return cancelReason; return cancelReason;
@ -120,5 +116,9 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
} }
private static final int ATTACK_DELAY_TICKS = 20; private static final int ATTACK_DELAY_TICKS = 20;
private static final double ATTACK_DISTANCE = 1.75 * 1.75; private static final double ATTACK_DISTANCE = 1.75 * 1.75;
private static final Location HANDLE_LOCATION = new Location(null, 0, 0, 0);
private static final Location TARGET_LOCATION = new Location(null, 0, 0, 0);
} }

View File

@ -32,9 +32,7 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
public class EntityHumanNPC extends EntityPlayer implements NPCHolder { public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
private final Location loadedLocation = new Location(null, 0, 0, 0);
private final CitizensNPC npc; private final CitizensNPC npc;
private final net.minecraft.server.v1_4_5.ItemStack[] previousEquipment = { null, null, null, null, null }; private final net.minecraft.server.v1_4_5.ItemStack[] previousEquipment = { null, null, null, null, null };
public EntityHumanNPC(MinecraftServer minecraftServer, World world, String string, public EntityHumanNPC(MinecraftServer minecraftServer, World world, String string,
@ -128,7 +126,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
if (npc == null) if (npc == null)
return; return;
if (getBukkitEntity() != null && Util.isLoaded(getBukkitEntity().getLocation(loadedLocation))) { if (getBukkitEntity() != null && Util.isLoaded(getBukkitEntity().getLocation(LOADED_LOCATION))) {
if (!npc.getNavigator().isNavigating() && !NMS.inWater(this)) if (!npc.getNavigator().isNavigating() && !NMS.inWater(this))
move(0, -0.2, 0); move(0, -0.2, 0);
// gravity. also works around an entity.onGround not updating issue // gravity. also works around an entity.onGround not updating issue
@ -227,4 +225,6 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder {
} }
private static final float EPSILON = 0.005F; private static final float EPSILON = 0.005F;
private static final Location LOADED_LOCATION = new Location(null, 0, 0, 0);
} }

View File

@ -21,7 +21,6 @@ import org.bukkit.entity.Player;
public class LookClose extends Trait implements Toggleable, CommandConfigurable { public class LookClose extends Trait implements Toggleable, CommandConfigurable {
private boolean enabled = Setting.DEFAULT_LOOK_CLOSE.asBoolean(); private boolean enabled = Setting.DEFAULT_LOOK_CLOSE.asBoolean();
private Player lookingAt; private Player lookingAt;
private final Location npcLocation = new Location(null, 0, 0, 0);
private double range = Setting.DEFAULT_LOOK_CLOSE_RANGE.asDouble(); private double range = Setting.DEFAULT_LOOK_CLOSE_RANGE.asDouble();
private boolean realisticLooking = Setting.DEFAULT_REALISTIC_LOOKING.asBoolean(); private boolean realisticLooking = Setting.DEFAULT_REALISTIC_LOOKING.asBoolean();
@ -43,7 +42,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
private void findNewTarget() { private void findNewTarget() {
List<Entity> nearby = npc.getBukkitEntity().getNearbyEntities(range, range, range); List<Entity> nearby = npc.getBukkitEntity().getNearbyEntities(range, range, range);
npc.getBukkitEntity().getLocation(npcLocation); final Location npcLocation = npc.getBukkitEntity().getLocation(NPC_LOCATION);
Collections.sort(nearby, new Comparator<Entity>() { Collections.sort(nearby, new Comparator<Entity>() {
@Override @Override
public int compare(Entity o1, Entity o2) { public int compare(Entity o1, Entity o2) {
@ -125,4 +124,6 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
public String toString() { public String toString() {
return "LookClose{" + enabled + "}"; return "LookClose{" + enabled + "}";
} }
private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
} }

View File

@ -22,14 +22,13 @@ import org.bukkit.util.Vector;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
public class Util { public class Util {
// Static class for small (emphasis small) utility methods // Static class for small (emphasis small) utility methods
private Util() { private Util() {
} }
private static final Location atLocation = new Location(null, 0, 0, 0); private static final Location AT_LOCATION = new Location(null, 0, 0, 0);
private static final Location fromLocation = new Location(null, 0, 0, 0); private static final Location FROM_LOCATION = new Location(null, 0, 0, 0);
private static Class<?> RNG_CLASS = null; private static Class<?> RNG_CLASS = null;
@ -42,7 +41,6 @@ public class Util {
if (NPCCollisionEvent.getHandlerList().getRegisteredListeners().length > 0) if (NPCCollisionEvent.getHandlerList().getRegisteredListeners().length > 0)
Bukkit.getPluginManager().callEvent(new NPCCollisionEvent(npc, entity.getBukkitEntity())); Bukkit.getPluginManager().callEvent(new NPCCollisionEvent(npc, entity.getBukkitEntity()));
} }
public static NPCPushEvent callPushEvent(NPC npc, Vector vector) { public static NPCPushEvent callPushEvent(NPC npc, Vector vector) {
NPCPushEvent event = new NPCPushEvent(npc, vector); NPCPushEvent event = new NPCPushEvent(npc, vector);
event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)); event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
@ -54,15 +52,11 @@ public class Util {
if (from.getWorld() != at.getWorld()) if (from.getWorld() != at.getWorld())
return; return;
double xDiff, yDiff, zDiff; double xDiff, yDiff, zDiff;
synchronized (fromLocation) { Location atLocation = at.getLocation(AT_LOCATION);
from.getLocation(fromLocation); Location fromLocation = from.getLocation(FROM_LOCATION);
synchronized (atLocation) { xDiff = atLocation.getX() - fromLocation.getX();
at.getLocation(atLocation); yDiff = atLocation.getY() - fromLocation.getY();
xDiff = atLocation.getX() - fromLocation.getX(); zDiff = atLocation.getZ() - fromLocation.getZ();
yDiff = atLocation.getY() - fromLocation.getY();
zDiff = atLocation.getZ() - fromLocation.getZ();
}
}
double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff); double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff); double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);