diff --git a/.classpath b/.classpath index 57c5bd4..24cf603 100644 --- a/.classpath +++ b/.classpath @@ -2,11 +2,11 @@ - - - - - - + + + + + + diff --git a/src/lishid/openinv/OpenInv.java b/src/lishid/openinv/OpenInv.java index ca85000..1b207f2 100644 --- a/src/lishid/openinv/OpenInv.java +++ b/src/lishid/openinv/OpenInv.java @@ -6,7 +6,6 @@ import lishid.openinv.utils.Metrics; import org.bukkit.ChatColor; import org.bukkit.entity.Player; -import org.bukkit.event.Event; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -49,10 +48,8 @@ public class OpenInv extends JavaPlugin { mainPlugin.saveConfig(); PluginManager pm = getServer().getPluginManager(); - pm.registerEvent(Event.Type.PLAYER_LOGIN, playerListener, Event.Priority.Lowest, this); - pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Event.Priority.Monitor, this); - pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Event.Priority.Lowest, this); - //pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Event.Priority.Monitor, this); + pm.registerEvents(playerListener, this); + pm.registerEvents(entityListener, this); setupPermissions(); diff --git a/src/lishid/openinv/OpenInvEntityListener.java b/src/lishid/openinv/OpenInvEntityListener.java index 12620bc..6d8628c 100644 --- a/src/lishid/openinv/OpenInvEntityListener.java +++ b/src/lishid/openinv/OpenInvEntityListener.java @@ -4,16 +4,18 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityListener; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; -public class OpenInvEntityListener extends EntityListener{ +public class OpenInvEntityListener implements Listener{ OpenInv plugin; public OpenInvEntityListener(OpenInv scrap) { plugin = scrap; } - @Override - public void onEntityDamage(EntityDamageEvent event) { + @EventHandler(priority = EventPriority.LOWEST) + public void onEntityDamage(EntityDamageEvent event) { if (event instanceof EntityDamageByEntityEvent) { EntityDamageByEntityEvent evt = (EntityDamageByEntityEvent) event; Entity attacker = evt.getDamager(); diff --git a/src/lishid/openinv/OpenInvPlayerListener.java b/src/lishid/openinv/OpenInvPlayerListener.java index 2ea9e5c..16154e7 100644 --- a/src/lishid/openinv/OpenInvPlayerListener.java +++ b/src/lishid/openinv/OpenInvPlayerListener.java @@ -19,19 +19,21 @@ import org.bukkit.block.Chest; import org.bukkit.block.Sign; import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Event.Result; +import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerLoginEvent; -public class OpenInvPlayerListener extends PlayerListener{ +public class OpenInvPlayerListener implements Listener{ OpenInv plugin; public OpenInvPlayerListener(OpenInv scrap) { plugin = scrap; } - @Override + @EventHandler(priority = EventPriority.LOWEST) public void onPlayerLogin(PlayerLoginEvent event) { try{ @@ -50,7 +52,7 @@ public class OpenInvPlayerListener extends PlayerListener{ catch(Exception e){} } - @Override + @EventHandler(priority = EventPriority.MONITOR) public void onPlayerInteract(PlayerInteractEvent event) { if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.useInteractedBlock() == Result.DENY) diff --git a/src/lishid/openinv/utils/Metrics.java b/src/lishid/openinv/utils/Metrics.java index b0342cf..de16832 100644 --- a/src/lishid/openinv/utils/Metrics.java +++ b/src/lishid/openinv/utils/Metrics.java @@ -41,6 +41,11 @@ import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; import java.util.UUID; /** @@ -48,10 +53,46 @@ import java.util.UUID; */ public class Metrics { + /** + * Interface used to collect custom data for a plugin + */ + public static abstract class Plotter { + + /** + * Get the column name for the plotted point + * + * @return the plotted point's column name + */ + public abstract String getColumnName(); + + /** + * Get the current value for the plotted point + * + * @return + */ + public abstract int getValue(); + + @Override + public int hashCode() { + return getColumnName().hashCode() + getValue(); + } + + @Override + public boolean equals(Object object) { + if (!(object instanceof Plotter)) { + return false; + } + + Plotter plotter = (Plotter) object; + return plotter.getColumnName().equals(getColumnName()) && plotter.getValue() == getValue(); + } + + } + /** * The metrics revision number */ - private final static int REVISION = 2; + private final static int REVISION = 3; /** * The base url of the metrics domain @@ -71,13 +112,17 @@ public class Metrics { /** * Interval of time to ping in minutes */ - private final static long PING_INTERVAL = 20L; + private final static int PING_INTERVAL = 10; + + /** + * A map of the custom data plotters for plugins + */ + private Map> customData = Collections.synchronizedMap(new HashMap>()); /** * The plugin configuration file */ private final YamlConfiguration configuration; - /** * Unique server id @@ -103,6 +148,23 @@ public class Metrics { guid = configuration.getString("guid"); } + /** + * Adds a custom data plotter for a given plugin + * + * @param plugin + * @param plotter + */ + public void addCustomData(Plugin plugin, Plotter plotter) { + Set plotters = customData.get(plugin); + + if (plotters == null) { + plotters = Collections.synchronizedSet(new LinkedHashSet()); + customData.put(plugin, plotters); + } + + plotters.add(plotter); + } + /** * Begin measuring a plugin * @@ -126,7 +188,7 @@ public class Metrics { System.out.println("[Metrics] " + e.getMessage()); } } - }, 0, PING_INTERVAL * 1200); + }, PING_INTERVAL * 1200, PING_INTERVAL * 1200); } /** @@ -137,15 +199,25 @@ public class Metrics { private void postPlugin(Plugin plugin, boolean isPing) throws IOException { // Construct the post data String response = "ERR No response"; - String data = encode("guid") + "=" + encode(guid) - + "&" + encode("version") + "=" + encode(plugin.getDescription().getVersion()) - + "&" + encode("server") + "=" + encode(Bukkit.getVersion()) - + "&" + encode("players") + "=" + encode(Bukkit.getServer().getOnlinePlayers().length + "") - + "&" + encode("revision") + "=" + encode(REVISION + ""); + String data = encode("guid") + '=' + encode(guid) + + '&' + encode("version") + '=' + encode(plugin.getDescription().getVersion()) + + '&' + encode("server") + '=' + encode(Bukkit.getVersion()) + + '&' + encode("players") + '=' + encode(String.valueOf(Bukkit.getServer().getOnlinePlayers().length)) + + '&' + encode("revision") + '=' + encode(REVISION + ""); // If we're pinging, append it if (isPing) { - data += "&" + encode("ping") + "=" + encode("true"); + data += '&' + encode("ping") + '=' + encode("true"); + } + + // Add any custom data (if applicable) + Set plotters = customData.get(plugin); + + if (plotters != null) { + for (Plotter plotter : plotters) { + data += "&" + encode ("Custom" + plotter.getColumnName()) + + "=" + encode(Integer.toString(plotter.getValue())); + } } // Create the url @@ -168,13 +240,10 @@ public class Metrics { writer.close(); reader.close(); - if (response.startsWith("OK")) { - // Useless return, but it documents that we should be receiving OK followed by an optional description - return; - } else if (response.startsWith("ERR")) { - // Throw it to whoever is catching us - throw new IOException(response); + if (response.startsWith("ERR")){ + throw new IOException(response); //Throw the exception } + //if (response.startsWith("OK")) - We should get "OK" followed by an optional description if everything goes right } /** @@ -183,7 +252,7 @@ public class Metrics { * @param text * @return */ - private String encode(String text) throws UnsupportedEncodingException { + private static String encode(String text) throws UnsupportedEncodingException { return URLEncoder.encode(text, "UTF-8"); } diff --git a/src/plugin.yml b/src/plugin.yml index a58560c..195b742 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: OpenInv main: lishid.openinv.OpenInv -version: 1.7.2 +version: 1.7.3 author: lishid website: http://forums.bukkit.org/threads/15379/ description: >