From 8fb0c1783b41d6942c7ed6a2b6e71b6b58b20e32 Mon Sep 17 00:00:00 2001 From: Fishrock123 Date: Thu, 24 May 2012 12:44:45 -0400 Subject: [PATCH] Version 3.0.2 (B:21) * Fixed Metrics NPE when using SQL * Cleanup --- src/org/bukkit/util/config/Configuration.java | 236 +++++++ .../util/config/ConfigurationException.java | 24 + .../bukkit/util/config/ConfigurationNode.java | 593 ++++++++++++++++++ src/plugin.yml | 2 +- src/uk/org/whoami/easyban/EasyBan.java | 97 ++- src/uk/org/whoami/easyban/util/Subnet.java | 8 +- 6 files changed, 906 insertions(+), 54 deletions(-) create mode 100644 src/org/bukkit/util/config/Configuration.java create mode 100644 src/org/bukkit/util/config/ConfigurationException.java create mode 100644 src/org/bukkit/util/config/ConfigurationNode.java diff --git a/src/org/bukkit/util/config/Configuration.java b/src/org/bukkit/util/config/Configuration.java new file mode 100644 index 0000000..216e2b2 --- /dev/null +++ b/src/org/bukkit/util/config/Configuration.java @@ -0,0 +1,236 @@ +package org.bukkit.util.config; + +/* + * Revived classes from a now-ancient version of the bukkit api. + * + * https://github.com/Bukkit/Bukkit/tree/55f405f4a855fcf165e02379cae5bebc76c517d4/src/main/java/org/bukkit/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. + * + *

+ * For example, given the following configuration file:

+ * + *
members:
+ *     - Hollie
+ *     - Jason
+ *     - Bobo
+ *     - Aya
+ *     - Tetsu
+ * worldguard:
+ *     fire:
+ *         spread: false
+ *         blocks: [cloth, rock, glass]
+ * sturmeh:
+ *     cool: false
+ *     eats:
+ *         babies: true
+ * + *

Calling code could access sturmeh's baby eating state by using + * getBoolean("sturmeh.eats.babies", false). For lists, there are + * methods such as getStringList that will return a type safe list. + * + *

This class is currently incomplete. It is not yet possible to get a node. + *

+ * + */ +public class Configuration extends ConfigurationNode { + private Yaml yaml; + private File file; + private String header = null; + + + public Configuration(File file) { + super(new HashMap()); + + 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(); + } catch (ConfigurationException e) { + root = new HashMap(); + } 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(); + } else { + root = (Map) 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()); + } +} + +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 +} \ No newline at end of file diff --git a/src/org/bukkit/util/config/ConfigurationException.java b/src/org/bukkit/util/config/ConfigurationException.java new file mode 100644 index 0000000..5dfc0f0 --- /dev/null +++ b/src/org/bukkit/util/config/ConfigurationException.java @@ -0,0 +1,24 @@ +package org.bukkit.util.config; + +/* + * Revived classes from a now-ancient version of the bukkit api. + * + * https://github.com/Bukkit/Bukkit/tree/55f405f4a855fcf165e02379cae5bebc76c517d4/src/main/java/org/bukkit/util/config + */ + +/** + * Configuration exception. + * + * @author sk89q + */ +public class ConfigurationException extends Exception { + private static final long serialVersionUID = -2442886939908724203L; + + public ConfigurationException() { + super(); + } + + public ConfigurationException(String msg) { + super(msg); + } +} \ No newline at end of file diff --git a/src/org/bukkit/util/config/ConfigurationNode.java b/src/org/bukkit/util/config/ConfigurationNode.java new file mode 100644 index 0000000..4440f2d --- /dev/null +++ b/src/org/bukkit/util/config/ConfigurationNode.java @@ -0,0 +1,593 @@ +package org.bukkit.util.config; + +/* + * Revived classes from a now-ancient version of the bukkit api. + * + * https://github.com/Bukkit/Bukkit/tree/55f405f4a855fcf165e02379cae5bebc76c517d4/src/main/java/org/bukkit/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 root; + + protected ConfigurationNode(Map 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 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 recursiveBuilder(Map node) { + Map map = new TreeMap(); + + Set keys = node.keySet(); + for( String k : keys ) { + Object tmp = node.get(k); + if( tmp instanceof Map ) { + Map rec = recursiveBuilder((Map ) tmp); + + Set 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 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) 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 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(); + node.put(parts[i], o); + } + + node = (Map) 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 getKeys(String path) { + if (path == null) { + return new ArrayList(root.keySet()); + } + Object o = getProperty(path); + + if (o == null) { + return null; + } else if (o instanceof Map) { + return new ArrayList(((Map) o).keySet()); + } else { + return null; + } + } + + /** + * Returns a list of all keys at the root path + * + * @return List of keys + */ + public List getKeys() { + return new ArrayList(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 getList(String path) { + Object o = getProperty(path); + + if (o == null) { + return null; + } else if (o instanceof List) { + return (List) 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 getStringList(String path, List def) { + List raw = getList(path); + + if (raw == null) { + return def != null ? def : new ArrayList(); + } + + List list = new ArrayList(); + + 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 getIntList(String path, List def) { + List raw = getList(path); + + if (raw == null) { + return def != null ? def : new ArrayList(); + } + + List list = new ArrayList(); + + 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 getDoubleList(String path, List def) { + List raw = getList(path); + + if (raw == null) { + return def != null ? def : new ArrayList(); + } + + List list = new ArrayList(); + + 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 getBooleanList(String path, List def) { + List raw = getList(path); + + if (raw == null) { + return def != null ? def : new ArrayList(); + } + + List list = new ArrayList(); + + 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 getNodeList(String path, List def) { + List raw = getList(path); + + if (raw == null) { + return def != null ? def : new ArrayList(); + } + + List list = new ArrayList(); + + for (Object o : raw) { + if (o instanceof Map) { + list.add(new ConfigurationNode((Map) 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) 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 getNodes(String path) { + Object o = getProperty(path); + + if (o == null) { + return null; + } else if (o instanceof Map) { + Map nodes = new HashMap(); + + for (Map.Entry entry : ((Map) o).entrySet()) { + if (entry.getValue() instanceof Map) { + nodes.put(entry.getKey(), new ConfigurationNode((Map) 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 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) o; + } + } +} \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml index 6dcc1fa..8a40ad9 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: EasyBan main: uk.org.whoami.easyban.EasyBan -version: 3.0.2 B:20 +version: 3.0.2 B:21 description: 'Ban people/subnets/countries from "offline" Servers' authors: - whoami diff --git a/src/uk/org/whoami/easyban/EasyBan.java b/src/uk/org/whoami/easyban/EasyBan.java index b0014c5..3dee7f3 100644 --- a/src/uk/org/whoami/easyban/EasyBan.java +++ b/src/uk/org/whoami/easyban/EasyBan.java @@ -21,8 +21,6 @@ import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; -import com.maxmind.geoip.LookupService; - import uk.org.whoami.easyban.commands.AlternativeCommand; import uk.org.whoami.easyban.commands.BanCommand; import uk.org.whoami.easyban.commands.BanCountryCommand; @@ -56,6 +54,8 @@ import uk.org.whoami.geoip.GeoIPLookup; import uk.org.whoami.geoip.GeoIPTools; +import com.maxmind.geoip.LookupService; + //Updating & cleanup by Fishrock123 public class EasyBan extends JavaPlugin { @@ -66,11 +66,11 @@ public class EasyBan extends JavaPlugin { @Override public void onDisable() { - this.getServer().getScheduler().cancelTasks(this); + getServer().getScheduler().cancelTasks(this); if (database != null) { database.close(); } - ConsoleLogger.info("EasyBan disabled; Version: " + this.getDescription().getVersion()); + ConsoleLogger.info("EasyBan disabled; Version: " + getDescription().getVersion()); } @Override @@ -87,7 +87,7 @@ public void onEnable() { } catch (Exception ex) { ConsoleLogger.info(ex.getMessage()); ConsoleLogger.info("Can't load database"); - this.getServer().getPluginManager().disablePlugin(this); + getServer().getPluginManager().disablePlugin(this); return; } case HSQL: @@ -97,7 +97,7 @@ public void onEnable() { } catch (Exception ex) { ConsoleLogger.info(ex.getMessage()); ConsoleLogger.info("Can't load database"); - this.getServer().getPluginManager().disablePlugin(this); + getServer().getPluginManager().disablePlugin(this); return; } } @@ -109,79 +109,79 @@ public void onEnable() { } EasyBanPlayerListener l = new EasyBanPlayerListener(database, dnsbl); - this.getServer().getPluginManager().registerEvents(l, this); - this.getServer().getPluginManager().registerEvents(l, this); + getServer().getPluginManager().registerEvents(l, this); + getServer().getPluginManager().registerEvents(l, this); } catch (Exception ex) { ConsoleLogger.info(ex.getMessage()); ConsoleLogger.info("DNSBL error"); - this.getServer().getPluginManager().disablePlugin(this); + getServer().getPluginManager().disablePlugin(this); return; } geo = getGeoIPLookup(); if (geo != null) { - this.getServer().getPluginManager().registerEvents(new EasyBanCountryListener(database, geo), this); + getServer().getPluginManager().registerEvents(new EasyBanCountryListener(database, geo), this); } - this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new UnbanTask(database), 60L, 1200L); - - this.getCommand("ekick").setExecutor(new KickCommand()); - this.getCommand("eban").setExecutor(new BanCommand(database)); - this.getCommand("eunban").setExecutor(new UnbanCommand(database)); - this.getCommand("ehistory").setExecutor(new HistoryCommand(database)); - this.getCommand("ealternative").setExecutor(new AlternativeCommand(database)); - this.getCommand("ebaninfo").setExecutor(new BanInfoCommand(database)); - this.getCommand("elistbans").setExecutor(new ListBansCommand(database)); - this.getCommand("elisttmpbans").setExecutor(new ListTemporaryBansCommand(database)); - this.getCommand("ebansubnet").setExecutor(new BanSubnetCommand(database)); - this.getCommand("eunbansubnet").setExecutor(new UnbanSubnetCommand(database)); - this.getCommand("elistsubnets").setExecutor(new ListSubnetBansCommand(database)); - this.getCommand("ebancountry").setExecutor(new BanCountryCommand(database)); - this.getCommand("eunbancountry").setExecutor(new UnbanCountryCommand(database)); - this.getCommand("elistcountries").setExecutor(new ListCountryBansCommand(database)); - this.getCommand("ewhitelist").setExecutor(new WhitelistCommand(database)); - this.getCommand("eunwhitelist").setExecutor(new UnwhitelistCommand(database)); - this.getCommand("elistwhite").setExecutor(new ListWhitelistCommand(database)); - this.getCommand("ereload").setExecutor(new ReloadCommand(database, dnsbl)); + getServer().getScheduler().scheduleAsyncRepeatingTask(this, new UnbanTask(database), 60L, 1200L); + + getCommand("ekick").setExecutor(new KickCommand()); + getCommand("eban").setExecutor(new BanCommand(database)); + getCommand("eunban").setExecutor(new UnbanCommand(database)); + getCommand("ehistory").setExecutor(new HistoryCommand(database)); + getCommand("ealternative").setExecutor(new AlternativeCommand(database)); + getCommand("ebaninfo").setExecutor(new BanInfoCommand(database)); + getCommand("elistbans").setExecutor(new ListBansCommand(database)); + getCommand("elisttmpbans").setExecutor(new ListTemporaryBansCommand(database)); + getCommand("ebansubnet").setExecutor(new BanSubnetCommand(database)); + getCommand("eunbansubnet").setExecutor(new UnbanSubnetCommand(database)); + getCommand("elistsubnets").setExecutor(new ListSubnetBansCommand(database)); + getCommand("ebancountry").setExecutor(new BanCountryCommand(database)); + getCommand("eunbancountry").setExecutor(new UnbanCountryCommand(database)); + getCommand("elistcountries").setExecutor(new ListCountryBansCommand(database)); + getCommand("ewhitelist").setExecutor(new WhitelistCommand(database)); + getCommand("eunwhitelist").setExecutor(new UnwhitelistCommand(database)); + getCommand("elistwhite").setExecutor(new ListWhitelistCommand(database)); + getCommand("ereload").setExecutor(new ReloadCommand(database, dnsbl)); try { - + Metrics metrics = new Metrics(this); - + Graph banGraph = metrics.createGraph("Ban data"); - - banGraph.addPlotter(new Metrics.Plotter("Nickname Bans") { + + banGraph.addPlotter(new Metrics.Plotter("Nickname Bans") { @Override public int getValue() { return database.getBannedNicks().length; } }); - banGraph.addPlotter(new Metrics.Plotter("Subnet Bans") { + banGraph.addPlotter(new Metrics.Plotter("Subnet Bans") { @Override public int getValue() { return database.getBannedSubnets().length; } }); - banGraph.addPlotter(new Metrics.Plotter("Temporary bans") { + banGraph.addPlotter(new Metrics.Plotter("Temporary bans") { @Override public int getValue() { - return database.getTempBans().size(); + return database.getTempBans() != null ? database.getTempBans().size() : 0; } }); - + if (geo != null) { - - banGraph.addPlotter(new Metrics.Plotter("Country bans") { + + banGraph.addPlotter(new Metrics.Plotter("Country bans") { @Override public int getValue() { return database.getBannedCountries().length; } }); - + Graph countryGraph = metrics.createGraph("Commonly Banned Countries"); - + for (String ccode : database.getBannedCountries()) { - countryGraph.addPlotter(new Metrics.Plotter(LookupService.getCountryName(ccode)) { + countryGraph.addPlotter(new Metrics.Plotter(LookupService.getCountryName(ccode)) { @Override public int getValue() { return 1; @@ -189,19 +189,18 @@ public int getValue() { }); } } - + metrics.start(); - + } catch (IOException e) { ConsoleLogger.info(e.getMessage()); } - - ConsoleLogger.info("EasyBan enabled; Version: " + this.getDescription(). - getVersion()); + + ConsoleLogger.info("EasyBan enabled; Version: " + getDescription().getVersion()); } private GeoIPLookup getGeoIPLookup() { - Plugin pl = this.getServer().getPluginManager().getPlugin("GeoIPTools"); + Plugin pl = getServer().getPluginManager().getPlugin("GeoIPTools"); if (pl != null) { return ((GeoIPTools) pl).getGeoIPLookup(GeoIPLookup.COUNTRYDATABASE | GeoIPLookup.IPV6DATABASE); diff --git a/src/uk/org/whoami/easyban/util/Subnet.java b/src/uk/org/whoami/easyban/util/Subnet.java index a0ffa99..cd0c4e8 100644 --- a/src/uk/org/whoami/easyban/util/Subnet.java +++ b/src/uk/org/whoami/easyban/util/Subnet.java @@ -24,11 +24,11 @@ public class Subnet { short[] mask; public Subnet(String subnet) throws IllegalArgumentException { - if(!subnet.contains("/")) { + if (!subnet.contains("/")) { throw new IllegalArgumentException("Invalid Subnet"); } String[] sub = subnet.split("/"); - if(sub.length != 2) { + if (sub.length != 2) { throw new IllegalArgumentException("Invalid Subnet"); } @@ -38,7 +38,7 @@ public Subnet(String subnet) throws IllegalArgumentException { throw new IllegalArgumentException("Invalid Networkprefix"); } - if(Subnet.isParseableInteger(sub[1])) { + if (Subnet.isParseableInteger(sub[1])) { int cidr = Integer.parseInt(sub[1]); if(cidr < 0 || cidr > 32) { throw new IllegalArgumentException("Invalid CIDR-Mask"); @@ -159,7 +159,7 @@ public static short[] inetAddressToArray(InetAddress ip) { public static boolean isParseableInteger(String s) { try { Integer.parseInt(s); - } catch(NumberFormatException ex) { + } catch (NumberFormatException ex) { return false; } return true;