initial
This commit is contained in:
75
src/main/java/net/t2code/alias/Bungee/Main.java
Normal file
75
src/main/java/net/t2code/alias/Bungee/Main.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package net.t2code.alias.Bungee;
|
||||
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.t2code.alias.Bungee.objects.Alias_Objekt;
|
||||
import net.t2code.alias.Bungee.system.Load;
|
||||
import net.t2code.alias.Util;
|
||||
import net.t2code.lib.Bungee.Lib.messages.BT2CodeTemplate;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public final class Main extends Plugin {
|
||||
public static File getPath() {
|
||||
return plugin.getDataFolder();
|
||||
}
|
||||
|
||||
public static String prefix = Util.Prefix;
|
||||
public static Integer spigotID = Util.SpigotID;
|
||||
public static Integer bstatsID = Util.BstatsID;
|
||||
public static String spigot = Util.Spigot;
|
||||
public static String discord = Util.Discord;
|
||||
|
||||
|
||||
public static String version;
|
||||
public static String autor;
|
||||
|
||||
public static Main plugin;
|
||||
|
||||
public static HashMap<String, Alias_Objekt> aliasHashMap = new HashMap<>();
|
||||
public static ArrayList<String> allAliases = new ArrayList<>();
|
||||
|
||||
private static Boolean enable= false;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Plugin startup logic
|
||||
plugin = this;
|
||||
autor = plugin.getDescription().getAuthor();
|
||||
version = plugin.getDescription().getVersion();
|
||||
if (pluginNotFound("T2CodeLib", 96388, Util.getRequiredT2CodeLibVersion())) return;
|
||||
Load.onLoad(prefix, autor, version, spigot, spigotID, discord, bstatsID);
|
||||
enable = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
aliasHashMap.clear();
|
||||
allAliases.clear();
|
||||
if (enable) return;
|
||||
BT2CodeTemplate.onDisable(prefix, autor, version, spigot, discord);
|
||||
}
|
||||
|
||||
public static Boolean pluginNotFound(String pl, Integer spigotID, double ver) {
|
||||
if (ProxyServer.getInstance().getPluginManager().getPlugin(pl) == null) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Plugin can not be loaded!");
|
||||
ProxyServer.getInstance().getConsole().sendMessage(Util.getPrefix() + " §e" + pl + " §4could not be found. Please download it here: " +
|
||||
"§6https://spigotmc.org/resources/" + pl + "." + spigotID + " §4to be able to use this plugin.");
|
||||
Main.plugin.onDisable();
|
||||
return true;
|
||||
} else {
|
||||
if (Double.parseDouble(Objects.requireNonNull(ProxyServer.getInstance().getPluginManager().getPlugin(pl)).getDescription().getVersion()) < ver) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Plugin can not be loaded!");
|
||||
ProxyServer.getInstance().getConsole().sendMessage(Util.getPrefix() + " §e" + pl + " §4is out of date! This plugin requires at least version §2" + ver + " §4of §6" + pl + " §4Please update it here: §6https://spigotmc.org/resources/" + pl + "." + spigotID + " §4to use this version of " + plugin.getDescription().getName() + ".");
|
||||
Main.plugin.onDisable();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,96 @@
|
||||
package net.t2code.alias.Bungee.cmdManagement;
|
||||
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.plugin.Command;
|
||||
import net.md_5.bungee.api.plugin.TabExecutor;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
import net.t2code.alias.Bungee.config.languages.SelectMessages;
|
||||
import net.t2code.lib.Bungee.Lib.messages.Bsend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class Alias_CmdExecuter extends Command implements TabExecutor {
|
||||
String Prefix;
|
||||
public Alias_CmdExecuter() {
|
||||
super("t2code-alias","t2code.alias.admin","t2c-a", "alias");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String[] args) {
|
||||
Prefix = Main.prefix;
|
||||
|
||||
if (args.length == 0) {
|
||||
// Command
|
||||
if (sender.hasPermission("t2code.alias.command.info")) {
|
||||
Commands.info(sender);
|
||||
} else Bsend.sender(sender, SelectMessages.NoPermissionForCommand.replace("[cmd]", "/t2code-alias info")
|
||||
.replace("[perm]", "t2code.alias.command.info"));
|
||||
} else {
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "reload":
|
||||
case "rl":
|
||||
if (sender.hasPermission("t2code.alias.command.reload")) {
|
||||
Commands.reload(sender);
|
||||
} else Bsend.sender(sender, SelectMessages.NoPermissionForCommand.replace("[cmd]", "/t2code-alias reload")
|
||||
.replace("[perm]", "t2code.alias.command.reload"));
|
||||
break;
|
||||
case "info":
|
||||
case "plugin":
|
||||
case "version":
|
||||
if (sender.hasPermission("t2code.alias.command.info")) {
|
||||
Commands.info(sender);
|
||||
} else Bsend.sender(sender, SelectMessages.NoPermissionForCommand.replace("[cmd]", "/t2code-alias info")
|
||||
.replace("[perm]", "t2code.alias.command.info"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TabCompleter
|
||||
private static HashMap<String, String> arg1 = new HashMap<String, String>() {{
|
||||
put("reload", "t2code.alias.command.reload");
|
||||
put("rl", "t2code.alias.command.reload");
|
||||
put("info", "t2code.alias.command.info");
|
||||
}};
|
||||
|
||||
@Override
|
||||
public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
List<String> list = new ArrayList<>();
|
||||
if (sender instanceof ProxiedPlayer) {
|
||||
ProxiedPlayer p = (ProxiedPlayer) sender;
|
||||
if (args.length == 1) {
|
||||
for (String command : arg1.keySet()) {
|
||||
Boolean passend = true;
|
||||
for (int i = 0; i < args[0].length(); i++) {
|
||||
if (args[0].length() >= command.length()) {
|
||||
passend = false;
|
||||
} else {
|
||||
if (args[0].charAt(i) != command.charAt(i)) {
|
||||
passend = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hasPermission(p, arg1.get(command)) && passend) {
|
||||
list.add(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static boolean hasPermission(ProxiedPlayer player, String permission) {
|
||||
String[] Permissions = permission.split(";");
|
||||
for (String perm : Permissions) {
|
||||
if (player.hasPermission(perm)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,38 @@
|
||||
package net.t2code.alias.Bungee.cmdManagement;
|
||||
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
import net.t2code.alias.Bungee.config.languages.SelectMessages;
|
||||
import net.t2code.alias.Bungee.system.Load;
|
||||
import net.t2code.lib.Bungee.Lib.messages.BT2CodeTemplate;
|
||||
import net.t2code.lib.Bungee.Lib.messages.Bsend;
|
||||
import net.t2code.lib.Bungee.Lib.update.BUpdateAPI;
|
||||
|
||||
|
||||
public class Commands {
|
||||
private static Plugin plugin = Main.plugin;
|
||||
private static String prefix = Main.prefix;
|
||||
private static String autor = Main.autor;
|
||||
private static String version = Main.version;
|
||||
private static String spigot = Main.spigot;
|
||||
private static String discord = Main.discord;
|
||||
|
||||
|
||||
public static void info(CommandSender sender) {
|
||||
BT2CodeTemplate.sendInfo(sender,prefix,spigot,discord,autor,version, BUpdateAPI.bungeePluginVersionen.get(plugin.getDescription().getName()).publicVersion);
|
||||
}
|
||||
|
||||
public static void reload(CommandSender sender) {
|
||||
if (sender instanceof ProxiedPlayer) sender.sendMessage(SelectMessages.ReloadStart);
|
||||
Bsend.console(Main.prefix + "§8-------------------------------");
|
||||
Bsend.console(Main.prefix + " §6Plugin reload...");
|
||||
Bsend.console(Main.prefix + "§8-------------------------------");
|
||||
Load.loadReload();
|
||||
if (sender instanceof ProxiedPlayer) sender.sendMessage(SelectMessages.ReloadEnd);
|
||||
Bsend.console(Main.prefix + "§8-------------------------------");
|
||||
Bsend.console(Main.prefix + " §2Plugin successfully reloaded.");
|
||||
Bsend.console(Main.prefix + "§8-------------------------------");
|
||||
}
|
||||
}
|
@@ -0,0 +1,114 @@
|
||||
package net.t2code.alias.Bungee.cmdManagement;
|
||||
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.plugin.Command;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
import net.t2code.alias.Bungee.config.languages.SelectMessages;
|
||||
import net.t2code.alias.Bungee.objects.Alias_Objekt;
|
||||
import net.t2code.lib.Bungee.Lib.commands.Bcmd;
|
||||
import net.t2code.lib.Bungee.Lib.messages.Bsend;
|
||||
import net.t2code.lib.Bungee.Lib.replace.BReplace;
|
||||
import net.t2code.lib.Spigot.Lib.messages.TextBuilder;
|
||||
|
||||
public class RegisterCommands extends Command {
|
||||
private final String alias;
|
||||
private final String prefix = Main.prefix;
|
||||
|
||||
public RegisterCommands(String alias) {
|
||||
super(alias, "t2code.alias.use." + alias.toLowerCase());
|
||||
this.alias = alias;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String[] args) {
|
||||
Alias_Objekt Alias = Main.aliasHashMap.get(alias);
|
||||
|
||||
if (!Alias.AliasEnable) {
|
||||
Bsend.sender(sender, SelectMessages.AliasDisabled);
|
||||
return;
|
||||
}
|
||||
if (sender instanceof ProxiedPlayer) {
|
||||
ProxiedPlayer player = (ProxiedPlayer) sender;
|
||||
|
||||
if (Alias.AdminEnable) {
|
||||
if (player.hasPermission(Alias.AdminPermission)) {
|
||||
if (Alias.AdminCommandEnable) {
|
||||
for (String cmd : Alias.AdminCommands) {
|
||||
if (Alias.AdminCommandAsConsole) {
|
||||
Bcmd.Console(cmd.replace("[player]", player.getName()));
|
||||
} else {
|
||||
Bcmd.Player(player, cmd.replace("[player]", player.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Alias.AdminMessageEnable) {
|
||||
for (String msg : Alias.AdminMessages) {
|
||||
String text = BReplace.replace(prefix, msg);
|
||||
String hover = BReplace.replace(prefix, Alias.AdminHover);
|
||||
if (Alias.AdminTextBuilder) {
|
||||
TextBuilder textBuilder = new TextBuilder(text);
|
||||
textBuilder.addHover(hover);
|
||||
if (Alias.AdminClickEvent) {
|
||||
textBuilder.addClickEvent(ClickEvent.Action.valueOf(Alias.AdminAction), Alias.AdminActionValue);
|
||||
}
|
||||
player.sendMessage(textBuilder.build());
|
||||
} else {
|
||||
Bsend.player(player, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (Alias.Perm_necessary) {
|
||||
if (!(player.hasPermission("t2code.alias.use." + alias.toLowerCase()) || player.hasPermission("t2code.alias.admin"))) {
|
||||
Bsend.player(player, SelectMessages.NoPermissionForCommand.replace("[cmd]", "/" + alias.toLowerCase())
|
||||
.replace("[perm]", "t2code.alias.use." + alias.toLowerCase()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (Alias.Command_Enable) {
|
||||
for (String cmd : Alias.Command) {
|
||||
if (Alias.CommandAsConsole) {
|
||||
Bcmd.Console(cmd.replace("[player]", player.getName()));
|
||||
} else {
|
||||
Bcmd.Player(player, cmd.replace("[player]", player.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Alias.Message_Enable) {
|
||||
for (String msg : Alias.Messages) {
|
||||
String text = BReplace.replace(prefix, msg);
|
||||
String hover = BReplace.replace(prefix, Alias.Hover);
|
||||
if (Alias.TextBuilder) {
|
||||
TextBuilder textBuilder = new TextBuilder(text);
|
||||
textBuilder.addHover(hover);
|
||||
if (Alias.ClickEvent) {
|
||||
textBuilder.addClickEvent(ClickEvent.Action.valueOf(Alias.Action), Alias.ActionValue);
|
||||
}
|
||||
player.sendMessage(textBuilder.build());
|
||||
} else {
|
||||
Bsend.player(player, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (Alias.ConsoleEnable) {
|
||||
if (Alias.ConsoleCommandEnable) {
|
||||
for (String cmd : Alias.ConsoleCommands) {
|
||||
Bcmd.Console(cmd.replace("[player]", sender.getName()));
|
||||
}
|
||||
}
|
||||
if (Alias.ConsoleMessageEnable) {
|
||||
for (String msg : Alias.ConsoleMessages) {
|
||||
Bsend.console(BReplace.replace(prefix, msg));
|
||||
}
|
||||
}
|
||||
} else Bsend.sender(sender, SelectMessages.OnlyForPlayer);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,35 @@
|
||||
package net.t2code.alias.Bungee.config.config;
|
||||
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
import net.t2code.lib.Bungee.Lib.messages.Bsend;
|
||||
import net.t2code.lib.Bungee.Lib.yamlConfiguration.BConfig;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CreateConfig {
|
||||
public static Integer ConfigVersion = 1;
|
||||
|
||||
public static void configCreate() throws IOException {
|
||||
Long long_ = Long.valueOf(System.currentTimeMillis());
|
||||
|
||||
File config = new File(Main.getPath(), "config.yml");
|
||||
if (!config.exists()) {
|
||||
config.createNewFile();
|
||||
}
|
||||
Configuration configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(config);
|
||||
|
||||
configuration.set("ConfigVersion", ConfigVersion);
|
||||
|
||||
//BConfig.set("Plugin.UpdateCheckOnJoin", true, configuration);
|
||||
BConfig.set("Plugin.language", "english", configuration);
|
||||
|
||||
ConfigurationProvider.getProvider(YamlConfiguration.class).save(configuration, config);
|
||||
|
||||
Bsend.console(Main.prefix + " §2config.yml were successfully created / updated." + " §7- §e" + (System.currentTimeMillis() - long_.longValue()) + "ms");
|
||||
}
|
||||
}
|
@@ -0,0 +1,107 @@
|
||||
package net.t2code.alias.Bungee.config.config;
|
||||
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
import net.t2code.lib.Bungee.Lib.messages.Bsend;
|
||||
import net.t2code.lib.Bungee.Lib.yamlConfiguration.BConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CreateExampleAliasConfig {
|
||||
|
||||
private static Boolean Enable = true;
|
||||
private static Boolean PermNecessary = true;
|
||||
|
||||
private static Boolean CommandEnable = false;
|
||||
private static Boolean CommandAsConsole = true;
|
||||
private static List Commands = Arrays.asList("end");
|
||||
|
||||
private static Boolean MessageEnable = true;
|
||||
private static List Messages = Arrays.asList("&5Hi", "&6This is an example message");
|
||||
private static Boolean TextBuilder = true;
|
||||
private static String Hover = "&4T2Code";
|
||||
private static Boolean ClickEvent = true;
|
||||
private static String Action = "OPEN_URL";
|
||||
private static String ActionValue = "git.t2code.net";
|
||||
|
||||
private static Boolean AdminEnable = false;
|
||||
private static String AdminPermission = "t2code.alias.admin";
|
||||
|
||||
private static Boolean AdminCommandEnable = true;
|
||||
private static Boolean AdminCommandAsConsole = true;
|
||||
private static List AdminCommands = Arrays.asList();
|
||||
|
||||
private static Boolean AdminMessageEnable = false;
|
||||
private static List AdminMessages = Arrays.asList();
|
||||
private static Boolean AdminTextBuilder = false;
|
||||
private static String AdminHover = "";
|
||||
private static Boolean AdminClickEvent = false;
|
||||
private static String AdminAction = "";
|
||||
private static String AdminActionValue = "";
|
||||
|
||||
private static Boolean ConsoleEnable = false;
|
||||
private static Boolean ConsoleCommandEnable = false;
|
||||
private static List<String> ConsoleCommands = Arrays.asList();
|
||||
private static Boolean ConsoleMessageEnable = false;
|
||||
private static List<String> ConsoleMessages = Arrays.asList();
|
||||
|
||||
public static void configCreate() throws IOException {
|
||||
Long long_ = Long.valueOf(System.currentTimeMillis());
|
||||
|
||||
File directory = new File(Main.getPath(), "Alias");
|
||||
if (!directory.exists()) {
|
||||
directory.mkdir();
|
||||
}
|
||||
File config = new File(Main.getPath(), "Alias/aliasexample.yml");
|
||||
if (!config.exists()) {
|
||||
config.createNewFile();
|
||||
}
|
||||
|
||||
Configuration configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(config);
|
||||
|
||||
|
||||
BConfig.set("Alias.Enable", Enable, configuration);
|
||||
BConfig.set("Alias.Permission.Necessary", PermNecessary, configuration);
|
||||
|
||||
BConfig.set("Alias.Command.Enable", CommandEnable, configuration);
|
||||
BConfig.set("Alias.Command.CommandAsConsole", CommandAsConsole, configuration);
|
||||
BConfig.set("Alias.Command.Commands", Commands, configuration);
|
||||
|
||||
BConfig.set("Alias.Message.Enable", MessageEnable, configuration);
|
||||
BConfig.set("Alias.Message.Messages", Messages, configuration);
|
||||
BConfig.set("Alias.Message.TextBuilder.Enable", TextBuilder, configuration);
|
||||
BConfig.set("Alias.Message.TextBuilder.Hover", Hover, configuration);
|
||||
BConfig.set("Alias.Message.TextBuilder.ClickEvent.Enable", ClickEvent, configuration);
|
||||
BConfig.set("Alias.Message.TextBuilder.ClickEvent.Action", Action, configuration);
|
||||
BConfig.set("Alias.Message.TextBuilder.ClickEvent.ActionValue", ActionValue, configuration);
|
||||
|
||||
|
||||
BConfig.set("Alias.Admin.Enable", AdminEnable, configuration);
|
||||
BConfig.set("Alias.Admin.Permission", AdminPermission, configuration);
|
||||
|
||||
BConfig.set("Alias.Admin.Command.Enable", AdminCommandEnable, configuration);
|
||||
BConfig.set("Alias.Admin.Command.CommandAsConsole", AdminCommandAsConsole, configuration);
|
||||
BConfig.set("Alias.Admin.Command.Commands", AdminCommands, configuration);
|
||||
|
||||
BConfig.set("Alias.Admin.Message.Enable", AdminMessageEnable, configuration);
|
||||
BConfig.set("Alias.Admin.Message.Messages", AdminMessages, configuration);
|
||||
BConfig.set("Alias.Admin.Message.TextBuilder.Enable", AdminTextBuilder, configuration);
|
||||
BConfig.set("Alias.Admin.Message.TextBuilder.Hover", AdminHover, configuration);
|
||||
BConfig.set("Alias.Admin.Message.TextBuilder.ClickEvent.Enable", AdminClickEvent, configuration);
|
||||
BConfig.set("Alias.Admin.Message.TextBuilder.ClickEvent.Action", AdminAction, configuration);
|
||||
BConfig.set("Alias.Admin.Message.TextBuilder.ClickEvent.ActionValue", AdminActionValue, configuration);
|
||||
|
||||
BConfig.set("Alias.Console.Enable", ConsoleEnable, configuration);
|
||||
BConfig.set("Alias.Console.Command.Enable", ConsoleCommandEnable, configuration);
|
||||
BConfig.set("Alias.Console.Command.Commands", ConsoleCommands, configuration);
|
||||
BConfig.set("Alias.Console.Message.Enable", ConsoleMessageEnable, configuration);
|
||||
BConfig.set("Alias.Console.Message.Messages", ConsoleMessages, configuration);
|
||||
ConfigurationProvider.getProvider(YamlConfiguration.class).save(configuration, config);
|
||||
Bsend.console(Main.prefix + " §2config.yml were successfully created / updated." + " §7- §e" + (System.currentTimeMillis() - long_.longValue()) + "ms");
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
package net.t2code.alias.Bungee.config.config;
|
||||
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
import net.t2code.alias.Bungee.system.AliasRegister;
|
||||
import net.t2code.alias.Bungee.objects.Alias_Objekt;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SelectAlias {
|
||||
public static void onSelect() throws IOException {
|
||||
Main.aliasHashMap.clear();
|
||||
Main.allAliases.clear();
|
||||
File f = new File(Main.getPath() + "/Alias/");
|
||||
File[] fileArray = f.listFiles();
|
||||
|
||||
assert fileArray != null;
|
||||
for (File config_gui : fileArray) {
|
||||
Main.allAliases.add(config_gui.getName().replace(".yml", ""));
|
||||
String sub = config_gui.getName().substring(config_gui.getName().length() - 4);
|
||||
if (sub.equals(".yml")) {
|
||||
Configuration configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(config_gui);
|
||||
|
||||
Alias_Objekt alias = new Alias_Objekt(
|
||||
configuration.getBoolean("Alias.Enable"),
|
||||
configuration.getBoolean("Alias.Permission.Necessary"),
|
||||
|
||||
configuration.getBoolean("Alias.Command.Enable"),
|
||||
configuration.getBoolean("Alias.Command.CommandAsConsole"),
|
||||
configuration.getStringList("Alias.Command.Commands"),
|
||||
|
||||
configuration.getBoolean("Alias.Message.Enable"),
|
||||
configuration.getStringList("Alias.Message.Messages"),
|
||||
configuration.getBoolean("Alias.Message.TextBuilder.Enable"),
|
||||
configuration.getString("Alias.Message.TextBuilder.Hover"),
|
||||
configuration.getBoolean("Alias.Message.TextBuilder.ClickEvent.Enable"),
|
||||
configuration.getString("Alias.Message.TextBuilder.ClickEvent.Action"),
|
||||
configuration.getString("Alias.Message.TextBuilder.ClickEvent.ActionValue"),
|
||||
|
||||
configuration.getBoolean("Alias.Admin.Enable"),
|
||||
configuration.getString("Alias.Admin.Permission"),
|
||||
|
||||
configuration.getBoolean("Alias.Admin.Command.Enable"),
|
||||
configuration.getBoolean("Alias.Admin.Command.CommandAsConsole"),
|
||||
configuration.getStringList("Alias.Admin.Command.Commands"),
|
||||
|
||||
configuration.getBoolean("Alias.Admin.Message.Enable"),
|
||||
configuration.getStringList("Alias.Admin.Message.Messages"),
|
||||
configuration.getBoolean("Alias.Admin.Message.TextBuilder.Enable"),
|
||||
configuration.getString("Alias.Admin.Message.TextBuilder.Hover"),
|
||||
configuration.getBoolean("Alias.Admin.Message.TextBuilder.ClickEvent.Enable"),
|
||||
configuration.getString("Alias.Admin.Message.TextBuilder.ClickEvent.Action"),
|
||||
configuration.getString("Alias.Admin.Message.TextBuilder.ClickEvent.ActionValue"),
|
||||
|
||||
configuration.getBoolean("Alias.Console.Enable"),
|
||||
configuration.getBoolean("Alias.Console.Command.Enable"),
|
||||
configuration.getStringList("Alias.Console.Command.Commands"),
|
||||
configuration.getBoolean("Alias.Console.Message.Enable"),
|
||||
configuration.getStringList("Alias.Console.Message.Messages"));
|
||||
Main.aliasHashMap.put(config_gui.getName().replace(".yml", ""), alias);
|
||||
AliasRegister.onRegister();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package net.t2code.alias.Bungee.config.config;
|
||||
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SelectConfig {
|
||||
public static Integer ConfigVersion;
|
||||
public static Boolean UpdateCheckOnJoin;
|
||||
public static String language;
|
||||
public static Boolean Bungee;
|
||||
public static String thisServer;
|
||||
public static void onSelect() throws IOException {
|
||||
File config = new File(Main.getPath(), "config.yml");
|
||||
Configuration configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(config);
|
||||
|
||||
ConfigVersion =configuration.getInt("ConfigVersion");
|
||||
|
||||
// UpdateCheckOnJoin = configuration.getBoolean("Plugin.UpdateCheckOnJoin");
|
||||
language = configuration.getString("Plugin.language");
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
package net.t2code.alias.Bungee.config.languages;
|
||||
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
import net.t2code.lib.Bungee.Lib.messages.Bsend;
|
||||
import net.t2code.lib.Bungee.Lib.yamlConfiguration.BConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class LanguagesCreate {
|
||||
private static Plugin plugin = Main.plugin;
|
||||
|
||||
public static void langCreate() throws IOException {
|
||||
Bsend.debug(plugin, "§4Language files are created / updated...");
|
||||
Long long_ = Long.valueOf(System.currentTimeMillis());
|
||||
|
||||
/**
|
||||
*
|
||||
* ENGLISH
|
||||
*
|
||||
*/
|
||||
File directory = new File(Main.getPath(), "languages");
|
||||
if (!directory.exists()) {
|
||||
directory.mkdir();
|
||||
}
|
||||
File messagesEN = new File(Main.getPath(), "languages/english.yml");
|
||||
if (!messagesEN.exists()) {
|
||||
messagesEN.createNewFile();
|
||||
}
|
||||
Configuration configurationEN = ConfigurationProvider.getProvider(YamlConfiguration.class).load(messagesEN);
|
||||
|
||||
BConfig.set("Plugin.OnlyForPlayer", MSG.EN_OnlyForPlayer, configurationEN);
|
||||
BConfig.set("Plugin.AliasDisabled", MSG.EN_Disabled, configurationEN);
|
||||
BConfig.set("Plugin.Reload.Start", MSG.EN_ReloadStart, configurationEN);
|
||||
BConfig.set("Plugin.Reload.End", MSG.EN_ReloadEnd, configurationEN);
|
||||
BConfig.set("Plugin.ForCommand", MSG.EN_NoPermissionForCommand, configurationEN);
|
||||
|
||||
ConfigurationProvider.getProvider(YamlConfiguration.class).save(configurationEN, messagesEN);
|
||||
|
||||
/**
|
||||
*
|
||||
* GERMAN
|
||||
*
|
||||
*/
|
||||
|
||||
File messagesDE = new File(Main.getPath(), "languages/german.yml");
|
||||
if (!messagesDE.exists()) {
|
||||
messagesDE.createNewFile();
|
||||
}
|
||||
Configuration configurationDE = ConfigurationProvider.getProvider(YamlConfiguration.class).load(messagesDE);
|
||||
|
||||
BConfig.set("Plugin.OnlyForPlayer", MSG.DE_OnlyForPlayer, configurationDE);
|
||||
BConfig.set("Plugin.AliasDisabled", MSG.DE_Disabled, configurationDE);
|
||||
BConfig.set("Plugin.Reload.Start", MSG.DE_ReloadStart, configurationDE);
|
||||
BConfig.set("Plugin.Reload.End", MSG.DE_ReloadEnd, configurationDE);
|
||||
BConfig.set("Plugin.ForCommand", MSG.DE_NoPermissionForCommand, configurationDE);
|
||||
|
||||
ConfigurationProvider.getProvider(YamlConfiguration.class).save(configurationEN, messagesDE);
|
||||
|
||||
Bsend.console(Main.prefix + " §2Language files were successfully created / updated." + " §7- §e" + (System.currentTimeMillis() - long_.longValue()) + "ms");
|
||||
}
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
// This claas was created by JaTiTV
|
||||
|
||||
// -----------------------------
|
||||
// _____ _____ _ _ _____
|
||||
// / ____/ ____| | | |_ _|
|
||||
// | | | | __| | | | | |
|
||||
// | | | | |_ | | | | | |
|
||||
// | |___| |__| | |__| |_| |_
|
||||
// \_____\_____|\____/|_____|
|
||||
// -----------------------------
|
||||
|
||||
package net.t2code.alias.Bungee.config.languages;
|
||||
|
||||
public class MSG {
|
||||
|
||||
// EN
|
||||
|
||||
public static String EN_OnlyForPlayer = "[prefix] &cThis command is for players only!";
|
||||
|
||||
public static String EN_Disabled = "Unknown command. Type ''/help'' for help.";
|
||||
|
||||
public static String EN_ReloadStart = "[prefix] &6Plugin is reloaded...";
|
||||
public static String EN_ReloadEnd = "[prefix] &2Plugin was successfully reloaded.";
|
||||
|
||||
public static String EN_NoPermissionForCommand = "[prefix] &cFor &b[cmd] &cyou lack the permission &6[perm]&c!";
|
||||
|
||||
// DE
|
||||
public static String DE_OnlyForPlayer = "[prefix] &cDieser Command ist nur f[ue]r Spieler!";
|
||||
|
||||
public static String DE_Disabled = "Unknown command. Type ''/help'' for help.";
|
||||
|
||||
public static String DE_ReloadStart = "[prefix] &6Plugin wird neu geladen...";
|
||||
public static String DE_ReloadEnd = "[prefix] &2Plugin wurde erfolgreich neu geladen.";
|
||||
|
||||
public static String DE_NoPermissionForCommand = "[prefix] &cF[ue]r &b[cmd] &cfehlt dir die Permission &6[perm]&c!";
|
||||
// FR
|
||||
|
||||
|
||||
// NO
|
||||
public static String NO_OnlyForPlayer = "[prefix] &cDenne kommandoen er for spillere kun!";
|
||||
|
||||
public static String NO_Disabled = "Unknown command. Type ''/help'' for help.";
|
||||
|
||||
public static String NO_ReloadStart = "[prefix] &6Pluginet blir relastet...";
|
||||
public static String NO_ReloadEnd = "[prefix] &2Pluginet har blitt lastet inn på nytt.";
|
||||
|
||||
public static String NO_NoPermissionForCommand = "[prefix] &cFor &b[cmd] &cmangler du tillatelsen &6[perm]&c!";
|
||||
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package net.t2code.alias.Bungee.config.languages;
|
||||
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
import net.t2code.alias.Bungee.config.config.SelectConfig;
|
||||
import net.t2code.lib.Bungee.Lib.messages.Bsend;
|
||||
import net.t2code.lib.Bungee.Lib.yamlConfiguration.BConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SelectMessages {
|
||||
private static String prefix = Main.prefix;
|
||||
|
||||
public static String selectMSG;
|
||||
|
||||
public static String OnlyForPlayer;
|
||||
public static String ReloadStart;
|
||||
public static String ReloadEnd;
|
||||
|
||||
public static String NoPermissionForCommand;
|
||||
public static String AliasDisabled;
|
||||
|
||||
|
||||
public static void onSelect(String Prefix)throws IOException {
|
||||
File msg = new File(Main.getPath(), "languages/" + SelectConfig.language + ".yml");
|
||||
Bsend.debug(Main.plugin, "§4Select language...");
|
||||
Long long_ = Long.valueOf(System.currentTimeMillis());
|
||||
if (!msg.isFile()) {
|
||||
Bsend.console(Prefix);
|
||||
Bsend.console(Prefix + " §4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
Bsend.console(Prefix + " §4The selected §c" + SelectConfig.language + " §4language file was not found.");
|
||||
Bsend.console(Prefix + " §6The default language §eEnglish §6is used!");
|
||||
Bsend.console(Prefix + " §4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
Bsend.console(Prefix);
|
||||
msg = new File(Main.getPath(), "languages/" + "english.yml");
|
||||
selectMSG = "english";
|
||||
} else selectMSG = SelectConfig.language;
|
||||
Configuration configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(msg);
|
||||
|
||||
OnlyForPlayer = BConfig.select(prefix, "Plugin.OnlyForPlayer", configuration);
|
||||
AliasDisabled = BConfig.select(prefix, "Plugin.AliasDisabled", configuration);
|
||||
ReloadStart = BConfig.select(prefix, "Plugin.Reload.Start", configuration);
|
||||
ReloadEnd = BConfig.select(prefix, "Plugin.Reload.End", configuration);
|
||||
NoPermissionForCommand = BConfig.select(prefix, "Plugin.ForCommand", configuration);
|
||||
|
||||
Bsend.console(Prefix + " §2Language successfully selected to: §6" + selectMSG + " §7- §e" + (System.currentTimeMillis() - long_.longValue()) + "ms");
|
||||
}
|
||||
}
|
103
src/main/java/net/t2code/alias/Bungee/objects/Alias_Objekt.java
Normal file
103
src/main/java/net/t2code/alias/Bungee/objects/Alias_Objekt.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package net.t2code.alias.Bungee.objects;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Alias_Objekt {
|
||||
public Boolean AliasEnable;
|
||||
public Boolean Command_Enable;
|
||||
public Boolean Perm_necessary;
|
||||
|
||||
public Boolean CommandAsConsole;
|
||||
public List<String> Command;
|
||||
public Boolean Message_Enable;
|
||||
public List<String> Messages;
|
||||
public Boolean TextBuilder;
|
||||
public String Hover;
|
||||
public Boolean ClickEvent;
|
||||
public String Action;
|
||||
public String ActionValue;
|
||||
|
||||
public Boolean AdminEnable;
|
||||
public String AdminPermission;
|
||||
public Boolean AdminCommandEnable;
|
||||
public Boolean AdminCommandAsConsole;
|
||||
public List<String> AdminCommands;
|
||||
public Boolean AdminMessageEnable;
|
||||
public List<String> AdminMessages;
|
||||
public Boolean AdminTextBuilder;
|
||||
public String AdminHover;
|
||||
public Boolean AdminClickEvent;
|
||||
public String AdminAction;
|
||||
public String AdminActionValue;
|
||||
public Boolean ConsoleEnable;
|
||||
public Boolean ConsoleCommandEnable;
|
||||
public List<String> ConsoleCommands;
|
||||
public Boolean ConsoleMessageEnable;
|
||||
public List<String> ConsoleMessages;
|
||||
|
||||
public Alias_Objekt(Boolean AliasEnable,
|
||||
Boolean Perm_necessary,
|
||||
|
||||
Boolean Command_Enable,
|
||||
Boolean CommandAsConsole,
|
||||
List Command,
|
||||
Boolean Message_Enable,
|
||||
List Messages,
|
||||
Boolean TextBuilder,
|
||||
String Hover,
|
||||
Boolean ClickEvent,
|
||||
String Action,
|
||||
String ActionValue,
|
||||
|
||||
Boolean AdminEnable,
|
||||
String AdminPermission,
|
||||
Boolean AdminCommandEnable,
|
||||
Boolean AdminCommandAsConsole,
|
||||
List AdminCommands,
|
||||
Boolean AdminMessageEnable,
|
||||
List AdminMessages,
|
||||
Boolean AdminTextBuilder,
|
||||
String AdminHover,
|
||||
Boolean AdminClickEvent,
|
||||
String AdminAction,
|
||||
String AdminActionValue,
|
||||
|
||||
Boolean ConsoleEnable,
|
||||
Boolean ConsoleCommandEnable,
|
||||
List<String> ConsoleCommands,
|
||||
Boolean ConsoleMessageEnable,
|
||||
List<String> ConsoleMessages) {
|
||||
this.AliasEnable = AliasEnable;
|
||||
this.Perm_necessary = Perm_necessary;
|
||||
|
||||
this.Command_Enable = Command_Enable;
|
||||
this.CommandAsConsole = CommandAsConsole;
|
||||
this.Command = Command;
|
||||
this.Message_Enable = Message_Enable;
|
||||
this.Messages = Messages;
|
||||
this.TextBuilder = TextBuilder;
|
||||
this.Hover = Hover;
|
||||
this.ClickEvent = ClickEvent;
|
||||
this.Action = Action;
|
||||
this.ActionValue = ActionValue;
|
||||
this.AdminEnable = AdminEnable;
|
||||
this.AdminPermission = AdminPermission;
|
||||
this.AdminCommandEnable = AdminCommandEnable;
|
||||
this.AdminCommandAsConsole = AdminCommandAsConsole;
|
||||
this.AdminCommands = AdminCommands;
|
||||
this.AdminMessageEnable = AdminMessageEnable;
|
||||
this.AdminMessages = AdminMessages;
|
||||
this.AdminTextBuilder = AdminTextBuilder;
|
||||
this.AdminHover = AdminHover;
|
||||
this.AdminClickEvent = AdminClickEvent;
|
||||
this.AdminAction = AdminAction;
|
||||
this.AdminActionValue = AdminActionValue;
|
||||
|
||||
this.ConsoleEnable = ConsoleEnable;
|
||||
this.ConsoleCommandEnable = ConsoleCommandEnable;
|
||||
this.ConsoleCommands = ConsoleCommands;
|
||||
this.ConsoleMessageEnable = ConsoleMessageEnable;
|
||||
this.ConsoleMessages = ConsoleMessages;
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package net.t2code.alias.Bungee.system;
|
||||
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
import net.t2code.alias.Bungee.cmdManagement.RegisterCommands;
|
||||
import net.t2code.lib.Bungee.Lib.messages.Bsend;
|
||||
|
||||
public class AliasRegister {
|
||||
public static void onRegister() {
|
||||
Plugin plugin = Main.plugin;
|
||||
for (String alias : Main.allAliases) {
|
||||
if (Main.aliasHashMap.get(alias) != null) {
|
||||
if (Main.aliasHashMap.get(alias).AliasEnable) {
|
||||
Bsend.debug(plugin, "Alias register 1.18_R1");
|
||||
ProxyServer.getInstance().getPluginManager().registerCommand(Main.plugin, new RegisterCommands(alias));
|
||||
}
|
||||
} else Bsend.warning(plugin, " §4AliasHashmap is null!");
|
||||
}
|
||||
}
|
||||
}
|
70
src/main/java/net/t2code/alias/Bungee/system/Load.java
Normal file
70
src/main/java/net/t2code/alias/Bungee/system/Load.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package net.t2code.alias.Bungee.system;
|
||||
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.t2code.alias.Bungee.Main;
|
||||
import net.t2code.alias.Bungee.cmdManagement.Alias_CmdExecuter;
|
||||
import net.t2code.alias.Bungee.config.config.CreateConfig;
|
||||
import net.t2code.alias.Bungee.config.config.CreateExampleAliasConfig;
|
||||
import net.t2code.alias.Bungee.config.config.SelectAlias;
|
||||
import net.t2code.alias.Bungee.config.config.SelectConfig;
|
||||
import net.t2code.alias.Bungee.config.languages.LanguagesCreate;
|
||||
import net.t2code.alias.Bungee.config.languages.SelectMessages;
|
||||
import net.t2code.lib.Bungee.Lib.messages.BT2CodeTemplate;
|
||||
import net.t2code.lib.Bungee.Lib.update.BUpdateAPI;
|
||||
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Load {
|
||||
static Plugin plugin = Main.plugin;
|
||||
public static void onLoad(String prefix, String autor, String version, String spigot, int spigotID, String discord, int bstatsID) {
|
||||
Long long_ = BT2CodeTemplate.onLoadHeader(prefix,autor,version,spigot,discord);
|
||||
if (!Main.getPath().exists()) Main.getPath().mkdir();
|
||||
ProxyServer.getInstance().getPluginManager().registerCommand(Main.plugin, new Alias_CmdExecuter());
|
||||
loadReload();
|
||||
BUpdateAPI.onUpdateCheckTimer(plugin, prefix, spigot, discord, spigotID);
|
||||
|
||||
BT2CodeTemplate.onLoadFooter(prefix, long_);
|
||||
Metrics metrics = new Metrics(plugin, bstatsID);
|
||||
}
|
||||
|
||||
public static void loadReload() {
|
||||
|
||||
if (!new File(Main.getPath(), "config.yml").exists()) {
|
||||
try {
|
||||
CreateExampleAliasConfig.configCreate();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
CreateConfig.configCreate();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
LanguagesCreate.langCreate();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
SelectConfig.onSelect();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
SelectMessages.onSelect(Main.prefix);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
SelectAlias.onSelect();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
846
src/main/java/net/t2code/alias/Bungee/system/Metrics.java
Normal file
846
src/main/java/net/t2code/alias/Bungee/system/Metrics.java
Normal file
@@ -0,0 +1,846 @@
|
||||
package net.t2code.alias.Bungee.system;
|
||||
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
public class Metrics {
|
||||
|
||||
private final Plugin plugin;
|
||||
|
||||
private final MetricsBase metricsBase;
|
||||
|
||||
private boolean enabled;
|
||||
|
||||
private String serverUUID;
|
||||
|
||||
private boolean logErrors = false;
|
||||
|
||||
private boolean logSentData;
|
||||
|
||||
private boolean logResponseStatusText;
|
||||
|
||||
/**
|
||||
* Creates a new Metrics instance.
|
||||
*
|
||||
* @param plugin Your plugin instance.
|
||||
* @param serviceId The id of the service. It can be found at <a
|
||||
* href="https://bstats.org/what-is-my-plugin-id">What is my plugin id?</a>
|
||||
*/
|
||||
public Metrics(Plugin plugin, int serviceId) {
|
||||
this.plugin = plugin;
|
||||
try {
|
||||
loadConfig();
|
||||
} catch (IOException e) {
|
||||
// Failed to load configuration
|
||||
plugin.getLogger().log(Level.WARNING, "Failed to load bStats config!", e);
|
||||
metricsBase = null;
|
||||
return;
|
||||
}
|
||||
metricsBase =
|
||||
new MetricsBase(
|
||||
"bungeecord",
|
||||
serverUUID,
|
||||
serviceId,
|
||||
enabled,
|
||||
this::appendPlatformData,
|
||||
this::appendServiceData,
|
||||
null,
|
||||
() -> true,
|
||||
(message, error) -> this.plugin.getLogger().log(Level.WARNING, message, error),
|
||||
(message) -> this.plugin.getLogger().log(Level.INFO, message),
|
||||
logErrors,
|
||||
logSentData,
|
||||
logResponseStatusText);
|
||||
}
|
||||
|
||||
/** Loads the bStats configuration. */
|
||||
private void loadConfig() throws IOException {
|
||||
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats");
|
||||
bStatsFolder.mkdirs();
|
||||
File configFile = new File(bStatsFolder, "config.yml");
|
||||
if (!configFile.exists()) {
|
||||
writeFile(
|
||||
configFile,
|
||||
"# bStats (https://bStats.org) collects some basic information for plugin authors, like how",
|
||||
"# many people use their plugin and their total player count. It's recommended to keep bStats",
|
||||
"# enabled, but if you're not comfortable with this, you can turn this setting off. There is no",
|
||||
"# performance penalty associated with having metrics enabled, and data sent to bStats is fully",
|
||||
"# anonymous.",
|
||||
"enabled: true",
|
||||
"serverUuid: \"" + UUID.randomUUID() + "\"",
|
||||
"logFailedRequests: false",
|
||||
"logSentData: false",
|
||||
"logResponseStatusText: false");
|
||||
}
|
||||
Configuration configuration =
|
||||
ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
||||
// Load configuration
|
||||
enabled = configuration.getBoolean("enabled", true);
|
||||
serverUUID = configuration.getString("serverUuid");
|
||||
logErrors = configuration.getBoolean("logFailedRequests", false);
|
||||
logSentData = configuration.getBoolean("logSentData", false);
|
||||
logResponseStatusText = configuration.getBoolean("logResponseStatusText", false);
|
||||
}
|
||||
|
||||
private void writeFile(File file, String... lines) throws IOException {
|
||||
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file))) {
|
||||
for (String line : lines) {
|
||||
bufferedWriter.write(line);
|
||||
bufferedWriter.newLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a custom chart.
|
||||
*
|
||||
* @param chart The chart to add.
|
||||
*/
|
||||
public void addCustomChart(CustomChart chart) {
|
||||
metricsBase.addCustomChart(chart);
|
||||
}
|
||||
|
||||
private void appendPlatformData(JsonObjectBuilder builder) {
|
||||
builder.appendField("playerAmount", plugin.getProxy().getOnlineCount());
|
||||
builder.appendField("managedServers", plugin.getProxy().getServers().size());
|
||||
builder.appendField("onlineMode", plugin.getProxy().getConfig().isOnlineMode() ? 1 : 0);
|
||||
builder.appendField("bungeecordVersion", plugin.getProxy().getVersion());
|
||||
builder.appendField("javaVersion", System.getProperty("java.version"));
|
||||
builder.appendField("osName", System.getProperty("os.name"));
|
||||
builder.appendField("osArch", System.getProperty("os.arch"));
|
||||
builder.appendField("osVersion", System.getProperty("os.version"));
|
||||
builder.appendField("coreCount", Runtime.getRuntime().availableProcessors());
|
||||
}
|
||||
|
||||
private void appendServiceData(JsonObjectBuilder builder) {
|
||||
builder.appendField("pluginVersion", plugin.getDescription().getVersion());
|
||||
}
|
||||
|
||||
public static class MetricsBase {
|
||||
|
||||
/** The version of the Metrics class. */
|
||||
public static final String METRICS_VERSION = "2.2.1";
|
||||
|
||||
private static final ScheduledExecutorService scheduler =
|
||||
Executors.newScheduledThreadPool(1, task -> new Thread(task, "bStats-Metrics"));
|
||||
|
||||
private static final String REPORT_URL = "https://bStats.org/api/v2/data/%s";
|
||||
|
||||
private final String platform;
|
||||
|
||||
private final String serverUuid;
|
||||
|
||||
private final int serviceId;
|
||||
|
||||
private final Consumer<JsonObjectBuilder> appendPlatformDataConsumer;
|
||||
|
||||
private final Consumer<JsonObjectBuilder> appendServiceDataConsumer;
|
||||
|
||||
private final Consumer<Runnable> submitTaskConsumer;
|
||||
|
||||
private final Supplier<Boolean> checkServiceEnabledSupplier;
|
||||
|
||||
private final BiConsumer<String, Throwable> errorLogger;
|
||||
|
||||
private final Consumer<String> infoLogger;
|
||||
|
||||
private final boolean logErrors;
|
||||
|
||||
private final boolean logSentData;
|
||||
|
||||
private final boolean logResponseStatusText;
|
||||
|
||||
private final Set<CustomChart> customCharts = new HashSet<>();
|
||||
|
||||
private final boolean enabled;
|
||||
|
||||
/**
|
||||
* Creates a new MetricsBase class instance.
|
||||
*
|
||||
* @param platform The platform of the service.
|
||||
* @param serviceId The id of the service.
|
||||
* @param serverUuid The server uuid.
|
||||
* @param enabled Whether or not data sending is enabled.
|
||||
* @param appendPlatformDataConsumer A consumer that receives a {@code JsonObjectBuilder} and
|
||||
* appends all platform-specific data.
|
||||
* @param appendServiceDataConsumer A consumer that receives a {@code JsonObjectBuilder} and
|
||||
* appends all service-specific data.
|
||||
* @param submitTaskConsumer A consumer that takes a runnable with the submit task. This can be
|
||||
* used to delegate the data collection to a another thread to prevent errors caused by
|
||||
* concurrency. Can be {@code null}.
|
||||
* @param checkServiceEnabledSupplier A supplier to check if the service is still enabled.
|
||||
* @param errorLogger A consumer that accepts log message and an error.
|
||||
* @param infoLogger A consumer that accepts info log messages.
|
||||
* @param logErrors Whether or not errors should be logged.
|
||||
* @param logSentData Whether or not the sent data should be logged.
|
||||
* @param logResponseStatusText Whether or not the response status text should be logged.
|
||||
*/
|
||||
public MetricsBase(
|
||||
String platform,
|
||||
String serverUuid,
|
||||
int serviceId,
|
||||
boolean enabled,
|
||||
Consumer<JsonObjectBuilder> appendPlatformDataConsumer,
|
||||
Consumer<JsonObjectBuilder> appendServiceDataConsumer,
|
||||
Consumer<Runnable> submitTaskConsumer,
|
||||
Supplier<Boolean> checkServiceEnabledSupplier,
|
||||
BiConsumer<String, Throwable> errorLogger,
|
||||
Consumer<String> infoLogger,
|
||||
boolean logErrors,
|
||||
boolean logSentData,
|
||||
boolean logResponseStatusText) {
|
||||
this.platform = platform;
|
||||
this.serverUuid = serverUuid;
|
||||
this.serviceId = serviceId;
|
||||
this.enabled = enabled;
|
||||
this.appendPlatformDataConsumer = appendPlatformDataConsumer;
|
||||
this.appendServiceDataConsumer = appendServiceDataConsumer;
|
||||
this.submitTaskConsumer = submitTaskConsumer;
|
||||
this.checkServiceEnabledSupplier = checkServiceEnabledSupplier;
|
||||
this.errorLogger = errorLogger;
|
||||
this.infoLogger = infoLogger;
|
||||
this.logErrors = logErrors;
|
||||
this.logSentData = logSentData;
|
||||
this.logResponseStatusText = logResponseStatusText;
|
||||
checkRelocation();
|
||||
if (enabled) {
|
||||
startSubmitting();
|
||||
}
|
||||
}
|
||||
|
||||
public void addCustomChart(CustomChart chart) {
|
||||
this.customCharts.add(chart);
|
||||
}
|
||||
|
||||
private void startSubmitting() {
|
||||
final Runnable submitTask =
|
||||
() -> {
|
||||
if (!enabled || !checkServiceEnabledSupplier.get()) {
|
||||
// Submitting data or service is disabled
|
||||
scheduler.shutdown();
|
||||
return;
|
||||
}
|
||||
if (submitTaskConsumer != null) {
|
||||
submitTaskConsumer.accept(this::submitData);
|
||||
} else {
|
||||
this.submitData();
|
||||
}
|
||||
};
|
||||
// Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution
|
||||
// of requests on the
|
||||
// bStats backend. To circumvent this problem, we introduce some randomness into the initial
|
||||
// and second delay.
|
||||
// WARNING: You must not modify and part of this Metrics class, including the submit delay or
|
||||
// frequency!
|
||||
// WARNING: Modifying this code will get your plugin banned on bStats. Just don't do it!
|
||||
long initialDelay = (long) (1000 * 60 * (3 + Math.random() * 3));
|
||||
long secondDelay = (long) (1000 * 60 * (Math.random() * 30));
|
||||
scheduler.schedule(submitTask, initialDelay, TimeUnit.MILLISECONDS);
|
||||
scheduler.scheduleAtFixedRate(
|
||||
submitTask, initialDelay + secondDelay, 1000 * 60 * 30, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
private void submitData() {
|
||||
final JsonObjectBuilder baseJsonBuilder = new JsonObjectBuilder();
|
||||
appendPlatformDataConsumer.accept(baseJsonBuilder);
|
||||
final JsonObjectBuilder serviceJsonBuilder = new JsonObjectBuilder();
|
||||
appendServiceDataConsumer.accept(serviceJsonBuilder);
|
||||
JsonObjectBuilder.JsonObject[] chartData =
|
||||
customCharts.stream()
|
||||
.map(customChart -> customChart.getRequestJsonObject(errorLogger, logErrors))
|
||||
.filter(Objects::nonNull)
|
||||
.toArray(JsonObjectBuilder.JsonObject[]::new);
|
||||
serviceJsonBuilder.appendField("id", serviceId);
|
||||
serviceJsonBuilder.appendField("customCharts", chartData);
|
||||
baseJsonBuilder.appendField("service", serviceJsonBuilder.build());
|
||||
baseJsonBuilder.appendField("serverUUID", serverUuid);
|
||||
baseJsonBuilder.appendField("metricsVersion", METRICS_VERSION);
|
||||
JsonObjectBuilder.JsonObject data = baseJsonBuilder.build();
|
||||
scheduler.execute(
|
||||
() -> {
|
||||
try {
|
||||
// Send the data
|
||||
sendData(data);
|
||||
} catch (Exception e) {
|
||||
// Something went wrong! :(
|
||||
if (logErrors) {
|
||||
errorLogger.accept("Could not submit bStats metrics data", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void sendData(JsonObjectBuilder.JsonObject data) throws Exception {
|
||||
if (logSentData) {
|
||||
infoLogger.accept("Sent bStats metrics data: " + data.toString());
|
||||
}
|
||||
String url = String.format(REPORT_URL, platform);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
|
||||
// Compress the data to save bandwidth
|
||||
byte[] compressedData = compress(data.toString());
|
||||
connection.setRequestMethod("POST");
|
||||
connection.addRequestProperty("Accept", "application/json");
|
||||
connection.addRequestProperty("Connection", "close");
|
||||
connection.addRequestProperty("Content-Encoding", "gzip");
|
||||
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setRequestProperty("User-Agent", "Metrics-Service/1");
|
||||
connection.setDoOutput(true);
|
||||
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
|
||||
outputStream.write(compressedData);
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
try (BufferedReader bufferedReader =
|
||||
new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
}
|
||||
if (logResponseStatusText) {
|
||||
infoLogger.accept("Sent data to bStats and received response: " + builder);
|
||||
}
|
||||
}
|
||||
|
||||
/** Checks that the class was properly relocated. */
|
||||
private void checkRelocation() {
|
||||
// You can use the property to disable the check in your test environment
|
||||
if (System.getProperty("bstats.relocatecheck") == null
|
||||
|| !System.getProperty("bstats.relocatecheck").equals("false")) {
|
||||
// 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 no one just copy & pastes the example and uses the wrong package
|
||||
// names
|
||||
if (MetricsBase.class.getPackage().getName().startsWith(defaultPackage)
|
||||
|| MetricsBase.class.getPackage().getName().startsWith(examplePackage)) {
|
||||
throw new IllegalStateException("bStats Metrics class has not been relocated correctly!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gzips the given string.
|
||||
*
|
||||
* @param str The string to gzip.
|
||||
* @return The gzipped string.
|
||||
*/
|
||||
private static byte[] compress(final String str) throws IOException {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
|
||||
gzip.write(str.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
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, Callable<Map<String, int[]>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
|
||||
Map<String, int[]> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, int[]> entry : map.entrySet()) {
|
||||
if (entry.getValue().length == 0) {
|
||||
// Skip this invalid
|
||||
continue;
|
||||
}
|
||||
allSkipped = false;
|
||||
valuesBuilder.appendField(entry.getKey(), entry.getValue());
|
||||
}
|
||||
if (allSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
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, Callable<Map<String, Integer>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
|
||||
Map<String, Integer> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
valuesBuilder.appendField(entry.getKey(), new int[] {entry.getValue()});
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
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, Callable<Map<String, Integer>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
|
||||
Map<String, Integer> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
if (entry.getValue() == 0) {
|
||||
// Skip this invalid
|
||||
continue;
|
||||
}
|
||||
allSkipped = false;
|
||||
valuesBuilder.appendField(entry.getKey(), entry.getValue());
|
||||
}
|
||||
if (allSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
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, Callable<Map<String, Integer>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
|
||||
Map<String, Integer> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
if (entry.getValue() == 0) {
|
||||
// Skip this invalid
|
||||
continue;
|
||||
}
|
||||
allSkipped = false;
|
||||
valuesBuilder.appendField(entry.getKey(), entry.getValue());
|
||||
}
|
||||
if (allSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class CustomChart {
|
||||
|
||||
private final String chartId;
|
||||
|
||||
protected CustomChart(String chartId) {
|
||||
if (chartId == null) {
|
||||
throw new IllegalArgumentException("chartId must not be null");
|
||||
}
|
||||
this.chartId = chartId;
|
||||
}
|
||||
|
||||
public JsonObjectBuilder.JsonObject getRequestJsonObject(
|
||||
BiConsumer<String, Throwable> errorLogger, boolean logErrors) {
|
||||
JsonObjectBuilder builder = new JsonObjectBuilder();
|
||||
builder.appendField("chartId", chartId);
|
||||
try {
|
||||
JsonObjectBuilder.JsonObject data = getChartData();
|
||||
if (data == null) {
|
||||
// If the data is null we don't send the chart.
|
||||
return null;
|
||||
}
|
||||
builder.appendField("data", data);
|
||||
} catch (Throwable t) {
|
||||
if (logErrors) {
|
||||
errorLogger.accept("Failed to get data for custom chart with id " + chartId, t);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
protected abstract JsonObjectBuilder.JsonObject getChartData() throws Exception;
|
||||
}
|
||||
|
||||
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 JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
int value = callable.call();
|
||||
if (value == 0) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("value", value).build();
|
||||
}
|
||||
}
|
||||
|
||||
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, Callable<String> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
String value = callable.call();
|
||||
if (value == null || value.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("value", value).build();
|
||||
}
|
||||
}
|
||||
|
||||
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 DrilldownPie(String chartId, Callable<Map<String, Map<String, Integer>>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
|
||||
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()) {
|
||||
JsonObjectBuilder valueBuilder = new JsonObjectBuilder();
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
|
||||
valueBuilder.appendField(valueEntry.getKey(), valueEntry.getValue());
|
||||
allSkipped = false;
|
||||
}
|
||||
if (!allSkipped) {
|
||||
reallyAllSkipped = false;
|
||||
valuesBuilder.appendField(entryValues.getKey(), valueBuilder.build());
|
||||
}
|
||||
}
|
||||
if (reallyAllSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An extremely simple JSON builder.
|
||||
*
|
||||
* <p>While this class is neither feature-rich nor the most performant one, it's sufficient enough
|
||||
* for its use-case.
|
||||
*/
|
||||
public static class JsonObjectBuilder {
|
||||
|
||||
private StringBuilder builder = new StringBuilder();
|
||||
|
||||
private boolean hasAtLeastOneField = false;
|
||||
|
||||
public JsonObjectBuilder() {
|
||||
builder.append("{");
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a null field to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendNull(String key) {
|
||||
appendFieldUnescaped(key, "null");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a string field to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param value The value of the field.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, String value) {
|
||||
if (value == null) {
|
||||
throw new IllegalArgumentException("JSON value must not be null");
|
||||
}
|
||||
appendFieldUnescaped(key, "\"" + escape(value) + "\"");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an integer field to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param value The value of the field.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, int value) {
|
||||
appendFieldUnescaped(key, String.valueOf(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an object to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param object The object.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, JsonObject object) {
|
||||
if (object == null) {
|
||||
throw new IllegalArgumentException("JSON object must not be null");
|
||||
}
|
||||
appendFieldUnescaped(key, object.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a string array to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param values The string array.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, String[] values) {
|
||||
if (values == null) {
|
||||
throw new IllegalArgumentException("JSON values must not be null");
|
||||
}
|
||||
String escapedValues =
|
||||
Arrays.stream(values)
|
||||
.map(value -> "\"" + escape(value) + "\"")
|
||||
.collect(Collectors.joining(","));
|
||||
appendFieldUnescaped(key, "[" + escapedValues + "]");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an integer array to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param values The integer array.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, int[] values) {
|
||||
if (values == null) {
|
||||
throw new IllegalArgumentException("JSON values must not be null");
|
||||
}
|
||||
String escapedValues =
|
||||
Arrays.stream(values).mapToObj(String::valueOf).collect(Collectors.joining(","));
|
||||
appendFieldUnescaped(key, "[" + escapedValues + "]");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an object array to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param values The integer array.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, JsonObject[] values) {
|
||||
if (values == null) {
|
||||
throw new IllegalArgumentException("JSON values must not be null");
|
||||
}
|
||||
String escapedValues =
|
||||
Arrays.stream(values).map(JsonObject::toString).collect(Collectors.joining(","));
|
||||
appendFieldUnescaped(key, "[" + escapedValues + "]");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a field to the object.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param escapedValue The escaped value of the field.
|
||||
*/
|
||||
private void appendFieldUnescaped(String key, String escapedValue) {
|
||||
if (builder == null) {
|
||||
throw new IllegalStateException("JSON has already been built");
|
||||
}
|
||||
if (key == null) {
|
||||
throw new IllegalArgumentException("JSON key must not be null");
|
||||
}
|
||||
if (hasAtLeastOneField) {
|
||||
builder.append(",");
|
||||
}
|
||||
builder.append("\"").append(escape(key)).append("\":").append(escapedValue);
|
||||
hasAtLeastOneField = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the JSON string and invalidates this builder.
|
||||
*
|
||||
* @return The built JSON string.
|
||||
*/
|
||||
public JsonObject build() {
|
||||
if (builder == null) {
|
||||
throw new IllegalStateException("JSON has already been built");
|
||||
}
|
||||
JsonObject object = new JsonObject(builder.append("}").toString());
|
||||
builder = null;
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes the given string like stated in https://www.ietf.org/rfc/rfc4627.txt.
|
||||
*
|
||||
* <p>This method escapes only the necessary characters '"', '\'. and '\u0000' - '\u001F'.
|
||||
* Compact escapes are not used (e.g., '\n' is escaped as "\u000a" and not as "\n").
|
||||
*
|
||||
* @param value The value to escape.
|
||||
* @return The escaped value.
|
||||
*/
|
||||
private static String escape(String value) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
char c = value.charAt(i);
|
||||
if (c == '"') {
|
||||
builder.append("\\\"");
|
||||
} else if (c == '\\') {
|
||||
builder.append("\\\\");
|
||||
} else if (c <= '\u000F') {
|
||||
builder.append("\\u000").append(Integer.toHexString(c));
|
||||
} else if (c <= '\u001F') {
|
||||
builder.append("\\u00").append(Integer.toHexString(c));
|
||||
} else {
|
||||
builder.append(c);
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A super simple representation of a JSON object.
|
||||
*
|
||||
* <p>This class only exists to make methods of the {@link JsonObjectBuilder} type-safe and not
|
||||
* allow a raw string inputs for methods like {@link JsonObjectBuilder#appendField(String,
|
||||
* JsonObject)}.
|
||||
*/
|
||||
public static class JsonObject {
|
||||
|
||||
private final String value;
|
||||
|
||||
private JsonObject(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
18
src/main/java/net/t2code/alias/Util.java
Normal file
18
src/main/java/net/t2code/alias/Util.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package net.t2code.alias;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
public class Util {
|
||||
@Getter
|
||||
public static double requiredT2CodeLibVersion = 10.6;
|
||||
@Getter
|
||||
public static String Prefix = "§8[§4T2Code§7-§aAlias§5Bungee§8]";
|
||||
@Getter
|
||||
public static Integer SpigotID = 00000;
|
||||
@Getter
|
||||
public static Integer BstatsID = 00000;
|
||||
@Getter
|
||||
public static String Spigot = "https://www.spigotmc.org/resources/" + SpigotID;
|
||||
@Getter
|
||||
public static String Discord = "http://dc.t2code.net";
|
||||
}
|
6
src/main/resources/bungee.yml
Normal file
6
src/main/resources/bungee.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
name: T2C-AliasBungee
|
||||
version: ${project.version}
|
||||
main: net.t2code.alias.Bungee.Main
|
||||
description: T2Code Alias Plugin
|
||||
author: JaTiTV
|
||||
softdepends: [T2CodeLib]
|
Reference in New Issue
Block a user