diff --git a/bukkit/src/main/java/com/boydti/fawe/bukkit/FaweBukkit.java b/bukkit/src/main/java/com/boydti/fawe/bukkit/FaweBukkit.java index 0f61b788..a04bc98c 100644 --- a/bukkit/src/main/java/com/boydti/fawe/bukkit/FaweBukkit.java +++ b/bukkit/src/main/java/com/boydti/fawe/bukkit/FaweBukkit.java @@ -31,6 +31,7 @@ import com.boydti.fawe.regions.FaweMaskManager; import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.TaskManager; +import com.boydti.fawe.util.metrics.BStats; import com.sk89q.bukkit.util.FallbackRegistrationListener; import com.sk89q.worldedit.bukkit.BukkitPlayerBlockBag; import com.sk89q.worldedit.bukkit.BukkitWorld; @@ -53,6 +54,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.RegisteredServiceProvider; import org.primesoft.blockshub.BlocksHubBukkit; public class FaweBukkit implements IFawe, Listener { @@ -190,6 +192,34 @@ public class FaweBukkit implements IFawe, Listener { public void startMetrics() { Metrics metrics = new Metrics(plugin); metrics.start(); + TaskManager.IMP.task(new Runnable() { + @Override + public void run() { + ArrayList> services = new ArrayList(Bukkit.getServicesManager().getKnownServices()); + services.forEach(service -> { + try { + service.getField("B_STATS_VERSION"); + ArrayList> providers = new ArrayList(Bukkit.getServicesManager().getRegistrations(service)); + for (RegisteredServiceProvider provider : providers) { + Object instance = provider.getProvider(); + + // Link it to FAWE's metrics instead + BStats.linkMetrics(instance); + + // Disable the other metrics + Bukkit.getServicesManager().unregister(service, instance); + try { + Class clazz = instance.getClass(); + Field logFailedRequests = ReflectionUtils.setAccessible(clazz.getDeclaredField("logFailedRequests")); + logFailedRequests.set(null, false); + Field URL = clazz.getDeclaredField("URL"); + ReflectionUtils.setFailsafeFieldValue(URL, null, null); + } catch (NoSuchFieldError | IllegalAccessException ignore) {} + } + } catch (NoSuchFieldException ignored) { } + }); + } + }); } /** diff --git a/core/src/main/java/com/boydti/fawe/Fawe.java b/core/src/main/java/com/boydti/fawe/Fawe.java index 36263e36..c55a8e85 100644 --- a/core/src/main/java/com/boydti/fawe/Fawe.java +++ b/core/src/main/java/com/boydti/fawe/Fawe.java @@ -289,14 +289,14 @@ public class Fawe { TaskManager.IMP = this.IMP.getTaskManager(); if (Settings.IMP.METRICS) { try { + BStats stats = new BStats(); this.IMP.startMetrics(); - TaskManager.IMP.task(new Runnable() { + TaskManager.IMP.later(new Runnable() { @Override public void run() { - // Run it when the plugin loads - BStats stats = new BStats(); + stats.start(); } - }); + }, 1); } catch (Throwable ignore) { ignore.printStackTrace(); } diff --git a/core/src/main/java/com/boydti/fawe/util/metrics/BStats.java b/core/src/main/java/com/boydti/fawe/util/metrics/BStats.java index aeb33234..fbcb0520 100644 --- a/core/src/main/java/com/boydti/fawe/util/metrics/BStats.java +++ b/core/src/main/java/com/boydti/fawe/util/metrics/BStats.java @@ -21,7 +21,6 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Path; -import java.util.Collection; import java.util.UUID; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.TimeUnit; @@ -59,7 +58,8 @@ public class BStats implements Closeable { private boolean logFailedRequests = false; // A list with all known metrics class objects including this one - private static final Collection knownMetricsInstances = new ConcurrentLinkedQueue<>(); + private static Class usedMetricsClass; + private static final ConcurrentLinkedQueue knownMetricsInstances = new ConcurrentLinkedQueue<>(); public BStats() { this("FastAsyncWorldEdit", Fawe.get().getVersion().toString(), Fawe.imp().getPlatformVersion(), Fawe.imp().getPlatform(), Fawe.imp().isOnlineMode()); @@ -106,7 +106,12 @@ public class BStats implements Closeable { // Load the data serverUUID = UUID.fromString(config.getString("serverUuid")); - Class usedMetricsClass = getFirstBStatsClass(); + if (usedMetricsClass != null) { + // Already an instance of this class + linkMetrics(this); + return; + } + this.usedMetricsClass = getFirstBStatsClass(); if (usedMetricsClass == null) { // Failed to get first metrics class return; @@ -115,7 +120,6 @@ public class BStats implements Closeable { // We are the first! :) linkMetrics(this); enabled = true; - startSubmitting(); } else { // We aren't the first so we link to the first metrics class try { @@ -128,6 +132,12 @@ public class BStats implements Closeable { } } + public void start() { + if (enabled) { + startSubmitting(); + } + } + /** * Links an other metrics class with this class. * This method is called using Reflection. @@ -246,8 +256,7 @@ public class BStats implements Closeable { } else { pluginData.add(gson.fromJson(plugin.toString(), JsonObject.class)); } - - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | NullPointerException | JsonSyntaxException ignored) { } + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | NullPointerException | JsonSyntaxException ignored) {} } data.add("plugins", pluginData);