Merge pull request #2280 from Multiverse/bstats

Fixes #2252, implements #1918.
This commit is contained in:
Jeremy Wood 2020-06-13 01:20:29 -04:00 committed by GitHub
commit 3fa2bfa7cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 138 additions and 107 deletions

15
pom.xml
View File

@ -33,10 +33,9 @@
<id>minebench-repo</id>
<url>https://repo.minebench.de/</url>
</repository>
<!-- Has a copy of metrics R8-SNAPSHOT !-->
<repository>
<id>elmakers-repo</id>
<url>http://maven.elmakers.com/repository/</url>
<id>CodeMC</id>
<url>https://repo.codemc.org/repository/maven-public</url>
</repository>
</repositories>
@ -203,8 +202,8 @@
<shadedPattern>com.onarandombox.buscript</shadedPattern>
</relocation>
<relocation>
<pattern>org.mcstats</pattern>
<shadedPattern>com.onarandombox.mcstats</shadedPattern>
<pattern>org.bstats</pattern>
<shadedPattern>com.onarandombox.bstats</shadedPattern>
</relocation>
<relocation>
<pattern>com.dumptruckman.minecraft.util.Logging</pattern>
@ -283,9 +282,9 @@
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mcstats.bukkit</groupId>
<artifactId>metrics</artifactId>
<version>R8-SNAPSHOT</version>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>1.7</version>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>

View File

@ -17,11 +17,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import buscript.Buscript;
@ -34,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;
@ -96,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;
@ -109,7 +107,6 @@ import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World.Environment;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.Configuration;
@ -122,7 +119,6 @@ import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.mcstats.Metrics;
/**
* The implementation of the Multiverse-{@link Core}.
@ -335,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());
@ -355,98 +352,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
buscript.setScriptVariable("multiverse", this);
}
/**
* Plotter for Environment-Values.
*/
private static final class EnvironmentPlotter extends Metrics.Plotter {
private MultiverseCore core;
private final Environment env;
public EnvironmentPlotter(MultiverseCore core, Environment env) {
super(envToString(env));
this.core = core;
this.env = env;
}
private static String envToString(Environment env) {
return new StringBuilder().append(env.name().toUpperCase().charAt(0))
.append(env.name().toLowerCase().substring(1)).toString();
}
@Override
public int getValue() {
int count = 0;
for (MultiverseWorld w : core.getMVWorldManager().getMVWorlds())
if (w.getEnvironment() == env)
count++;
core.log(Level.FINE, String.format("Tracking %d worlds of type %s", count, env));
return count;
}
}
/**
* Plotter for Generator-Values.
*/
private static final class GeneratorPlotter extends Metrics.Plotter {
private MultiverseCore core;
private final String gen;
public GeneratorPlotter(MultiverseCore core, String gen) {
super(gen);
this.core = core;
this.gen = gen;
}
@Override
public int getValue() {
int count = 0;
for (MultiverseWorld w : core.getMVWorldManager().getMVWorlds())
if (gen.equals(w.getGenerator()))
count++;
core.log(Level.FINE, String.format("Tracking %d worlds of type %s", count, gen));
return count;
}
}
private void setupMetrics() {
try {
Metrics m = new Metrics(this);
Metrics.Graph envGraph = m.createGraph("Worlds by environment");
for (Environment env : Environment.values())
envGraph.addPlotter(new EnvironmentPlotter(this, env));
Metrics.Graph loadedWorldsGraph = m.createGraph("Worlds by environment");
loadedWorldsGraph.addPlotter(new Metrics.Plotter("Loaded worlds") {
@Override
public int getValue() {
return getMVWorldManager().getMVWorlds().size();
}
});
loadedWorldsGraph.addPlotter(new Metrics.Plotter("Total number of worlds") {
@Override
public int getValue() {
return getMVWorldManager().getMVWorlds().size()
+ getMVWorldManager().getUnloadedWorlds().size();
}
});
Set<String> gens = new HashSet<String>();
for (MultiverseWorld w : this.getMVWorldManager().getMVWorlds())
gens.add(w.getGenerator());
gens.remove(null);
gens.remove("null");
Metrics.Graph genGraph = m.createGraph("Custom Generators");
for (String gen : gens)
genGraph.addPlotter(new GeneratorPlotter(this, gen));
m.start();
log(Level.FINE, "Metrics have run!");
} catch (Exception e) {
log(Level.WARNING, "There was an issue while enabling metrics: " + e.getMessage());
}
}
private void initializeDestinationFactory() {
this.destFactory = new DestinationFactory(this);
this.destFactory.registerDestinationType(WorldDestination.class, "");

View File

@ -0,0 +1,94 @@
package com.onarandombox.MultiverseCore.utils.metrics;
import java.util.Collection;
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.MVWorldManager;
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 MVWorldManager getWorldManager() {
return plugin.getMVWorldManager();
}
private Collection<MultiverseWorld> getMVWorlds() {
return getWorldManager().getMVWorlds();
}
private void initMetrics() {
try {
addCustomGeneratorsMetric();
addEnvironmentsMetric();
addWorldCountMetric();
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 : getMVWorlds()) {
MetricsHelper.incrementCount(map, getGeneratorName(w));
}
});
}
private String getGeneratorName(MultiverseWorld world) {
return world.getGenerator() != null ? world.getGenerator() : NO_GENERATOR_NAME;
}
private void addEnvironmentsMetric() {
addAdvancedPieMetric("environments", map -> {
for (MultiverseWorld w : 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 addWorldCountMetric() {
addMultiLineMetric("world_count", map -> {
int loadedWorldsCount = getMVWorlds().size();
map.put("Loaded worlds", loadedWorldsCount);
map.put("Total number of worlds", loadedWorldsCount + getWorldManager().getUnloadedWorlds().size());
});
}
private void addAdvancedPieMetric(String chartId, Consumer<Map<String, Integer>> metricsFunc) {
metrics.addCustomChart(MetricsHelper.createAdvancedPieChart(chartId, metricsFunc));
}
private void addMultiLineMetric(String chartId, Consumer<Map<String, Integer>> metricsFunc) {
metrics.addCustomChart(MetricsHelper.createMultiLineChart(chartId, metricsFunc));
}
}

View File

@ -0,0 +1,32 @@
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<String, Integer> map, String key) {
Integer count = map.getOrDefault(key, 0);
map.put(key, count + 1);
}
static Metrics.AdvancedPie createAdvancedPieChart(String chartId, Consumer<Map<String, Integer>> metricsFunc) {
Map<String, Integer> map = new HashMap<>();
metricsFunc.accept(map);
return new Metrics.AdvancedPie(chartId, () -> map);
}
static Metrics.MultiLineChart createMultiLineChart(String chartId, Consumer<Map<String, Integer>> metricsFunc) {
Map<String, Integer> map = new HashMap<>();
metricsFunc.accept(map);
return new Metrics.MultiLineChart(chartId, () -> map);
}
}

View File

@ -1,7 +1,8 @@
name: Multiverse-Core
main: com.onarandombox.MultiverseCore.MultiverseCore
authors: ['Rigby', 'fernferret', 'lithium3141', 'main--', 'dumptruckman']
authors: ['dumptruckman', 'Rigby', 'fernferret', 'lithium3141', 'main--']
website: 'https://dev.bukkit.org/projects/multiverse-core'
softdepend: ['Vault']
api-version: 1.13
version: maven-version-number
commands: