From 4702a9d91828348842108c27e90077f881b9cc62 Mon Sep 17 00:00:00 2001 From: fullwall Date: Sat, 5 Jan 2013 23:48:47 +0800 Subject: [PATCH] Switch to new methods in TraitInfo --- .../net/citizensnpcs/npc/CitizensNPC.java | 2 +- .../npc/CitizensTraitFactory.java | 39 +++++++++---------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index 2cf3ba5b3..53a117f10 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -199,7 +199,7 @@ public class CitizensNPC extends AbstractNPC { } entityController.spawn(at, this); - EntityLiving mcEntity = getHandle(); + EntityLiving mcEntity = ((CraftLivingEntity) getBukkitEntity()).getHandle(); boolean couldSpawn = !Util.isLoaded(at) ? false : mcEntity.world.addEntity(mcEntity, SpawnReason.CUSTOM); if (!couldSpawn) { Messaging.debug("Retrying spawn of", getId(), "later due to chunk being unloaded."); diff --git a/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java b/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java index e405ad1d2..dac518b3f 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java @@ -40,7 +40,7 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; public class CitizensTraitFactory implements TraitFactory { - private final Map> registered = Maps.newHashMap(); + private final Map registered = Maps.newHashMap(); public CitizensTraitFactory() { registerTrait(TraitInfo.create(Age.class).withName("age")); @@ -68,15 +68,16 @@ public class CitizensTraitFactory implements TraitFactory { registerTrait(TraitInfo.create(WoolColor.class).withName("woolcolor")); registerTrait(TraitInfo.create(ZombieModifier.class).withName("zombiemodifier")); - for (String trait : registered.keySet()) + for (String trait : registered.keySet()) { INTERNAL_TRAITS.add(trait); + } } public void addPlotters(Graph graph) { - for (Map.Entry> entry : registered.entrySet()) { + for (Map.Entry entry : registered.entrySet()) { if (INTERNAL_TRAITS.contains(entry.getKey())) continue; - final Class traitClass = entry.getValue(); + final Class traitClass = entry.getValue().getTraitClass(); graph.addPlotter(new Metrics.Plotter(entry.getKey()) { @Override public int getValue() { @@ -91,34 +92,32 @@ public class CitizensTraitFactory implements TraitFactory { } } - private T create(Class trait) { - try { - return trait.newInstance(); - } catch (Exception ex) { - ex.printStackTrace(); - return null; - } + private T create(TraitInfo info) { + return info.tryCreateInstance(); } @Override public T getTrait(Class clazz) { - if (!registered.containsValue(clazz)) - return null; - return create(clazz); + for (TraitInfo entry : registered.values()) { + if (clazz == entry.getTraitClass()) + return create(entry); + } + return null; } @Override @SuppressWarnings("unchecked") public T getTrait(String name) { - Class clazz = registered.get(name); - if (clazz == null) + TraitInfo info = registered.get(name); + if (info == null) return null; - return (T) create(clazz); + return (T) create(info); } @Override public Class getTraitClass(String name) { - return registered.get(name.toLowerCase()); + TraitInfo info = registered.get(name.toLowerCase()); + return info == null ? null : info.getTraitClass(); } @Override @@ -129,9 +128,9 @@ public class CitizensTraitFactory implements TraitFactory { @Override public void registerTrait(TraitInfo info) { Preconditions.checkNotNull(info, "info cannot be null"); - if (registered.containsKey(info)) + if (registered.containsKey(info.getTraitName())) throw new IllegalArgumentException("trait name already registered"); - registered.put(info.getTraitName(), info.getTraitClass()); + registered.put(info.getTraitName(), info); } private static final Set INTERNAL_TRAITS = Sets.newHashSet();