mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-06-26 14:35:03 +02:00
fix saving bugs
This commit is contained in:
parent
6adc538b1e
commit
b490a5e03b
6
TODO
Normal file
6
TODO
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
-Citizens2 TODO List-
|
||||||
|
|
||||||
|
-Properly implement commands
|
||||||
|
-Finish saving
|
||||||
|
-Finish pathfinding API
|
||||||
|
-Add database support (MySQL and/or SQLite)
|
|
@ -30,6 +30,8 @@ public class Citizens extends JavaPlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
|
save();
|
||||||
|
|
||||||
Messaging.log("v" + getDescription().getVersion() + " disabled.");
|
Messaging.log("v" + getDescription().getVersion() + " disabled.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,6 +83,7 @@ public class Citizens extends JavaPlugin {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO possibly separate this out some more
|
||||||
private void setupNPCs() throws NPCLoadException {
|
private void setupNPCs() throws NPCLoadException {
|
||||||
traitManager.registerTrait(LocationTrait.class);
|
traitManager.registerTrait(LocationTrait.class);
|
||||||
int spawned = 0;
|
int spawned = 0;
|
||||||
|
@ -91,9 +94,15 @@ public class Citizens extends JavaPlugin {
|
||||||
Character character = characterManager.getCharacter(key.getString("character"));
|
Character character = characterManager.getCharacter(key.getString("character"));
|
||||||
NPC npc = npcManager.createNPC(key.getString("name"), character);
|
NPC npc = npcManager.createNPC(key.getString("name"), character);
|
||||||
|
|
||||||
// Load the character if it exists
|
// Load the character if it exists, otherwise remove the character
|
||||||
if (character != null) {
|
if (character != null)
|
||||||
character.load(key);
|
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
|
// Load traits
|
||||||
|
@ -106,7 +115,7 @@ public class Citizens extends JavaPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Trait trait : npc.getTraits()) {
|
for (Trait trait : npc.getTraits()) {
|
||||||
trait.load(key);
|
trait.load(key.getRelative(trait.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spawn the NPC
|
// Spawn the NPC
|
||||||
|
@ -117,4 +126,24 @@ public class Citizens extends JavaPlugin {
|
||||||
}
|
}
|
||||||
Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs (" + spawned + " spawned).");
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -63,7 +63,7 @@ public class CitizensNPC implements NPC {
|
||||||
if (!hasTrait(trait))
|
if (!hasTrait(trait))
|
||||||
traits.put(trait, factory.create(trait));
|
traits.put(trait, factory.create(trait));
|
||||||
else
|
else
|
||||||
Messaging.debug("The NPC already has the trait '" + trait.getName() + "'.");
|
Messaging.debug("The NPC already has the trait '" + getTrait(trait).getName() + "'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -62,6 +62,10 @@ public class YamlStorage implements Storage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean pathExists(String key) {
|
||||||
|
return config.get(key) != null;
|
||||||
|
}
|
||||||
|
|
||||||
public class YamlKey extends DataKey {
|
public class YamlKey extends DataKey {
|
||||||
private final String current;
|
private final String current;
|
||||||
|
|
||||||
|
@ -79,7 +83,13 @@ public class YamlStorage implements Storage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean getBoolean(String key) {
|
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
|
@Override
|
||||||
|
@ -89,7 +99,16 @@ public class YamlStorage implements Storage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getDouble(String key) {
|
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
|
@Override
|
||||||
|
@ -99,12 +118,18 @@ public class YamlStorage implements Storage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getInt(String key) {
|
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
|
@Override
|
||||||
public int getInt(String key, int value) {
|
public int getInt(String key, int def) {
|
||||||
return config.getInt(getKeyExt(key), value);
|
return config.getInt(getKeyExt(key), def);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -128,7 +153,16 @@ public class YamlStorage implements Storage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getLong(String key) {
|
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
|
@Override
|
||||||
|
@ -143,7 +177,11 @@ public class YamlStorage implements Storage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getString(String key) {
|
public String getString(String key) {
|
||||||
return config.getString(getKeyExt(key));
|
String path = getKeyExt(key);
|
||||||
|
if (pathExists(path)) {
|
||||||
|
return config.get(path).toString();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue
Block a user