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

136 lines
4.4 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) {
2012-08-05 07:19:16 +02:00
config = new YamlStorage(new File(folder, "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);
2012-07-19 17:10:30 +02:00
setting.setAtKey(root);
2012-04-15 01:32:43 +02:00
} else
2012-07-19 17:10:30 +02:00
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))
2012-07-19 17:10:30 +02:00
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),
2012-08-05 15:07:42 +02:00
DEFAULT_LOOK_CLOSE("npc.default.look-close.enabled", false),
DEFAULT_LOOK_CLOSE_RANGE("npc.default.look-close.range", 5),
2012-08-29 15:37:00 +02:00
DEFAULT_NPC_LIMIT("npc.limits.default-limit", 10),
2012-08-05 15:07:42 +02:00
DEFAULT_PATHFINDING_RANGE("npc.default.pathfinding.range", 25F),
DEFAULT_RANDOM_TALKER("npc.default.random-talker", true),
DEFAULT_REALISTIC_LOOKING("npc.default.realistic-looking", false),
2012-08-05 15:07:42 +02:00
DEFAULT_TALK_CLOSE("npc.default.talk-close.enabled", false),
DEFAULT_TALK_CLOSE_RANGE("npc.default.talk-close.range", 5),
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
2012-07-19 17:10:30 +02:00
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-08-29 15:37:00 +02:00
MAX_NPC_LIMIT_CHECKS("npc.limits.max-permission-checks", 100),
2012-08-15 18:06:13 +02:00
NPC_COST("economy.npc.cost", 100D),
2012-02-12 19:29:33 +01:00
QUICK_SELECT("npc.selection.quick-select", false),
2012-08-15 18:06:13 +02:00
SAVE_TASK_DELAY("storage.save-task.delay", 20 * 60 * 60),
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 17:10:30 +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-04-13 17:59:51 +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-07-28 09:02:57 +02:00
return ((Number) value).doubleValue();
2012-01-21 14:56:50 +01:00
}
public float asFloat() {
2012-07-28 09:02:57 +02:00
return ((Number) value).floatValue();
}
2012-01-30 12:58:59 +01:00
public int asInt() {
2012-07-28 09:02:57 +02:00
if (value instanceof String) {
return Integer.parseInt(value.toString());
}
return ((Number) value).intValue();
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-07-28 09:02:57 +02:00
return ((Number) value).longValue();
2012-01-21 14:56:50 +01:00
}
2012-01-30 12:58:59 +01:00
public String asString() {
2012-01-21 14:56:50 +01:00
return value.toString();
}
2012-07-19 17:10:30 +02:00
protected void loadFromKey(DataKey root) {
value = root.getRaw(path);
2012-05-27 10:30:27 +02:00
}
2012-07-19 17:10:30 +02:00
protected void setAtKey(DataKey root) {
root.setRaw(path, value);
2012-01-21 14:56:50 +01:00
}
}
}