From b490a5e03b2f5f6949887d07c0637afd0dad7c01 Mon Sep 17 00:00:00 2001 From: aPunch Date: Sat, 21 Jan 2012 06:53:58 -0600 Subject: [PATCH] fix saving bugs --- TODO | 6 +++ src/net/citizensnpcs/Citizens.java | 37 +++++++++++-- src/net/citizensnpcs/npc/CitizensNPC.java | 2 +- .../storage/flatfile/YamlStorage.java | 52 ++++++++++++++++--- 4 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 TODO diff --git a/TODO b/TODO new file mode 100644 index 000000000..73f9bb292 --- /dev/null +++ b/TODO @@ -0,0 +1,6 @@ +-Citizens2 TODO List- + +-Properly implement commands +-Finish saving +-Finish pathfinding API +-Add database support (MySQL and/or SQLite) \ No newline at end of file diff --git a/src/net/citizensnpcs/Citizens.java b/src/net/citizensnpcs/Citizens.java index f04e2f061..8d670049b 100644 --- a/src/net/citizensnpcs/Citizens.java +++ b/src/net/citizensnpcs/Citizens.java @@ -30,6 +30,8 @@ public class Citizens extends JavaPlugin { @Override public void onDisable() { + save(); + Messaging.log("v" + getDescription().getVersion() + " disabled."); } @@ -81,6 +83,7 @@ public class Citizens extends JavaPlugin { return true; } + // TODO possibly separate this out some more private void setupNPCs() throws NPCLoadException { traitManager.registerTrait(LocationTrait.class); int spawned = 0; @@ -91,9 +94,15 @@ public class Citizens extends JavaPlugin { Character character = characterManager.getCharacter(key.getString("character")); NPC npc = npcManager.createNPC(key.getString("name"), character); - // Load the character if it exists - if (character != null) { - character.load(key); + // Load the character if it exists, otherwise remove the character + if (character != null) + character.load(key.getRelative(character.getName())); + else { + if (key.keyExists("character")) { + Messaging.debug("Character '" + key.getString("character") + + "' does not exist. Removing character from the NPC with ID '" + npc.getId() + "'."); + key.removeKey("character"); + } } // Load traits @@ -106,7 +115,7 @@ public class Citizens extends JavaPlugin { } } for (Trait trait : npc.getTraits()) { - trait.load(key); + trait.load(key.getRelative(trait.getName())); } // Spawn the NPC @@ -117,4 +126,24 @@ public class Citizens extends JavaPlugin { } Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs (" + spawned + " spawned)."); } + + private void save() { + for (NPC npc : npcManager.getNPCs()) { + DataKey root = saves.getKey("npc." + npc.getId()); + root.setString("name", npc.getFullName()); + root.setBoolean("spawned", npc.isSpawned()); + + // Save the character if it exists + if (npc.getCharacter() != null) { + root.setString("character", npc.getCharacter().getName()); + npc.getCharacter().save(root.getRelative(npc.getCharacter().getName())); + } + + // Save all existing traits + for (Trait trait : npc.getTraits()) { + trait.save(root.getRelative(trait.getName())); + } + } + saves.save(); + } } \ No newline at end of file diff --git a/src/net/citizensnpcs/npc/CitizensNPC.java b/src/net/citizensnpcs/npc/CitizensNPC.java index 931e251e8..c71affcac 100644 --- a/src/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/net/citizensnpcs/npc/CitizensNPC.java @@ -63,7 +63,7 @@ public class CitizensNPC implements NPC { if (!hasTrait(trait)) traits.put(trait, factory.create(trait)); else - Messaging.debug("The NPC already has the trait '" + trait.getName() + "'."); + Messaging.debug("The NPC already has the trait '" + getTrait(trait).getName() + "'."); } @Override diff --git a/src/net/citizensnpcs/storage/flatfile/YamlStorage.java b/src/net/citizensnpcs/storage/flatfile/YamlStorage.java index 7a39ed206..1665080c9 100644 --- a/src/net/citizensnpcs/storage/flatfile/YamlStorage.java +++ b/src/net/citizensnpcs/storage/flatfile/YamlStorage.java @@ -62,6 +62,10 @@ public class YamlStorage implements Storage { } } + private boolean pathExists(String key) { + return config.get(key) != null; + } + public class YamlKey extends DataKey { private final String current; @@ -79,7 +83,13 @@ public class YamlStorage implements Storage { @Override public boolean getBoolean(String key) { - return config.getBoolean(getKeyExt(key)); + String path = getKeyExt(key); + if (pathExists(path)) { + if (config.getString(path) == null) + return config.getBoolean(path); + return Boolean.parseBoolean(config.getString(path)); + } + return false; } @Override @@ -89,7 +99,16 @@ public class YamlStorage implements Storage { @Override public double getDouble(String key) { - return config.getDouble(getKeyExt(key)); + String path = getKeyExt(key); + if (pathExists(path)) { + if (config.getString(path) == null) { + if (config.get(path) instanceof Integer) + return config.getInt(path); + return config.getDouble(path); + } + return Double.parseDouble(config.getString(path)); + } + return 0; } @Override @@ -99,12 +118,18 @@ public class YamlStorage implements Storage { @Override public int getInt(String key) { - return config.getInt(getKeyExt(key)); + String path = getKeyExt(key); + if (pathExists(path)) { + if (config.getString(path) == null) + return config.getInt(path); + return Integer.parseInt(config.getString(path)); + } + return 0; } @Override - public int getInt(String key, int value) { - return config.getInt(getKeyExt(key), value); + public int getInt(String key, int def) { + return config.getInt(getKeyExt(key), def); } @Override @@ -128,7 +153,16 @@ public class YamlStorage implements Storage { @Override public long getLong(String key) { - return config.getLong(getKeyExt(key)); + String path = getKeyExt(key); + if (pathExists(path)) { + if (config.getString(path) == null) { + if (config.get(path) instanceof Integer) + return config.getInt(path); + return config.getLong(path); + } + return Long.parseLong(config.getString(path)); + } + return 0; } @Override @@ -143,7 +177,11 @@ public class YamlStorage implements Storage { @Override public String getString(String key) { - return config.getString(getKeyExt(key)); + String path = getKeyExt(key); + if (pathExists(path)) { + return config.get(path).toString(); + } + return ""; } @Override