Connect bStats
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package io.akarin.server.core;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import com.destroystokyo.paper.Metrics;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
|
||||
public abstract class MetricsBootstrap {
|
||||
// Dragged from 'PaperMetrics' cause it's a private subclass, this will be called from mixined 'PaperConfig' class
|
||||
public static void startMetrics() {
|
||||
// Get the config file
|
||||
File configFile = new File(new File((File) MinecraftServer.getServer().options.valueOf("plugins"), "bStats"), "config.yml");
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
|
||||
|
||||
// Check if the config file exists
|
||||
if (!config.isSet("serverUuid")) {
|
||||
|
||||
// Add default values
|
||||
config.addDefault("enabled", true);
|
||||
// Every server gets it's unique random id.
|
||||
config.addDefault("serverUuid", UUID.randomUUID().toString());
|
||||
// Should failed request be logged?
|
||||
config.addDefault("logFailedRequests", false);
|
||||
|
||||
// Inform the server owners about bStats
|
||||
config.options().header(
|
||||
"bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
|
||||
"To honor their work, you should not disable it.\n" +
|
||||
"This has nearly no effect on the server performance!\n" +
|
||||
"Check out https://bStats.org/ to learn more :)"
|
||||
).copyDefaults(true);
|
||||
try {
|
||||
config.save(configFile);
|
||||
} catch (IOException ignored) {
|
||||
;
|
||||
}
|
||||
}
|
||||
// Load the data
|
||||
String serverUUID = config.getString("serverUuid");
|
||||
boolean logFailedRequests = config.getBoolean("logFailedRequests", false);
|
||||
// Only start Metrics, if it's enabled in the config
|
||||
if (config.getBoolean("enabled", true)) {
|
||||
new Metrics("Akarin", serverUUID, logFailedRequests, Bukkit.getLogger()); // Paper -> Akarin
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package io.akarin.server.mixin.core;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import io.akarin.api.LogWrapper;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Mutable;
|
||||
import org.spongepowered.asm.mixin.Overwrite;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import com.destroystokyo.paper.Metrics;
|
||||
import com.destroystokyo.paper.Metrics.CustomChart;
|
||||
|
||||
@Mixin(value = Metrics.class, remap = false)
|
||||
public class MixinMetrics {
|
||||
// The url to which the data is sent - bukkit/Torch (keep our old name)
|
||||
@Shadow @Mutable @Final public static String URL = "https://bStats.org/submitData/bukkit";
|
||||
|
||||
// The name of the server software
|
||||
@Shadow @Final private String name;
|
||||
|
||||
// A list with all custom charts
|
||||
@Shadow @Final private List<CustomChart> charts;
|
||||
|
||||
/**
|
||||
* Injects the plugin specific data - insert version
|
||||
*
|
||||
* @return The plugin specific data.
|
||||
*/
|
||||
@Overwrite
|
||||
private JSONObject getPluginData() {
|
||||
JSONObject data = new JSONObject();
|
||||
|
||||
data.put("pluginName", name); // Append the name of the server software
|
||||
data.put("pluginVersion", Metrics.class.getPackage().getImplementationVersion() != null ? Metrics.class.getPackage().getImplementationVersion() : "unknown"); // Akarin
|
||||
JSONArray customCharts = new JSONArray();
|
||||
data.put("customCharts", customCharts);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// The uuid of the server
|
||||
@Shadow @Final private String serverUUID;
|
||||
|
||||
/**
|
||||
* Gets the server specific data - insert minecraft data
|
||||
*
|
||||
* @return The server specific data.
|
||||
*/
|
||||
@Overwrite
|
||||
private JSONObject getServerData() {
|
||||
// Minecraft specific data
|
||||
int playerAmount = Bukkit.getOnlinePlayers().size();
|
||||
int onlineMode = Bukkit.getOnlineMode() ? 1 : 0;
|
||||
String bukkitVersion = org.bukkit.Bukkit.getVersion();
|
||||
bukkitVersion = bukkitVersion.substring(bukkitVersion.indexOf("MC: ") + 4, bukkitVersion.length() - 1);
|
||||
LogWrapper.logger.warn("Akarin debug: " + URL);
|
||||
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("playerAmount", playerAmount);
|
||||
data.put("onlineMode", onlineMode);
|
||||
data.put("bukkitVersion", bukkitVersion);
|
||||
|
||||
// OS specific data
|
||||
String osName = System.getProperty("os.name");
|
||||
String osArch = System.getProperty("os.arch");
|
||||
String osVersion = System.getProperty("os.version");
|
||||
int coreCount = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
data.put("serverUUID", serverUUID);
|
||||
|
||||
data.put("osName", osName);
|
||||
data.put("osArch", osArch);
|
||||
data.put("osVersion", osVersion);
|
||||
data.put("coreCount", coreCount);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.akarin.server.mixin.core;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Overwrite;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
|
||||
import com.destroystokyo.paper.PaperConfig;
|
||||
|
||||
import io.akarin.server.core.MetricsBootstrap;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
|
||||
@Mixin(value = PaperConfig.class, remap = false)
|
||||
public class MixinPaperConfig {
|
||||
@Shadow static Map<String, Command> commands;
|
||||
@Shadow private static boolean metricsStarted;
|
||||
|
||||
@Overwrite
|
||||
public static void registerCommands() {
|
||||
for (Map.Entry<String, Command> entry : commands.entrySet()) {
|
||||
MinecraftServer.getServer().server.getCommandMap().register(entry.getKey(), "Paper", entry.getValue());
|
||||
}
|
||||
|
||||
if (!metricsStarted) {
|
||||
MetricsBootstrap.startMetrics();
|
||||
metricsStarted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user