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.
This commit is contained in:
Steven Lawson 2012-12-07 20:01:52 -05:00
parent bb1fd44a2b
commit 417dbc9589
5 changed files with 125 additions and 48 deletions

View File

@ -12,7 +12,8 @@ public class Command_blockcommand extends TFM_Command
@Override @Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) 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; return false;
} }
@ -29,13 +30,11 @@ public class Command_blockcommand extends TFM_Command
TFM_UserInfo playerdata = TFM_UserInfo.getPlayerData(p); 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.adminAction(sender.getName(), (playerdata.allCommandsBlocked() ? "B" : "Unb") + "locking all commands for " + p.getName(), true);
TFM_Util.playerMsg(sender, (playerdata.areCommandsBlocked() ? "B" : "Unb") + "locked all commands."); TFM_Util.playerMsg(sender, (playerdata.allCommandsBlocked() ? "B" : "Unb") + "locked all commands.");
return true; return true;
} }
} }

View File

@ -397,10 +397,11 @@ public class TFM_PlayerListener implements Listener
return; 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); event.setCancelled(true);
return;
} }
if (TotalFreedomMod.preprocessLogEnabled) if (TotalFreedomMod.preprocessLogEnabled)

View File

@ -17,11 +17,9 @@ import org.bukkit.entity.Player;
public class TFM_UserInfo public class TFM_UserInfo
{ {
public final static Map<Player, TFM_UserInfo> userinfo = new HashMap<Player, TFM_UserInfo>(); public final static Map<Player, TFM_UserInfo> userinfo = new HashMap<Player, TFM_UserInfo>();
private final Player player; private final Player player;
private final String ip_address; private final String ip_address;
private final String player_name; private final String player_name;
private boolean user_frozen = false; private boolean user_frozen = false;
private boolean is_muted = false; private boolean is_muted = false;
private boolean is_halted = false; private boolean is_halted = false;
@ -47,7 +45,7 @@ public class TFM_UserInfo
private int lockup_schedule_id = -1; private int lockup_schedule_id = -1;
private String last_message = ""; private String last_message = "";
private boolean in_adminchat = false; private boolean in_adminchat = false;
private boolean commands_blocked = false; private boolean all_commands_blocked = false;
public TFM_UserInfo(Player player) public TFM_UserInfo(Player player)
{ {
@ -430,13 +428,13 @@ public class TFM_UserInfo
return this.in_adminchat; 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) public void setCommandsBlocked(boolean commands_blocked)
{ {
this.commands_blocked = commands_blocked; this.all_commands_blocked = commands_blocked;
} }
} }

View File

@ -4,7 +4,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
import java.util.logging.Level;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command; import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command;
import me.StevenLawson.TotalFreedomMod.Listener.TFM_BlockListener; import me.StevenLawson.TotalFreedomMod.Listener.TFM_BlockListener;
import me.StevenLawson.TotalFreedomMod.Listener.TFM_EntityListener; 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.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.mcstats.Metrics;
public class TotalFreedomMod extends JavaPlugin 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); server.getScheduler().scheduleAsyncRepeatingTask(this, new TFM_Heartbeat(this), HEARTBEAT_RATE * 20L, HEARTBEAT_RATE * 20L);
// metrics @ http://mcstats.org/plugin/TotalFreedomMod // metrics @ http://mcstats.org/plugin/TotalFreedomMod
try { try
{
Metrics metrics = new Metrics(plugin); Metrics metrics = new Metrics(plugin);
metrics.start(); metrics.start();
} catch (IOException e) { }
// Failed to submit the stats :-( catch (IOException ex)
TFM_Log.warning("Failed to submit metrics data"); {
TFM_Log.warning("Failed to submit metrics data: " + ex.getMessage());
} }
TFM_Log.info("Plugin Enabled - Version: " + TotalFreedomMod.pluginVersion + "." + TotalFreedomMod.buildNumber + " by Madgeek1450 and DarthSalamon"); TFM_Log.info("Plugin Enabled - Version: " + TotalFreedomMod.pluginVersion + "." + TotalFreedomMod.buildNumber + " by Madgeek1450 and DarthSalamon");

View File

@ -1,15 +1,39 @@
package me.StevenLawson.TotalFreedomMod;
/* /*
* Copyright 2011 Tyler Blair. All rights reserved. * 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.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.scheduler.BukkitTask;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -29,12 +53,25 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
class Metrics { /**
* <p>
* The metrics class obtains data about a plugin and submits statistics about it to the metrics backend.
* </p>
* <p>
* Public methods provided by this class:
* </p>
* <code>
* Graph createGraph(String name); <br/>
* void addCustomData(Metrics.Plotter plotter); <br/>
* void start(); <br/>
* </code>
*/
public class Metrics {
/** /**
* The current revision number * The current revision number
*/ */
private final static int REVISION = 5; private final static int REVISION = 6;
/** /**
* The base url of the metrics domain * The base url of the metrics domain
@ -76,7 +113,7 @@ class Metrics {
* The plugin configuration file * The plugin configuration file
*/ */
private final YamlConfiguration configuration; private final YamlConfiguration configuration;
/** /**
* The plugin configuration file * The plugin configuration file
*/ */
@ -87,15 +124,20 @@ class Metrics {
*/ */
private final String guid; private final String guid;
/**
* Debug mode
*/
private final boolean debug;
/** /**
* Lock for synchronization * Lock for synchronization
*/ */
private final Object optOutLock = new Object(); 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 { public Metrics(final Plugin plugin) throws IOException {
if (plugin == null) { if (plugin == null) {
@ -111,6 +153,7 @@ class Metrics {
// add some defaults // add some defaults
configuration.addDefault("opt-out", false); configuration.addDefault("opt-out", false);
configuration.addDefault("guid", UUID.randomUUID().toString()); configuration.addDefault("guid", UUID.randomUUID().toString());
configuration.addDefault("debug", false);
// Do we need to create the file? // Do we need to create the file?
if (configuration.get("guid", null) == null) { if (configuration.get("guid", null) == null) {
@ -120,6 +163,7 @@ class Metrics {
// Load the guid then // Load the guid then
guid = configuration.getString("guid"); guid = configuration.getString("guid");
debug = configuration.getBoolean("debug", false);
} }
/** /**
@ -189,12 +233,12 @@ class Metrics {
} }
// Is metrics already running? // Is metrics already running?
if (taskId >= 0) { if (task != null) {
return true; return true;
} }
// Begin hitting the server with glorious data // 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; private boolean firstPost = true;
@ -203,9 +247,9 @@ class Metrics {
// This has to be synchronized or it can collide with the disable method. // This has to be synchronized or it can collide with the disable method.
synchronized (optOutLock) { synchronized (optOutLock) {
// Disable Task, if it is running and the server owner decided to opt-out // Disable Task, if it is running and the server owner decided to opt-out
if (isOptOut() && taskId > 0) { if (isOptOut() && task != null) {
plugin.getServer().getScheduler().cancelTask(taskId); task.cancel();
taskId = -1; task = null;
// Tell all plotters to stop gathering information. // Tell all plotters to stop gathering information.
for (Graph graph : graphs){ for (Graph graph : graphs){
graph.onOptOut(); graph.onOptOut();
@ -222,7 +266,9 @@ class Metrics {
// Each post thereafter will be a ping // Each post thereafter will be a ping
firstPost = false; firstPost = false;
} catch (IOException e) { } 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); }, 0, PING_INTERVAL * 1200);
@ -242,10 +288,14 @@ class Metrics {
// Reload the metrics file // Reload the metrics file
configuration.load(getConfigFile()); configuration.load(getConfigFile());
} catch (IOException ex) { } catch (IOException ex) {
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage()); if (debug) {
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage());
}
return true; return true;
} catch (InvalidConfigurationException ex) { } catch (InvalidConfigurationException ex) {
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage()); if (debug) {
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage());
}
return true; return true;
} }
return configuration.getBoolean("opt-out", false); return configuration.getBoolean("opt-out", false);
@ -267,7 +317,7 @@ class Metrics {
} }
// Enable Task, if it is not running // Enable Task, if it is not running
if (taskId < 0) { if (task == null) {
start(); start();
} }
} }
@ -288,9 +338,9 @@ class Metrics {
} }
// Disable Task, if it is running // Disable Task, if it is running
if (taskId > 0) { if (task != null) {
this.plugin.getServer().getScheduler().cancelTask(taskId); task.cancel();
taskId = -1; task = null;
} }
} }
} }
@ -316,17 +366,45 @@ class Metrics {
* Generic method that posts a plugin to the metrics website * Generic method that posts a plugin to the metrics website
*/ */
private void postPlugin(final boolean isPing) throws IOException { 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 // Server software specific section
final PluginDescriptionFile description = plugin.getDescription(); 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 // Construct the post data
final StringBuilder data = new StringBuilder(); 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)); data.append(encode("guid")).append('=').append(encode(guid));
encodeDataPair(data, "version", description.getVersion()); encodeDataPair(data, "version", pluginVersion);
encodeDataPair(data, "server", Bukkit.getVersion()); encodeDataPair(data, "server", serverVersion);
encodeDataPair(data, "players", Integer.toString(Bukkit.getServer().getOnlinePlayers().length)); encodeDataPair(data, "players", Integer.toString(playersOnline));
encodeDataPair(data, "revision", String.valueOf(REVISION)); 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 we're pinging, append it
if (isPing) { if (isPing) {
encodeDataPair(data, "ping", "true"); encodeDataPair(data, "ping", "true");
@ -357,7 +435,7 @@ class Metrics {
} }
// Create the url // 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 // Connect to the website
URLConnection connection; URLConnection connection;
@ -593,5 +671,4 @@ class Metrics {
} }
} }