diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index 62551e8db..91cc8b203 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -118,7 +118,7 @@ public class Citizens extends JavaPlugin { @Override public void onDisable() { - restoreOldClassLoader(); + tearDownScripting(); // Don't bother with this part if MC versions are not compatible if (compatible) { save(); @@ -145,7 +145,6 @@ public class Citizens extends JavaPlugin { return; } registerScriptHelpers(); - replaceClassLoader(); config = new Settings(getDataFolder()); @@ -176,6 +175,10 @@ public class Citizens extends JavaPlugin { } // Run metrics last + startMetrics(); + } + + private void startMetrics() { new Thread() { @Override public void run() { @@ -233,6 +236,7 @@ public class Citizens extends JavaPlugin { } private void registerScriptHelpers() { + setupScripting(); ScriptCompiler compiler = CitizensAPI.getScriptCompiler(); compiler.registerGlobalContextProvider(new EventRegistrar(this)); compiler.registerGlobalContextProvider(new PluginProvider(this)); @@ -247,13 +251,13 @@ public class Citizens extends JavaPlugin { getServer().getPluginManager().callEvent(new CitizensReloadEvent()); } - private void replaceClassLoader() { + private void setupScripting() { contextClassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(getClassLoader()); // workaround to fix scripts not loading plugin classes properly } - private void restoreOldClassLoader() { + private void tearDownScripting() { Thread.currentThread().setContextClassLoader(contextClassLoader); } diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index 2bef6d0d8..a49ca08ed 100644 --- a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java @@ -17,7 +17,6 @@ import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.Requirements; import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.NoPermissionsException; -import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.npc.CitizensTraitManager; import net.citizensnpcs.trait.Age; @@ -100,7 +99,7 @@ public class NPCCommands { npc.removeTrait(Controllable.class); Messaging.send(player, StringHelper.wrap(npc.getName()) + " can no longer be controlled."); } else { - npc.addTrait(new Controllable((CitizensNPC) npc)); + npc.addTrait(traitManager.getTrait(Controllable.class, npc)); Messaging.send(player, StringHelper.wrap(npc.getName()) + " can now be controlled."); } diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index f515d4970..74eed03fb 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -42,6 +42,8 @@ public abstract class CitizensNPC extends AbstractNPC { @Override public void addTrait(Trait trait) { + // TODO: right now every addTrait call has to be wrapped with + // TraitManager.getTrait(Class, NPC) -- this is bad, need to fix this. if (trait == null) { Bukkit.getLogger().log(Level.SEVERE, "Cannot register a null trait. Was it registered properly?"); return; @@ -52,8 +54,9 @@ public abstract class CitizensNPC extends AbstractNPC { if (traits.containsKey(trait.getClass())) runnables.remove(traits.get(trait.getClass())); } - if (trait instanceof Listener) + if (trait instanceof Listener) { Bukkit.getPluginManager().registerEvents((Listener) trait, trait.getPlugin()); + } Map, Trait> map = traits.get(trait.getPlugin()); if (map == null)