fix saving bugs

This commit is contained in:
aPunch 2012-01-21 06:53:58 -06:00
parent 0511dd53b8
commit 8ab0c815d9
4 changed files with 85 additions and 12 deletions

6
TODO Normal file
View File

@ -0,0 +1,6 @@
-Citizens2 TODO List-
-Properly implement commands
-Finish saving
-Finish pathfinding API
-Add database support (MySQL and/or SQLite)

View File

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

View File

@ -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

View File

@ -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