/** * Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task. * * @throws java.io.IOException */ public void enable() throws IOException { // This has to be synchronized or it can collide with the check in the task. synchronized (optOutLock) { // Check if the server owner has already set opt-out, if not, set it. if (isOptOut()) { properties.setProperty("opt-out", "false"); properties.store(new FileOutputStream(configurationFile), "http://mcstats.org"); } // Enable Task, if it is not running if (thread == null) { start(); } } }
String pluginVersion = description.getVersion(); String serverVersion = Bukkit.getVersion(); int playersOnline = this.getOnlinePlayers(); appendJSONPair(json, "guid", guid); appendJSONPair(json, "plugin_version", pluginVersion); appendJSONPair(json, "server_version", serverVersion); appendJSONPair(json, "players_online", Integer.toString(playersOnline)); appendJSONPair(json, "osname", osname); appendJSONPair(json, "osarch", osarch); appendJSONPair(json, "osversion", osversion); appendJSONPair(json, "cores", Integer.toString(coreCount)); appendJSONPair(json, "auth_mode", onlineMode ? "1" : "0"); appendJSONPair(json, "java_version", java_version); appendJSONPair(json, "ping", "1"); appendJSONPair(graphJson, plotter.getColumnName(), Integer.toString(plotter.getValue())); json.append(escapeJSON(graph.getName())); json.append(':'); json.append(graphJson); URL url = new URL(BASE_URL + String.format(REPORT_URL, urlEncode(pluginName))); if (isMineshafterPresent()) { connection = url.openConnection(Proxy.NO_PROXY); } else {
public Metrics(final Plugin plugin) throws IOException { if (plugin == null) { throw new IllegalArgumentException("Plugin cannot be null"); } this.plugin = plugin; // load the config configurationFile = getConfigFile(); if (!configurationFile.exists()) { if (configurationFile.getPath().contains("/") || configurationFile.getPath().contains("\\")) { File parent = new File(configurationFile.getParent()); if (!parent.exists()) { parent.mkdir(); } } configurationFile.createNewFile(); // config file properties.put("opt-out", "false"); properties.put("guid", UUID.randomUUID().toString()); properties.put("debug", "false"); properties.store(new FileOutputStream(configurationFile), "http://mcstats.org"); } else { properties.load(new FileInputStream(configurationFile)); } // Load the guid then guid = properties.getProperty("guid"); debug = Boolean.parseBoolean(properties.getProperty("debug")); }
private void startStatistics() { try { new org.mcstats.Metrics(this).start(); } catch (IOException ex) { ChestShop.getBukkitLogger().severe("There was an error while submitting MCStats statistics."); } new org.bstats.bukkit.MetricsLite(this); }
/** * Disables metrics for the server by setting "opt-out" to true in the config file and canceling the metrics task. * * @throws java.io.IOException */ public void disable() throws IOException { // This has to be synchronized or it can collide with the check in the task. synchronized (optOutLock) { // Check if the server owner has already set opt-out, if not, set it. if (!isOptOut()) { properties.setProperty("opt-out", "true"); properties.store(new FileOutputStream(configurationFile), "http://mcstats.org"); } // Disable Task, if it is running if (thread != null) { thread.cancel(); thread = null; } } }
/** * Appends a json encoded key/value pair to the given string builder. * * @param json * @param key * @param value * @throws UnsupportedEncodingException */ private static void appendJSONPair(StringBuilder json, String key, String value) throws UnsupportedEncodingException { boolean isValueNumeric = false; try { if (value.equals("0") || !value.endsWith("0")) { Double.parseDouble(value); isValueNumeric = true; } } catch (NumberFormatException e) { isValueNumeric = false; } if (json.charAt(json.length() - 1) != '{') { json.append(','); } json.append(escapeJSON(key)); json.append(':'); if (isValueNumeric) { json.append(value); } else { json.append(escapeJSON(value)); } }
if (isOptOut() && thread != null) { ScheduledTask temp = thread; thread = null;
/** * Appends a json encoded key/value pair to the given string builder. * * @param json * @param key * @param value * @throws UnsupportedEncodingException */ private static void appendJSONPair(StringBuilder json, String key, String value) throws UnsupportedEncodingException { boolean isValueNumeric = false; try { if (value.equals("0") || !value.endsWith("0")) { Double.parseDouble(value); isValueNumeric = true; } } catch (NumberFormatException e) { isValueNumeric = false; } if (json.charAt(json.length() - 1) != '{') { json.append(','); } json.append(escapeJSON(key)); json.append(':'); if (isValueNumeric) { json.append(value); } else { json.append(escapeJSON(value)); } }
appendJSONPair(json, "guid", guid); appendJSONPair(json, "plugin_version", pluginVersion); appendJSONPair(json, "server_version", serverVersion); appendJSONPair(json, "players_online", Integer.toString(playersOnline)); appendJSONPair(json, "osname", osname); appendJSONPair(json, "osarch", osarch); appendJSONPair(json, "osversion", osversion); appendJSONPair(json, "cores", Integer.toString(coreCount)); appendJSONPair(json, "auth_mode", onlineMode ? "1" : "0"); appendJSONPair(json, "java_version", java_version); appendJSONPair(json, "ping", "1"); appendJSONPair(graphJson, plotter.getColumnName(), Integer.toString(plotter.getValue())); json.append(escapeJSON(graph.getName())); json.append(':'); json.append(graphJson); URL url = new URL(BASE_URL + String.format(REPORT_URL, urlEncode(pluginName))); if (isMineshafterPresent()) { connection = url.openConnection(Proxy.NO_PROXY); } else { byte[] compressed = gzip(json.toString());
/** * Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task. * * @throws java.io.IOException */ public void enable() throws IOException { // This has to be synchronized or it can collide with the check in the task. synchronized (optOutLock) { // Check if the server owner has already set opt-out, if not, set it. if (isOptOut()) { configuration.set("opt-out", false); configuration.save(configurationFile); } // Enable Task, if it is not running if (task == null) { start(); } } }
public void run() { try { // 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() && task != null) { task.cancel(); task = null; // Tell all plotters to stop gathering information. for (Graph graph : graphs) { graph.onOptOut(); } } } // We use the inverse of firstPost because if it is the first time we are posting, // it is not a interval ping, so it evaluates to FALSE // Each time thereafter it will evaluate to TRUE, i.e PING! postPlugin(!firstPost); // After the first post we set firstPost to false // Each post thereafter will be a ping firstPost = false; } catch (IOException e) { if (debug) { Bukkit.getLogger().log(Level.INFO, "[Metrics] " + e.getMessage()); } } } }, 0, PING_INTERVAL * 1200);
/** * Has the server owner denied plugin metrics? * * @return true if metrics should be opted out of it */ public boolean isOptOut() { synchronized (optOutLock) { try { // Reload the metrics file configuration.load(getConfigFile()); } catch (IOException ex) { if (debug) { Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage()); } return true; } catch (InvalidConfigurationException ex) { if (debug) { Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage()); } return true; } return configuration.getBoolean("opt-out", false); } }
json.append(escapeJSON(key)); json.append(':'); json.append(escapeJSON(value));
appendJSONPair(json, "guid", guid); appendJSONPair(json, "plugin_version", pluginVersion); appendJSONPair(json, "server_version", serverVersion); appendJSONPair(json, "players_online", Integer.toString(playersOnline)); appendJSONPair(json, "osname", osname); appendJSONPair(json, "osarch", osarch); appendJSONPair(json, "osversion", osversion); appendJSONPair(json, "cores", Integer.toString(coreCount)); appendJSONPair(json, "auth_mode", onlineMode ? "1" : "0"); appendJSONPair(json, "java_version", java_version); appendJSONPair(json, "ping", "1"); appendJSONPair(graphJson, plotter.getColumnName(), Integer.toString(plotter.getValue())); json.append(escapeJSON(graph.getName())); json.append(':'); json.append(graphJson); URL url = new URL(BASE_URL + String.format(REPORT_URL, urlEncode(pluginName))); if (isMineshafterPresent()) byte[] compressed = gzip(json.toString());
/** * Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task. * * @throws java.io.IOException */ public void enable() throws IOException { // This has to be synchronized or it can collide with the check in the task. synchronized (optOutLock) { // Check if the server owner has already set opt-out, if not, set it. if (isOptOut()) { configuration.set("opt-out", false); configuration.save(configurationFile); } // Enable Task, if it is not running if (task == null) { start(); } } }
if (isOptOut() && task != null)
configuration.load(getConfigFile());
/** * Disables metrics for the server by setting "opt-out" to true in the config file and canceling the metrics task. * * @throws java.io.IOException */ public void disable() throws IOException { // This has to be synchronized or it can collide with the check in the task. synchronized (optOutLock) { // Check if the server owner has already set opt-out, if not, set it. if (!isOptOut()) { configuration.set("opt-out", true); configuration.save(configurationFile); } // Disable Task, if it is running if (task != null) { task.cancel(); task = null; } } }
public Metrics(final Plugin plugin) throws IOException { if (plugin == null) { throw new IllegalArgumentException("Plugin cannot be null"); } this.plugin = plugin; // load the config configurationFile = getConfigFile(); configuration = YamlConfiguration.loadConfiguration(configurationFile); // 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) { configuration.options().header("http://mcstats.org").copyDefaults(true); configuration.save(configurationFile); } // Load the guid then guid = configuration.getString("guid"); debug = configuration.getBoolean("debug", false); }