diff --git a/lib/CitizensAPI.jar b/lib/CitizensAPI.jar index 41f4bc27b..95400e3fc 100644 Binary files a/lib/CitizensAPI.jar and b/lib/CitizensAPI.jar differ diff --git a/src/net/citizensnpcs/NPCUpdater.java b/src/net/citizensnpcs/NPCUpdater.java index 1138779d4..29149b54b 100644 --- a/src/net/citizensnpcs/NPCUpdater.java +++ b/src/net/citizensnpcs/NPCUpdater.java @@ -1,14 +1,8 @@ package net.citizensnpcs; -import org.bukkit.Location; -import org.bukkit.entity.Entity; - import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.CitizensNPCManager; -import net.citizensnpcs.trait.LookClose; - -import net.minecraft.server.EntityLiving; public class NPCUpdater implements Runnable { private final CitizensNPCManager npcManager; @@ -24,34 +18,6 @@ public class NPCUpdater implements Runnable { continue; CitizensNPC handle = (CitizensNPC) npc; handle.update(); - - // This needs to be handled somewhere...is this the best place? - EntityLiving search = null; - if ((search = handle.getHandle().world.findNearbyPlayer(handle.getHandle(), 5)) != null - && npc.getTrait(LookClose.class).shouldLookClose()) - faceEntity(handle, search.getBukkitEntity()); } } - - private void faceEntity(CitizensNPC npc, Entity target) { - if (npc.getBukkitEntity().getWorld() != target.getWorld()) - return; - Location loc = npc.getBukkitEntity().getLocation(); - - double xDiff = target.getLocation().getX() - loc.getX(); - double yDiff = target.getLocation().getY() - loc.getY(); - double zDiff = target.getLocation().getZ() - loc.getZ(); - - double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff); - double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff); - - double yaw = (Math.acos(xDiff / distanceXZ) * 180 / Math.PI); - double pitch = (Math.acos(yDiff / distanceY) * 180 / Math.PI) - 90; - if (zDiff < 0.0) { - yaw = yaw + (Math.abs(180 - yaw) * 2); - } - - npc.getHandle().yaw = (float) yaw - 90; - npc.getHandle().pitch = (float) pitch; - } } \ No newline at end of file diff --git a/src/net/citizensnpcs/npc/CitizensNPC.java b/src/net/citizensnpcs/npc/CitizensNPC.java index 42a236646..19992e9e8 100644 --- a/src/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/net/citizensnpcs/npc/CitizensNPC.java @@ -8,7 +8,7 @@ import net.citizensnpcs.api.npc.trait.trait.Spawned; import net.citizensnpcs.npc.ai.CitizensAI; import net.citizensnpcs.trait.Inventory; import net.citizensnpcs.util.Messaging; -import net.citizensnpcs.util.StringHelper; + import net.minecraft.server.EntityLiving; import org.bukkit.Bukkit; @@ -23,7 +23,7 @@ public abstract class CitizensNPC extends AbstractNPC { protected final NPCInventory inventory; protected CitizensNPC(CitizensNPCManager manager, int id, String name) { - super(id, StringHelper.parseColors(name)); + super(id, name); this.manager = manager; inventory = new NPCInventory(this); } diff --git a/src/net/citizensnpcs/npc/NPCInventory.java b/src/net/citizensnpcs/npc/NPCInventory.java index aa33493be..822537a1d 100644 --- a/src/net/citizensnpcs/npc/NPCInventory.java +++ b/src/net/citizensnpcs/npc/NPCInventory.java @@ -1,6 +1,7 @@ package net.citizensnpcs.npc; import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.util.StringHelper; import net.minecraft.server.EntityHuman; import net.minecraft.server.IInventory; import net.minecraft.server.ItemStack; @@ -20,7 +21,7 @@ public class NPCInventory implements IInventory { public NPCInventory(NPC npc) { this.npc = npc; - name = npc.getFullName(); + name = StringHelper.parseColors(npc.getFullName()); contents = new ItemStack[size]; } diff --git a/src/net/citizensnpcs/npc/entity/CitizensHumanNPC.java b/src/net/citizensnpcs/npc/entity/CitizensHumanNPC.java index 97d8e41a7..c0cefda96 100644 --- a/src/net/citizensnpcs/npc/entity/CitizensHumanNPC.java +++ b/src/net/citizensnpcs/npc/entity/CitizensHumanNPC.java @@ -3,6 +3,7 @@ package net.citizensnpcs.npc.entity; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.resource.lib.EntityHumanNPC; +import net.citizensnpcs.util.StringHelper; import net.minecraft.server.EntityLiving; import net.minecraft.server.ItemInWorldManager; import net.minecraft.server.WorldServer; @@ -12,6 +13,7 @@ import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.entity.Player; public class CitizensHumanNPC extends CitizensNPC { + public CitizensHumanNPC(CitizensNPCManager manager, int id, String name) { super(manager, id, name); } @@ -39,8 +41,8 @@ public class CitizensHumanNPC extends CitizensNPC { @Override protected EntityLiving createHandle(Location loc) { WorldServer ws = ((CraftWorld) loc.getWorld()).getHandle(); - EntityHumanNPC handle = new EntityHumanNPC(ws.getServer().getServer(), ws, getFullName(), - new ItemInWorldManager(ws)); + EntityHumanNPC handle = new EntityHumanNPC(ws.getServer().getServer(), ws, + StringHelper.parseColors(getFullName()), new ItemInWorldManager(ws)); handle.removeFromPlayerMap(getFullName()); handle.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); return handle; diff --git a/src/net/citizensnpcs/trait/LookClose.java b/src/net/citizensnpcs/trait/LookClose.java index 59a627364..a2875e3ed 100644 --- a/src/net/citizensnpcs/trait/LookClose.java +++ b/src/net/citizensnpcs/trait/LookClose.java @@ -1,10 +1,15 @@ package net.citizensnpcs.trait; +import org.bukkit.Location; +import org.bukkit.entity.Entity; + import net.citizensnpcs.api.DataKey; import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.trait.SaveId; import net.citizensnpcs.api.npc.trait.Trait; +import net.citizensnpcs.npc.CitizensNPC; +import net.minecraft.server.EntityLiving; @SaveId("look-close") public class LookClose implements Trait, Runnable { @@ -17,11 +22,7 @@ public class LookClose implements Trait, Runnable { @Override public void load(DataKey key) throws NPCLoadException { - try { - shouldLookClose = key.getBoolean(""); - } catch (Exception ex) { - throw new NPCLoadException("Invalid value. Valid values: true or false"); - } + shouldLookClose = key.getBoolean(""); } @Override @@ -31,8 +32,10 @@ public class LookClose implements Trait, Runnable { @Override public void run() { - // TODO Auto-generated method stub - + EntityLiving search = null; + CitizensNPC handle = (CitizensNPC) npc; + if ((search = handle.getHandle().world.findNearbyPlayer(handle.getHandle(), 5)) != null && shouldLookClose) + faceEntity(handle, search.getBukkitEntity()); } public void setLookClose(boolean shouldLookClose) { @@ -47,6 +50,28 @@ public class LookClose implements Trait, Runnable { shouldLookClose = !shouldLookClose; } + private void faceEntity(CitizensNPC npc, Entity target) { + if (npc.getBukkitEntity().getWorld() != target.getWorld()) + return; + Location loc = npc.getBukkitEntity().getLocation(); + + double xDiff = target.getLocation().getX() - loc.getX(); + double yDiff = target.getLocation().getY() - loc.getY(); + double zDiff = target.getLocation().getZ() - loc.getZ(); + + double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff); + double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff); + + double yaw = (Math.acos(xDiff / distanceXZ) * 180 / Math.PI); + double pitch = (Math.acos(yDiff / distanceY) * 180 / Math.PI) - 90; + if (zDiff < 0.0) { + yaw = yaw + (Math.abs(180 - yaw) * 2); + } + + npc.getHandle().yaw = (float) yaw - 90; + npc.getHandle().pitch = (float) pitch; + } + @Override public String toString() { return "LookClose{" + shouldLookClose + "}";