moved NPC rotation to LookClose trait, fix name color saving

This commit is contained in:
aPunch 2012-02-07 04:37:12 -06:00
parent 3c1f9fd8c0
commit 455e49b4eb
6 changed files with 40 additions and 46 deletions

Binary file not shown.

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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];
}

View File

@ -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;

View File

@ -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 + "}";