diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 22ac04c6d..352c25b41 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -24,6 +24,7 @@ import com.earth2me.essentials.commands.EssentialsCommand; import com.earth2me.essentials.commands.IEssentialsCommand; import com.earth2me.essentials.commands.NoChargeException; import com.earth2me.essentials.commands.NotEnoughArgumentsException; +import com.earth2me.essentials.metrics.Metrics; import com.earth2me.essentials.metrics.MetricsListener; import com.earth2me.essentials.metrics.MetricsStarter; import com.earth2me.essentials.perm.PermissionsHandler; @@ -84,6 +85,7 @@ public class Essentials extends JavaPlugin implements IEssentials private transient UserMap userMap; private transient ExecuteTimer execTimer; private transient I18n i18n; + private transient Metrics metrics; @Override public ISettings getSettings() @@ -239,13 +241,13 @@ public class Essentials extends JavaPlugin implements IEssentials getScheduler().scheduleSyncRepeatingTask(this, timer, 1, 100); Economy.setEss(this); execTimer.mark("RegListeners"); - + final MetricsStarter metricsStarter = new MetricsStarter(this); - if (metricsStarter.getStart()) + if (metricsStarter.getStart() != null && metricsStarter.getStart() == true) { getScheduler().scheduleAsyncDelayedTask(this, metricsStarter, 1); } - else if (metricsStarter.getStart() == false) + else if (metricsStarter.getStart() != null && metricsStarter.getStart() == false) { final MetricsListener metricsListener = new MetricsListener(this, metricsStarter); pm.registerEvents(metricsListener, this); @@ -449,6 +451,16 @@ public class Essentials extends JavaPlugin implements IEssentials return backup; } + public Metrics getMetrics() + { + return metrics; + } + + public void setMetrics(Metrics metrics) + { + this.metrics = metrics; + } + @Override public User getUser(final Object base) { diff --git a/Essentials/src/com/earth2me/essentials/IEssentials.java b/Essentials/src/com/earth2me/essentials/IEssentials.java index 6b820440a..83c2e7325 100644 --- a/Essentials/src/com/earth2me/essentials/IEssentials.java +++ b/Essentials/src/com/earth2me/essentials/IEssentials.java @@ -1,6 +1,7 @@ package com.earth2me.essentials; import com.earth2me.essentials.api.IJails; +import com.earth2me.essentials.metrics.Metrics; import com.earth2me.essentials.perm.PermissionsHandler; import com.earth2me.essentials.register.payment.Methods; import org.bukkit.World; @@ -65,4 +66,9 @@ public interface IEssentials extends Plugin ItemDb getItemDb(); UserMap getUserMap(); + + Metrics getMetrics(); + + void setMetrics(Metrics metrics); + } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java b/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java index ce2f03af9..bf1077c30 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java @@ -2,8 +2,12 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.Util; +import com.earth2me.essentials.metrics.Metrics; +import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Server; @@ -40,6 +44,10 @@ public class Commandessentials extends EssentialsCommand { run_moo(server, sender, commandLabel, args); } + else if (args[0].equalsIgnoreCase("opt-out")) + { + run_optout(server, sender, commandLabel, args); + } else { run_reload(server, sender, commandLabel, args); } @@ -171,4 +179,19 @@ public class Commandessentials extends EssentialsCommand else sender.sendMessage(new String[]{" (__)", " (oo)", " /------\\/", " / | | |", " * /\\---/\\", " ~~ ~~", "....\"Have you mooed today?\"..." } ); } + + private void run_optout(final Server server, final CommandSender sender, final String command, final String args[]) + { + final Metrics metrics = ess.getMetrics(); + try + { + sender.sendMessage("Essentials collects simple metrics to highlight which features to concentrate work on in the future."); + metrics.setOptOut(!metrics.isOptOut()); + sender.sendMessage("Annonmous Metrics are now: " + (metrics.isOptOut() ? "disabled" : "enabled")); + } + catch (IOException ex) + { + sender.sendMessage("Unable to modify 'plugins/PluginMetrics/config.yml': " + ex.getMessage()); + } + } } diff --git a/Essentials/src/com/earth2me/essentials/metrics/Metrics.java b/Essentials/src/com/earth2me/essentials/metrics/Metrics.java index 3a43af70f..a5de7816c 100644 --- a/Essentials/src/com/earth2me/essentials/metrics/Metrics.java +++ b/Essentials/src/com/earth2me/essentials/metrics/Metrics.java @@ -97,7 +97,11 @@ public class Metrics /** * The plugin configuration file */ - private final YamlConfiguration configuration; + private final YamlConfiguration configuration; + /** + * The plugin configuration file + */ + private final File configurationFile; /** * Unique server id */ @@ -113,8 +117,8 @@ public class Metrics this.plugin = plugin; // load the config - File file = new File(CONFIG_FILE); - configuration = YamlConfiguration.loadConfiguration(file); + configurationFile = new File(CONFIG_FILE); + configuration = YamlConfiguration.loadConfiguration(configurationFile); // add some defaults configuration.addDefault("opt-out", false); @@ -124,7 +128,7 @@ public class Metrics if (configuration.get("guid", null) == null) { configuration.options().header("http://metrics.griefcraft.com").copyDefaults(true); - configuration.save(file); + configuration.save(configurationFile); } // Load the guid then @@ -178,6 +182,12 @@ public class Metrics { return configuration.getBoolean("opt-out", false); } + + public void setOptOut(final boolean toggle) throws IOException + { + configuration.set("opt-out", toggle); + configuration.save(configurationFile); + } /** * Start measuring statistics. This will immediately create an async repeating task as the plugin and send the diff --git a/Essentials/src/com/earth2me/essentials/metrics/MetricsListener.java b/Essentials/src/com/earth2me/essentials/metrics/MetricsListener.java index fb2db2e15..4af8f9173 100644 --- a/Essentials/src/com/earth2me/essentials/metrics/MetricsListener.java +++ b/Essentials/src/com/earth2me/essentials/metrics/MetricsListener.java @@ -31,7 +31,7 @@ public class MetricsListener implements Listener if (ess.getSettings().isMetricsEnabled() == false && (player.isAuthorized("essentials.essentials") || player.isAuthorized("bukkit.broadcast.admin"))) { player.sendMessage("PluginMetrics collects minimal statistic data, starting in about 5 minutes."); - player.sendMessage("To opt out, edit plugins/PluginMetrics/config.yml."); + player.sendMessage("To opt out, run /essentials opt-out"); ess.getLogger().log(Level.INFO, "[Metrics] Admin join - Starting 5 minute opt-out period."); ess.getSettings().setMetricsEnabled(true); ess.getScheduler().scheduleAsyncDelayedTask(ess, starter, 5 * 1200); diff --git a/Essentials/src/com/earth2me/essentials/metrics/MetricsStarter.java b/Essentials/src/com/earth2me/essentials/metrics/MetricsStarter.java index 7c79b6406..ddd4c197c 100644 --- a/Essentials/src/com/earth2me/essentials/metrics/MetricsStarter.java +++ b/Essentials/src/com/earth2me/essentials/metrics/MetricsStarter.java @@ -29,7 +29,9 @@ public class MetricsStarter implements Runnable ess = plugin; try { + final Metrics metrics = new Metrics(ess); + ess.setMetrics(metrics); if (!metrics.isOptOut()) { @@ -40,14 +42,14 @@ public class MetricsStarter implements Runnable else { ess.getLogger().info("This plugin collects minimal statistic data and sends it to http://metrics.essentials3.net."); - ess.getLogger().info("You can opt out by changing plugins/PluginMetrics/config.yml, set opt-out to true."); + ess.getLogger().info("You can opt out by running /essentials opt-out"); ess.getLogger().info("This will start 5 minutes after the first admin/op joins."); start = false; } return; } } - catch (IOException ex) + catch (Exception ex) { metricsError(ex); } @@ -58,9 +60,9 @@ public class MetricsStarter implements Runnable { try { - final Metrics metrics = new Metrics(ess); + final Metrics metrics = ess.getMetrics(); - Graph moduleGraph = metrics.createGraph("Modules Used"); + final Graph moduleGraph = metrics.createGraph("Modules Used"); for (Modules module : Modules.values()) { final String moduleName = module.toString(); @@ -70,10 +72,10 @@ public class MetricsStarter implements Runnable } } - Graph localeGraph = metrics.createGraph("Locale"); + final Graph localeGraph = metrics.createGraph("Locale"); localeGraph.addPlotter(new SimplePlotter(ess.getI18n().getCurrentLocale().getDisplayLanguage(Locale.ENGLISH))); - Graph featureGraph = metrics.createGraph("Features"); + final Graph featureGraph = metrics.createGraph("Features"); featureGraph.addPlotter(new Plotter("Unique Accounts") { @Override @@ -102,13 +104,13 @@ public class MetricsStarter implements Runnable metrics.start(); } - catch (IOException ex) + catch (Exception ex) { metricsError(ex); } } - public void metricsError(IOException ex) + public void metricsError(final Exception ex) { if (ess.getSettings().isDebug()) {