From b7b322a52580c9fe41a252bb0e8843472ded9c2e Mon Sep 17 00:00:00 2001 From: Myles Date: Fri, 30 Sep 2016 00:34:16 +0100 Subject: [PATCH] Full config api for all platforms using snakeyaml + comments --- .../java/us/myles/ViaVersion/ViaConfig.java | 210 ------------------ .../us/myles/ViaVersion/ViaVersionPlugin.java | 9 +- .../CommandBlockListener.java | 2 +- .../bukkit/platform/BukkitConfigAPI.java | 173 +++++++++++++++ .../bukkit/platform/BukkitViaAPI.java | 2 +- .../bukkit/platform/BukkitViaInjector.java | 2 +- .../BukkitViaMovementTransmitter.java | 2 +- .../main/java/us/myles/ViaVersion/Bungee.java | 4 +- .../bungee/platform/BungeeConfigAPI.java | 95 ++++---- .../bungee/platform/BungeeViaLoader.java | 1 - .../bungee/util/BungeePipelineUtil.java | 4 - common/pom.xml | 10 + .../myles/ViaVersion/api/PacketWrapper.java | 1 - .../commands/ViaCommandHandler.java | 1 + .../protocols/protocol1_9to1_8/ArmorType.java | 7 +- .../myles/ViaVersion/util/CommentStore.java | 99 ++------- .../java/us/myles/ViaVersion/util/Config.java | 150 +++++++++++++ .../myles/ViaVersion/util/ReflectionUtil.java | 1 - .../src/main/resources/config.yml | 2 +- .../sponge/platform/SpongeConfigAPI.java | 138 ++---------- 20 files changed, 427 insertions(+), 486 deletions(-) delete mode 100644 bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java create mode 100644 bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitConfigAPI.java rename bukkit/src/main/java/us/myles/ViaVersion/bukkit/util/Configuration.java => common/src/main/java/us/myles/ViaVersion/util/CommentStore.java (67%) create mode 100644 common/src/main/java/us/myles/ViaVersion/util/Config.java rename {bukkit => common}/src/main/resources/config.yml (99%) diff --git a/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java b/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java deleted file mode 100644 index b1e1fec38..000000000 --- a/bukkit/src/main/java/us/myles/ViaVersion/ViaConfig.java +++ /dev/null @@ -1,210 +0,0 @@ -package us.myles.ViaVersion; - -import us.myles.ViaVersion.api.ViaVersionConfig; -import us.myles.ViaVersion.api.configuration.ConfigurationProvider; -import us.myles.ViaVersion.bukkit.util.Configuration; - -import java.io.File; -import java.util.List; -import java.util.Map; - -public class ViaConfig implements ViaVersionConfig, ConfigurationProvider { - private final ViaVersionPlugin plugin; - - public ViaConfig(ViaVersionPlugin plugin) { - this.plugin = plugin; - reloadConfig(); - } - - public void generateConfig() { - File file = new File(plugin.getDataFolder(), "config.yml"); - if (file.exists()) { - // Update conf options - Configuration oldConfig = new Configuration(file); - oldConfig.reload(false); // Load current options from conf - file.delete(); // Delete old conf - plugin.saveDefaultConfig(); // Generate new conf - Configuration newConfig = new Configuration(file); - newConfig.reload(true); // Load default options - for (String key : oldConfig.getKeys(false)) { - // Set option in new conf if exists - if (newConfig.contains(key)) { - newConfig.set(key, oldConfig.get(key)); - } - } - newConfig.save(); - } else { - plugin.saveDefaultConfig(); - } - } - - @Override - public boolean isCheckForUpdates() { - return plugin.getConfig().getBoolean("checkforupdates", true); - } - - @Override - public boolean isPreventCollision() { - return plugin.getConfig().getBoolean("prevent-collision", true); - } - - @Override - public boolean isNewEffectIndicator() { - return plugin.getConfig().getBoolean("use-new-effect-indicator", true); - } - - @Override - public boolean isShowNewDeathMessages() { - return plugin.getConfig().getBoolean("use-new-deathmessages", false); - } - - @Override - public boolean isSuppressMetadataErrors() { - return plugin.getConfig().getBoolean("suppress-metadata-errors", false); - } - - @Override - public boolean isShieldBlocking() { - return plugin.getConfig().getBoolean("shield-blocking", true); - } - - @Override - public boolean isHologramPatch() { - return plugin.getConfig().getBoolean("hologram-patch", false); - } - - @Override - public boolean isBossbarPatch() { - return plugin.getConfig().getBoolean("bossbar-patch", true); - } - - @Override - public boolean isBossbarAntiflicker() { - return plugin.getConfig().getBoolean("bossbar-anti-flicker", false); - } - - @Override - public boolean isUnknownEntitiesSuppressed() { - return false; - } - - @Override - public double getHologramYOffset() { - return plugin.getConfig().getDouble("hologram-y", -0.96D); - } - - @Override - public boolean isBlockBreakPatch() { - return false; - } - - @Override - public int getMaxPPS() { - return plugin.getConfig().getInt("max-pps", 140); - } - - @Override - public String getMaxPPSKickMessage() { - return plugin.getConfig().getString("max-pps-kick-msg", "Sending packets too fast? lag?"); - } - - @Override - public int getTrackingPeriod() { - return plugin.getConfig().getInt("tracking-period", 6); - } - - @Override - public int getWarningPPS() { - return plugin.getConfig().getInt("tracking-warning-pps", 120); - } - - @Override - public int getMaxWarnings() { - return plugin.getConfig().getInt("tracking-max-warnings", 3); - } - - @Override - public String getMaxWarningsKickMessage() { - return plugin.getConfig().getString("tracking-max-kick-msg", "You are sending too many packets, :("); - } - - @Override - public boolean isAntiXRay() { - return plugin.getConfig().getBoolean("anti-xray-patch", true); - } - - @Override - public boolean isSendSupportedVersions() { - return plugin.getConfig().getBoolean("send-supported-versions", false); - } - - @Override - public boolean isStimulatePlayerTick() { - return plugin.getConfig().getBoolean("simulate-pt", true); - } - - @Override - public boolean isItemCache() { - return plugin.getConfig().getBoolean("item-cache", true); - } - - @Override - public boolean isNMSPlayerTicking() { - return plugin.getConfig().getBoolean("nms-player-ticking", true); - } - - @Override - public boolean isReplacePistons() { - return plugin.getConfig().getBoolean("replace-pistons", false); - } - - @Override - public int getPistonReplacementId() { - return plugin.getConfig().getInt("replacement-piston-id", 0); - } - - public boolean isAutoTeam() { - // Collision has to be enabled first - return isPreventCollision() && plugin.getConfig().getBoolean("auto-team", true); - } - - @Override - public boolean isForceJsonTransform() { - return plugin.getConfig().getBoolean("force-json-transform", false); - } - - @Override - public List getBlockedProtocols() { - return plugin.getConfig().getIntegerList("block-protocols"); - } - - @Override - public String getBlockedDisconnectMsg() { - return plugin.getConfig().getString("block-disconnect-msg", "You are using an unsupported Minecraft version!"); - } - - @Override - 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 void reloadConfig() { - generateConfig(); - } - - @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 74862c742..70603a278 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -17,10 +17,7 @@ import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.bukkit.classgenerator.ClassGenerator; import us.myles.ViaVersion.bukkit.commands.BukkitCommandHandler; import us.myles.ViaVersion.bukkit.commands.BukkitCommandSender; -import us.myles.ViaVersion.bukkit.platform.BukkitTaskId; -import us.myles.ViaVersion.bukkit.platform.BukkitViaAPI; -import us.myles.ViaVersion.bukkit.platform.BukkitViaInjector; -import us.myles.ViaVersion.bukkit.platform.BukkitViaLoader; +import us.myles.ViaVersion.bukkit.platform.*; import us.myles.ViaVersion.bukkit.util.NMSUtil; import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.util.GsonUtil; @@ -37,7 +34,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform { private boolean lateBind = false; private boolean protocolSupport = false; @Getter - private ViaConfig conf; + private BukkitConfigAPI conf; @Getter private ViaAPI api = new BukkitViaAPI(this); private List queuedTasks = new ArrayList<>(); @@ -45,7 +42,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform { public ViaVersionPlugin() { // Config magic - conf = new ViaConfig(this); + conf = new BukkitConfigAPI(); // Command handler commandHandler = new BukkitCommandHandler(); // Init platform diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/protocol1_9to1_8/CommandBlockListener.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/protocol1_9to1_8/CommandBlockListener.java index 7477023b1..e75aa3af8 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/protocol1_9to1_8/CommandBlockListener.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/protocol1_9to1_8/CommandBlockListener.java @@ -22,8 +22,8 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.bukkit.listeners.ViaBukkitListener; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; import us.myles.ViaVersion.bukkit.util.NMSUtil; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; import us.myles.ViaVersion.util.ReflectionUtil; import java.io.DataOutput; diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitConfigAPI.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitConfigAPI.java new file mode 100644 index 000000000..3ed1fd2f5 --- /dev/null +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitConfigAPI.java @@ -0,0 +1,173 @@ +package us.myles.ViaVersion.bukkit.platform; + +import us.myles.ViaVersion.ViaVersionPlugin; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.ViaVersionConfig; +import us.myles.ViaVersion.util.Config; + +import java.io.File; +import java.util.Arrays; +import java.util.List; + +public class BukkitConfigAPI extends Config implements ViaVersionConfig { + private static List UNSUPPORTED = Arrays.asList(); + + public BukkitConfigAPI() { + super(new File(((ViaVersionPlugin) Via.getPlatform()).getDataFolder(), "config.yml")); + } + + @Override + public boolean isCheckForUpdates() { + return getBoolean("checkforupdates", true); + } + + @Override + public boolean isPreventCollision() { + return getBoolean("prevent-collision", true); + } + + @Override + public boolean isNewEffectIndicator() { + return getBoolean("use-new-effect-indicator", true); + } + + @Override + public boolean isShowNewDeathMessages() { + return getBoolean("use-new-deathmessages", false); + } + + @Override + public boolean isSuppressMetadataErrors() { + return getBoolean("suppress-metadata-errors", false); + } + + @Override + public boolean isShieldBlocking() { + return getBoolean("shield-blocking", true); + } + + @Override + public boolean isHologramPatch() { + return getBoolean("hologram-patch", false); + } + + @Override + public boolean isBossbarPatch() { + return getBoolean("bossbar-patch", true); + } + + @Override + public boolean isBossbarAntiflicker() { + return getBoolean("bossbar-anti-flicker", false); + } + + @Override + public boolean isUnknownEntitiesSuppressed() { + return false; + } + + @Override + public double getHologramYOffset() { + return getDouble("hologram-y", -0.96D); + } + + @Override + public boolean isBlockBreakPatch() { + return false; + } + + @Override + public int getMaxPPS() { + return getInt("max-pps", 800); + } + + @Override + public String getMaxPPSKickMessage() { + return getString("max-pps-kick-msg", "Sending packets too fast? lag?"); + } + + @Override + public int getTrackingPeriod() { + return getInt("tracking-period", 6); + } + + @Override + public int getWarningPPS() { + return getInt("tracking-warning-pps", 120); + } + + @Override + public int getMaxWarnings() { + return getInt("tracking-max-warnings", 3); + } + + @Override + public String getMaxWarningsKickMessage() { + return getString("tracking-max-kick-msg", "You are sending too many packets, :("); + } + + @Override + public boolean isAntiXRay() { + return getBoolean("anti-xray-patch", true); + } + + @Override + public boolean isSendSupportedVersions() { + return getBoolean("send-supported-versions", false); + } + + @Override + public boolean isStimulatePlayerTick() { + return getBoolean("simulate-pt", true); + } + + @Override + public boolean isItemCache() { + return getBoolean("item-cache", true); + } + + @Override + public boolean isNMSPlayerTicking() { + return getBoolean("nms-player-ticking", true); + } + + @Override + public boolean isReplacePistons() { + return getBoolean("replace-pistons", false); + } + + @Override + public int getPistonReplacementId() { + 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 getBoolean("force-json-transform", false); + } + + @Override + public List getBlockedProtocols() { + return getIntegerList("block-protocols"); + } + + @Override + public String getBlockedDisconnectMsg() { + return getString("block-disconnect-msg", "You are using an unsupported Minecraft version!"); + } + + @Override + public String getReloadDisconnectMsg() { + return getString("reload-disconnect-msg", "Server reload, please rejoin!"); + } + + @Override + public List getUnsupportedOptions() { + return UNSUPPORTED; + } +} diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaAPI.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaAPI.java index 9d3cff21e..d76cece62 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaAPI.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaAPI.java @@ -16,8 +16,8 @@ import us.myles.ViaVersion.api.command.ViaVersionCommand; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.boss.ViaBossBar; -import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.bukkit.util.ProtocolSupportUtil; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; import java.util.Map; import java.util.SortedSet; diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaInjector.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaInjector.java index 7eccdaf93..ef06a642e 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaInjector.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/platform/BukkitViaInjector.java @@ -9,9 +9,9 @@ import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.platform.ViaInjector; import us.myles.ViaVersion.bukkit.handlers.BukkitChannelInitializer; +import us.myles.ViaVersion.bukkit.util.NMSUtil; import us.myles.ViaVersion.util.ConcurrentList; import us.myles.ViaVersion.util.ListWrapper; -import us.myles.ViaVersion.bukkit.util.NMSUtil; import us.myles.ViaVersion.util.ReflectionUtil; import java.lang.reflect.Field; diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/providers/BukkitViaMovementTransmitter.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/providers/BukkitViaMovementTransmitter.java index 30bcaf1b4..4c9147d98 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/providers/BukkitViaMovementTransmitter.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/providers/BukkitViaMovementTransmitter.java @@ -4,10 +4,10 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.bukkit.util.NMSUtil; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.MovementTracker; -import us.myles.ViaVersion.bukkit.util.NMSUtil; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; diff --git a/bungee/src/main/java/us/myles/ViaVersion/Bungee.java b/bungee/src/main/java/us/myles/ViaVersion/Bungee.java index f8cb3b654..b8670fe47 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/Bungee.java +++ b/bungee/src/main/java/us/myles/ViaVersion/Bungee.java @@ -12,10 +12,10 @@ import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.ViaPlatform; -import us.myles.ViaVersion.bungee.platform.*; import us.myles.ViaVersion.bungee.commands.BungeeCommand; import us.myles.ViaVersion.bungee.commands.BungeeCommandHandler; import us.myles.ViaVersion.bungee.commands.BungeeCommandSender; +import us.myles.ViaVersion.bungee.platform.*; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -29,7 +29,7 @@ public class Bungee extends Plugin implements ViaPlatform { @Override public void onLoad() { api = new BungeeViaAPI(); - config = new BungeeConfigAPI(); + config = new BungeeConfigAPI(getDataFolder()); commandHandler = new BungeeCommandHandler(); ProxyServer.getInstance().getPluginManager().registerCommand(this, new BungeeCommand(commandHandler)); // Init platform diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeConfigAPI.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeConfigAPI.java index 9b1cdadb7..19092b5f9 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeConfigAPI.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeConfigAPI.java @@ -1,57 +1,66 @@ package us.myles.ViaVersion.bungee.platform; import us.myles.ViaVersion.api.ViaVersionConfig; -import us.myles.ViaVersion.api.configuration.ConfigurationProvider; +import us.myles.ViaVersion.util.Config; +import java.io.File; import java.util.Arrays; import java.util.List; -import java.util.Map; -// TODO implement -public class BungeeConfigAPI implements ViaVersionConfig, ConfigurationProvider { +public class BungeeConfigAPI extends Config implements ViaVersionConfig { + private static List UNSUPPORTED = Arrays.asList("nms-player-ticking", "item-cache", "anti-xray-patch"); + + public BungeeConfigAPI(File configFile) { + super(new File(configFile, "config.yml")); + } + @Override + public List getUnsupportedOptions() { + return UNSUPPORTED; + } + 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 @@ -61,12 +70,7 @@ public class BungeeConfigAPI implements ViaVersionConfig, ConfigurationProvider @Override public double getHologramYOffset() { - return 0; - } - - @Override - public boolean isAutoTeam() { - return false; + return getDouble("hologram-y", -0.96D); } @Override @@ -76,32 +80,32 @@ public class BungeeConfigAPI implements ViaVersionConfig, ConfigurationProvider @Override public int getMaxPPS() { - return 0; + return getInt("max-pps", 800); } @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 @@ -111,12 +115,12 @@ public class BungeeConfigAPI implements ViaVersionConfig, ConfigurationProvider @Override public boolean isSendSupportedVersions() { - return false; + return getBoolean("send-supported-versions", false); } @Override public boolean isStimulatePlayerTick() { - return true; + return getBoolean("simulate-pt", true); } @Override @@ -131,51 +135,36 @@ public class BungeeConfigAPI implements ViaVersionConfig, ConfigurationProvider @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(); + return getIntegerList("block-protocols"); } @Override public String getBlockedDisconnectMsg() { - return null; + return getString("block-disconnect-msg", "You are using an unsupported Minecraft version!"); } @Override public String getReloadDisconnectMsg() { - return null; - } - - @Override - public void set(String path, Object value) { - - } - - @Override - public void saveConfig() { - - } - - @Override - public void reloadConfig() { - - } - - @Override - public Map getValues() { - return null; + return getString("reload-disconnect-msg", "Server reload, please rejoin!"); } } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java index 436a99216..9e44f86af 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/platform/BungeeViaLoader.java @@ -8,7 +8,6 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmit public class BungeeViaLoader implements ViaPlatformLoader { @Override public void load() { - // TODO: Config Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BungeeMovementTransmitter()); } } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java index 535df323c..5d81f876a 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java @@ -4,10 +4,6 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToMessageDecoder; -import io.netty.handler.codec.MessageToMessageEncoder; -import net.md_5.bungee.protocol.DefinedPacket; -import net.md_5.bungee.protocol.MinecraftDecoder; -import net.md_5.bungee.protocol.MinecraftEncoder; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; diff --git a/common/pom.xml b/common/pom.xml index 8477ad2cb..fa13a94c6 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -11,4 +11,14 @@ 4.0.0 viaversion-common + + + + + org.yaml + snakeyaml + 1.18-SNAPSHOT + provided + + \ No newline at end of file diff --git a/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java b/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java index de15e497e..7d4f7730d 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java +++ b/common/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java @@ -15,7 +15,6 @@ import us.myles.ViaVersion.exception.InformativeException; import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.base.ProtocolInfo; -import us.myles.ViaVersion.util.PipelineUtil; import java.io.IOException; import java.util.ArrayList; diff --git a/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java b/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java index f4774baa2..00ac8421b 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/ViaCommandHandler.java @@ -102,6 +102,7 @@ public abstract class ViaCommandHandler implements ViaVersionCommand { /** * Shows the ViaVersion help to a sender + * * @param sender The sender to send the help to */ public void showHelp(ViaCommandSender sender) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ArmorType.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ArmorType.java index 9f3e30508..b4f830521 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ArmorType.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ArmorType.java @@ -46,8 +46,8 @@ public enum ArmorType { /** * Find an armour type by the item id - * @param id ID of the item * + * @param id ID of the item * @return Return the ArmourType, ArmourType.NONE if not found */ public static ArmorType findById(int id) { @@ -59,8 +59,8 @@ public enum ArmorType { /** * Find an armour type by the item string - * @param type String name for the item * + * @param type String name for the item * @return Return the ArmourType, ArmourType.NONE if not found */ public static ArmorType findByType(String type) { @@ -72,6 +72,7 @@ public enum ArmorType { /** * Check if an item id is armour + * * @param id The item ID * @return True if the item is a piece of armour */ @@ -84,6 +85,7 @@ public enum ArmorType { /** * Check if an item id is armour + * * @param type The item material name * @return True if the item is a piece of armour */ @@ -96,6 +98,7 @@ public enum ArmorType { /** * Get the Minecraft ID for the Armour Type + * * @return The ID */ public int getId() { diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/util/Configuration.java b/common/src/main/java/us/myles/ViaVersion/util/CommentStore.java similarity index 67% rename from bukkit/src/main/java/us/myles/ViaVersion/bukkit/util/Configuration.java rename to common/src/main/java/us/myles/ViaVersion/util/CommentStore.java index 0e3ba1797..12bd9b031 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/util/Configuration.java +++ b/common/src/main/java/us/myles/ViaVersion/util/CommentStore.java @@ -1,29 +1,25 @@ -package us.myles.ViaVersion.bukkit.util; +package us.myles.ViaVersion.util; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import org.bukkit.Bukkit; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; +import com.google.common.io.CharStreams; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.logging.Level; import java.util.regex.Pattern; -public class Configuration extends YamlConfiguration { +public class CommentStore { private final Map> headers = Maps.newConcurrentMap(); - private final File file; + private final char pathSeperator; + private final int indents; private List mainHeader = Lists.newArrayList(); - private boolean loadHeaders; - public Configuration(File file) { - this.file = file; + public CommentStore(char pathSeperator, int indents) { + this.pathSeperator = pathSeperator; + this.indents = indents; } /** @@ -65,43 +61,13 @@ public class Configuration extends YamlConfiguration { return headers.get(key); } - public T get(String key, Class type) { - return type.cast(get(key)); - } - - /** - * Reload config from file. - */ - public void reload() { - reload(headers.isEmpty()); - } - - /** - * Reload config from file. - * - * @param loadHeaders Whether or not to load headers. - */ - public void reload(boolean loadHeaders) { - this.loadHeaders = loadHeaders; - try { - load(file); - } catch (Exception e) { - Bukkit.getLogger().log(Level.WARNING, "failed to reload file", e); - } - } - - @Override - public void loadFromString(String contents) throws InvalidConfigurationException { - if (!loadHeaders) { - super.loadFromString(contents); - return; - } + public void storeComments(InputStream inputStream) throws IOException { + InputStreamReader reader = new InputStreamReader(inputStream); + String contents = CharStreams.toString(reader); StringBuilder memoryData = new StringBuilder(); - // Parse headers - final int indentLength = options().indent(); - final String pathSeparator = Character.toString(options().pathSeparator()); + final String pathSeparator = Character.toString(this.pathSeperator); int currentIndents = 0; String key = ""; List headers = Lists.newArrayList(); @@ -122,12 +88,12 @@ public class Configuration extends YamlConfiguration { continue; } - int indents = indent / indentLength; + int indents = indent / this.indents; if (indents <= currentIndents) { // Remove last section of key String[] array = key.split(Pattern.quote(pathSeparator)); int backspace = currentIndents - indents + 1; - key = join(array, options().pathSeparator(), 0, array.length - backspace); + key = join(array, this.pathSeperator, 0, array.length - backspace); } // Add new section to key @@ -143,32 +109,13 @@ public class Configuration extends YamlConfiguration { headers = Lists.newArrayList(); } } - - // Parse remaining text - super.loadFromString(memoryData.toString()); - - // Clear bukkit header - options().header(null); } - /** - * Save config to file - */ - public void save() { - if (headers.isEmpty() && mainHeader.isEmpty()) { - try { - super.save(file); - } catch (IOException e) { - Bukkit.getLogger().log(Level.WARNING, "Failed to save file", e); - } - return; - } - + public void writeComments(String yaml, File output) throws IOException { // Custom save - final int indentLength = options().indent(); - final String pathSeparator = Character.toString(options().pathSeparator()); - String content = saveToString(); - StringBuilder fileData = new StringBuilder(buildHeader()); + final int indentLength = this.indents; + final String pathSeparator = Character.toString(this.pathSeperator); + StringBuilder fileData = new StringBuilder(); int currentIndents = 0; String key = ""; for (String h : mainHeader) { @@ -176,7 +123,7 @@ public class Configuration extends YamlConfiguration { fileData.append("#> ").append(h).append('\n'); } - for (String line : content.split("\n")) { + for (String line : yaml.split("\n")) { if (line.isEmpty()) continue; // Skip empty lines int indent = getSuccessiveCharCount(line, ' '); int indents = indent / indentLength; @@ -185,7 +132,7 @@ public class Configuration extends YamlConfiguration { // Remove last section of key String[] array = key.split(Pattern.quote(pathSeparator)); int backspace = currentIndents - indents + 1; - key = join(array, options().pathSeparator(), 0, array.length - backspace); + key = join(array, this.pathSeperator, 0, array.length - backspace); } // Add new section to key @@ -203,11 +150,9 @@ public class Configuration extends YamlConfiguration { // Write data to file FileWriter writer = null; try { - writer = new FileWriter(file); + writer = new FileWriter(output); writer.write(fileData.toString()); writer.flush(); - } catch (IOException e) { - Bukkit.getLogger().log(Level.WARNING, "Failed to save file", e); } finally { if (writer != null) { try { diff --git a/common/src/main/java/us/myles/ViaVersion/util/Config.java b/common/src/main/java/us/myles/ViaVersion/util/Config.java new file mode 100644 index 000000000..91dc44884 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/util/Config.java @@ -0,0 +1,150 @@ +package us.myles.ViaVersion.util; + +import org.yaml.snakeyaml.Yaml; +import us.myles.ViaVersion.api.configuration.ConfigurationProvider; + +import java.io.*; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public abstract class Config implements ConfigurationProvider { + private static ThreadLocal yaml = new ThreadLocal() { + @Override + protected Yaml initialValue() { + return new Yaml(); + } + }; + private CommentStore commentStore = new CommentStore('.', 2); + private final File configFile; + private Map config; + + public Config(File configFile) { + this.configFile = configFile; + reloadConfig(); + } + + public Map loadConfig(File location) { + List unsupported = getUnsupportedOptions(); + URL jarConfigFile = Config.class.getClassLoader().getResource("config.yml"); + try { + commentStore.storeComments(jarConfigFile.openStream()); + for (String option : unsupported) { + List comments = commentStore.header(option); + if (comments != null) { + comments.clear(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + Map config = null; + if (location.exists()) { + try (FileInputStream input = new FileInputStream(location)) { + config = (Map) yaml.get().load(input); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (config == null) { + config = new HashMap<>(); + } + + Map defaults = config; + try (InputStream stream = jarConfigFile.openStream()) { + defaults = (Map) yaml.get().load(stream); + for (String option : unsupported) { + defaults.remove(option); + } + // Merge with defaultLoader + for (Object key : config.keySet()) { + // Set option in new conf if exists + if (defaults.containsKey(key) && !unsupported.contains(key.toString())) { + defaults.put((String) key, config.get(key)); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + // Save + saveConfig(location, defaults); + + return defaults; + } + + public void saveConfig(File location, Map config) { + try { + commentStore.writeComments(yaml.get().dump(config), location); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public abstract List getUnsupportedOptions(); + + @Override + public void set(String path, Object value) { + set(path, value); + } + + @Override + public void saveConfig() { + this.configFile.getParentFile().mkdirs(); + saveConfig(this.configFile, this.config); + } + + @Override + public void reloadConfig() { + this.configFile.getParentFile().mkdirs(); + this.config = loadConfig(this.configFile); + } + + @Override + public Map getValues() { + return this.config; + } + + 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<>(); + } + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java b/common/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java index dbf3af7ee..f44622297 100644 --- a/common/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java +++ b/common/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java @@ -5,7 +5,6 @@ import com.google.common.collect.Maps; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.util.Collection; import java.util.Collections; import java.util.Map; diff --git a/bukkit/src/main/resources/config.yml b/common/src/main/resources/config.yml similarity index 99% rename from bukkit/src/main/resources/config.yml rename to common/src/main/resources/config.yml index aac3ad695..ffb3bdf32 100644 --- a/bukkit/src/main/resources/config.yml +++ b/common/src/main/resources/config.yml @@ -32,7 +32,7 @@ reload-disconnect-msg: "Server reload, please rejoin!" # # What is the maximum per second a client can send (Use %pps to display their pps) # Use -1 to disable. -max-pps: 600 +max-pps: 800 max-pps-kick-msg: "You are sending too many packets!" # # We can also kick them if over a period they send over a threshold a certain amount of times. diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeConfigAPI.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeConfigAPI.java index e2ad17622..abab23463 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeConfigAPI.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/platform/SpongeConfigAPI.java @@ -1,29 +1,22 @@ package us.myles.ViaVersion.sponge.platform; -import org.yaml.snakeyaml.Yaml; import us.myles.ViaVersion.api.ViaVersionConfig; -import us.myles.ViaVersion.api.configuration.ConfigurationProvider; +import us.myles.ViaVersion.util.Config; -import java.io.*; -import java.net.URL; -import java.util.ArrayList; -import java.util.HashMap; +import java.io.File; +import java.util.Arrays; import java.util.List; -import java.util.Map; -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 class SpongeConfigAPI extends Config implements ViaVersionConfig { + private static List UNSUPPORTED = Arrays.asList("anti-xray-patch"); - public SpongeConfigAPI(File defaultConfig) { - this.defaultConfig = defaultConfig; - reloadConfig(); + public SpongeConfigAPI(File configFile) { + super(new File(configFile, "config.yml")); + } + + @Override + public List getUnsupportedOptions() { + return UNSUPPORTED; } public boolean isCheckForUpdates() { @@ -87,7 +80,7 @@ public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider @Override public int getMaxPPS() { - return getInt("max-pps", 140); + return getInt("max-pps", 800); } @Override @@ -117,7 +110,7 @@ public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider @Override public boolean isAntiXRay() { - return getBoolean("anti-xray-patch", true); + return false; } @Override @@ -174,107 +167,4 @@ public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider public String getReloadDisconnectMsg() { 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 (Map) ((Map) this.config); - } }