Citizens2/src/main/java/net/citizensnpcs/Settings.java

121 lines
3.7 KiB
Java
Raw Normal View History

2012-01-21 14:56:50 +01:00
package net.citizensnpcs;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
2012-01-21 14:56:50 +01:00
2012-02-12 09:00:19 +01:00
import net.citizensnpcs.api.util.DataKey;
2012-03-10 11:33:11 +01:00
import net.citizensnpcs.api.util.Storage;
2012-02-12 09:00:19 +01:00
import net.citizensnpcs.api.util.YamlStorage;
2012-01-21 14:56:50 +01:00
import net.citizensnpcs.util.Messaging;
import com.google.common.collect.Lists;
2012-01-21 14:56:50 +01:00
public class Settings {
2012-05-23 12:57:01 +02:00
private final Storage config;
2012-04-15 01:32:43 +02:00
private final DataKey root;
2012-03-10 11:33:11 +01:00
public Settings(File folder) {
config = new YamlStorage(folder + File.separator + "config.yml", "Citizens Configuration");
2012-04-15 01:32:43 +02:00
root = config.getKey("");
2012-01-23 09:45:34 +01:00
config.load();
2012-01-23 09:45:34 +01:00
for (Setting setting : Setting.values()) {
2012-01-30 12:58:59 +01:00
if (!root.keyExists(setting.path)) {
Messaging.logF("Writing default setting: '%s'", setting.path);
setting.setAtKey(root);
2012-04-15 01:32:43 +02:00
} else
setting.loadFromKey(root);
2012-01-23 09:45:34 +01:00
}
2012-04-15 01:32:43 +02:00
save();
}
public void reload() {
config.load();
for (Setting setting : Setting.values())
if (root.keyExists(setting.path))
setting.loadFromKey(root);
2012-04-15 01:32:43 +02:00
save();
2012-01-23 09:45:34 +01:00
}
public void save() {
config.save();
}
2012-01-21 14:56:50 +01:00
public enum Setting {
2012-02-12 19:29:33 +01:00
CHAT_PREFIX("npc.chat.prefix", "[<npc>]: "),
2012-03-30 16:01:36 +02:00
DATABASE_DRIVER("storage.database.driver", ""),
DATABASE_PASSWORD("storage.database.password", ""),
DATABASE_URL("storage.database.url", ""),
DATABASE_USERNAME("storage.database.username", ""),
2012-02-12 08:19:32 +01:00
DEBUG_MODE("general.debug-mode", false),
DEFAULT_LOOK_CLOSE("npc.default.look-close", false),
DEFAULT_RANDOM_TALKER("npc.default.random-talker", true),
DEFAULT_TALK_CLOSE("npc.default.talk-close", false),
2012-05-27 10:31:08 +02:00
DEFAULT_TEXT("npc.default.text.0", "Hi, I'm <npc>!") {
2012-05-23 12:57:01 +02:00
@Override
public void loadFromKey(DataKey root) {
2012-05-23 12:57:01 +02:00
List<String> list = new ArrayList<String>();
for (DataKey key : root.getRelative("npc.default.text").getSubKeys())
list.add(key.getString(""));
value = list;
}
},
2012-02-12 19:29:33 +01:00
QUICK_SELECT("npc.selection.quick-select", false),
SELECTION_ITEM("npc.selection.item", "280"),
2012-02-12 08:19:32 +01:00
SELECTION_MESSAGE("npc.selection.message", "<b>You selected <a><npc><b>!"),
SERVER_OWNS_NPCS("npc.server-ownership", false),
2012-04-13 17:59:51 +02:00
STORAGE_FILE("storage.file", "saves.yml"),
STORAGE_TYPE("storage.type", "yaml"),
2012-07-19 14:58:22 +02:00
SUBPLUGIN_FOLDER("subplugins.folder", "plugins"),
TALK_CLOSE_MAXIMUM_COOLDOWN("npc.text.max-talk-cooldown", 60),
TALK_CLOSE_MINIMUM_COOLDOWN("npc.text.min-talk-cooldown", 30),
2012-07-19 14:58:22 +02:00
TALK_ITEM("npc.text.talk-item", "340");
2012-01-21 14:56:50 +01:00
2012-05-23 12:57:01 +02:00
protected String path;
protected Object value;
2012-01-21 14:56:50 +01:00
Setting(String path, Object value) {
this.path = path;
this.value = value;
}
2012-01-30 12:58:59 +01:00
public boolean asBoolean() {
2012-01-23 09:45:34 +01:00
return (Boolean) value;
2012-01-21 14:56:50 +01:00
}
2012-01-30 12:58:59 +01:00
public double asDouble() {
2012-01-21 14:56:50 +01:00
return (Double) value;
}
2012-01-30 12:58:59 +01:00
public int asInt() {
return Integer.parseInt(value.toString());
2012-01-23 09:45:34 +01:00
}
2012-05-23 12:57:01 +02:00
@SuppressWarnings("unchecked")
public List<String> asList() {
if (!(value instanceof List)) {
value = Lists.newArrayList(value);
}
return (List<String>) value;
2012-03-27 16:42:15 +02:00
}
2012-01-30 12:58:59 +01:00
public long asLong() {
2012-01-21 14:56:50 +01:00
return (Long) value;
}
2012-01-30 12:58:59 +01:00
public String asString() {
2012-01-21 14:56:50 +01:00
return value.toString();
}
protected void loadFromKey(DataKey root) {
value = root.getRaw(path);
2012-05-27 10:30:27 +02:00
}
protected void setAtKey(DataKey root) {
root.setRaw(path, value);
2012-01-21 14:56:50 +01:00
}
}
}