From 8b9a1750de524d790f91e2b3c7ec27b8baa2f117 Mon Sep 17 00:00:00 2001 From: Myles Date: Mon, 26 Sep 2016 20:12:34 +0100 Subject: [PATCH] Basic Config API for sponge, need to do comments --- TODOLIST | 3 +- .../java/us/myles/ViaVersion/ViaConfig.java | 7 +- .../main/java/us/myles/ViaVersion/Bungee.java | 6 +- ...nfigProvider.java => BungeeConfigAPI.java} | 7 +- .../configuration/ConfigurationProvider.java | 5 + .../commands/defaultsubs/ReloadSubCmd.java | 2 +- .../us/myles/ViaVersion/SpongePlugin.java | 15 +- .../ViaVersion/sponge/SpongeConfigAPI.java | 182 +++++++++++++----- 8 files changed, 173 insertions(+), 54 deletions(-) rename bungee/src/main/java/us/myles/ViaVersion/bungee/config/{BungeeConfigProvider.java => BungeeConfigAPI.java} (95%) diff --git a/TODOLIST b/TODOLIST index d6b06797d..f3b52ace6 100644 --- a/TODOLIST +++ b/TODOLIST @@ -1,8 +1,7 @@ PORT STUFF TO GUAVA :D (so we dont need to include commons) -Test on SpongeForge, only tested SpongeVanilla Stop using new Gson() everywhere Java docs (for platforms etc) -Config implementation for sponge +Add comments to sponge api Port bukkit listeners to sponge maybe Fix task ids, methods for sponge Find all the TODO's and check they're done. \ No newline at end of file diff --git a/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java b/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java index d3b30aa63..d63372589 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java @@ -13,7 +13,7 @@ public class ViaConfig implements ViaVersionConfig, ConfigurationProvider { public ViaConfig(ViaVersionPlugin plugin) { this.plugin = plugin; - generateConfig(); + reloadConfig(); } public void generateConfig() { @@ -198,6 +198,11 @@ public class ViaConfig implements ViaVersionConfig, ConfigurationProvider { plugin.saveConfig(); } + @Override + public void reloadConfig() { + generateConfig(); + } + @Override public Map getValues() { return plugin.getConfig().getValues(false); diff --git a/bungee/src/main/java/us/myles/ViaVersion/Bungee.java b/bungee/src/main/java/us/myles/ViaVersion/Bungee.java index a5ba702de..71c4af280 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/Bungee.java +++ b/bungee/src/main/java/us/myles/ViaVersion/Bungee.java @@ -17,7 +17,7 @@ import us.myles.ViaVersion.bungee.BungeeViaAPI; import us.myles.ViaVersion.bungee.BungeeViaInjector; import us.myles.ViaVersion.bungee.BungeeViaLoader; import us.myles.ViaVersion.bungee.command.BungeeCommandSender; -import us.myles.ViaVersion.bungee.config.BungeeConfigProvider; +import us.myles.ViaVersion.bungee.config.BungeeConfigAPI; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -25,13 +25,13 @@ import java.util.concurrent.TimeUnit; public class Bungee extends Plugin implements ViaPlatform { private BungeeViaAPI api; - private BungeeConfigProvider config; + private BungeeConfigAPI config; private BungeeCommandHandler commandHandler; @Override public void onLoad() { api = new BungeeViaAPI(); - config = new BungeeConfigProvider(); + config = new BungeeConfigAPI(); commandHandler = new BungeeCommandHandler(); ProxyServer.getInstance().getPluginManager().registerCommand(this, new BungeeCommand(commandHandler)); // Init platform diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/config/BungeeConfigProvider.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/config/BungeeConfigAPI.java similarity index 95% rename from bungee/src/main/java/us/myles/ViaVersion/bungee/config/BungeeConfigProvider.java rename to bungee/src/main/java/us/myles/ViaVersion/bungee/config/BungeeConfigAPI.java index 688d367a7..4b4ca1bfb 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/config/BungeeConfigProvider.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/config/BungeeConfigAPI.java @@ -8,7 +8,7 @@ import java.util.List; import java.util.Map; // TODO implement -public class BungeeConfigProvider implements ViaVersionConfig, ConfigurationProvider { +public class BungeeConfigAPI implements ViaVersionConfig, ConfigurationProvider { @Override public boolean isCheckForUpdates() { return false; @@ -169,6 +169,11 @@ public class BungeeConfigProvider implements ViaVersionConfig, ConfigurationProv } + @Override + public void reloadConfig() { + + } + @Override public Map getValues() { return null; 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 index 66caaee7b..1566598eb 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/configuration/ConfigurationProvider.java +++ b/common/src/main/java/us/myles/ViaVersion/api/configuration/ConfigurationProvider.java @@ -17,6 +17,11 @@ public interface ConfigurationProvider { */ void saveConfig(); + /** + * Reloads the config + */ + void reloadConfig(); + /** * Get all the configuration values * 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 176cfb6f5..8a1bf109c 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.getPlatform().getConfigurationProvider().saveConfig(); + Via.getPlatform().getConfigurationProvider().reloadConfig(); sendMessage(sender, "&6Configuration successfully reloaded! Some features may need a restart."); return true; } diff --git a/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java b/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java index b235d06be..0e4ba8f88 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java +++ b/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java @@ -3,7 +3,11 @@ package us.myles.ViaVersion; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.inject.Inject; +import ninja.leaping.configurate.commented.CommentedConfigurationNode; +import ninja.leaping.configurate.loader.ConfigurationLoader; +import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; import org.spongepowered.api.Game; +import org.spongepowered.api.config.DefaultConfig; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.game.state.GameAboutToStartServerEvent; @@ -21,6 +25,7 @@ import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.sponge.*; +import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -37,13 +42,18 @@ import java.util.logging.Logger; public class SpongePlugin implements ViaPlatform { @Inject private Game game; + @Inject private PluginContainer container; + @Inject + @DefaultConfig(sharedRoot = false) + private File defaultConfig; + + private SpongeViaAPI api = new SpongeViaAPI(); private SpongeExecutorService asyncExecutor; private SpongeExecutorService syncExecutor; - private SpongeConfigAPI conf = new SpongeConfigAPI(this); - private SpongeViaAPI api = new SpongeViaAPI(); + private SpongeConfigAPI conf; private Logger logger; @Listener @@ -51,6 +61,7 @@ public class SpongePlugin implements ViaPlatform { // Setup Logger logger = new LoggerWrapper(container.getLogger()); // Setup Plugin + conf = new SpongeConfigAPI(defaultConfig.getParentFile()); syncExecutor = game.getScheduler().createSyncExecutor(this); asyncExecutor = game.getScheduler().createAsyncExecutor(this); SpongeCommandHandler commandHandler = new SpongeCommandHandler(); diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeConfigAPI.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeConfigAPI.java index df02666e9..d5f85d6fb 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeConfigAPI.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/SpongeConfigAPI.java @@ -1,64 +1,70 @@ package us.myles.ViaVersion.sponge; -import us.myles.ViaVersion.SpongePlugin; +import org.yaml.snakeyaml.Yaml; import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.configuration.ConfigurationProvider; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.io.*; +import java.net.URL; +import java.util.*; -public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider{ - private final SpongePlugin spongePlugin; +public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider { + private final File defaultConfig; + private Map config; + private ThreadLocal yaml = new ThreadLocal() { + @Override + protected Yaml initialValue() { + return new Yaml(); + } + }; - public SpongeConfigAPI(SpongePlugin spongePlugin) { - this.spongePlugin = spongePlugin; + public SpongeConfigAPI(File defaultConfig) { + this.defaultConfig = defaultConfig; + reloadConfig(); } - @Override public boolean isCheckForUpdates() { - return false; + return getBoolean("checkforupdates", true); } @Override public boolean isPreventCollision() { - return false; + return getBoolean("prevent-collision", true); } @Override public boolean isNewEffectIndicator() { - return false; + return getBoolean("use-new-effect-indicator", true); } @Override public boolean isShowNewDeathMessages() { - return false; + return getBoolean("use-new-deathmessages", false); } @Override public boolean isSuppressMetadataErrors() { - return false; + return getBoolean("suppress-metadata-errors", false); } @Override public boolean isShieldBlocking() { - return false; + return getBoolean("shield-blocking", true); } @Override public boolean isHologramPatch() { - return false; + return getBoolean("hologram-patch", false); } @Override public boolean isBossbarPatch() { - return false; + return getBoolean("bossbar-patch", true); } @Override public boolean isBossbarAntiflicker() { - return false; + return getBoolean("bossbar-anti-flicker", false); } @Override @@ -68,12 +74,7 @@ public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider{ @Override public double getHologramYOffset() { - return 0; - } - - @Override - public boolean isAutoTeam() { - return false; + return getDouble("hologram-y", -0.96D); } @Override @@ -83,101 +84,194 @@ public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider{ @Override public int getMaxPPS() { - return 0; + return getInt("max-pps", 140); } @Override public String getMaxPPSKickMessage() { - return null; + return getString("max-pps-kick-msg", "Sending packets too fast? lag?"); } @Override public int getTrackingPeriod() { - return 0; + return getInt("tracking-period", 6); } @Override public int getWarningPPS() { - return 0; + return getInt("tracking-warning-pps", 120); } @Override public int getMaxWarnings() { - return 0; + return getInt("tracking-max-warnings", 3); } @Override public String getMaxWarningsKickMessage() { - return null; + return getString("tracking-max-kick-msg", "You are sending too many packets, :("); } @Override public boolean isAntiXRay() { - return false; + return getBoolean("anti-xray-patch", true); } @Override public boolean isSendSupportedVersions() { - return false; + return getBoolean("send-supported-versions", false); } @Override public boolean isStimulatePlayerTick() { - return false; + return getBoolean("simulate-pt", true); } @Override public boolean isItemCache() { - return false; + return getBoolean("item-cache", true); } @Override public boolean isNMSPlayerTicking() { - return false; + return getBoolean("nms-player-ticking", true); } @Override public boolean isReplacePistons() { - return false; + return getBoolean("replace-pistons", false); } @Override public int getPistonReplacementId() { - return 0; + return getInt("replacement-piston-id", 0); + } + + public boolean isAutoTeam() { + // Collision has to be enabled first + return isPreventCollision() && getBoolean("auto-team", true); } @Override public boolean isForceJsonTransform() { - return false; + return getBoolean("force-json-transform", false); } @Override public List getBlockedProtocols() { - return Arrays.asList(0); + return getIntegerList("block-protocols"); } @Override public String getBlockedDisconnectMsg() { - return "Boop"; + return getString("block-disconnect-msg", "You are using an unsupported Minecraft version!"); } @Override public String getReloadDisconnectMsg() { - return "Beep"; + return getString("reload-disconnect-msg", "Server reload, please rejoin!"); } @Override public void set(String path, Object value) { - + config.put(path, value); } @Override public void saveConfig() { + if (!defaultConfig.isDirectory()) { + defaultConfig.mkdir(); + } + File config = new File(defaultConfig, "config.yml"); + try (FileWriter fw = new FileWriter(config)) { + yaml.get().dump(this.config, fw); + } catch (IOException e) { + e.printStackTrace(); + } + } + @Override + public void reloadConfig() { + if (!defaultConfig.isDirectory()) { + defaultConfig.mkdir(); + } + File config = new File(defaultConfig, "config.yml"); + URL jarConfigFile = this.getClass().getClassLoader().getResource("config.yml"); + this.config = null; + if (config.exists()) { + try (FileInputStream input = new FileInputStream(config)) { + this.config = (Map) yaml.get().load(input); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (this.config == null) { + this.config = new HashMap<>(); + } + Map defaults; + try (InputStream stream = jarConfigFile.openStream()) { + defaults = (Map) yaml.get().load(stream); + } catch (IOException e) { + e.printStackTrace(); + return; + } + // Merge with defaultLoader + for (Object key : this.config.keySet()) { + // Set option in new conf if exists + if (defaults.containsKey(key)) { + defaults.put(key, this.config.get(key)); + } + } + this.config = defaults; + // Save + saveConfig(); + + } + + public boolean getBoolean(String key, boolean def) { + if (this.config.containsKey(key)) { + return (boolean) this.config.get(key); + } else { + return def; + } + } + + public String getString(String key, String def) { + if (this.config.containsKey(key)) { + return (String) this.config.get(key); + } else { + return def; + } + } + + public int getInt(String key, int def) { + if (this.config.containsKey(key)) { + return (int) this.config.get(key); + } else { + return def; + } + } + + public double getDouble(String key, double def) { + if (this.config.containsKey(key)) { + return (double) this.config.get(key); + } else { + return def; + } + } + + public List getIntegerList(String key) { + if (this.config.containsKey(key)) { + return (List) this.config.get(key); + } else { + return new ArrayList<>(); + } } @Override public Map getValues() { - return new HashMap<>(); + return (Map) ((Map) this.config); } }