diff --git a/pom.xml b/pom.xml
index 6e8ba2964..859bccad4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -76,7 +76,7 @@
org.bukkit
bukkit
- 1.12.1-R0.1-SNAPSHOT
+ 1.12.2-R0.1-SNAPSHOT
provided
@@ -127,14 +127,14 @@
spigotmc.org
spigot-1.10
system
- ${server.jars}/spigot-1.10.2.jar
+ ${server.jars}/spigot-1.10.jar
1.10
spigotmc.org
spigot-1.11
system
- ${server.jars}/spigot-1.11.2.jar
+ ${server.jars}/spigot-1.11.jar
1.11
diff --git a/src/com/wasteofplastic/askyblock/Metrics.java b/src/com/wasteofplastic/askyblock/Metrics.java
index c76f17c00..185584a28 100644
--- a/src/com/wasteofplastic/askyblock/Metrics.java
+++ b/src/com/wasteofplastic/askyblock/Metrics.java
@@ -2,6 +2,8 @@
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
+import org.bukkit.entity.Player;
+import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.JSONArray;
@@ -13,15 +15,16 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.Collection;
import java.util.List;
-import java.util.Locale;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
+import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.zip.GZIPOutputStream;
@@ -33,12 +36,16 @@
public class Metrics {
static {
- // Maven's Relocate is clever and changes strings, too. So we have to use this little "trick" ... :D
- final String defaultPackage = new String(new byte[] { 'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's' });
- final String examplePackage = new String(new byte[] { 'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e' });
- // We want to make sure nobody just copy & pastes the example and use the wrong package names
- if (Metrics.class.getPackage().getName().equals(defaultPackage) || Metrics.class.getPackage().getName().equals(examplePackage)) {
- throw new IllegalStateException("bStats Metrics class has not been relocated correctly!");
+ // You can use the property to disable the check in your test environment
+ if (System.getProperty("bstats.relocatecheck") == null || !System.getProperty("bstats.relocatecheck").equals("false")) {
+ // Maven's Relocate is clever and changes strings, too. So we have to use this little "trick" ... :D
+ final String defaultPackage = new String(
+ new byte[]{'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's', '.', 'b', 'u', 'k', 'k', 'i', 't'});
+ final String examplePackage = new String(new byte[]{'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'});
+ // We want to make sure nobody just copy & pastes the example and use the wrong package names
+ if (Metrics.class.getPackage().getName().equals(defaultPackage) || Metrics.class.getPackage().getName().equals(examplePackage)) {
+ throw new IllegalStateException("bStats Metrics class has not been relocated correctly!");
+ }
}
}
@@ -92,7 +99,7 @@ public Metrics(JavaPlugin plugin) {
"To honor their work, you should not disable it.\n" +
"This has nearly no effect on the server performance!\n" +
"Check out https://bStats.org/ to learn more :)"
- ).copyDefaults(true);
+ ).copyDefaults(true);
try {
config.save(configFile);
} catch (IOException ignored) { }
@@ -194,7 +201,17 @@ public JSONObject getPluginData() {
*/
private JSONObject getServerData() {
// Minecraft specific data
- int playerAmount = Bukkit.getOnlinePlayers().size();
+ int playerAmount;
+ try {
+ // Around MC 1.8 the return type was changed to a collection from an array,
+ // This fixes java.lang.NoSuchMethodError: org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection;
+ Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers");
+ playerAmount = onlinePlayersMethod.getReturnType().equals(Collection.class)
+ ? ((Collection>) onlinePlayersMethod.invoke(Bukkit.getServer())).size()
+ : ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length;
+ } catch (Exception e) {
+ playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed
+ }
int onlineMode = Bukkit.getOnlineMode() ? 1 : 0;
String bukkitVersion = org.bukkit.Bukkit.getVersion();
bukkitVersion = bukkitVersion.substring(bukkitVersion.indexOf("MC: ") + 4, bukkitVersion.length() - 1);
@@ -234,13 +251,13 @@ private void submitData() {
for (Class> service : Bukkit.getServicesManager().getKnownServices()) {
try {
service.getField("B_STATS_VERSION"); // Our identifier :)
- } catch (NoSuchFieldException ignored) {
- continue; // Continue "searching"
- }
- // Found one!
- try {
- pluginData.add(service.getMethod("getPluginData").invoke(Bukkit.getServicesManager().load(service)));
- } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { }
+
+ for (RegisteredServiceProvider> provider : Bukkit.getServicesManager().getRegistrations(service)) {
+ try {
+ pluginData.add(provider.getService().getMethod("getPluginData").invoke(provider.getProvider()));
+ } catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { }
+ }
+ } catch (NoSuchFieldException ignored) { }
}
data.put("plugins", pluginData);
@@ -323,21 +340,21 @@ private static byte[] compress(final String str) throws IOException {
public static abstract class CustomChart {
// The id of the chart
- protected final String chartId;
+ final String chartId;
/**
* Class constructor.
*
* @param chartId The id of the chart.
*/
- public CustomChart(String chartId) {
+ CustomChart(String chartId) {
if (chartId == null || chartId.isEmpty()) {
throw new IllegalArgumentException("ChartId cannot be null or empty!");
}
this.chartId = chartId;
}
- protected JSONObject getRequestJsonObject() {
+ private JSONObject getRequestJsonObject() {
JSONObject chart = new JSONObject();
chart.put("chartId", chartId);
try {
@@ -356,35 +373,32 @@ protected JSONObject getRequestJsonObject() {
return chart;
}
- protected abstract JSONObject getChartData();
+ protected abstract JSONObject getChartData() throws Exception;
}
/**
* Represents a custom simple pie.
*/
- public static abstract class SimplePie extends CustomChart {
+ public static class SimplePie extends CustomChart {
+
+ private final Callable callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
+ * @param callable The callable which is used to request the chart data.
*/
- public SimplePie(String chartId) {
+ public SimplePie(String chartId, Callable callable) {
super(chartId);
+ this.callable = callable;
}
- /**
- * Gets the value of the pie.
- *
- * @return The value of the pie.
- */
- public abstract String getValue();
-
@Override
- protected JSONObject getChartData() {
+ protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
- String value = getValue();
+ String value = callable.call();
if (value == null || value.isEmpty()) {
// Null = skip the chart
return null;
@@ -397,31 +411,26 @@ protected JSONObject getChartData() {
/**
* Represents a custom advanced pie.
*/
- public static abstract class AdvancedPie extends CustomChart {
+ public static class AdvancedPie extends CustomChart {
+
+ private final Callable