Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated to compile

  • Loading branch information...
commit 54af4078269a8c186d1b33234c0047f31b195038 1 parent 537e95e
@mbax mbax authored
View
61 com/ensifera/animosity/craftirc/CraftIRC.java
@@ -2,23 +2,13 @@
import java.io.File;
import java.lang.reflect.Field;
+import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Timer;
-import java.util.TimerTask;
import net.milkbowl.vault.chat.Chat;
-import net.milkbowl.vault.permission.Permission;
import net.minecraft.server.MinecraftServer;
import org.bukkit.ChatColor;
@@ -28,16 +18,12 @@
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.entity.Player;
-import org.bukkit.event.Event;
-import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.RegisteredServiceProvider;
-// import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.java.JavaPlugin;
-import org.bukkit.util.config.Configuration;
-import org.bukkit.util.config.ConfigurationNode;
-import net.minecraft.server.MinecraftServer;
-//import net.minecraft.server.ICommandListener;
+
+import com.sk89q.util.config.Configuration;
+import com.sk89q.util.config.ConfigurationNode;
/**
* @author Animosity
@@ -73,10 +59,12 @@
private HashMap<Integer, ArrayList<String>> channames;
protected HashMap<DualKey, String> chanTagMap;
protected Chat vault;
+ private Configuration configuration;
public void onEnable() {
try {
-
+ configuration = new Configuration(new File(getDataFolder().getPath() + "/config.yml"));
+ configuration.load();
PluginDescriptionFile desc = this.getDescription();
VERSION = desc.getVersion();
server = this.getServer();
@@ -85,8 +73,8 @@ public void onEnable() {
cfield.setAccessible(true);
console = (MinecraftServer) cfield.get((CraftServer)getServer());
- bots = new ArrayList<ConfigurationNode>(getConfiguration().getNodeList("bots", null));
- colormap = new ArrayList<ConfigurationNode>(getConfiguration().getNodeList("colormap", null));
+ bots = new ArrayList<ConfigurationNode>(configuration.getNodeList("bots", null));
+ colormap = new ArrayList<ConfigurationNode>(configuration.getNodeList("colormap", null));
channodes = new HashMap<Integer, ArrayList<ConfigurationNode>>();
channames = new HashMap<Integer, ArrayList<String>>();
chanTagMap = new HashMap<DualKey, String>();
@@ -116,7 +104,6 @@ public void onEnable() {
//Hold timers
hold = new HashMap<HoldType, Boolean>();
holdTimer = new Timer();
- Date now = new Date();
if (cHold("chat") > 0) {
hold.put(HoldType.CHAT, true);
holdTimer.schedule(new RemoveHoldTask(this, HoldType.CHAT), cHold("chat"));
@@ -149,7 +136,11 @@ public void onEnable() {
this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
public void run() {
if(CraftIRC.this.getServer().getPluginManager().isPluginEnabled("Vault")){
- CraftIRC.this.vault=((RegisteredServiceProvider<Chat>)getServer().getServicesManager().getRegistration(Chat.class)).getProvider();
+ try{
+ CraftIRC.this.vault=((RegisteredServiceProvider<Chat>)getServer().getServicesManager().getRegistration(Chat.class)).getProvider();
+ } catch (Exception e){
+
+ }
}
}
});
@@ -450,10 +441,10 @@ public void sendMessageToTag(String message, String tag) {
* CraftIRC API call - getBotFromTag(tag) Gets the bot id# from a source tag
* @param tag
* @return
- */
+
private int getBotIdFromTag(String tag) {
return 0;
- }
+ }*/
/** TODO: MAKE THIS
* CraftIRC API call - getBotFromId(id) Gets the bot id# from a source tag
@@ -519,23 +510,23 @@ private ConfigurationNode getChanNode(int bot, String channel) {
}
protected boolean cDebug() {
- return getConfiguration().getBoolean("settings.debug", false);
+ return configuration.getBoolean("settings.debug", false);
}
protected String cAdminsCmd() {
- return getConfiguration().getString("settings.admins-cmd", "/admins!");
+ return configuration.getString("settings.admins-cmd", "/admins!");
}
protected ArrayList<String> cConsoleCommands() {
- return new ArrayList<String>(getConfiguration().getStringList("settings.console-commands", null));
+ return new ArrayList<String>(configuration.getStringList("settings.console-commands", null));
}
protected ArrayList<String> cIgnoredPrefixes(String source) {
- return new ArrayList<String>(getConfiguration().getStringList("settings.ignored-prefixes." + source, null));
+ return new ArrayList<String>(configuration.getStringList("settings.ignored-prefixes." + source, null));
}
protected int cHold(String eventType) {
- return getConfiguration().getInt("settings.hold-after-enable." + eventType, 0);
+ return configuration.getInt("settings.hold-after-enable." + eventType, 0);
}
protected String cFormatting(String eventType, int bot, String channel) {
@@ -545,7 +536,7 @@ protected String cFormatting(String eventType, int bot, String channel) {
if (source == null || source.getString("formatting." + eventType) == null)
source = bots.get(bot);
if (source == null || source.getString("formatting." + eventType) == null)
- result = getConfiguration().getString("settings.formatting." + eventType, null);
+ result = configuration.getString("settings.formatting." + eventType, null);
else
result = source.getString("formatting." + eventType, null);
return result;
@@ -560,7 +551,7 @@ protected boolean cEvents(String eventType, int bot, String channel) {
if ((source == null || source.getProperty("events." + eventType) == null) && bot > -1)
source = bots.get(bot);
if (source == null || source.getProperty("events." + eventType) == null)
- return getConfiguration().getBoolean("settings.events." + eventType, def);
+ return configuration.getBoolean("settings.events." + eventType, def);
else
return source.getBoolean("events." + eventType, false);
}
@@ -617,7 +608,7 @@ protected String cColorGameFromName(String name) {
//For binding Minebot to a particular local address
protected String cBindLocalAddr() {
- return getConfiguration().getString("settings.bind-address","");
+ return configuration.getString("settings.bind-address","");
}
protected ArrayList<String> cBotChannels(int bot) {
@@ -657,7 +648,7 @@ protected int cBotMessageDelay(int bot) {
}
protected String cCommandPrefix(int bot) {
- return bots.get(bot).getString("command-prefix", getConfiguration().getString("settings.command-prefix", "."));
+ return bots.get(bot).getString("command-prefix", configuration.getString("settings.command-prefix", "."));
}
protected ArrayList<String> cBotAdminPrefixes(int bot) {
@@ -720,7 +711,7 @@ protected boolean cChanNameColors(int bot, String channel) {
protected boolean cChanCheckTag(String tag, int bot, String channel) {
if (tag == null || tag.equals(""))
return false;
- if (getConfiguration().getString("settings.tag", "all").equalsIgnoreCase(tag))
+ if (configuration.getString("settings.tag", "all").equalsIgnoreCase(tag))
return true;
if (bots.get(bot).getString("tag", "").equalsIgnoreCase(tag))
return true;
View
7 com/ensifera/animosity/craftirc/CraftIRCListener.java
@@ -1,14 +1,9 @@
package com.ensifera.animosity.craftirc;
-import java.lang.Exception;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
-import org.bukkit.event.player.PlayerChatEvent;
-import org.bukkit.event.player.PlayerCommandPreprocessEvent;
-import org.bukkit.event.player.PlayerKickEvent;
-import org.bukkit.event.player.PlayerJoinEvent;
-import org.bukkit.event.player.PlayerQuitEvent;
+import org.bukkit.event.player.*;
public class CraftIRCListener implements Listener {
View
11 com/ensifera/animosity/craftirc/Minebot.java
@@ -6,12 +6,13 @@
import java.util.Iterator;
import java.util.logging.Level;
-import org.bukkit.command.ConsoleCommandSender;
-import org.bukkit.entity.Player;
-import org.bukkit.craftbukkit.CraftServer;
-import org.jibble.pircbot.*;
import org.bukkit.ChatColor;
+import org.bukkit.entity.Player;
import org.bukkit.event.Event;
+import org.jibble.pircbot.IrcException;
+import org.jibble.pircbot.PircBot;
+import org.jibble.pircbot.TrustingSSLSocketFactory;
+import org.jibble.pircbot.User;
import com.ensifera.animosity.craftirc.IRCEvent.Mode;
@@ -525,7 +526,7 @@ private boolean routeCommand(String fullCommand, RelayedMessage ircConCmd) {
return false;
}
- if (this.plugin.defaultConsoleCommands.contains(rootCommand)) {
+ if (CraftIRC.defaultConsoleCommands.contains(rootCommand)) {
if (this.plugin.isDebug()) {
CraftIRC.log.info(String.format(CraftIRC.NAME + " Minebot routeCommand(default) fullCommand=" + fullCommand
+ " -- rootCommand=" + rootCommand));
View
2  com/ensifera/animosity/craftirc/example/CraftIRCPluginExample.java
@@ -2,8 +2,10 @@
import java.util.ArrayList;
import java.util.logging.Logger;
+
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
+
import com.ensifera.animosity.craftirc.CraftIRC;
/**
View
230 com/sk89q/util/config/Configuration.java
@@ -0,0 +1,230 @@
+package com.sk89q.util.config;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.util.HashMap;
+import java.util.Map;
+import org.yaml.snakeyaml.DumperOptions;
+import org.yaml.snakeyaml.Yaml;
+import org.yaml.snakeyaml.constructor.SafeConstructor;
+import org.yaml.snakeyaml.introspector.Property;
+import org.yaml.snakeyaml.nodes.CollectionNode;
+import org.yaml.snakeyaml.nodes.MappingNode;
+import org.yaml.snakeyaml.nodes.Node;
+import org.yaml.snakeyaml.nodes.NodeTuple;
+import org.yaml.snakeyaml.nodes.SequenceNode;
+import org.yaml.snakeyaml.nodes.Tag;
+import org.yaml.snakeyaml.reader.UnicodeReader;
+import org.yaml.snakeyaml.representer.Represent;
+import org.yaml.snakeyaml.representer.Representer;
+
+/**
+ * YAML configuration loader. To use this class, construct it with path to
+ * a file and call its load() method. For specifying node paths in the
+ * various get*() methods, they support SK's path notation, allowing you to
+ * select child nodes by delimiting node names with periods.
+ *
+ * <p>
+ * For example, given the following configuration file:</p>
+ *
+ * <pre>members:
+ * - Hollie
+ * - Jason
+ * - Bobo
+ * - Aya
+ * - Tetsu
+ * worldguard:
+ * fire:
+ * spread: false
+ * blocks: [cloth, rock, glass]
+ * sturmeh:
+ * cool: false
+ * eats:
+ * babies: true</pre>
+ *
+ * <p>Calling code could access sturmeh's baby eating state by using
+ * <code>getBoolean("sturmeh.eats.babies", false)</code>. For lists, there are
+ * methods such as <code>getStringList</code> that will return a type safe list.
+ *
+ * <p>This class is currently incomplete. It is not yet possible to get a node.
+ * </p>
+ *
+ */
+
+public class Configuration extends ConfigurationNode {
+ private Yaml yaml;
+ private File file;
+ private String header = null;
+
+ public Configuration(File file) {
+ super(new HashMap<String, Object>());
+
+ DumperOptions options = new DumperOptions();
+
+ options.setIndent(4);
+ options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
+
+ yaml = new Yaml(new SafeConstructor(), new EmptyNullRepresenter(), options);
+
+ this.file = file;
+ }
+
+ /**
+ * Loads the configuration file. All errors are thrown away.
+ */
+ public void load() {
+ FileInputStream stream = null;
+
+ try {
+ stream = new FileInputStream(file);
+ read(yaml.load(new UnicodeReader(stream)));
+ } catch (IOException e) {
+ root = new HashMap<String, Object>();
+ } catch (ConfigurationException e) {
+ root = new HashMap<String, Object>();
+ } finally {
+ try {
+ if (stream != null) {
+ stream.close();
+ }
+ } catch (IOException e) {}
+ }
+ }
+
+ /**
+ * Set the header for the file as a series of lines that are terminated
+ * by a new line sequence.
+ *
+ * @param headerLines header lines to prepend
+ */
+ public void setHeader(String... headerLines) {
+ StringBuilder header = new StringBuilder();
+
+ for (String line : headerLines) {
+ if (header.length() > 0) {
+ header.append("\r\n");
+ }
+ header.append(line);
+ }
+
+ setHeader(header.toString());
+ }
+
+ /**
+ * Set the header for the file. A header can be provided to prepend the
+ * YAML data output on configuration save. The header is
+ * printed raw and so must be manually commented if used. A new line will
+ * be appended after the header, however, if a header is provided.
+ *
+ * @param header header to prepend
+ */
+ public void setHeader(String header) {
+ this.header = header;
+ }
+
+ /**
+ * Return the set header.
+ *
+ * @return The header comment.
+ */
+ public String getHeader() {
+ return header;
+ }
+
+ /**
+ * Saves the configuration to disk. All errors are clobbered.
+ *
+ * @return true if it was successful
+ */
+ public boolean save() {
+ FileOutputStream stream = null;
+
+ File parent = file.getParentFile();
+
+ if (parent != null) {
+ parent.mkdirs();
+ }
+
+ try {
+ stream = new FileOutputStream(file);
+ OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8");
+ if (header != null) {
+ writer.append(header);
+ writer.append("\r\n");
+ }
+ yaml.dump(root, writer);
+ return true;
+ } catch (IOException e) {} finally {
+ try {
+ if (stream != null) {
+ stream.close();
+ }
+ } catch (IOException e) {}
+ }
+
+ return false;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void read(Object input) throws ConfigurationException {
+ try {
+ if (null == input) {
+ root = new HashMap<String, Object>();
+ } else {
+ root = (Map<String, Object>) input;
+ }
+ } catch (ClassCastException e) {
+ throw new ConfigurationException("Root document must be an key-value structure");
+ }
+ }
+
+ /**
+ * This method returns an empty ConfigurationNode for using as a
+ * default in methods that select a node from a node list.
+ * @return The empty node.
+ */
+ public static ConfigurationNode getEmptyNode() {
+ return new ConfigurationNode(new HashMap<String, Object>());
+ }
+}
+
+class EmptyNullRepresenter extends Representer {
+
+ public EmptyNullRepresenter() {
+ super();
+ this.nullRepresenter = new EmptyRepresentNull();
+ }
+
+ protected class EmptyRepresentNull implements Represent {
+ public Node representData(Object data) {
+ return representScalar(Tag.NULL, ""); // Changed "null" to "" so as to avoid writing nulls
+ }
+ }
+
+ // Code borrowed from snakeyaml (http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyaml/issues/issue60/SkipBeanTest.java)
+ @Override
+ protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) {
+ NodeTuple tuple = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
+ Node valueNode = tuple.getValueNode();
+ if (valueNode instanceof CollectionNode) {
+ // Removed null check
+ if (Tag.SEQ.equals(valueNode.getTag())) {
+ SequenceNode seq = (SequenceNode) valueNode;
+ if (seq.getValue().isEmpty()) {
+ return null; // skip empty lists
+ }
+ }
+ if (Tag.MAP.equals(valueNode.getTag())) {
+ MappingNode seq = (MappingNode) valueNode;
+ if (seq.getValue().isEmpty()) {
+ return null; // skip empty maps
+ }
+ }
+ }
+ return tuple;
+ }
+ // End of borrowed code
+}
View
16 com/sk89q/util/config/ConfigurationException.java
@@ -0,0 +1,16 @@
+package com.sk89q.util.config;
+
+/**
+ * Configuration exception.
+ */
+public class ConfigurationException extends Exception {
+ private static final long serialVersionUID = -2442886939908724203L;
+
+ public ConfigurationException() {
+ super();
+ }
+
+ public ConfigurationException(String msg) {
+ super(msg);
+ }
+}
View
587 com/sk89q/util/config/ConfigurationNode.java
@@ -0,0 +1,587 @@
+package com.sk89q.util.config;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+/**
+ * Represents a configuration node.
+ */
+public class ConfigurationNode {
+ protected Map<String, Object> root;
+
+ protected ConfigurationNode(Map<String, Object> root) {
+ this.root = root;
+ }
+
+ /**
+ * Gets all of the cofiguration values within the Node as
+ * a key value pair, with the key being the full path and the
+ * value being the Object that is at the path.
+ *
+ * @return A map of key value pairs with the path as the key and the object as the value
+ */
+ public Map<String, Object> getAll() {
+ return recursiveBuilder(root);
+ }
+
+ /**
+ * A helper method for the getAll method that deals with the recursion
+ * involved in traversing the tree
+ *
+ * @param node The map for that node of the tree
+ * @return The fully pathed map for that point in the tree, with the path as the key
+ */
+ @SuppressWarnings("unchecked")
+ protected Map<String, Object> recursiveBuilder(Map<String, Object> node) {
+ Map<String, Object> map = new TreeMap<String, Object>();
+
+ Set<String> keys = node.keySet();
+ for( String k : keys ) {
+ Object tmp = node.get(k);
+ if( tmp instanceof Map<?,?> ) {
+ Map<String, Object> rec = recursiveBuilder((Map <String,Object>) tmp);
+
+ Set<String> subkeys = rec.keySet();
+ for( String sk : subkeys ) {
+ map.put(k + "." + sk, rec.get(sk));
+ }
+ }
+ else {
+ map.put(k, tmp);
+ }
+ }
+
+ return map;
+ }
+
+ /**
+ * Gets a property at a location. This will either return an Object
+ * or null, with null meaning that no configuration value exists at
+ * that location. This could potentially return a default value (not yet
+ * implemented) as defined by a plugin, if this is a plugin-tied
+ * configuration.
+ *
+ * @param path path to node (dot notation)
+ * @return object or null
+ */
+ @SuppressWarnings("unchecked")
+ public Object getProperty(String path) {
+ if (!path.contains(".")) {
+ Object val = root.get(path);
+
+ if (val == null) {
+ return null;
+ }
+ return val;
+ }
+
+ String[] parts = path.split("\\.");
+ Map<String, Object> node = root;
+
+ for (int i = 0; i < parts.length; i++) {
+ Object o = node.get(parts[i]);
+
+ if (o == null) {
+ return null;
+ }
+
+ if (i == parts.length - 1) {
+ return o;
+ }
+
+ try {
+ node = (Map<String, Object>) o;
+ } catch (ClassCastException e) {
+ return null;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Set the property at a location. This will override existing
+ * configuration data to have it conform to key/value mappings.
+ *
+ * @param path The property path
+ * @param value The new value
+ */
+ @SuppressWarnings("unchecked")
+ public void setProperty(String path, Object value) {
+ if (!path.contains(".")) {
+ root.put(path, value);
+ return;
+ }
+
+ String[] parts = path.split("\\.");
+ Map<String, Object> node = root;
+
+ for (int i = 0; i < parts.length; i++) {
+ Object o = node.get(parts[i]);
+
+ // Found our target!
+ if (i == parts.length - 1) {
+ node.put(parts[i], value);
+ return;
+ }
+
+ if (o == null || !(o instanceof Map)) {
+ // This will override existing configuration data!
+ o = new HashMap<String, Object>();
+ node.put(parts[i], o);
+ }
+
+ node = (Map<String, Object>) o;
+ }
+ }
+
+ /**
+ * Gets a string at a location. This will either return an String
+ * or null, with null meaning that no configuration value exists at
+ * that location. If the object at the particular location is not actually
+ * a string, it will be converted to its string representation.
+ *
+ * @param path path to node (dot notation)
+ * @return string or null
+ */
+ public String getString(String path) {
+ Object o = getProperty(path);
+
+ if (o == null) {
+ return null;
+ }
+ return o.toString();
+ }
+
+ /**
+ * Gets a string at a location. This will either return an String
+ * or the default value. If the object at the particular location is not
+ * actually a string, it will be converted to its string representation.
+ *
+ * @param path path to node (dot notation)
+ * @param def default value
+ * @return string or default
+ */
+ public String getString(String path, String def) {
+ String o = getString(path);
+
+ if (o == null) {
+ setProperty(path, def);
+ return def;
+ }
+ return o;
+ }
+
+ /**
+ * Gets an integer at a location. This will either return an integer
+ * or the default value. If the object at the particular location is not
+ * actually a integer, the default value will be returned. However, other
+ * number types will be casted to an integer.
+ *
+ * @param path path to node (dot notation)
+ * @param def default value
+ * @return int or default
+ */
+ public int getInt(String path, int def) {
+ Integer o = castInt(getProperty(path));
+
+ if (o == null) {
+ setProperty(path, def);
+ return def;
+ } else {
+ return o;
+ }
+ }
+
+ /**
+ * Gets a double at a location. This will either return an double
+ * or the default value. If the object at the particular location is not
+ * actually a double, the default value will be returned. However, other
+ * number types will be casted to an double.
+ *
+ * @param path path to node (dot notation)
+ * @param def default value
+ * @return double or default
+ */
+ public double getDouble(String path, double def) {
+ Double o = castDouble(getProperty(path));
+
+ if (o == null) {
+ setProperty(path, def);
+ return def;
+ } else {
+ return o;
+ }
+ }
+
+ /**
+ * Gets a boolean at a location. This will either return an boolean
+ * or the default value. If the object at the particular location is not
+ * actually a boolean, the default value will be returned.
+ *
+ * @param path path to node (dot notation)
+ * @param def default value
+ * @return boolean or default
+ */
+ public boolean getBoolean(String path, boolean def) {
+ Boolean o = castBoolean(getProperty(path));
+
+ if (o == null) {
+ setProperty(path, def);
+ return def;
+ } else {
+ return o;
+ }
+ }
+
+ /**
+ * Get a list of keys at a location. If the map at the particular location
+ * does not exist or it is not a map, null will be returned.
+ *
+ * @param path path to node (dot notation)
+ * @return list of keys
+ */
+ @SuppressWarnings("unchecked")
+ public List<String> getKeys(String path) {
+ if (path == null) {
+ return new ArrayList<String>(root.keySet());
+ }
+ Object o = getProperty(path);
+
+ if (o == null) {
+ return null;
+ } else if (o instanceof Map) {
+ return new ArrayList<String>(((Map<String, Object>) o).keySet());
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns a list of all keys at the root path
+ *
+ * @return List of keys
+ */
+ public List<String> getKeys() {
+ return new ArrayList<String>(root.keySet());
+ }
+
+ /**
+ * Gets a list of objects at a location. If the list is not defined,
+ * null will be returned. The node must be an actual list.
+ *
+ * @param path path to node (dot notation)
+ * @return boolean or default
+ */
+ @SuppressWarnings("unchecked")
+ public List<Object> getList(String path) {
+ Object o = getProperty(path);
+
+ if (o == null) {
+ return null;
+ } else if (o instanceof List) {
+ return (List<Object>) o;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Gets a list of strings. Non-valid entries will not be in the list.
+ * There will be no null slots. If the list is not defined, the
+ * default will be returned. 'null' can be passed for the default
+ * and an empty list will be returned instead. If an item in the list
+ * is not a string, it will be converted to a string. The node must be
+ * an actual list and not just a string.
+ *
+ * @param path path to node (dot notation)
+ * @param def default value or null for an empty list as default
+ * @return list of strings
+ */
+ public List<String> getStringList(String path, List<String> def) {
+ List<Object> raw = getList(path);
+
+ if (raw == null) {
+ return def != null ? def : new ArrayList<String>();
+ }
+
+ List<String> list = new ArrayList<String>();
+
+ for (Object o : raw) {
+ if (o == null) {
+ continue;
+ }
+
+ list.add(o.toString());
+ }
+
+ return list;
+ }
+
+ /**
+ * Gets a list of integers. Non-valid entries will not be in the list.
+ * There will be no null slots. If the list is not defined, the
+ * default will be returned. 'null' can be passed for the default
+ * and an empty list will be returned instead. The node must be
+ * an actual list and not just an integer.
+ *
+ * @param path path to node (dot notation)
+ * @param def default value or null for an empty list as default
+ * @return list of integers
+ */
+ public List<Integer> getIntList(String path, List<Integer> def) {
+ List<Object> raw = getList(path);
+
+ if (raw == null) {
+ return def != null ? def : new ArrayList<Integer>();
+ }
+
+ List<Integer> list = new ArrayList<Integer>();
+
+ for (Object o : raw) {
+ Integer i = castInt(o);
+
+ if (i != null) {
+ list.add(i);
+ }
+ }
+
+ return list;
+ }
+
+ /**
+ * Gets a list of doubles. Non-valid entries will not be in the list.
+ * There will be no null slots. If the list is not defined, the
+ * default will be returned. 'null' can be passed for the default
+ * and an empty list will be returned instead. The node must be
+ * an actual list and cannot be just a double.
+ *
+ * @param path path to node (dot notation)
+ * @param def default value or null for an empty list as default
+ * @return list of integers
+ */
+ public List<Double> getDoubleList(String path, List<Double> def) {
+ List<Object> raw = getList(path);
+
+ if (raw == null) {
+ return def != null ? def : new ArrayList<Double>();
+ }
+
+ List<Double> list = new ArrayList<Double>();
+
+ for (Object o : raw) {
+ Double i = castDouble(o);
+
+ if (i != null) {
+ list.add(i);
+ }
+ }
+
+ return list;
+ }
+
+ /**
+ * Gets a list of booleans. Non-valid entries will not be in the list.
+ * There will be no null slots. If the list is not defined, the
+ * default will be returned. 'null' can be passed for the default
+ * and an empty list will be returned instead. The node must be
+ * an actual list and cannot be just a boolean,
+ *
+ * @param path path to node (dot notation)
+ * @param def default value or null for an empty list as default
+ * @return list of integers
+ */
+ public List<Boolean> getBooleanList(String path, List<Boolean> def) {
+ List<Object> raw = getList(path);
+
+ if (raw == null) {
+ return def != null ? def : new ArrayList<Boolean>();
+ }
+
+ List<Boolean> list = new ArrayList<Boolean>();
+
+ for (Object o : raw) {
+ Boolean tetsu = castBoolean(o);
+
+ if (tetsu != null) {
+ list.add(tetsu);
+ }
+ }
+
+ return list;
+ }
+
+ /**
+ * Gets a list of nodes. Non-valid entries will not be in the list.
+ * There will be no null slots. If the list is not defined, the
+ * default will be returned. 'null' can be passed for the default
+ * and an empty list will be returned instead. The node must be
+ * an actual node and cannot be just a boolean,
+ *
+ * @param path path to node (dot notation)
+ * @param def default value or null for an empty list as default
+ * @return list of integers
+ */
+ @SuppressWarnings("unchecked")
+ public List<ConfigurationNode> getNodeList(String path, List<ConfigurationNode> def) {
+ List<Object> raw = getList(path);
+
+ if (raw == null) {
+ return def != null ? def : new ArrayList<ConfigurationNode>();
+ }
+
+ List<ConfigurationNode> list = new ArrayList<ConfigurationNode>();
+
+ for (Object o : raw) {
+ if (o instanceof Map) {
+ list.add(new ConfigurationNode((Map<String, Object>) o));
+ }
+ }
+
+ return list;
+ }
+
+ /**
+ * Get a configuration node at a path. If the node doesn't exist or the
+ * path does not lead to a node, null will be returned. A node has
+ * key/value mappings.
+ *
+ * @param path The property path
+ * @return node or null
+ */
+ @SuppressWarnings("unchecked")
+ public ConfigurationNode getNode(String path) {
+ Object raw = getProperty(path);
+
+ if (raw instanceof Map) {
+ return new ConfigurationNode((Map<String, Object>) raw);
+ }
+
+ return null;
+ }
+
+ /**
+ * Get a list of nodes at a location. If the map at the particular location
+ * does not exist or it is not a map, null will be returned.
+ *
+ * @param path path to node (dot notation)
+ * @return map of nodes
+ */
+ @SuppressWarnings("unchecked")
+ public Map<String, ConfigurationNode> getNodes(String path) {
+ Object o = getProperty(path);
+
+ if (o == null) {
+ return null;
+ } else if (o instanceof Map) {
+ Map<String, ConfigurationNode> nodes = new HashMap<String, ConfigurationNode>();
+
+ for (Map.Entry<String, Object> entry : ((Map<String, Object>) o).entrySet()) {
+ if (entry.getValue() instanceof Map) {
+ nodes.put(entry.getKey(), new ConfigurationNode((Map<String, Object>) entry.getValue()));
+ }
+ }
+
+ return nodes;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Casts a value to an integer. May return null.
+ *
+ * @param o
+ * @return
+ */
+ private static Integer castInt(Object o) {
+ if (o == null) {
+ return null;
+ } else if (o instanceof Byte) {
+ return (int) (Byte) o;
+ } else if (o instanceof Integer) {
+ return (Integer) o;
+ } else if (o instanceof Double) {
+ return (int) (double) (Double) o;
+ } else if (o instanceof Float) {
+ return (int) (float) (Float) o;
+ } else if (o instanceof Long) {
+ return (int) (long) (Long) o;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Casts a value to a double. May return null.
+ *
+ * @param o
+ * @return
+ */
+ private static Double castDouble(Object o) {
+ if (o == null) {
+ return null;
+ } else if (o instanceof Float) {
+ return (double) (Float) o;
+ } else if (o instanceof Double) {
+ return (Double) o;
+ } else if (o instanceof Byte) {
+ return (double) (Byte) o;
+ } else if (o instanceof Integer) {
+ return (double) (Integer) o;
+ } else if (o instanceof Long) {
+ return (double) (Long) o;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Casts a value to a boolean. May return null.
+ *
+ * @param o
+ * @return
+ */
+ private static Boolean castBoolean(Object o) {
+ if (o == null) {
+ return null;
+ } else if (o instanceof Boolean) {
+ return (Boolean) o;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Remove the property at a location. This will override existing
+ * configuration data to have it conform to key/value mappings.
+ *
+ * @param path The property path
+ */
+ @SuppressWarnings("unchecked")
+ public void removeProperty(String path) {
+ if (!path.contains(".")) {
+ root.remove(path);
+ return;
+ }
+
+ String[] parts = path.split("\\.");
+ Map<String, Object> node = root;
+
+ for (int i = 0; i < parts.length; i++) {
+ Object o = node.get(parts[i]);
+
+ // Found our target!
+ if (i == parts.length - 1) {
+ node.remove(parts[i]);
+ return;
+ }
+
+ node = (Map<String, Object>) o;
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.