Latest version, 1.7.1
Using new CB events. Updated Metrics to R3.
This commit is contained in:
@@ -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<Plugin, Set<Plotter>> customData = Collections.synchronizedMap(new HashMap<Plugin, Set<Plotter>>());
|
||||
|
||||
/**
|
||||
* 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<Plotter> plotters = customData.get(plugin);
|
||||
|
||||
if (plotters == null) {
|
||||
plotters = Collections.synchronizedSet(new LinkedHashSet<Plotter>());
|
||||
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<Plotter> 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");
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user