diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 52b9074a..772a4c72 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -32,7 +32,6 @@ import com.onarandombox.MultiverseCore.api.MVPlugin; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseCoreConfig; import com.onarandombox.MultiverseCore.api.MultiverseMessaging; -import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.commands.AnchorCommand; import com.onarandombox.MultiverseCore.commands.CheckCommand; @@ -94,6 +93,7 @@ import com.onarandombox.MultiverseCore.utils.MVMessaging; import com.onarandombox.MultiverseCore.utils.MVPermissions; import com.onarandombox.MultiverseCore.utils.MVPlayerSession; import com.onarandombox.MultiverseCore.utils.MaterialConverter; +import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator; import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety; import com.onarandombox.MultiverseCore.utils.SimpleLocationManipulation; import com.onarandombox.MultiverseCore.utils.SimpleSafeTTeleporter; @@ -103,8 +103,6 @@ import com.onarandombox.MultiverseCore.utils.WorldManager; import com.pneumaticraft.commandhandler.CommandHandler; import me.main__.util.SerializationConfig.NoSuchPropertyException; import me.main__.util.SerializationConfig.SerializationConfig; -import org.apache.commons.lang.StringUtils; -import org.bstats.bukkit.Metrics; import org.bukkit.ChatColor; import org.bukkit.Difficulty; import org.bukkit.GameMode; @@ -333,7 +331,8 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { getServer().getPluginManager().registerEvents(this.chatListener, this); this.initializeBuscript(); - this.setupMetrics(); + + MetricsConfigurator.configureMetrics(this); // Output a little snippet to show it's enabled. Logging.config("Version %s (API v%s) Enabled - By %s", this.getDescription().getVersion(), PROTOCOL, getAuthors()); @@ -353,41 +352,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { buscript.setScriptVariable("multiverse", this); } - private void setupMetrics() { - try { - Metrics metrics = new Metrics(this, 7765); - - metrics.addCustomChart(new Metrics.AdvancedPie("custom_generators", () -> { - Map map = new HashMap<>(); - for (MultiverseWorld w : this.getMVWorldManager().getMVWorlds()) { - String gen = w.getGenerator() != null ? w.getGenerator() : "N/A"; - map.putIfAbsent(gen, 0); - map.put(gen, map.get(gen) + 1); - } - - return map; - })); - - metrics.addCustomChart(new Metrics.AdvancedPie("environments", () -> { - Map map = new HashMap<>(); - for (MultiverseWorld w : this.getMVWorldManager().getMVWorlds()) { - String env = w.getEnvironment().name().replace('_', ' '); - env = StringUtils.capitalize(env.toLowerCase()); - map.putIfAbsent(env, 0); - map.put(env, map.get(env) + 1); - } - - return map; - })); - - // TODO: add Worlds vs Loaded Worlds once bStats adds support for multi-line charts - log(Level.FINE, "Metrics were set up!"); - } catch (Exception e) { - log(Level.WARNING, "There was an issue while enabling metrics:"); - e.printStackTrace(); - } - } - private void initializeDestinationFactory() { this.destFactory = new DestinationFactory(this); this.destFactory.registerDestinationType(WorldDestination.class, ""); diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java new file mode 100644 index 00000000..30cc0353 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java @@ -0,0 +1,71 @@ +package com.onarandombox.MultiverseCore.utils.metrics; + +import java.util.Map; +import java.util.function.Consumer; + +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import org.apache.commons.lang.StringUtils; +import org.bstats.bukkit.Metrics; +import org.bukkit.World; + +public class MetricsConfigurator { + + private static final int PLUGIN_ID = 7765; + private static final String NO_GENERATOR_NAME = "N/A"; + + public static void configureMetrics(MultiverseCore plugin) { + MetricsConfigurator configurator = new MetricsConfigurator(plugin); + configurator.initMetrics(); + } + + private final MultiverseCore plugin; + private final Metrics metrics; + + private MetricsConfigurator(MultiverseCore plugin) { + this.plugin = plugin; + this.metrics = new Metrics(plugin, PLUGIN_ID); + } + + private void initMetrics() { + try { + addCustomGeneratorsMetric(); + createEnvironmentsMetric(); + + Logging.fine("Metrics enabled."); + } catch (Exception e) { + Logging.warning("There was an issue while enabling metrics:"); + e.printStackTrace(); + } + } + + private void addCustomGeneratorsMetric() { + addAdvancedPieMetric("custom_generators", map -> { + for (MultiverseWorld w : plugin.getMVWorldManager().getMVWorlds()) { + MetricsHelper.incrementCount(map, getGeneratorName(w)); + } + }); + } + + private String getGeneratorName(MultiverseWorld world) { + return world.getGenerator() != null ? world.getGenerator() : NO_GENERATOR_NAME; + } + + private void createEnvironmentsMetric() { + addAdvancedPieMetric("environments", map -> { + for (MultiverseWorld w : plugin.getMVWorldManager().getMVWorlds()) { + MetricsHelper.incrementCount(map, titleCaseEnv(w.getEnvironment())); + } + }); + } + + private String titleCaseEnv(World.Environment env) { + String envName = env.name().replaceAll("_+", " "); + return StringUtils.capitalize(envName.toLowerCase()); + } + + private void addAdvancedPieMetric(String chartId, Consumer> metricsFunc) { + metrics.addCustomChart(MetricsHelper.createAdvancedPieChart(chartId, metricsFunc)); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java new file mode 100644 index 00000000..b7a92a5e --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java @@ -0,0 +1,26 @@ +package com.onarandombox.MultiverseCore.utils.metrics; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +import org.bstats.bukkit.Metrics; + +enum MetricsHelper { + ; + + /** + * Adds one to the value in the given map with the given key. If the key does not exist in the map, it will be added with a value of 1. + */ + static void incrementCount(Map map, String key) { + Integer count = map.getOrDefault(key, 0); + map.put(key, count + 1); + } + + static Metrics.AdvancedPie createAdvancedPieChart(String chartId, Consumer> metricsFunc) { + Map map = new HashMap<>(); + metricsFunc.accept(map); + return new Metrics.AdvancedPie(chartId, () -> map); + } + +}