Minor cleanup

This commit is contained in:
fullwall 2012-04-18 10:12:17 +08:00
parent f993b128f7
commit 1048787305
2 changed files with 38 additions and 38 deletions

View File

@ -55,10 +55,10 @@ public class Citizens extends JavaPlugin {
private final CommandManager commands = new CommandManager(); private final CommandManager commands = new CommandManager();
private boolean compatible; private boolean compatible;
private Settings config; private Settings config;
private ClassLoader contextClassLoader;
private CitizensNPCManager npcManager; private CitizensNPCManager npcManager;
private Storage saves; private Storage saves;
private CitizensTraitManager traitManager; private CitizensTraitManager traitManager;
private ClassLoader contextClassLoader;
public CommandManager getCommandManager() { public CommandManager getCommandManager() {
return commands; return commands;
@ -118,15 +118,14 @@ public class Citizens extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
Thread.currentThread().setContextClassLoader(contextClassLoader); restoreOldClassLoader();
// replace previous classloader
// Don't bother with this part if MC versions are not compatible // Don't bother with this part if MC versions are not compatible
if (compatible) { if (compatible) {
save(); save();
while (npcManager.iterator().hasNext()) { while (npcManager.iterator().hasNext()) {
npcManager.iterator().next().despawn(); npcManager.iterator().next().despawn();
npcManager.iterator().remove(); npcManager.iterator().remove();
} } // TODO: clean up
npcManager = null; npcManager = null;
getServer().getScheduler().cancelTasks(this); getServer().getScheduler().cancelTasks(this);
} }
@ -145,44 +144,21 @@ public class Citizens extends JavaPlugin {
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
return; return;
} }
contextClassLoader = Thread.currentThread().getContextClassLoader(); registerScriptHelpers();
Thread.currentThread().setContextClassLoader(getClassLoader()); replaceClassLoader();
// workaround to fix scripts not loading plugin classes properly
config = new Settings(getDataFolder()); config = new Settings(getDataFolder());
// NPC storage setupStorage();
String type = Setting.STORAGE_TYPE.asString();
if (type.equalsIgnoreCase("db") || type.equalsIgnoreCase("database")) {
try {
saves = new DatabaseStorage(Setting.DATABASE_DRIVER.asString(), Setting.DATABASE_URL.asString(),
Setting.DATABASE_USERNAME.asString(), Setting.DATABASE_PASSWORD.asString());
} catch (SQLException e) {
e.printStackTrace();
Messaging.log("Unable to connect to database, falling back to YAML");
}
} else if (type.equalsIgnoreCase("nbt")) {
saves = new NBTStorage(getDataFolder() + File.separator + Setting.STORAGE_FILE.asString(),
"Citizens NPC Storage");
}
if (saves == null) {
saves = new YamlStorage(getDataFolder() + File.separator + Setting.STORAGE_FILE.asString(),
"Citizens NPC Storage");
}
Messaging.log("Save method set to", saves.toString());
// Register API managers
npcManager = new CitizensNPCManager(this, saves); npcManager = new CitizensNPCManager(this, saves);
traitManager = new CitizensTraitManager(this); traitManager = new CitizensTraitManager(this);
CitizensAPI.setNPCManager(npcManager); CitizensAPI.setNPCManager(npcManager);
CitizensAPI.setCharacterManager(characterManager); CitizensAPI.setCharacterManager(characterManager);
CitizensAPI.setTraitManager(traitManager); CitizensAPI.setTraitManager(traitManager);
// Register events
getServer().getPluginManager().registerEvents(new EventListen(npcManager), this); getServer().getPluginManager().registerEvents(new EventListen(npcManager), this);
// Register commands
registerCommands(); registerCommands();
getServer().getScheduler().scheduleSyncRepeatingTask(this, new NPCUpdater(npcManager), 0, 1); getServer().getScheduler().scheduleSyncRepeatingTask(this, new NPCUpdater(npcManager), 0, 1);
@ -196,7 +172,6 @@ public class Citizens extends JavaPlugin {
public void run() { public void run() {
try { try {
setupNPCs(); setupNPCs();
registerScriptHelpers();
} catch (NPCLoadException ex) { } catch (NPCLoadException ex) {
Messaging.log(Level.SEVERE, "Issue when loading NPCs: " + ex.getMessage()); Messaging.log(Level.SEVERE, "Issue when loading NPCs: " + ex.getMessage());
} }
@ -231,6 +206,27 @@ public class Citizens extends JavaPlugin {
}.start(); }.start();
} }
private void setupStorage() {
String type = Setting.STORAGE_TYPE.asString();
if (type.equalsIgnoreCase("db") || type.equalsIgnoreCase("database")) {
try {
saves = new DatabaseStorage(Setting.DATABASE_DRIVER.asString(), Setting.DATABASE_URL.asString(),
Setting.DATABASE_USERNAME.asString(), Setting.DATABASE_PASSWORD.asString());
} catch (SQLException e) {
e.printStackTrace();
Messaging.log("Unable to connect to database, falling back to YAML");
}
} else if (type.equalsIgnoreCase("nbt")) {
saves = new NBTStorage(getDataFolder() + File.separator + Setting.STORAGE_FILE.asString(),
"Citizens NPC Storage");
}
if (saves == null) {
saves = new YamlStorage(getDataFolder() + File.separator + Setting.STORAGE_FILE.asString(),
"Citizens NPC Storage");
}
Messaging.log("Save method set to", saves.toString());
}
private void registerCommands() { private void registerCommands() {
commands.setInjector(new Injector(this)); commands.setInjector(new Injector(this));
@ -257,6 +253,16 @@ public class Citizens extends JavaPlugin {
getServer().getPluginManager().callEvent(new CitizensReloadEvent()); getServer().getPluginManager().callEvent(new CitizensReloadEvent());
} }
private void replaceClassLoader() {
contextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClassLoader());
// workaround to fix scripts not loading plugin classes properly
}
private void restoreOldClassLoader() {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
public void save() { public void save() {
for (NPC npc : npcManager) for (NPC npc : npcManager)
((CitizensNPC) npc).save(saves.getKey("npc." + npc.getId())); ((CitizensNPC) npc).save(saves.getKey("npc." + npc.getId()));
@ -264,6 +270,7 @@ public class Citizens extends JavaPlugin {
saves.save(); saves.save();
} }
// TODO: refactor
private void setupNPCs() throws NPCLoadException { private void setupNPCs() throws NPCLoadException {
saves.load(); saves.load();
int created = 0, spawned = 0; int created = 0, spawned = 0;

View File

@ -1,8 +1,6 @@
package net.citizensnpcs.npc.entity; package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.trait.Equipment; import net.citizensnpcs.api.trait.trait.Equipment;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.editor.Equipable; import net.citizensnpcs.editor.Equipable;
import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.npc.CitizensNPCManager;
@ -110,11 +108,6 @@ public class CitizensHumanNPC extends CitizensNPC implements Equipable {
return (EntityHumanNPC) mcEntity; return (EntityHumanNPC) mcEntity;
} }
@Override
public void load(DataKey key) throws NPCLoadException {
super.load(key);
}
@Override @Override
public void setName(String name) { public void setName(String name) {
super.setName(name); super.setName(name);