From cda0152a4b5b5c43047f9b1829a953bed7cd8269 Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Tue, 12 Nov 2019 20:07:33 -0300 Subject: [PATCH] Add config file --- build.gradle.kts | 4 + .../creeper123123321/viafabric/ViaFabric.java | 5 ++ .../viafabric/config/VRConfig.java | 75 +++++++++++++++++++ .../mixin/client/MixinMultiplayerScreen.java | 35 +++++---- .../viafabric/platform/VRPlatform.java | 3 - .../viafabric/platform/VRViaConfig.java | 1 - .../providers/VRVersionProvider.java | 7 +- .../resources/assets/viafabric/config.yml | 6 ++ 8 files changed, 112 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/github/creeper123123321/viafabric/config/VRConfig.java create mode 100644 src/main/resources/assets/viafabric/config.yml diff --git a/build.gradle.kts b/build.gradle.kts index 2dd2b0d..faefc16 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -63,3 +63,7 @@ dependencies { minecraft { } + +license { + include("**/*.java") +} diff --git a/src/main/java/com/github/creeper123123321/viafabric/ViaFabric.java b/src/main/java/com/github/creeper123123321/viafabric/ViaFabric.java index 82dadc6..19aa58f 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/ViaFabric.java +++ b/src/main/java/com/github/creeper123123321/viafabric/ViaFabric.java @@ -25,6 +25,7 @@ package com.github.creeper123123321.viafabric; import com.github.creeper123123321.viafabric.commands.VRCommandHandler; +import com.github.creeper123123321.viafabric.config.VRConfig; import com.github.creeper123123321.viafabric.platform.VRInjector; import com.github.creeper123123321.viafabric.platform.VRLoader; import com.github.creeper123123321.viafabric.platform.VRPlatform; @@ -52,6 +53,7 @@ public class ViaFabric implements ModInitializer { public static final Logger JLOGGER = new JLoggerToLog4j(LogManager.getLogger("ViaFabric")); public static final ExecutorService ASYNC_EXECUTOR; public static final EventLoop EVENT_LOOP; + public static VRConfig config; static { ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("ViaFabric-%d").build(); @@ -89,5 +91,8 @@ public class ViaFabric implements ModInitializer { CommandRegistry.INSTANCE.register(false, c -> c.register(command("viaversion"))); CommandRegistry.INSTANCE.register(false, c -> c.register(command("viaver"))); CommandRegistry.INSTANCE.register(false, c -> c.register(command("vvfabric"))); + + config = new VRConfig(FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric") + .resolve("viafabric.yml").toFile()); } } diff --git a/src/main/java/com/github/creeper123123321/viafabric/config/VRConfig.java b/src/main/java/com/github/creeper123123321/viafabric/config/VRConfig.java new file mode 100644 index 0000000..9b58270 --- /dev/null +++ b/src/main/java/com/github/creeper123123321/viafabric/config/VRConfig.java @@ -0,0 +1,75 @@ +/* + * MIT License + * + * Copyright (c) 2018 creeper123123321 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.creeper123123321.viafabric.config; + +import net.minecraft.SharedConstants; +import us.myles.ViaVersion.util.Config; + +import java.io.File; +import java.net.URL; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class VRConfig extends Config { + public static final String ENABLE_CLIENT_SIDE = "enable-client-side"; + public static final String CLIENT_SIDE_VERSION = "client-side-version"; + + public VRConfig(File configFile) { + super(configFile); + reloadConfig(); + } + + @Override + public URL getDefaultConfigURL() { + return getClass().getClassLoader().getResource("assets/viafabric/config.yml"); + } + + @Override + protected void handleConfig(Map map) { + } + + @Override + public List getUnsupportedOptions() { + return Collections.emptyList(); + } + + public boolean isClientSideEnabled() { + return getBoolean(ENABLE_CLIENT_SIDE, false); + } + + public int getClientSideVersion() { + int nat = SharedConstants.getGameVersion().getProtocolVersion(); + return !isClientSideEnabled() ? nat : getInt(CLIENT_SIDE_VERSION, -1); + } + + public void setClientSideEnabled(boolean val) { + set(ENABLE_CLIENT_SIDE, val); + } + + public void setClientSideVersion(int val) { + set(CLIENT_SIDE_VERSION, val); + } +} diff --git a/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinMultiplayerScreen.java b/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinMultiplayerScreen.java index a4c0fe9..56074a3 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinMultiplayerScreen.java +++ b/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinMultiplayerScreen.java @@ -24,9 +24,8 @@ package com.github.creeper123123321.viafabric.mixin.client; -import com.github.creeper123123321.viafabric.providers.VRVersionProvider; +import com.github.creeper123123321.viafabric.ViaFabric; import com.github.creeper123123321.viafabric.util.VersionFormatFilter; -import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.ConfirmScreen; import net.minecraft.client.gui.screen.Screen; @@ -43,14 +42,12 @@ import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; -import us.myles.ViaVersion.protocols.base.VersionProvider; -import java.io.IOException; import java.util.Arrays; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -64,6 +61,8 @@ public abstract class MixinMultiplayerScreen extends Screen { private boolean validProtocol; @Unique private boolean supportedProtocol; + @Unique + private CompletableFuture latestProtocolSave; protected MixinMultiplayerScreen(Text title, UnsupportedOperationException e) { super(title); @@ -76,7 +75,7 @@ public abstract class MixinMultiplayerScreen extends Screen { protocolVersion.setTextPredicate(new VersionFormatFilter()); protocolVersion.setChangedListener((text) -> { protocolVersion.setSuggestion(null); - int newVersion = ((VRVersionProvider) Via.getManager().getProviders().get(VersionProvider.class)).clientSideModeVersion; + int newVersion = ViaFabric.config.getClientSideVersion(); validProtocol = true; try { newVersion = Integer.parseInt(text); @@ -102,9 +101,17 @@ public abstract class MixinMultiplayerScreen extends Screen { } supportedProtocol = isSupported(newVersion); protocolVersion.setEditableColor(getTextColor()); - ((VRVersionProvider) Via.getManager().getProviders().get(VersionProvider.class)).clientSideModeVersion = newVersion; + int finalNewVersion = newVersion; + if (latestProtocolSave == null) latestProtocolSave = CompletableFuture.completedFuture(null); + latestProtocolSave = latestProtocolSave.thenRunAsync(() -> { + ViaFabric.config.setClientSideVersion(finalNewVersion); + ViaFabric.config.saveConfig(); + }, ViaFabric.ASYNC_EXECUTOR); }); - int clientSideVersion = ((VRVersionProvider) Via.getManager().getProviders().get(VersionProvider.class)).clientSideModeVersion; + int clientSideVersion = ViaFabric.config.getClientSideVersion(); + + protocolVersion.setVisible(ViaFabric.config.isClientSideEnabled()); + protocolVersion.setText(ProtocolVersion.isRegistered(clientSideVersion) ? ProtocolVersion.getProtocol(clientSideVersion).getName() : Integer.toString(clientSideVersion)); @@ -120,13 +127,10 @@ public abstract class MixinMultiplayerScreen extends Screen { answer -> { MinecraftClient.getInstance().openScreen(this); if (answer) { - try { - FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric").resolve("enable_client_side").toFile().createNewFile(); - protocolVersion.setVisible(true); - enableClientSideViaVersion.visible = false; - } catch (IOException e) { - e.printStackTrace(); - } + ViaFabric.config.setClientSideEnabled(true); + ViaFabric.config.saveConfig(); + protocolVersion.setVisible(true); + enableClientSideViaVersion.visible = false; } }, new TranslatableText("gui.enable_client_side.question"), @@ -135,7 +139,6 @@ public abstract class MixinMultiplayerScreen extends Screen { I18n.translate("gui.cancel") )), I18n.translate("gui.enable_client_side_button")); - protocolVersion.setVisible(FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric").resolve("enable_client_side").toFile().exists()); enableClientSideViaVersion.visible = !protocolVersion.isVisible(); addButton(enableClientSideViaVersion); } diff --git a/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java b/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java index 63fb755..5a1ad2e 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java +++ b/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java @@ -27,7 +27,6 @@ package com.github.creeper123123321.viafabric.platform; import com.github.creeper123123321.viafabric.ViaFabric; import com.github.creeper123123321.viafabric.commands.NMSCommandSender; import com.github.creeper123123321.viafabric.commands.UserCommandSender; -import com.github.creeper123123321.viafabric.providers.VRVersionProvider; import com.github.creeper123123321.viafabric.util.FutureTaskId; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -52,7 +51,6 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.dump.PluginInfo; -import us.myles.ViaVersion.protocols.base.VersionProvider; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.sponge.VersionInfo; import us.myles.ViaVersion.util.GsonUtil; @@ -295,7 +293,6 @@ public class VRPlatform implements ViaPlatform { } platformSpecific.add("mods", GsonUtil.getGson().toJsonTree(mods)); - platformSpecific.addProperty("client-sided-version", ((VRVersionProvider) Via.getManager().getProviders().get(VersionProvider.class)).clientSideModeVersion); return platformSpecific; } diff --git a/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaConfig.java b/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaConfig.java index 926660a..df933d7 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaConfig.java +++ b/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaConfig.java @@ -25,7 +25,6 @@ package com.github.creeper123123321.viafabric.platform; import us.myles.ViaVersion.AbstractViaConfig; -import us.myles.ViaVersion.util.Config; import java.io.File; import java.net.URL; diff --git a/src/main/java/com/github/creeper123123321/viafabric/providers/VRVersionProvider.java b/src/main/java/com/github/creeper123123321/viafabric/providers/VRVersionProvider.java index 5efd148..cd6bc04 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/providers/VRVersionProvider.java +++ b/src/main/java/com/github/creeper123123321/viafabric/providers/VRVersionProvider.java @@ -24,17 +24,16 @@ package com.github.creeper123123321.viafabric.providers; +import com.github.creeper123123321.viafabric.ViaFabric; import com.github.creeper123123321.viafabric.platform.VRClientSideUserConnection; -import net.minecraft.SharedConstants; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.protocols.base.VersionProvider; public class VRVersionProvider extends VersionProvider { - public int clientSideModeVersion = SharedConstants.getGameVersion().getProtocolVersion(); - @Override public int getServerProtocol(UserConnection connection) throws Exception { - if (connection instanceof VRClientSideUserConnection) return clientSideModeVersion; + if (connection instanceof VRClientSideUserConnection) + return ViaFabric.config.getClientSideVersion(); return super.getServerProtocol(connection); } } diff --git a/src/main/resources/assets/viafabric/config.yml b/src/main/resources/assets/viafabric/config.yml new file mode 100644 index 0000000..e553f3f --- /dev/null +++ b/src/main/resources/assets/viafabric/config.yml @@ -0,0 +1,6 @@ +# WARNING +# I can not guarantee that this mod is allowed on every (or even any) server. This mod may cause problems with anti cheat plugins. USE AT OWN RISK +# This option enables client-side transforming (can also be enabled in-game) +enable-client-side: false +# This option sets the protocol version to be used when connection to the server (can also be changed in-game) +client-side-version: -1 \ No newline at end of file