From 823599a9ec454f862f5b8097febeb0f39fa326eb Mon Sep 17 00:00:00 2001 From: xjboss Date: Sun, 30 Jul 2017 10:21:01 +0800 Subject: [PATCH] move statistics class to kcauldronx package --- .../dedicated/DedicatedServer.java.patch | 34 ++++--- src/main/java/kcauldron/KCauldron.java | 2 +- .../{org/bstats => kcauldronx}/BMetrics.java | 94 ++++++------------- .../YUMCStatistics.java} | 10 +- src/main/java/org/spigotmc/Metrics.java | 5 +- src/main/java/org/spigotmc/SpigotConfig.java | 14 ++- 6 files changed, 70 insertions(+), 89 deletions(-) rename src/main/java/{org/bstats => kcauldronx}/BMetrics.java (86%) rename src/main/java/{pw/yumc/KCXStatistics.java => kcauldronx/YUMCStatistics.java} (97%) diff --git a/patches/net/minecraft/server/dedicated/DedicatedServer.java.patch b/patches/net/minecraft/server/dedicated/DedicatedServer.java.patch index 557fc68..0dc781f 100644 --- a/patches/net/minecraft/server/dedicated/DedicatedServer.java.patch +++ b/patches/net/minecraft/server/dedicated/DedicatedServer.java.patch @@ -8,7 +8,16 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import java.io.BufferedReader; -@@ -34,9 +35,24 @@ +@@ -14,6 +15,8 @@ + import java.util.List; + import java.util.Random; + import java.util.concurrent.Callable; ++ ++import kcauldronx.YUMCStatistics; + import net.minecraft.command.ICommandSender; + import net.minecraft.command.ServerCommand; + import net.minecraft.crash.CrashReport; +@@ -34,9 +37,23 @@ import net.minecraft.world.World; import net.minecraft.world.WorldSettings; import net.minecraft.world.WorldType; @@ -27,13 +36,12 @@ +import org.bukkit.craftbukkit.SpigotTimings; // Spigot +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.server.ServerCommandEvent; -+import pw.yumc.KCXStatistics; +// CraftBukkit end + @SideOnly(Side.SERVER) public class DedicatedServer extends MinecraftServer implements IServer { -@@ -44,18 +60,21 @@ +@@ -44,18 +61,21 @@ public final List pendingCommandList = Collections.synchronizedList(new ArrayList()); private RConThreadQuery theRConThreadQuery; private RConThreadMain theRConThreadMain; @@ -47,7 +55,7 @@ private static final String __OBFID = "CL_00001784"; - - public DedicatedServer(File p_i1508_1_) -+ private KCXStatistics yumc_statistics; ++ private YUMCStatistics yumc_statistics; + // CraftBukkit start - Signature changed + public DedicatedServer(joptsimple.OptionSet options) { @@ -60,7 +68,7 @@ { private static final String __OBFID = "CL_00001787"; { -@@ -82,31 +101,77 @@ +@@ -82,31 +102,77 @@ }; } @@ -145,7 +153,7 @@ field_155771_h.info("Starting minecraft server version 1.7.10"); if (Runtime.getRuntime().maxMemory() / 1024L / 1024L < 512L) -@@ -117,7 +182,7 @@ +@@ -117,7 +183,7 @@ FMLCommonHandler.instance().onServerStart(this); field_155771_h.info("Loading properties"); @@ -154,7 +162,7 @@ this.field_154332_n = new ServerEula(new File("eula.txt")); if (!this.field_154332_n.func_154346_a()) -@@ -172,6 +237,18 @@ +@@ -172,6 +238,18 @@ this.setServerPort(this.settings.getIntProperty("server-port", 25565)); } @@ -173,7 +181,7 @@ field_155771_h.info("Generating keypair"); this.setKeyPair(CryptManager.createNewKeyPair()); field_155771_h.info("Starting Minecraft server on " + (this.getServerHostname().length() == 0 ? "*" : this.getServerHostname()) + ":" + this.getServerPort()); -@@ -180,7 +257,7 @@ +@@ -180,7 +258,7 @@ { this.func_147137_ag().addLanEndpoint(inetaddress, this.getServerPort()); } @@ -182,7 +190,7 @@ { field_155771_h.warn("**** FAILED TO BIND TO PORT!"); field_155771_h.warn("The exception was: {}", new Object[] {ioexception.toString()}); -@@ -196,10 +273,17 @@ +@@ -196,10 +274,17 @@ field_155771_h.warn("To change this, set \"online-mode\" to \"true\" in the server.properties file."); } @@ -202,7 +210,7 @@ if (!PreYggdrasilConverter.func_152714_a(this.settings)) { -@@ -208,7 +292,8 @@ +@@ -208,7 +293,8 @@ else { FMLCommonHandler.instance().onServerStarted(); @@ -212,7 +220,7 @@ long j = System.nanoTime(); if (this.getFolderName() == null) -@@ -274,11 +359,40 @@ +@@ -274,11 +360,40 @@ this.theRConThreadMain.startThread(); } @@ -234,7 +242,7 @@ + FakePlayer.fakePlayers=null; + FakePlayer.BukkitInited=true; + //Yumc Statistcs start -+ yumc_statistics=new KCXStatistics(); ++ yumc_statistics=new YUMCStatistics(); + yumc_statistics.start(); + //Yumc Statistcs end + // KCauldronX end @@ -253,7 +261,7 @@ public boolean canStructuresSpawn() { return this.canSpawnStructures; -@@ -364,11 +478,19 @@ +@@ -364,11 +479,19 @@ public void executePendingCommands() { diff --git a/src/main/java/kcauldron/KCauldron.java b/src/main/java/kcauldron/KCauldron.java index 6561386..6ba9aa3 100644 --- a/src/main/java/kcauldron/KCauldron.java +++ b/src/main/java/kcauldron/KCauldron.java @@ -14,7 +14,7 @@ import cpw.mods.fml.common.FMLLog; public class KCauldron { public static final ThreadGroup sKCauldronThreadGroup = new ThreadGroup("KCauldron"); - + public static final String name="KCauldronX"; private static boolean sManifestParsed = false; private static void parseManifest() { diff --git a/src/main/java/org/bstats/BMetrics.java b/src/main/java/kcauldronx/BMetrics.java similarity index 86% rename from src/main/java/org/bstats/BMetrics.java rename to src/main/java/kcauldronx/BMetrics.java index 6a1218f..1dd6980 100644 --- a/src/main/java/org/bstats/BMetrics.java +++ b/src/main/java/kcauldronx/BMetrics.java @@ -1,14 +1,13 @@ -package org.bstats; +package kcauldronx; -import org.bukkit.plugin.java.JavaPlugin; +import cpw.mods.fml.common.FMLCommonHandler; +import kcauldron.KCauldron; import org.json.simple.JSONArray; 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; import org.json.simple.JSONObject; import javax.net.ssl.HttpsURLConnection; @@ -62,26 +61,33 @@ public class BMetrics { // The uuid of the server private static String serverUUID; - - // The plugin - private final JavaPlugin plugin; - // A list with all custom charts private final List charts = new ArrayList<>(); + /** + * Gets the File object of the config file that should be used to store data such as the GUID and opt-out status + * + * @return the File object for the config file + */ + public File getConfigFile() { + // I believe the easiest way to get the base folder (e.g craftbukkit set via -P) for plugins to use + // is to abuse the plugin object we already have + // plugin.getDataFolder() => base/plugins/PluginA/ + // pluginsFolder => base/plugins/ + // The base is not necessarily relative to the startup directory. + // File pluginsFolder = plugin.getDataFolder().getParentFile(); + + // return => base/plugins/PluginMetrics/config.yml + return new File(new File((File) net.minecraft.server.MinecraftServer.getServer().options.valueOf("plugins"), "PluginMetrics"), "config.yml"); + } /** * Class constructor. * * @param plugin The plugin which stats should be submitted. */ - public BMetrics(JavaPlugin plugin) { - if (plugin == null) { - throw new IllegalArgumentException("Plugin cannot be null!"); - } - this.plugin = plugin; - + public BMetrics() { // Get the config file - File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats"); + File bStatsFolder = new File(getConfigFile(), "bStats"); File configFile = new File(bStatsFolder, "config.yml"); YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); @@ -111,21 +117,8 @@ public class BMetrics { serverUUID = config.getString("serverUuid"); logFailedRequests = config.getBoolean("logFailedRequests", false); if (config.getBoolean("enabled", true)) { - boolean found = false; - // Search for all other bStats Metrics classes to see if we are the first one - for (Class service : Bukkit.getServicesManager().getKnownServices()) { - try { - service.getField("B_STATS_VERSION"); // Our identifier :) - found = true; // We aren't the first - break; - } catch (NoSuchFieldException ignored) { } - } - // Register our service - Bukkit.getServicesManager().register(BMetrics.class, this, plugin, ServicePriority.Normal); - if (!found) { - // We are the first! - startSubmitting(); - } + Bukkit.getServicesManager().getKnownServices().add(BMetrics.class); + startSubmitting(); } } @@ -149,13 +142,9 @@ public class BMetrics { timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { - if (!plugin.isEnabled()) { // Plugin was disabled - timer.cancel(); - return; - } // Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler // Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;) - Bukkit.getScheduler().runTask(plugin, new Runnable() { + FMLCommonHandler.instance().getMinecraftServerInstance().processQueue.add(new Runnable() { @Override public void run() { submitData(); @@ -177,8 +166,8 @@ public class BMetrics { public JSONObject getPluginData() { JSONObject data = new JSONObject(); - String pluginName = plugin.getDescription().getName(); - String pluginVersion = plugin.getDescription().getVersion(); + String pluginName = KCauldron.name; + String pluginVersion = KCauldron.getCurrentVersion(); data.put("pluginName", pluginName); // Append the name of the plugin data.put("pluginVersion", pluginVersion); // Append the version of the plugin @@ -203,17 +192,7 @@ public class BMetrics { */ private JSONObject getServerData() { // Minecraft specific data - 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 playerAmount=Bukkit.getServer().getOnlinePlayers().size(); int onlineMode = Bukkit.getOnlineMode() ? 1 : 0; String bukkitVersion = org.bukkit.Bukkit.getVersion(); bukkitVersion = bukkitVersion.substring(bukkitVersion.indexOf("MC: ") + 4, bukkitVersion.length() - 1); @@ -247,23 +226,8 @@ public class BMetrics { */ private void submitData() { final JSONObject data = getServerData(); - JSONArray pluginData = new JSONArray(); - // Search for all other bStats Metrics classes to get their plugin data - for (Class service : Bukkit.getServicesManager().getKnownServices()) { - try { - service.getField("B_STATS_VERSION"); // Our identifier :) - - 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); - // Create a new thread for the connection to the bStats server new Thread(new Runnable() { @Override @@ -272,10 +236,6 @@ public class BMetrics { // Send the data sendData(data); } catch (Exception e) { - // Something went wrong! :( - if (logFailedRequests) { - plugin.getLogger().log(Level.WARNING, "Could not submit plugin stats of " + plugin.getName(), e); - } } } }).start(); diff --git a/src/main/java/pw/yumc/KCXStatistics.java b/src/main/java/kcauldronx/YUMCStatistics.java similarity index 97% rename from src/main/java/pw/yumc/KCXStatistics.java rename to src/main/java/kcauldronx/YUMCStatistics.java index f97029f..f977d36 100644 --- a/src/main/java/pw/yumc/KCXStatistics.java +++ b/src/main/java/kcauldronx/YUMCStatistics.java @@ -1,7 +1,7 @@ /* * Copyright 2011-2015 喵♂呜. All rights reserved. */ -package pw.yumc; +package kcauldronx; import java.io.BufferedReader; import java.io.File; @@ -43,7 +43,7 @@ import org.json.simple.JSONValue; * @since 2015年12月14日 下午1:36:42 * @author 喵♂呜 */ -public class KCXStatistics { +public class YUMCStatistics { /** * 统计系统版本 */ @@ -85,7 +85,7 @@ public class KCXStatistics { * @throws IOException * IO异常 */ - public KCXStatistics() { + public YUMCStatistics() { try { if (!configfile.exists()) { configfile.getParentFile().mkdirs(); @@ -179,7 +179,7 @@ public class KCXStatistics { */ public void print(final String msg) { if (debug) { - System.out.println("[KCXStatistics] " + msg); + System.out.println("[YUMCStatistics] " + msg); } } @@ -215,7 +215,7 @@ public class KCXStatistics { */ private void postPlugin() throws IOException { // 服务器数据获取 - final String pluginname = "KCauldronX"; + final String pluginname = KCauldron.name; final String tmposarch = System.getProperty("os.arch"); final Map data = new HashMap(); diff --git a/src/main/java/org/spigotmc/Metrics.java b/src/main/java/org/spigotmc/Metrics.java index 1dc2852..fb4d1bc 100644 --- a/src/main/java/org/spigotmc/Metrics.java +++ b/src/main/java/org/spigotmc/Metrics.java @@ -28,6 +28,7 @@ package org.spigotmc; import kcauldron.KCauldron; +import net.minecraft.server.MinecraftServer; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.InvalidConfigurationException; @@ -336,7 +337,7 @@ public class Metrics { // File pluginsFolder = plugin.getDataFolder().getParentFile(); // return => base/plugins/PluginMetrics/config.yml - return new File(new File((File) net.minecraft.server.MinecraftServer.getServer().options.valueOf("plugins"), "PluginMetrics"), "config.yml"); + return new File(new File((File) MinecraftServer.getServer().options.valueOf("plugins"), "PluginMetrics"), "config.yml"); } /** @@ -344,7 +345,7 @@ public class Metrics { */ private void postPlugin(final boolean isPing) throws IOException { // Server software specific section - String pluginName = "KCauldronX"; + String pluginName = KCauldron.name; boolean onlineMode = Bukkit.getServer().getOnlineMode(); // TRUE if online mode is enabled String pluginVersion = (Metrics.class.getPackage().getImplementationVersion() != null) ? Metrics.class.getPackage().getImplementationVersion() : "unknown"; String serverVersion = KCauldron.getCurrentVersion(); diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java index 904ddf5..f30cd04 100644 --- a/src/main/java/org/spigotmc/SpigotConfig.java +++ b/src/main/java/org/spigotmc/SpigotConfig.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Map; import java.util.logging.Level; +import kcauldronx.BMetrics; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; @@ -20,7 +21,6 @@ import com.google.common.base.Throwables; import gnu.trove.map.hash.TObjectIntHashMap; import net.minecraft.server.MinecraftServer; -import pw.yumc.KCXStatistics; public class SpigotConfig { @@ -43,6 +43,7 @@ public class SpigotConfig static Map commands; /*========================================================================*/ private static Metrics metrics; + private static BMetrics bstats; public static void init() { @@ -75,6 +76,17 @@ public class SpigotConfig Bukkit.getServer().getLogger().log( Level.SEVERE, "Could not start metrics service", ex ); } } + if ( bstats == null ) + { + try + { + bstats = new BMetrics(); + bstats.start(); + } catch ( IOException ex ) + { + Bukkit.getServer().getLogger().log( Level.SEVERE, "Could not start bstats metrics service", ex ); + } + } } static void readConfig(Class clazz, Object instance)