1.16.3 Update / Added bstats Metrics

This commit is contained in:
LOOHP 2020-09-11 11:02:35 +08:00
parent 10789c3c60
commit 26c4b9d960
6 changed files with 1001 additions and 958 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>Limbo</groupId>
<artifactId>Limbo</artifactId>
<version>0.3.2-ALPHA</version>
<version>0.3.3-ALPHA</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
@ -53,7 +53,7 @@
</configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}-${project.version}-1.16.2</finalName>
<finalName>${project.artifactId}-${project.version}-1.16.3</finalName>
</build>
<repositories>

View File

@ -18,26 +18,30 @@ import com.loohp.limbo.Utils.YamlOrder;
public class FileConfiguration {
File file;
Map<String, Object> mapping;
String header;
public FileConfiguration(File file) {
public FileConfiguration(File file) throws FileNotFoundException {
this.file = file;
if (file.exists()) {
try {
reloadConfig(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
reloadConfig();
} else {
mapping = new LinkedHashMap<>();
}
}
@Deprecated
public FileConfiguration(InputStream input){
reloadConfig(input);
}
public FileConfiguration reloadConfig(InputStream input) {
public FileConfiguration reloadConfig() throws FileNotFoundException {
return reloadConfig(new FileInputStream(file));
}
private FileConfiguration reloadConfig(InputStream input) {
Yaml yml = new Yaml();
mapping = yml.load(input);
return this;
@ -76,7 +80,7 @@ public class FileConfiguration {
}
map = map1;
}
if (value == null) {
if (value != null) {
map.put(tree[tree.length - 1], (T) value);
} else {
map.remove(tree[tree.length - 1]);
@ -93,9 +97,13 @@ public class FileConfiguration {
customRepresenter.setPropertyUtils(customProperty);
Yaml yaml = new Yaml(customRepresenter, options);
if (file.getParentFile() != null) {
file.getParentFile().mkdirs();
}
PrintWriter pw = new PrintWriter(file, StandardCharsets.UTF_8.toString());
if (header != null) {
pw.println(header);
pw.println("#" + header.replace("\n", "\n#"));
}
yaml.dump(mapping, pw);
pw.flush();

View File

@ -17,8 +17,6 @@ import com.loohp.limbo.World.World;
public class ServerProperties {
public static final String JSON_BASE_RESPONSE = "{\"version\":{\"name\":\"%VERSION%\",\"protocol\":%PROTOCOL%},\"players\":{\"max\":%MAXPLAYERS%,\"online\":%ONLINECLIENTS%},\"description\":%MOTD%,%FAVICON%\"modinfo\":{\"type\":\"FML\",\"modList\":[]}}";
File file;
int maxPlayers;
int serverPort;
@ -146,10 +144,6 @@ public class ServerProperties {
return allowFlight;
}
public static String getJsonBaseResponse() {
return JSON_BASE_RESPONSE;
}
public String getMotdJson() {
return motdJson;
}

View File

@ -2,11 +2,13 @@ package com.loohp.limbo;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
@ -36,6 +38,7 @@ import com.loohp.limbo.Events.EventsManager;
import com.loohp.limbo.File.ServerProperties;
import com.loohp.limbo.GUI.GUI;
import com.loohp.limbo.Location.Location;
import com.loohp.limbo.Metrics.Metrics;
import com.loohp.limbo.Permissions.PermissionsManager;
import com.loohp.limbo.Player.Player;
import com.loohp.limbo.Plugins.LimboPlugin;
@ -97,8 +100,8 @@ public class Limbo {
//===========================
public final String serverImplementationVersion = "1.16.2";
public final int serverImplmentationProtocol = 751;
public final String serverImplementationVersion = "1.16.3";
public final int serverImplmentationProtocol = 753;
private ServerConnection server;
private Console console;
@ -118,6 +121,8 @@ public class Limbo {
private DimensionRegistry dimensionRegistry;
private Metrics metrics;
public AtomicInteger entityIdCount = new AtomicInteger();
@SuppressWarnings("deprecation")
@ -136,6 +141,8 @@ public class Limbo {
console = new Console(System.in, System.out, System.err);
}
console.sendMessage("Loading Limbo Version " + new BufferedReader(new InputStreamReader(Limbo.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF"))).lines().filter(each -> each.startsWith("Limbo-Version:")).findFirst().orElse("Limbo-Version: unknown").substring(14).trim());
String spName = "server.properties";
File sp = new File(spName);
if (!sp.exists()) {
@ -283,6 +290,8 @@ public class Limbo {
server = new ServerConnection(properties.getServerIp(), properties.getServerPort());
metrics = new Metrics();
console.run();
}
@ -354,6 +363,10 @@ public class Limbo {
return console;
}
public Metrics getMetrics() {
return metrics;
}
public Set<Player> getPlayers() {
return new HashSet<>(playersByUUID.values());
}

View File

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
@ -15,6 +16,7 @@ import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.zip.GZIPOutputStream;
import javax.net.ssl.HttpsURLConnection;
@ -33,38 +35,40 @@ import com.loohp.limbo.File.FileConfiguration;
@SuppressWarnings("unchecked")
public class Metrics {
static {
// Maven's Relocate is clever and changes strings, too. So we have to use this little "trick" ... :D
final String defaultPackage = new String(new byte[] { 'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's' });
final String examplePackage = new String(new byte[] { 'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e' });
// We want to make sure nobody just copy & pastes the example and use the wrong package names
if (Metrics.class.getPackage().getName().equals(defaultPackage) || Metrics.class.getPackage().getName().equals(examplePackage)) {
throw new IllegalStateException("bStats Metrics class has not been relocated correctly!");
}
}
// The version of this bStats class
public static final int B_STATS_VERSION = 1;
// The url to which the data is sent
private static final String URL = "https://bStats.org/submitData/bukkit";
private static final String URL = "https://bStats.org/submitData/server-implementation";
// Should failed requests be logged?
private static boolean logFailedRequests;
private static boolean logFailedRequests = false;
// The name of the server software
private final String name;
// The uuid of the server
private static String serverUUID;
private final String serverUUID;
private final String limboVersion;
// A list with all custom charts
private final List<CustomChart> charts = new ArrayList<>();
/**
* Class constructor.
*
* @param name The name of the server software.
* @param serverUUID The uuid of the server.
* @param logFailedRequests Whether failed requests should be logged or not.
* @param logger The logger for the failed requests.
* @throws FileNotFoundException
*/
public Metrics() {
public Metrics() throws FileNotFoundException {
name = "Limbo";
// Get the config file
File configFile = new File(new File("plugins", "bStats"), "config.yml");
File configFile = new File("plugins/bStats", "config.yml");
FileConfiguration config = new FileConfiguration(configFile);
// Check if the config file exists
@ -86,8 +90,12 @@ public class Metrics {
);
try {
config.saveConfig(configFile);
} catch (IOException ignored) {}
} catch (IOException e) {
e.printStackTrace();
}
}
limboVersion = new BufferedReader(new InputStreamReader(Limbo.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF"))).lines().filter(each -> each.startsWith("Limbo-Version:")).findFirst().orElse("Limbo-Version: unknown").substring(14).trim();
// Load the data
serverUUID = config.get("serverUuid", String.class);
@ -95,6 +103,20 @@ public class Metrics {
if (config.get("enabled", Boolean.class)) {
startSubmitting();
}
addCustomChart(new Metrics.SingleLineChart("players", new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return Limbo.getInstance().getPlayers().size();
}
}));
addCustomChart(new Metrics.SimplePie("limbo_version", new Callable<String>() {
@Override
public String call() throws Exception {
return limboVersion;
}
}));
}
/**
@ -130,14 +152,10 @@ public class Metrics {
*
* @return The plugin specific data.
*/
public JSONObject getPluginData() {
private JSONObject getPluginData() {
JSONObject data = new JSONObject();
String pluginName = "Limbo";
String pluginVersion = new BufferedReader(new InputStreamReader(Limbo.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF"))).lines().filter(each -> each.startsWith("Limbo-Version:")).findFirst().orElse("Limbo-Version: unknown").substring(14).trim();
data.put("pluginName", pluginName); // Append the name of the plugin
data.put("pluginVersion", pluginVersion); // Append the version of the plugin
data.put("pluginName", name); // Append the name of the server software
JSONArray customCharts = new JSONArray();
for (CustomChart customChart : charts) {
// Add the data of the custom charts
@ -158,14 +176,7 @@ public class Metrics {
* @return The server specific data.
*/
private JSONObject getServerData() {
// Minecraft specific data
int playerAmount = Limbo.getInstance().getPlayers().size();
int onlineMode = 0;
String limboVersion = Limbo.getInstance().serverImplementationVersion;
limboVersion = limboVersion.substring(limboVersion.indexOf("MC: ") + 4, limboVersion.length() - 1);
// OS/Java specific data
String javaVersion = System.getProperty("java.version");
// OS specific data
String osName = System.getProperty("os.name");
String osArch = System.getProperty("os.arch");
String osVersion = System.getProperty("os.version");
@ -175,11 +186,7 @@ public class Metrics {
data.put("serverUUID", serverUUID);
data.put("playerAmount", playerAmount);
data.put("onlineMode", onlineMode);
data.put("limboVersion", limboVersion);
data.put("javaVersion", javaVersion);
data.put("playerAmount", Limbo.getInstance().getPlayers().size());
data.put("osName", osName);
data.put("osArch", osArch);
data.put("osVersion", osVersion);
@ -195,21 +202,18 @@ public class Metrics {
final JSONObject data = getServerData();
JSONArray pluginData = new JSONArray();
pluginData.add(this.getPluginData());
pluginData.add(getPluginData());
data.put("plugins", pluginData);
// Create a new thread for the connection to the bStats server
new Thread(() -> {
try {
// Send the data
// We are still in the Thread of the timer, so nothing get blocked :)
sendData(data);
} catch (Exception e) {
// Something went wrong! :(
if (logFailedRequests) {
System.err.println("Could not submit stats for Limbo " + e);
Limbo.getInstance().getConsole().sendMessage("Could not submit stats of " + name + "\n" + e);
}
}
}, "Limbo Metrics Submission Thread").start();
}
/**
@ -270,21 +274,21 @@ public class Metrics {
public static abstract class CustomChart {
// The id of the chart
protected final String chartId;
final String chartId;
/**
* Class constructor.
*
* @param chartId The id of the chart.
*/
public CustomChart(String chartId) {
CustomChart(String chartId) {
if (chartId == null || chartId.isEmpty()) {
throw new IllegalArgumentException("ChartId cannot be null or empty!");
}
this.chartId = chartId;
}
protected JSONObject getRequestJsonObject() {
private JSONObject getRequestJsonObject() {
JSONObject chart = new JSONObject();
chart.put("chartId", chartId);
try {
@ -296,42 +300,39 @@ public class Metrics {
chart.put("data", data);
} catch (Throwable t) {
if (logFailedRequests) {
System.err.println("Failed to get data for custom chart with id " + chartId + t);
Limbo.getInstance().getConsole().sendMessage("Failed to get data for custom chart with id " + chartId + "\n" + t);
}
return null;
}
return chart;
}
protected abstract JSONObject getChartData();
protected abstract JSONObject getChartData() throws Exception;
}
/**
* Represents a custom simple pie.
*/
public static abstract class SimplePie extends CustomChart {
public static class SimplePie extends CustomChart {
private final Callable<String> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SimplePie(String chartId) {
public SimplePie(String chartId, Callable<String> callable) {
super(chartId);
this.callable = callable;
}
/**
* Gets the value of the pie.
*
* @return The value of the pie.
*/
public abstract String getValue();
@Override
protected JSONObject getChartData() {
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
String value = getValue();
String value = callable.call();
if (value == null || value.isEmpty()) {
// Null = skip the chart
return null;
@ -344,31 +345,26 @@ public class Metrics {
/**
* Represents a custom advanced pie.
*/
public static abstract class AdvancedPie extends CustomChart {
public static class AdvancedPie extends CustomChart {
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public AdvancedPie(String chartId) {
public AdvancedPie(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
/**
* Gets the values of the pie.
*
* @param valueMap Just an empty map. The only reason it exists is to make your life easier.
* You don't have to create a map yourself!
* @return The values of the pie.
*/
public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap);
@Override
protected JSONObject getChartData() {
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
HashMap<String, Integer> map = getValues(new HashMap<String, Integer>());
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
@ -391,30 +387,76 @@ public class Metrics {
}
/**
* Represents a custom single line chart.
* Represents a custom drilldown pie.
*/
public static abstract class SingleLineChart extends CustomChart {
public static class DrilldownPie extends CustomChart {
private final Callable<Map<String, Map<String, Integer>>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SingleLineChart(String chartId) {
public DrilldownPie(String chartId, Callable<Map<String, Map<String, Integer>>> callable) {
super(chartId);
this.callable = callable;
}
@Override
public JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, Map<String, Integer>> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean reallyAllSkipped = true;
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) {
JSONObject value = new JSONObject();
boolean allSkipped = true;
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
value.put(valueEntry.getKey(), valueEntry.getValue());
allSkipped = false;
}
if (!allSkipped) {
reallyAllSkipped = false;
values.put(entryValues.getKey(), value);
}
}
if (reallyAllSkipped) {
// Null = skip the chart
return null;
}
data.put("values", values);
return data;
}
}
/**
* Gets the value of the chart.
*
* @return The value of the chart.
* Represents a custom single line chart.
*/
public abstract int getValue();
public static class SingleLineChart extends CustomChart {
private final Callable<Integer> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SingleLineChart(String chartId, Callable<Integer> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() {
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
int value = getValue();
int value = callable.call();
if (value == 0) {
// Null = skip the chart
return null;
@ -428,31 +470,26 @@ public class Metrics {
/**
* Represents a custom multi line chart.
*/
public static abstract class MultiLineChart extends CustomChart {
public static class MultiLineChart extends CustomChart {
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public MultiLineChart(String chartId) {
public MultiLineChart(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
/**
* Gets the values of the chart.
*
* @param valueMap Just an empty map. The only reason it exists is to make your life easier.
* You don't have to create a map yourself!
* @return The values of the chart.
*/
public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap);
@Override
protected JSONObject getChartData() {
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
HashMap<String, Integer> map = getValues(new HashMap<String, Integer>());
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
@ -478,31 +515,26 @@ public class Metrics {
/**
* Represents a custom simple bar chart.
*/
public static abstract class SimpleBarChart extends CustomChart {
public static class SimpleBarChart extends CustomChart {
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SimpleBarChart(String chartId) {
public SimpleBarChart(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
/**
* Gets the value of the chart.
*
* @param valueMap Just an empty map. The only reason it exists is to make your life easier.
* You don't have to create a map yourself!
* @return The value of the chart.
*/
public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap);
@Override
protected JSONObject getChartData() {
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
HashMap<String, Integer> map = getValues(new HashMap<String, Integer>());
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
@ -521,31 +553,26 @@ public class Metrics {
/**
* Represents a custom advanced bar chart.
*/
public static abstract class AdvancedBarChart extends CustomChart {
public static class AdvancedBarChart extends CustomChart {
private final Callable<Map<String, int[]>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public AdvancedBarChart(String chartId) {
public AdvancedBarChart(String chartId, Callable<Map<String, int[]>> callable) {
super(chartId);
this.callable = callable;
}
/**
* Gets the value of the chart.
*
* @param valueMap Just an empty map. The only reason it exists is to make your life easier.
* You don't have to create a map yourself!
* @return The value of the chart.
*/
public abstract HashMap<String, int[]> getValues(HashMap<String, int[]> valueMap);
@Override
protected JSONObject getChartData() {
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
HashMap<String, int[]> map = getValues(new HashMap<String, int[]>());
Map<String, int[]> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;

View File

@ -43,6 +43,7 @@ public class PluginManager {
if (name.endsWith("plugin.yml") || name.endsWith("limbo.yml")) {
found = true;
@SuppressWarnings("deprecation")
FileConfiguration pluginYaml = new FileConfiguration(zip);
String main = pluginYaml.get("main", String.class);
String pluginName = pluginYaml.get("name", String.class);