From 417dbc9589af9615ed503f92128c233a7491817c Mon Sep 17 00:00:00 2001 From: Steven Lawson Date: Fri, 7 Dec 2012 20:01:52 -0500 Subject: [PATCH] Update to latest versions of MCStats' Metrics class. Next time you add something like this, let me know first so I can handle the administrative bits of taking ownership of the plugin and such... Cleanup. --- .../Commands/Command_blockcommand.java | 11 +- .../Listener/TFM_PlayerListener.java | 5 +- .../TotalFreedomMod/TFM_UserInfo.java | 10 +- .../TotalFreedomMod/TotalFreedomMod.java | 12 +- .../mcstats}/Metrics.java | 135 ++++++++++++++---- 5 files changed, 125 insertions(+), 48 deletions(-) rename src/{me/StevenLawson/TotalFreedomMod => org/mcstats}/Metrics.java (79%) diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_blockcommand.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_blockcommand.java index fe1d7870..c69428f9 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_blockcommand.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_blockcommand.java @@ -12,7 +12,8 @@ public class Command_blockcommand extends TFM_Command @Override public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { - if (args.length != 1) { + if (args.length != 1) + { return false; } @@ -29,13 +30,11 @@ public class Command_blockcommand extends TFM_Command TFM_UserInfo playerdata = TFM_UserInfo.getPlayerData(p); - playerdata.setCommandsBlocked(!playerdata.areCommandsBlocked()); + playerdata.setCommandsBlocked(!playerdata.allCommandsBlocked()); - TFM_Util.adminAction(sender.getName(), (playerdata.areCommandsBlocked() ? "B" : "Unb") + "locking all commands for " + p.getName(), true); - TFM_Util.playerMsg(sender, (playerdata.areCommandsBlocked() ? "B" : "Unb") + "locked all commands."); + TFM_Util.adminAction(sender.getName(), (playerdata.allCommandsBlocked() ? "B" : "Unb") + "locking all commands for " + p.getName(), true); + TFM_Util.playerMsg(sender, (playerdata.allCommandsBlocked() ? "B" : "Unb") + "locked all commands."); return true; } } - - diff --git a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java index 38fc899d..d71e486c 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java +++ b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java @@ -397,10 +397,11 @@ public class TFM_PlayerListener implements Listener return; } - if(playerdata.areCommandsBlocked()) + if (playerdata.allCommandsBlocked()) { - TFM_Util.playerMsg(p, "Your commands have been blocked by an admin", ChatColor.RED); + TFM_Util.playerMsg(p, "Your commands have been blocked by an admin.", ChatColor.RED); event.setCancelled(true); + return; } if (TotalFreedomMod.preprocessLogEnabled) diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_UserInfo.java b/src/me/StevenLawson/TotalFreedomMod/TFM_UserInfo.java index f227c7ea..fee1b057 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_UserInfo.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_UserInfo.java @@ -17,11 +17,9 @@ import org.bukkit.entity.Player; public class TFM_UserInfo { public final static Map userinfo = new HashMap(); - private final Player player; private final String ip_address; private final String player_name; - private boolean user_frozen = false; private boolean is_muted = false; private boolean is_halted = false; @@ -47,7 +45,7 @@ public class TFM_UserInfo private int lockup_schedule_id = -1; private String last_message = ""; private boolean in_adminchat = false; - private boolean commands_blocked = false; + private boolean all_commands_blocked = false; public TFM_UserInfo(Player player) { @@ -430,13 +428,13 @@ public class TFM_UserInfo return this.in_adminchat; } - public boolean areCommandsBlocked() + public boolean allCommandsBlocked() { - return this.commands_blocked; + return this.all_commands_blocked; } public void setCommandsBlocked(boolean commands_blocked) { - this.commands_blocked = commands_blocked; + this.all_commands_blocked = commands_blocked; } } diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java index 0548e509..d4a44ac9 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java @@ -4,7 +4,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.*; -import java.util.logging.Level; import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command; import me.StevenLawson.TotalFreedomMod.Listener.TFM_BlockListener; import me.StevenLawson.TotalFreedomMod.Listener.TFM_EntityListener; @@ -23,6 +22,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; +import org.mcstats.Metrics; public class TotalFreedomMod extends JavaPlugin { @@ -102,12 +102,14 @@ public class TotalFreedomMod extends JavaPlugin server.getScheduler().scheduleAsyncRepeatingTask(this, new TFM_Heartbeat(this), HEARTBEAT_RATE * 20L, HEARTBEAT_RATE * 20L); // metrics @ http://mcstats.org/plugin/TotalFreedomMod - try { + try + { Metrics metrics = new Metrics(plugin); metrics.start(); - } catch (IOException e) { - // Failed to submit the stats :-( - TFM_Log.warning("Failed to submit metrics data"); + } + catch (IOException ex) + { + TFM_Log.warning("Failed to submit metrics data: " + ex.getMessage()); } TFM_Log.info("Plugin Enabled - Version: " + TotalFreedomMod.pluginVersion + "." + TotalFreedomMod.buildNumber + " by Madgeek1450 and DarthSalamon"); diff --git a/src/me/StevenLawson/TotalFreedomMod/Metrics.java b/src/org/mcstats/Metrics.java similarity index 79% rename from src/me/StevenLawson/TotalFreedomMod/Metrics.java rename to src/org/mcstats/Metrics.java index 64554a0c..9d1e0328 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Metrics.java +++ b/src/org/mcstats/Metrics.java @@ -1,15 +1,39 @@ -package me.StevenLawson.TotalFreedomMod; - /* * Copyright 2011 Tyler Blair. All rights reserved. - * mcstats.org -*/ + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and contributors and should not be interpreted as representing official policies, + * either expressed or implied, of anybody else. + */ + +package org.mcstats; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.scheduler.BukkitTask; import java.io.BufferedReader; import java.io.File; @@ -29,12 +53,25 @@ import java.util.Set; import java.util.UUID; import java.util.logging.Level; -class Metrics { +/** + *

+ * The metrics class obtains data about a plugin and submits statistics about it to the metrics backend. + *

+ *

+ * Public methods provided by this class: + *

+ * + * Graph createGraph(String name);
+ * void addCustomData(Metrics.Plotter plotter);
+ * void start();
+ *
+ */ +public class Metrics { /** * The current revision number */ - private final static int REVISION = 5; + private final static int REVISION = 6; /** * The base url of the metrics domain @@ -76,7 +113,7 @@ class Metrics { * The plugin configuration file */ private final YamlConfiguration configuration; - + /** * The plugin configuration file */ @@ -87,15 +124,20 @@ class Metrics { */ private final String guid; + /** + * Debug mode + */ + private final boolean debug; + /** * Lock for synchronization */ private final Object optOutLock = new Object(); /** - * Id of the scheduled task + * The scheduled task */ - private volatile int taskId = -1; + private volatile BukkitTask task = null; public Metrics(final Plugin plugin) throws IOException { if (plugin == null) { @@ -111,6 +153,7 @@ class Metrics { // add some defaults configuration.addDefault("opt-out", false); configuration.addDefault("guid", UUID.randomUUID().toString()); + configuration.addDefault("debug", false); // Do we need to create the file? if (configuration.get("guid", null) == null) { @@ -120,6 +163,7 @@ class Metrics { // Load the guid then guid = configuration.getString("guid"); + debug = configuration.getBoolean("debug", false); } /** @@ -189,12 +233,12 @@ class Metrics { } // Is metrics already running? - if (taskId >= 0) { + if (task != null) { return true; } // Begin hitting the server with glorious data - taskId = plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() { + task = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() { private boolean firstPost = true; @@ -203,9 +247,9 @@ class Metrics { // This has to be synchronized or it can collide with the disable method. synchronized (optOutLock) { // Disable Task, if it is running and the server owner decided to opt-out - if (isOptOut() && taskId > 0) { - plugin.getServer().getScheduler().cancelTask(taskId); - taskId = -1; + if (isOptOut() && task != null) { + task.cancel(); + task = null; // Tell all plotters to stop gathering information. for (Graph graph : graphs){ graph.onOptOut(); @@ -222,7 +266,9 @@ class Metrics { // Each post thereafter will be a ping firstPost = false; } catch (IOException e) { - Bukkit.getLogger().log(Level.INFO, "[Metrics] " + e.getMessage()); + if (debug) { + Bukkit.getLogger().log(Level.INFO, "[Metrics] " + e.getMessage()); + } } } }, 0, PING_INTERVAL * 1200); @@ -242,10 +288,14 @@ class Metrics { // Reload the metrics file configuration.load(getConfigFile()); } catch (IOException ex) { - Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage()); + if (debug) { + Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage()); + } return true; } catch (InvalidConfigurationException ex) { - Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage()); + if (debug) { + Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage()); + } return true; } return configuration.getBoolean("opt-out", false); @@ -267,7 +317,7 @@ class Metrics { } // Enable Task, if it is not running - if (taskId < 0) { + if (task == null) { start(); } } @@ -288,9 +338,9 @@ class Metrics { } // Disable Task, if it is running - if (taskId > 0) { - this.plugin.getServer().getScheduler().cancelTask(taskId); - taskId = -1; + if (task != null) { + task.cancel(); + task = null; } } } @@ -316,17 +366,45 @@ class Metrics { * Generic method that posts a plugin to the metrics website */ private void postPlugin(final boolean isPing) throws IOException { - // The plugin's description file containg all of the plugin data such as name, version, author, etc - final PluginDescriptionFile description = plugin.getDescription(); + // Server software specific section + PluginDescriptionFile description = plugin.getDescription(); + String pluginName = description.getName(); + boolean onlineMode = Bukkit.getServer().getOnlineMode(); // TRUE if online mode is enabled + String pluginVersion = description.getVersion(); + String serverVersion = Bukkit.getVersion(); + int playersOnline = Bukkit.getServer().getOnlinePlayers().length; + + // END server software specific section -- all code below does not use any code outside of this class / Java // Construct the post data final StringBuilder data = new StringBuilder(); + + // The plugin's description file containg all of the plugin data such as name, version, author, etc data.append(encode("guid")).append('=').append(encode(guid)); - encodeDataPair(data, "version", description.getVersion()); - encodeDataPair(data, "server", Bukkit.getVersion()); - encodeDataPair(data, "players", Integer.toString(Bukkit.getServer().getOnlinePlayers().length)); + encodeDataPair(data, "version", pluginVersion); + encodeDataPair(data, "server", serverVersion); + encodeDataPair(data, "players", Integer.toString(playersOnline)); encodeDataPair(data, "revision", String.valueOf(REVISION)); + // New data as of R6 + String osname = System.getProperty("os.name"); + String osarch = System.getProperty("os.arch"); + String osversion = System.getProperty("os.version"); + String java_version = System.getProperty("java.version"); + int coreCount = Runtime.getRuntime().availableProcessors(); + + // normalize os arch .. amd64 -> x86_64 + if (osarch.equals("amd64")) { + osarch = "x86_64"; + } + + encodeDataPair(data, "osname", osname); + encodeDataPair(data, "osarch", osarch); + encodeDataPair(data, "osversion", osversion); + encodeDataPair(data, "cores", Integer.toString(coreCount)); + encodeDataPair(data, "online-mode", Boolean.toString(onlineMode)); + encodeDataPair(data, "java_version", java_version); + // If we're pinging, append it if (isPing) { encodeDataPair(data, "ping", "true"); @@ -357,7 +435,7 @@ class Metrics { } // Create the url - URL url = new URL(BASE_URL + String.format(REPORT_URL, encode(plugin.getDescription().getName()))); + URL url = new URL(BASE_URL + String.format(REPORT_URL, encode(pluginName))); // Connect to the website URLConnection connection; @@ -593,5 +671,4 @@ class Metrics { } -} - +} \ No newline at end of file