Fix for controllables not creating the trait properly

This commit is contained in:
fullwall 2012-04-20 21:49:30 +08:00
parent 8cf64ddc73
commit 8225f3e57b
3 changed files with 13 additions and 7 deletions

View File

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

View File

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

View File

@ -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<Class<? extends Trait>, Trait> map = traits.get(trait.getPlugin());
if (map == null)