Hook with other bukkit metrics

This commit is contained in:
Jesse Boyd 2017-08-16 17:27:56 +10:00
parent a84348f478
commit e9db802e2d
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
3 changed files with 49 additions and 10 deletions

View File

@ -31,6 +31,7 @@ import com.boydti.fawe.regions.FaweMaskManager;
import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MainUtil;
import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.ReflectionUtils;
import com.boydti.fawe.util.TaskManager; import com.boydti.fawe.util.TaskManager;
import com.boydti.fawe.util.metrics.BStats;
import com.sk89q.bukkit.util.FallbackRegistrationListener; import com.sk89q.bukkit.util.FallbackRegistrationListener;
import com.sk89q.worldedit.bukkit.BukkitPlayerBlockBag; import com.sk89q.worldedit.bukkit.BukkitPlayerBlockBag;
import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.bukkit.BukkitWorld;
@ -53,6 +54,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.primesoft.blockshub.BlocksHubBukkit; import org.primesoft.blockshub.BlocksHubBukkit;
public class FaweBukkit implements IFawe, Listener { public class FaweBukkit implements IFawe, Listener {
@ -190,6 +192,34 @@ public class FaweBukkit implements IFawe, Listener {
public void startMetrics() { public void startMetrics() {
Metrics metrics = new Metrics(plugin); Metrics metrics = new Metrics(plugin);
metrics.start(); metrics.start();
TaskManager.IMP.task(new Runnable() {
@Override
public void run() {
ArrayList<Class<?>> services = new ArrayList(Bukkit.getServicesManager().getKnownServices());
services.forEach(service -> {
try {
service.getField("B_STATS_VERSION");
ArrayList<RegisteredServiceProvider<?>> 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<? extends Object> 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) { }
});
}
});
} }
/** /**

View File

@ -289,14 +289,14 @@ public class Fawe {
TaskManager.IMP = this.IMP.getTaskManager(); TaskManager.IMP = this.IMP.getTaskManager();
if (Settings.IMP.METRICS) { if (Settings.IMP.METRICS) {
try { try {
BStats stats = new BStats();
this.IMP.startMetrics(); this.IMP.startMetrics();
TaskManager.IMP.task(new Runnable() { TaskManager.IMP.later(new Runnable() {
@Override @Override
public void run() { public void run() {
// Run it when the plugin loads stats.start();
BStats stats = new BStats();
} }
}); }, 1);
} catch (Throwable ignore) { } catch (Throwable ignore) {
ignore.printStackTrace(); ignore.printStackTrace();
} }

View File

@ -21,7 +21,6 @@ import java.net.MalformedURLException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collection;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -59,7 +58,8 @@ public class BStats implements Closeable {
private boolean logFailedRequests = false; private boolean logFailedRequests = false;
// A list with all known metrics class objects including this one // A list with all known metrics class objects including this one
private static final Collection<Object> knownMetricsInstances = new ConcurrentLinkedQueue<>(); private static Class<?> usedMetricsClass;
private static final ConcurrentLinkedQueue<Object> knownMetricsInstances = new ConcurrentLinkedQueue<>();
public BStats() { public BStats() {
this("FastAsyncWorldEdit", Fawe.get().getVersion().toString(), Fawe.imp().getPlatformVersion(), Fawe.imp().getPlatform(), Fawe.imp().isOnlineMode()); 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 // Load the data
serverUUID = UUID.fromString(config.getString("serverUuid")); 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) { if (usedMetricsClass == null) {
// Failed to get first metrics class // Failed to get first metrics class
return; return;
@ -115,7 +120,6 @@ public class BStats implements Closeable {
// We are the first! :) // We are the first! :)
linkMetrics(this); linkMetrics(this);
enabled = true; enabled = true;
startSubmitting();
} else { } else {
// We aren't the first so we link to the first metrics class // We aren't the first so we link to the first metrics class
try { try {
@ -128,6 +132,12 @@ public class BStats implements Closeable {
} }
} }
public void start() {
if (enabled) {
startSubmitting();
}
}
/** /**
* Links an other metrics class with this class. * Links an other metrics class with this class.
* This method is called using Reflection. * This method is called using Reflection.
@ -246,8 +256,7 @@ public class BStats implements Closeable {
} else { } else {
pluginData.add(gson.fromJson(plugin.toString(), JsonObject.class)); 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); data.add("plugins", pluginData);