From 79d353d458fe9a07cfcf590b925cd50f8b0fe4b9 Mon Sep 17 00:00:00 2001 From: Matsv Date: Sun, 25 Sep 2016 20:41:34 +0200 Subject: [PATCH] Basic configuration api and add platform specific dump info --- .../java/us/myles/ViaVersion/ViaConfig.java | 19 +++++++- .../us/myles/ViaVersion/ViaVersionPlugin.java | 45 ++++++++++++++++--- .../ViaVersion/api/ViaVersionConfig.java | 5 --- .../configuration/ConfigurationProvider.java | 26 +++++++++++ .../ViaVersion/api/platform/ViaPlatform.java | 35 +++++++++------ .../commands/defaultsubs/AutoTeamSubCmd.java | 11 +++-- .../commands/defaultsubs/DontBugMeSubCmd.java | 13 +++--- .../commands/defaultsubs/DumpSubCmd.java | 21 +++++---- .../commands/defaultsubs/ReloadSubCmd.java | 2 +- .../myles/ViaVersion/dump/DumpTemplate.java | 3 +- .../us/myles/ViaVersion/dump/VersionInfo.java | 4 +- 11 files changed, 132 insertions(+), 52 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/configuration/ConfigurationProvider.java diff --git a/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java b/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java index 35f2aab4e..d3b30aa63 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java @@ -1,12 +1,14 @@ package us.myles.ViaVersion; import us.myles.ViaVersion.api.ViaVersionConfig; +import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.util.Configuration; import java.io.File; import java.util.List; +import java.util.Map; -public class ViaConfig implements ViaVersionConfig { +public class ViaConfig implements ViaVersionConfig, ConfigurationProvider { private final ViaVersionPlugin plugin; public ViaConfig(ViaVersionPlugin plugin) { @@ -185,4 +187,19 @@ public class ViaConfig implements ViaVersionConfig { public String getReloadDisconnectMsg() { return plugin.getConfig().getString("reload-disconnect-msg", "Server reload, please rejoin!"); } + + @Override + public void set(String path, Object value) { + plugin.getConfig().set(path, value); + } + + @Override + public void saveConfig() { + plugin.saveConfig(); + } + + @Override + public Map getValues() { + return plugin.getConfig().getValues(false); + } } diff --git a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 0e4bde244..f935b0107 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -1,14 +1,18 @@ package us.myles.ViaVersion; +import com.google.gson.Gson; +import com.google.gson.JsonObject; import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.command.ViaCommandSender; +import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.api.protocol.ProtocolRegistry; @@ -17,8 +21,11 @@ import us.myles.ViaVersion.bukkit.BukkitCommandSender; import us.myles.ViaVersion.bukkit.BukkitViaAPI; import us.myles.ViaVersion.bukkit.BukkitViaInjector; import us.myles.ViaVersion.classgenerator.ClassGenerator; +import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.util.ReflectionUtil; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import java.util.concurrent.Callable; import java.util.concurrent.Future; @@ -89,7 +96,6 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform { Via.getManager().init(); } - getCommand("viaversion").setExecutor(commandHandler = new BukkitCommandHandler()); getCommand("viaversion").setTabCompleter(commandHandler); @@ -177,13 +183,23 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform { } @Override - public void runAsync(Runnable runnable) { - getServer().getScheduler().runTaskAsynchronously(this, runnable); + public int runAsync(Runnable runnable) { + return getServer().getScheduler().runTaskAsynchronously(this, runnable).getTaskId(); } @Override - public void runSync(Runnable runnable) { - getServer().getScheduler().runTask(this, runnable); + public int runSync(Runnable runnable) { + return getServer().getScheduler().runTask(this, runnable).getTaskId(); + } + + @Override + public int runRepeatingSync(Runnable runnable, Long ticks) { + return getServer().getScheduler().runTaskTimer(this, runnable, ticks, 0).getTaskId(); // TODO or the other way around? + } + + @Override + public void cancelTask(int taskId) { + getServer().getScheduler().cancelTask(taskId); } @Override @@ -220,6 +236,11 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform { return Bukkit.getPluginManager().getPlugin("ViaVersion").isEnabled(); } + @Override + public ConfigurationProvider getConfigurationProvider() { + return conf; + } + @Override public void onReload() { if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) { @@ -232,4 +253,18 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform { getLogger().severe("ViaVersion is already loaded, this should work fine. If you get any console errors, try rebooting."); } } + + @Override + public JsonObject getDump() { + JsonObject platformSpecific = new JsonObject(); + + List plugins = new ArrayList<>(); + for (Plugin p : Bukkit.getPluginManager().getPlugins()) + plugins.add(new PluginInfo(p.isEnabled(), p.getDescription().getName(), p.getDescription().getVersion(), p.getDescription().getMain(), p.getDescription().getAuthors())); + + platformSpecific.add("plugins", new Gson().toJsonTree(plugins)); + // TODO more? ProtocolLib things etc? + + return platformSpecific; + } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java b/common/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java index 365392cd3..ac8c1d1cd 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java +++ b/common/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java @@ -217,9 +217,4 @@ public interface ViaVersionConfig { * @return Disconnect message */ String getReloadDisconnectMsg(); - - /** - * Reloads the config from disk - */ - void reloadConfig(); } diff --git a/common/src/main/java/us/myles/ViaVersion/api/configuration/ConfigurationProvider.java b/common/src/main/java/us/myles/ViaVersion/api/configuration/ConfigurationProvider.java new file mode 100644 index 000000000..5a1457e60 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/configuration/ConfigurationProvider.java @@ -0,0 +1,26 @@ +package us.myles.ViaVersion.api.configuration; + +import java.util.Map; + +public interface ConfigurationProvider { + + /** + * Sets the specified path to the given value. + * + * @param path Path of the object to set. + * @param value New value to set the path to + */ + void set(String path, Object value); + + /** + * Saves the config + */ + void saveConfig(); + + /** + * Get all the configuration values + * + * @return Map with key->values + */ + Map getValues(); +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/platform/ViaPlatform.java b/common/src/main/java/us/myles/ViaVersion/api/platform/ViaPlatform.java index 59ad00051..5ab61b11f 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/platform/ViaPlatform.java +++ b/common/src/main/java/us/myles/ViaVersion/api/platform/ViaPlatform.java @@ -1,8 +1,11 @@ package us.myles.ViaVersion.api.platform; +import com.google.gson.JsonObject; +import jdk.nashorn.internal.runtime.regexp.joni.Config; import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.command.ViaCommandSender; +import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import java.util.UUID; import java.util.logging.Logger; @@ -13,29 +16,35 @@ import java.util.logging.Logger; * @param - The player type for the platform, used for API related methods */ public interface ViaPlatform { - public Logger getLogger(); + Logger getLogger(); - public String getPlatformName(); + String getPlatformName(); - public String getPluginVersion(); + String getPluginVersion(); - public void runAsync(Runnable runnable); + int runAsync(Runnable runnable); - public void runSync(Runnable runnable); + int runSync(Runnable runnable); - public void runRepeatingSync(Runnable runnable, Long ticks); + int runRepeatingSync(Runnable runnable, Long ticks); - public ViaCommandSender[] getOnlinePlayers(); + void cancelTask(int taskId); - public void sendMessage(UUID uuid, String message); + ViaCommandSender[] getOnlinePlayers(); - public boolean kickPlayer(UUID uuid, String message); + void sendMessage(UUID uuid, String message); - public boolean isPluginEnabled(); + boolean kickPlayer(UUID uuid, String message); - public ViaAPI getApi(); + boolean isPluginEnabled(); - public ViaVersionConfig getConf(); + ViaAPI getApi(); - public void onReload(); + ViaVersionConfig getConf(); + + ConfigurationProvider getConfigurationProvider(); + + void onReload(); + + JsonObject getDump(); } diff --git a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/AutoTeamSubCmd.java b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/AutoTeamSubCmd.java index e495f1cb4..eeecc9188 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/AutoTeamSubCmd.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/AutoTeamSubCmd.java @@ -1,10 +1,9 @@ package us.myles.ViaVersion.commands.defaultsubs; -import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.command.ViaSubCommand; +import us.myles.ViaVersion.api.configuration.ConfigurationProvider; public class AutoTeamSubCmd extends ViaSubCommand { @Override @@ -19,11 +18,11 @@ public class AutoTeamSubCmd extends ViaSubCommand { @Override public boolean execute(ViaCommandSender sender, String[] args) { - ViaVersionPlugin plugin = (ViaVersionPlugin) ViaVersion.getInstance(); - + ConfigurationProvider provider = Via.getPlatform().getConfigurationProvider(); boolean newValue = !Via.getConfig().isAutoTeam(); - plugin.getConfig().set("auto-team", newValue); - plugin.saveConfig(); + + provider.set("auto-team", newValue); + provider.saveConfig(); sendMessage(sender, "&6We will %s", (newValue ? "&aautomatically team players" : "&cno longer auto team players")); sendMessage(sender, "&6All players will need to re-login for the change to take place."); diff --git a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DontBugMeSubCmd.java b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DontBugMeSubCmd.java index b35ecf382..95d92d27d 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DontBugMeSubCmd.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DontBugMeSubCmd.java @@ -1,10 +1,9 @@ package us.myles.ViaVersion.commands.defaultsubs; -import org.bukkit.command.CommandSender; -import us.myles.ViaVersion.ViaVersionPlugin; -import us.myles.ViaVersion.api.ViaVersion; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.command.ViaSubCommand; +import us.myles.ViaVersion.api.configuration.ConfigurationProvider; public class DontBugMeSubCmd extends ViaSubCommand { @Override @@ -19,11 +18,11 @@ public class DontBugMeSubCmd extends ViaSubCommand { @Override public boolean execute(ViaCommandSender sender, String[] args) { - ViaVersionPlugin plugin = (ViaVersionPlugin) ViaVersion.getInstance(); + ConfigurationProvider provider = Via.getPlatform().getConfigurationProvider(); + boolean newValue = !Via.getConfig().isCheckForUpdates(); - boolean newValue = !ViaVersion.getConfig().isCheckForUpdates(); - plugin.getConfig().set("checkforupdates", newValue); - plugin.saveConfig(); + provider.set("checkforupdates", newValue); + provider.saveConfig(); sendMessage(sender, "&6We will %snotify you about updates.", (newValue ? "&a" : "&cnot ")); return true; diff --git a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java index eaa45d65a..0b90c8c52 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java @@ -3,6 +3,8 @@ package us.myles.ViaVersion.commands.defaultsubs; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; +import net.md_5.bungee.api.ChatColor; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.command.ViaSubCommand; import us.myles.ViaVersion.api.protocol.ProtocolRegistry; @@ -37,22 +39,19 @@ public class DumpSubCmd extends ViaSubCommand { @Override public boolean execute(final ViaCommandSender sender, String[] args) { VersionInfo version = new VersionInfo( - Bukkit.getServer().getVersion(), - Bukkit.getServer().getBukkitVersion(), System.getProperty("java.version"), System.getProperty("os.name"), ProtocolRegistry.SERVER_PROTOCOL, - ProtocolRegistry.getSupportedVersions()); + ProtocolRegistry.getSupportedVersions(), + Via.getPlatform().getPlatformName(), + Via.getPlatform().getPluginVersion() + ); - List plugins = new ArrayList<>(); - for (Plugin p : Bukkit.getPluginManager().getPlugins()) - plugins.add(new PluginInfo(p.isEnabled(), p.getDescription().getName(), p.getDescription().getVersion(), p.getDescription().getMain(), p.getDescription().getAuthors())); + Map configuration = Via.getPlatform().getConfigurationProvider().getValues(); - Map configuration = ((ViaVersionPlugin) ViaVersion.getInstance()).getConfig().getValues(false); + final DumpTemplate template = new DumpTemplate(version, configuration, Via.getPlatform().getDump()); - final DumpTemplate template = new DumpTemplate(version, configuration, plugins); - - Bukkit.getScheduler().runTaskAsynchronously((ViaVersionPlugin) ViaVersion.getInstance(), new Runnable() { + Via.getPlatform().runAsync(new Runnable() { @Override public void run() { try { @@ -75,7 +74,7 @@ public class DumpSubCmd extends ViaSubCommand { sender.sendMessage(ChatColor.GREEN + "We've made a dump with useful information, report your issue and provide this url: " + getUrl(output.get("key").getAsString())); } catch (Exception e) { sender.sendMessage(ChatColor.RED + "Failed to dump, please check the console for more information"); - ((ViaVersionPlugin) ViaVersion.getInstance()).getLogger().log(Level.WARNING, "Could not paste ViaVersion dump to Hastebin", e); + Via.getPlatform().getLogger().log(Level.WARNING, "Could not paste ViaVersion dump to Hastebin", e); } } }); diff --git a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/ReloadSubCmd.java b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/ReloadSubCmd.java index 938651a36..176cfb6f5 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/ReloadSubCmd.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/ReloadSubCmd.java @@ -17,7 +17,7 @@ public class ReloadSubCmd extends ViaSubCommand { @Override public boolean execute(ViaCommandSender sender, String[] args) { - Via.getConfig().reloadConfig(); + Via.getPlatform().getConfigurationProvider().saveConfig(); sendMessage(sender, "&6Configuration successfully reloaded! Some features may need a restart."); return true; } diff --git a/common/src/main/java/us/myles/ViaVersion/dump/DumpTemplate.java b/common/src/main/java/us/myles/ViaVersion/dump/DumpTemplate.java index e6b5451ba..4722fe54f 100644 --- a/common/src/main/java/us/myles/ViaVersion/dump/DumpTemplate.java +++ b/common/src/main/java/us/myles/ViaVersion/dump/DumpTemplate.java @@ -1,5 +1,6 @@ package us.myles.ViaVersion.dump; +import com.google.gson.JsonObject; import lombok.AllArgsConstructor; import lombok.Data; @@ -11,5 +12,5 @@ import java.util.Map; public class DumpTemplate { private VersionInfo versionInfo; private Map configuration; - private List plugins; + private JsonObject platformDump; } diff --git a/common/src/main/java/us/myles/ViaVersion/dump/VersionInfo.java b/common/src/main/java/us/myles/ViaVersion/dump/VersionInfo.java index 75dcef344..9e0da92de 100644 --- a/common/src/main/java/us/myles/ViaVersion/dump/VersionInfo.java +++ b/common/src/main/java/us/myles/ViaVersion/dump/VersionInfo.java @@ -8,11 +8,11 @@ import java.util.Set; @Data @AllArgsConstructor public class VersionInfo { - private String version; - private String bukkitVersion; private String javaVersion; private String operatingSystem; private int serverProtocol; private Set enabledPipelines; + private String platformName; + private String platformVersion; }