diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index 4933e9cbb..48c2fba1d 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -28,10 +28,12 @@ public abstract class CitizensNPC extends AbstractNPC { protected final CitizensAI ai = new CitizensAI(this); protected final CitizensNPCManager manager; protected EntityLiving mcEntity; + private final CitizensTraitManager traitManager; protected CitizensNPC(CitizensNPCManager manager, int id, String name) { super(id, name); this.manager = manager; + traitManager = (CitizensTraitManager) CitizensAPI.getTraitManager(); } @Override @@ -83,6 +85,15 @@ public abstract class CitizensNPC extends AbstractNPC { return inventory; } + @Override + public T getTrait(Class clazz) { + Trait t = traits.get(clazz); + if (t == null) + addTrait(traitManager.getTrait(clazz, this)); + + return traits.get(clazz) != null ? clazz.cast(traits.get(clazz)) : null; + } + @Override public boolean isSpawned() { return getHandle() != null; @@ -139,7 +150,7 @@ public abstract class CitizensNPC extends AbstractNPC { // Load traits for (DataKey traitKey : root.getRelative("traits").getSubKeys()) { - Trait trait = CitizensAPI.getTraitManager().getTrait(traitKey.name(), this); + Trait trait = traitManager.getTrait(traitKey.name(), this); if (trait == null) throw new NPCLoadException("No trait with the name '" + traitKey.name() + "' exists. Was it registered properly?"); diff --git a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java index f48c1d1ac..5bab8e6cc 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java @@ -35,22 +35,6 @@ public class CitizensTraitManager implements TraitManager { registerTrait(new TraitFactory(Waypoints.class).withName("waypoints")); } - @SuppressWarnings("unchecked") - @Override - public T getTrait(String name, NPC npc) { - if (!registered.containsKey(name)) - return null; - Trait t = getTrait(registered.get(name), npc); - try { - if (t.getName() == null) - t.setName(name); - return (T) t; - } catch (TraitException ex) { - ex.printStackTrace(); - } - return null; - } - @Override public T getTrait(Class clazz) { return getTrait(clazz, null); @@ -58,6 +42,18 @@ public class CitizensTraitManager implements TraitManager { @SuppressWarnings("unchecked") @Override + public T getTrait(String name) { + if (!registered.containsKey(name)) + return null; + return (T) create(registered.get(name), null); + } + + @Override + public void registerTrait(TraitFactory factory) { + registered.put(factory.getName(), factory.getTraitClass()); + } + + @SuppressWarnings("unchecked") public T getTrait(Class clazz, NPC npc) { for (String name : registered.keySet()) if (registered.get(name).equals(clazz)) { @@ -74,16 +70,18 @@ public class CitizensTraitManager implements TraitManager { } @SuppressWarnings("unchecked") - @Override - public T getTrait(String name) { + public T getTrait(String name, NPC npc) { if (!registered.containsKey(name)) return null; - return (T) create(registered.get(name), null); - } - - @Override - public void registerTrait(TraitFactory factory) { - registered.put(factory.getName(), factory.getTraitClass()); + Trait t = getTrait(registered.get(name), npc); + try { + if (t.getName() == null) + t.setName(name); + return (T) t; + } catch (TraitException ex) { + ex.printStackTrace(); + } + return null; } @SuppressWarnings("unchecked") diff --git a/src/main/java/net/citizensnpcs/npc/ai/NavigationStrategy.java b/src/main/java/net/citizensnpcs/npc/ai/NavigationStrategy.java index 36030de21..76439f159 100644 --- a/src/main/java/net/citizensnpcs/npc/ai/NavigationStrategy.java +++ b/src/main/java/net/citizensnpcs/npc/ai/NavigationStrategy.java @@ -23,7 +23,7 @@ public class NavigationStrategy implements PathStrategy { NavigationStrategy(EntityLiving entity, EntityLiving target) { if (entity instanceof EntityHumanNPC) - entity = entity; + this.entity = (EntityHumanNPC) entity; navigation = entity.ak(); navigation.a(target, getSpeed(entity)); }