implemented settings

This commit is contained in:
aPunch 2012-01-21 07:56:50 -06:00
parent 8ab0c815d9
commit 8c797f7a78
4 changed files with 105 additions and 17 deletions

View File

@ -23,20 +23,14 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class Citizens extends JavaPlugin {
private CitizensNPCManager npcManager;
private CitizensCharacterManager characterManager;
private CitizensTraitManager traitManager;
private final CitizensNPCManager npcManager;
private final CitizensCharacterManager characterManager;
private final CitizensTraitManager traitManager;
private Storage saves;
private Settings config;
@Override
public void onDisable() {
save();
Messaging.log("v" + getDescription().getVersion() + " disabled.");
}
@Override
public void onEnable() {
public Citizens() {
super();
// Register API managers
npcManager = new CitizensNPCManager();
characterManager = new CitizensCharacterManager();
@ -44,6 +38,21 @@ public class Citizens extends JavaPlugin {
CitizensAPI.setNPCManager(npcManager);
CitizensAPI.setCharacterManager(characterManager);
CitizensAPI.setTraitManager(traitManager);
}
@Override
public void onDisable() {
config.save();
saveNPCs();
Messaging.log("v" + getDescription().getVersion() + " disabled.");
}
@Override
public void onEnable() {
// Configuration file
config = new Settings(this);
config.load();
// TODO database support
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml");
@ -127,7 +136,7 @@ public class Citizens extends JavaPlugin {
Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs (" + spawned + " spawned).");
}
private void save() {
private void saveNPCs() {
for (NPC npc : npcManager.getNPCs()) {
DataKey root = saves.getKey("npc." + npc.getId());
root.setString("name", npc.getFullName());

View File

@ -0,0 +1,77 @@
package net.citizensnpcs;
import java.io.File;
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.storage.flatfile.YamlStorage;
import net.citizensnpcs.util.Messaging;
public class Settings {
public enum Setting {
DEBUG_MODE("general.debug-mode", false),
TEST_DOUBLE("hi", 3.4);
private String path;
private Object value;
Setting(String path, Object value) {
this.path = path;
this.value = value;
}
public String getPath() {
return path;
}
public int getInt() {
return (Integer) value;
}
public double getDouble() {
return (Double) value;
}
public long getLong() {
return (Long) value;
}
public boolean getBoolean() {
return (Boolean) value;
}
public String getString() {
return value.toString();
}
private Object get() {
return value;
}
private void set(Object value) {
this.value = value;
}
}
private YamlStorage config;
private final DataKey root;
public Settings(Citizens plugin) {
config = new YamlStorage(plugin.getDataFolder() + File.separator + "config.yml");
root = config.getKey("");
}
public void load() {
for (Setting setting : Setting.values()) {
if (!root.keyExists(setting.getPath())) {
Messaging.log("Writing default setting: '" + setting.getPath() + "'");
root.setRaw(setting.getPath(), setting.get());
} else
setting.set(root.getRaw(setting.getPath()));
}
save();
}
public void save() {
config.save();
}
}

View File

@ -29,8 +29,8 @@ import net.minecraft.server.Packet29DestroyEntity;
import net.minecraft.server.WorldServer;
public class CitizensNPCManager implements NPCManager {
private Map<Entity, NPC> spawned = new ConcurrentHashMap<Entity, NPC>();
private Map<Integer, NPC> byID = new ConcurrentHashMap<Integer, NPC>();
private final Map<Entity, NPC> spawned = new ConcurrentHashMap<Entity, NPC>();
private final Map<Integer, NPC> byID = new ConcurrentHashMap<Integer, NPC>();
@Override
public NPC createNPC(String name) {

View File

@ -2,6 +2,8 @@ package net.citizensnpcs.util;
import java.util.logging.Level;
import net.citizensnpcs.Settings.Setting;
import org.bukkit.Bukkit;
public class Messaging {
@ -15,7 +17,7 @@ public class Messaging {
}
public static void debug(Object msg) {
// TODO add debug setting
log(msg);
if (Setting.DEBUG_MODE.getBoolean())
log(msg);
}
}