diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index 3422fc0e9..b0cc7c85e 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -16,7 +16,6 @@ import net.citizensnpcs.npc.ai.CitizensAI; import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.StringHelper; - import net.minecraft.server.EntityLiving; import org.bukkit.Bukkit; @@ -26,8 +25,8 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; public abstract class CitizensNPC extends AbstractNPC { - protected final CitizensAI ai = new CitizensAI(this); - protected final CitizensNPCManager manager; + private final CitizensAI ai = new CitizensAI(this); + private final CitizensNPCManager manager; protected EntityLiving mcEntity; private final CitizensTraitManager traitManager; @@ -139,6 +138,7 @@ public abstract class CitizensNPC extends AbstractNPC { return true; } + @Override public void update() { super.update(); ai.update(); diff --git a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java index 39c8dd588..0efd91215 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensTraitManager.java @@ -3,8 +3,8 @@ package net.citizensnpcs.npc; import java.lang.reflect.Constructor; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; -import net.citizensnpcs.api.exception.TraitException; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.TraitFactory; @@ -20,6 +20,8 @@ import net.citizensnpcs.trait.Powered; import net.citizensnpcs.trait.text.Text; import net.citizensnpcs.trait.waypoint.Waypoints; +import com.google.common.collect.Maps; + public class CitizensTraitManager implements TraitManager { private final Map> registered = new HashMap>(); @@ -57,52 +59,54 @@ public class CitizensTraitManager implements TraitManager { @SuppressWarnings("unchecked") public T getTrait(Class clazz, NPC npc) { - for (String name : registered.keySet()) - if (registered.get(name).equals(clazz)) { - Trait t = create(registered.get(name), npc); - try { - if (t.getName() == null) - t.setName(name); - return (T) t; - } catch (TraitException ex) { - ex.printStackTrace(); - } - } + for (Entry> entry : registered.entrySet()) { + if (!entry.getValue().equals(clazz)) + continue; + Trait t = create(entry.getValue(), npc); + t.setName(entry.getKey()); + return (T) t; + } return null; } @SuppressWarnings("unchecked") public T getTrait(String name, NPC npc) { - if (!registered.containsKey(name)) + // TODO: we could replace NPC with Object... and search for the + // constructor + Class clazz = registered.get(name); + if (clazz == null) 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; + Trait t = getTrait(clazz, npc); + t.setName(name); + return (T) t; } @SuppressWarnings("unchecked") private T create(Class trait, NPC npc) { Constructor constructor; - try { - constructor = trait.getConstructor(NPC.class); - } catch (Exception ex) { - constructor = null; + if (!CACHED_CTORS.containsKey(trait)) { + try { + constructor = trait.getConstructor(NPC.class); + constructor.setAccessible(true); // do we want to allow private + // constructors? + } catch (Exception ex) { + constructor = null; + } + CACHED_CTORS.put(trait, constructor); + } else { + constructor = CACHED_CTORS.get(trait); } try { - if (npc == null) - return (T) trait.newInstance(); - return constructor != null ? (T) constructor.newInstance(npc) : (T) trait.newInstance(); + if (constructor == null || npc == null) + return trait.newInstance(); + return (T) constructor.newInstance(npc); } catch (Exception ex) { ex.printStackTrace(); return null; } } + + private final Map, Constructor> CACHED_CTORS = Maps.newHashMap(); } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/npc/ai/CitizensAI.java b/src/main/java/net/citizensnpcs/npc/ai/CitizensAI.java index d627ac2ff..e5a80ae58 100644 --- a/src/main/java/net/citizensnpcs/npc/ai/CitizensAI.java +++ b/src/main/java/net/citizensnpcs/npc/ai/CitizensAI.java @@ -8,7 +8,7 @@ import java.util.List; import net.citizensnpcs.api.ai.AI; import net.citizensnpcs.api.ai.Goal; import net.citizensnpcs.api.ai.NavigationCallback; -import net.citizensnpcs.api.ai.NavigationCallback.PathCancelReason; +import net.citizensnpcs.api.ai.NavigationCallback.CancelReason; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.util.Messaging; @@ -44,7 +44,7 @@ public class CitizensAI implements AI { Iterator> itr = callbacks.iterator(); while (itr.hasNext()) { NavigationCallback next = itr.next().get(); - if (next == null || next.onCancel(this, PathCancelReason.CANCEL)) { + if (next == null || next.onCancel(this, CancelReason.CANCEL)) { itr.remove(); } } @@ -104,7 +104,7 @@ public class CitizensAI implements AI { Iterator> itr = callbacks.iterator(); while (itr.hasNext()) { NavigationCallback next = itr.next().get(); - if (next == null || (replaced && next.onCancel(this, PathCancelReason.REPLACE)) || next.onBegin(this)) { + if (next == null || (replaced && next.onCancel(this, CancelReason.REPLACE)) || next.onBegin(this)) { itr.remove(); } } @@ -121,7 +121,7 @@ public class CitizensAI implements AI { Iterator> itr = callbacks.iterator(); while (itr.hasNext()) { NavigationCallback next = itr.next().get(); - if (next == null || (replaced && next.onCancel(this, PathCancelReason.REPLACE)) || next.onBegin(this)) { + if (next == null || (replaced && next.onCancel(this, CancelReason.REPLACE)) || next.onBegin(this)) { itr.remove(); } } diff --git a/src/main/java/net/citizensnpcs/trait/waypoint/GenericWaypointCallback.java b/src/main/java/net/citizensnpcs/trait/waypoint/GenericWaypointCallback.java index f7ba1ebd5..44a9f0889 100644 --- a/src/main/java/net/citizensnpcs/trait/waypoint/GenericWaypointCallback.java +++ b/src/main/java/net/citizensnpcs/trait/waypoint/GenericWaypointCallback.java @@ -8,8 +8,8 @@ import net.citizensnpcs.api.ai.NavigationCallback; import org.bukkit.Location; public class GenericWaypointCallback extends NavigationCallback { - private Location dest; private AI ai; + private Location dest; private boolean executing; private Iterator itr; private final Iterable provider; @@ -27,7 +27,7 @@ public class GenericWaypointCallback extends NavigationCallback { @Override public void onAttach(AI ai) { this.ai = ai; - executing = !ai.hasDestination(); + executing |= !ai.hasDestination(); if (!executing) return; if (dest == null) { @@ -42,17 +42,17 @@ public class GenericWaypointCallback extends NavigationCallback { } @Override - public boolean onCancel(AI ai, PathCancelReason reason) { - if (executing && reason == PathCancelReason.REPLACE) { + public boolean onCancel(AI ai, CancelReason reason) { + if (executing && reason == CancelReason.REPLACE) { executing = false; - } else { - executing = true; - ensureItr(); - if (dest != null) - ai.setDestination(dest); - else if (itr.hasNext()) { - ai.setDestination(itr.next().getLocation()); - } + return false; + } + executing = true; + ensureItr(); + if (dest == null && itr.hasNext()) + dest = itr.next().getLocation(); + if (dest != null) { + ai.setDestination(dest); } return false; } @@ -60,8 +60,7 @@ public class GenericWaypointCallback extends NavigationCallback { @Override public boolean onCompletion(AI ai) { if (executing) { // if we're executing, we need to get the next waypoint - if (!itr.hasNext()) - itr = provider.iterator(); + ensureItr(); dest = itr.hasNext() ? itr.next().getLocation() : null; } else { executing = true; diff --git a/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java b/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java index 4d044132d..6b5506846 100644 --- a/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java +++ b/src/main/java/net/citizensnpcs/trait/waypoint/LinearWaypointProvider.java @@ -50,8 +50,8 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable 0) { waypoints.remove(waypoints.size() - 1); - Messaging.send(player, String.format("Removed a waypoint (%d remaining)", waypoints - .size())); + Messaging.send(player, + String.format("Removed a waypoint (%d remaining)", waypoints.size())); } } }; @@ -80,16 +80,17 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable clazz : providers.keySet()) - if (providers.get(clazz).equals(providerName)) { - provider = create(clazz); + for (Entry, String> entry : providers.entrySet()) { + if (entry.getValue().equals(providerName)) { + provider = create(entry.getKey()); break; } + } if (provider == null) return; provider.load(key.getRelative(providerName));