From f0f07cc822d20ae8d9cd271477c7f4fa6fb6b231 Mon Sep 17 00:00:00 2001 From: Flooflez Date: Tue, 26 Sep 2023 13:58:10 -0400 Subject: [PATCH] Port to 1.20.2 --- gradle.properties | 10 +- remappedSrc/alujjdnd/ngrok/lan/NgrokLan.java | 4 +- .../ngrok/lan/NgrokServerInitialiser.java | 20 ++- .../ngrok/lan/command/LanDeopCommand.java | 5 +- .../ngrok/lan/command/LanOpCommand.java | 9 +- .../lan/command/LanWhitelistCommand.java | 52 +++++--- .../lan/command/ReloadJsonListsCommand.java | 42 ++----- .../ngrok/lan/mixin/GameMenuScreenMixin.java | 96 +++++++++++++++ .../lan/mixin/IntegratedServerAccessor.java | 15 +++ .../ngrok/lan/mixin/OpenToLanScreenMixin.java | 114 ++++++++---------- .../ngrok/lan/mixin/WindowTitleMixin.java | 6 +- .../java/alujjdnd/ngrok/lan/NgrokLan.java | 3 - .../lan/command/LanWhitelistCommand.java | 10 +- src/main/resources/fabric.mod.json | 2 +- 14 files changed, 244 insertions(+), 144 deletions(-) create mode 100644 remappedSrc/alujjdnd/ngrok/lan/mixin/GameMenuScreenMixin.java create mode 100644 remappedSrc/alujjdnd/ngrok/lan/mixin/IntegratedServerAccessor.java diff --git a/gradle.properties b/gradle.properties index f7ab6c5..b53d092 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,14 +3,14 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/develop - minecraft_version=1.20.1 - yarn_mappings=1.20.1+build.1 - loader_version=0.14.21 + minecraft_version=1.20.2 + yarn_mappings=1.20.2+build.1 + loader_version=0.14.22 # Mod Properties - mod_version = beta-1.5.1-1.20.1 + mod_version = 1.5.1-1.20.2 maven_group = alujjdnd.ngrok.lan archives_base_name = ngrok-lan-expose-mod # Dependencies - fabric_version=0.83.0+1.20.1 + fabric_version=0.89.2+1.20.2 diff --git a/remappedSrc/alujjdnd/ngrok/lan/NgrokLan.java b/remappedSrc/alujjdnd/ngrok/lan/NgrokLan.java index bbcde24..efde24c 100644 --- a/remappedSrc/alujjdnd/ngrok/lan/NgrokLan.java +++ b/remappedSrc/alujjdnd/ngrok/lan/NgrokLan.java @@ -6,7 +6,7 @@ import alujjdnd.ngrok.lan.command.ReloadJsonListsCommand; import com.github.alexdlaird.ngrok.NgrokClient; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +40,7 @@ public void onInitialize() { AutoConfig.register(NLanConfig.class, JanksonConfigSerializer::new); //register commands - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { LanOpCommand.register(dispatcher); LanDeopCommand.register(dispatcher); diff --git a/remappedSrc/alujjdnd/ngrok/lan/NgrokServerInitialiser.java b/remappedSrc/alujjdnd/ngrok/lan/NgrokServerInitialiser.java index f592d44..a204e89 100644 --- a/remappedSrc/alujjdnd/ngrok/lan/NgrokServerInitialiser.java +++ b/remappedSrc/alujjdnd/ngrok/lan/NgrokServerInitialiser.java @@ -8,7 +8,7 @@ import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,19 +19,15 @@ public class NgrokServerInitialiser implements DedicatedServerModInitializer public static final Logger LOGGER = LoggerFactory.getLogger(NgrokLan.MODID); @Override - // Initialize the differents parts of the mod when lauched on server - public void onInitializeServer() - { + public void onInitializeServer() { + LOGGER.info("World is Open to LAN with Ngrok LAN"); - //register commands - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> - { - LanOpCommand.register(dispatcher); - LanDeopCommand.register(dispatcher); - LanWhitelistCommand.register(dispatcher); - ReloadJsonListsCommand.register(dispatcher); - }); + // Initialize the differents parts of the mod when lauched on server + CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> LanOpCommand.register(dispatcher))); + CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> LanDeopCommand.register(dispatcher))); + CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> LanWhitelistCommand.register(dispatcher))); + CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> ReloadJsonListsCommand.register(dispatcher))); } } diff --git a/remappedSrc/alujjdnd/ngrok/lan/command/LanDeopCommand.java b/remappedSrc/alujjdnd/ngrok/lan/command/LanDeopCommand.java index 8edeeaf..5df19f0 100644 --- a/remappedSrc/alujjdnd/ngrok/lan/command/LanDeopCommand.java +++ b/remappedSrc/alujjdnd/ngrok/lan/command/LanDeopCommand.java @@ -13,6 +13,7 @@ import net.minecraft.text.Text; import java.util.Collection; +import java.util.Iterator; public class LanDeopCommand { private static final SimpleCommandExceptionType ALREADY_DEOPPED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.deop.failed")); @@ -38,7 +39,9 @@ private static int deop(ServerCommandSource source, Collection targ if (playerManager.isOperator(gameProfile)) { playerManager.removeFromOperators(gameProfile); ++i; - source.sendFeedback(new TranslatableText("commands.deop.success", targets.iterator().next().getName()), true); + source.sendFeedback(() -> { + return Text.translatable("commands.deop.success", new Object[]{((GameProfile) targets.iterator().next()).getName()}); + }, true); } } diff --git a/remappedSrc/alujjdnd/ngrok/lan/command/LanOpCommand.java b/remappedSrc/alujjdnd/ngrok/lan/command/LanOpCommand.java index aab6d92..b38917a 100644 --- a/remappedSrc/alujjdnd/ngrok/lan/command/LanOpCommand.java +++ b/remappedSrc/alujjdnd/ngrok/lan/command/LanOpCommand.java @@ -10,12 +10,13 @@ import net.minecraft.server.PlayerManager; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.text.TranslatableText; +import net.minecraft.text.Text; import java.util.Collection; +import java.util.Iterator; public class LanOpCommand { - private static final SimpleCommandExceptionType ALREADY_OPPED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.op.failed")); + private static final SimpleCommandExceptionType ALREADY_OPPED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.op.failed")); public LanOpCommand() { } @@ -43,7 +44,9 @@ private static int op(ServerCommandSource source, Collection target if (!playerManager.isOperator(gameProfile)) { playerManager.addToOperators(gameProfile); ++i; - source.sendFeedback(new TranslatableText("commands.op.success", targets.iterator().next().getName()), true); + source.sendFeedback(() -> { + return Text.translatable("commands.op.success", new Object[]{((GameProfile) targets.iterator().next()).getName()}); + }, true); } } diff --git a/remappedSrc/alujjdnd/ngrok/lan/command/LanWhitelistCommand.java b/remappedSrc/alujjdnd/ngrok/lan/command/LanWhitelistCommand.java index 3e20064..95209f2 100644 --- a/remappedSrc/alujjdnd/ngrok/lan/command/LanWhitelistCommand.java +++ b/remappedSrc/alujjdnd/ngrok/lan/command/LanWhitelistCommand.java @@ -1,5 +1,6 @@ package alujjdnd.ngrok.lan.command; +import com.google.common.collect.Lists; import com.mojang.authlib.GameProfile; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.builder.LiteralArgumentBuilder; @@ -12,16 +13,19 @@ import net.minecraft.server.WhitelistEntry; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Texts; -import net.minecraft.text.TranslatableText; +import net.minecraft.text.Text; import java.util.Collection; +import java.util.Iterator; +import java.util.List; public class LanWhitelistCommand { - private static final SimpleCommandExceptionType ALREADY_ON_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.whitelist.alreadyOn")); - private static final SimpleCommandExceptionType ALREADY_OFF_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.whitelist.alreadyOff")); - private static final SimpleCommandExceptionType ADD_FAILED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.whitelist.add.failed")); - private static final SimpleCommandExceptionType REMOVE_FAILED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.whitelist.remove.failed")); + private static final SimpleCommandExceptionType ALREADY_ON_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.whitelist.alreadyOn")); + private static final SimpleCommandExceptionType ALREADY_OFF_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.whitelist.alreadyOff")); + private static final SimpleCommandExceptionType ADD_FAILED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.whitelist.add.failed")); + private static final SimpleCommandExceptionType REMOVE_FAILED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.whitelist.remove.failed")); public LanWhitelistCommand() { } @@ -55,8 +59,8 @@ public static void register(CommandDispatcher dispatcher) { private static int executeReload(ServerCommandSource source) { source.getServer().getPlayerManager().reloadWhitelist(); - source.sendFeedback(new TranslatableText("commands.whitelist.reloaded"), true); - source.getServer().kickNonWhitelistedPlayers(source); + source.sendFeedback(() -> Text.translatable("commands.whitelist.reloaded"), true); + kickNonWhitelistedPlayers(source); return 1; } @@ -68,7 +72,7 @@ private static int executeAdd(ServerCommandSource source, Collection Text.translatable("commands.whitelist.add.success", Texts.toText(gameProfile)), true); ++i; } } @@ -88,7 +92,7 @@ private static int executeRemove(ServerCommandSource source, Collection Text.translatable("commands.whitelist.remove.success", Texts.toText(gameProfile)), true); ++i; } } @@ -96,7 +100,7 @@ private static int executeRemove(ServerCommandSource source, Collection Text.translatable("commands.whitelist.enabled"), true); + kickNonWhitelistedPlayers(source); return 1; } } @@ -119,7 +123,7 @@ private static int executeOff(ServerCommandSource source) throws CommandSyntaxEx throw ALREADY_OFF_EXCEPTION.create(); } else { playerManager.setWhitelistEnabled(false); - source.sendFeedback(new TranslatableText("commands.whitelist.disabled"), true); + source.sendFeedback(() -> Text.translatable("commands.whitelist.disabled"), true); return 1; } } @@ -127,11 +131,29 @@ private static int executeOff(ServerCommandSource source) throws CommandSyntaxEx private static int executeList(ServerCommandSource source) { String[] strings = source.getServer().getPlayerManager().getWhitelistedNames(); if (strings.length == 0) { - source.sendFeedback(new TranslatableText("commands.whitelist.none"), false); + source.sendFeedback(() -> Text.translatable("commands.whitelist.none"), false); } else { - source.sendFeedback(new TranslatableText("commands.whitelist.list", strings.length, String.join(", ", strings)), false); + source.sendFeedback(() -> Text.translatable("commands.whitelist.list"), false); } return strings.length; } + private static void kickNonWhitelistedPlayers(ServerCommandSource source){ + PlayerManager playerManager = source.getServer().getPlayerManager(); + if (playerManager.isWhitelistEnabled()) { + Whitelist whitelist = playerManager.getWhitelist(); + List list = Lists.newArrayList(playerManager.getPlayerList()); + + for (ServerPlayerEntity serverPlayerEntity : list) { + GameProfile profile = serverPlayerEntity.getGameProfile(); + if(!source.getServer().isHost(profile)){ + if (!whitelist.isAllowed(profile) && !playerManager.isOperator(profile)) { + serverPlayerEntity.networkHandler.disconnect(Text.translatable("multiplayer.disconnect.not_whitelisted")); + } + } + + } + + } + } } diff --git a/remappedSrc/alujjdnd/ngrok/lan/command/ReloadJsonListsCommand.java b/remappedSrc/alujjdnd/ngrok/lan/command/ReloadJsonListsCommand.java index 423dc0c..777523e 100644 --- a/remappedSrc/alujjdnd/ngrok/lan/command/ReloadJsonListsCommand.java +++ b/remappedSrc/alujjdnd/ngrok/lan/command/ReloadJsonListsCommand.java @@ -1,28 +1,19 @@ package alujjdnd.ngrok.lan.command; -import alujjdnd.ngrok.lan.NgrokLan; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import net.minecraft.client.MinecraftClient; -import net.minecraft.command.CommandSource; -import net.minecraft.command.argument.GameProfileArgumentType; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.PlayerManager; -import net.minecraft.server.ServerConfigHandler; +import net.minecraft.server.*; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.text.TranslatableText; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; public class ReloadJsonListsCommand { - //check if player has permission (same permission level as /op and /deop) - //run loadJson(); - //get source.getServer() from command - - private static final SimpleCommandExceptionType LOAD_JSON_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("text.info.ngroklan.reload.message")); + private static final SimpleCommandExceptionType LOAD_JSON_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("text.info.ngroklan.reload.message")); public ReloadJsonListsCommand() { } @@ -34,30 +25,21 @@ public static void register(CommandDispatcher dispatcher) { } private static int loadJson(CommandContext serverCommandSourceCommandContext) throws CommandSyntaxException { - MinecraftServer server = serverCommandSourceCommandContext.getSource().getServer(); - - boolean bl3 = ServerConfigHandler.convertOperators(server); + PlayerManager playerManager = serverCommandSourceCommandContext.getSource().getServer().getPlayerManager(); - boolean bl4 = ServerConfigHandler.convertWhitelist(server); + Whitelist whitelist = playerManager.getWhitelist(); + OperatorList opList = playerManager.getOpList(); + try { + whitelist.load(); + opList.load(); - if(! (bl3 && bl4) ){ + MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(Text.translatable("text.info.ngroklan.reload.success").styled(style -> style.withColor(Formatting.GREEN) )); + } catch (Exception e) { throw LOAD_JSON_EXCEPTION.create(); } - else{ - MinecraftClient.getInstance().inGameHud.getChatHud().addMessage( new TranslatableText("text.info.ngroklan.reload.success")); - } return 1; } - - - - private static void sleepFiveSeconds() { - try { - Thread.sleep(5000L); - } catch (InterruptedException ignored) { - } - } } diff --git a/remappedSrc/alujjdnd/ngrok/lan/mixin/GameMenuScreenMixin.java b/remappedSrc/alujjdnd/ngrok/lan/mixin/GameMenuScreenMixin.java new file mode 100644 index 0000000..59eedc1 --- /dev/null +++ b/remappedSrc/alujjdnd/ngrok/lan/mixin/GameMenuScreenMixin.java @@ -0,0 +1,96 @@ +package alujjdnd.ngrok.lan.mixin; + + +import alujjdnd.ngrok.lan.NgrokLan; +import alujjdnd.ngrok.lan.config.NLanConfig; +import com.google.common.collect.Lists; +import com.mojang.authlib.GameProfile; +import com.terraformersmc.modmenu.mixin.AccessorGridWidget; +import me.shedaniel.autoconfig.AutoConfig; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.GameMenuScreen; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.GridWidget; +import net.minecraft.client.gui.widget.Widget; +import net.minecraft.server.PlayerManager; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; +import net.minecraft.text.TextContent; +import net.minecraft.text.TranslatableTextContent; +import net.minecraft.util.Formatting; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import java.util.List; + +@Mixin(GameMenuScreen.class) +public class GameMenuScreenMixin extends Screen { + + NLanConfig config = AutoConfig.getConfigHolder(NLanConfig.class).getConfig(); + MinecraftClient mc = MinecraftClient.getInstance(); + + protected GameMenuScreenMixin(Text title) { + super(title); + } + + + @Inject(method = "initWidgets", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/widget/GridWidget;forEachChild(Ljava/util/function/Consumer;)V"), locals = LocalCapture.CAPTURE_FAILEXCEPTION) + private void onInitWidgets(CallbackInfo ci, GridWidget gridWidget, GridWidget.Adder adder, Text text) { + + if (config.enabledCheckBox && NgrokLan.serverOpen) { //if ngrok server open + if (gridWidget != null) { + final List buttons = ((AccessorGridWidget) gridWidget).getChildren(); + + for (Widget widget : buttons) { + if (buttonHasText(widget, "menu.playerReporting")) { //find player report button + this.addDrawableChild(ButtonWidget.builder(Text.translatable("text.UI.ngroklan.closeServerButton"), (button) -> { + this.client.setScreen(null); + closeServer(); + }).dimensions(widget.getX() + 100, widget.getY(), 80, widget.getHeight()).build()); + } + } + } + } + } + + private boolean buttonHasText(Widget widget, String translationKey) { + if (widget instanceof ButtonWidget button) { + Text text = button.getMessage(); + TextContent textContent = text.getContent(); + return textContent instanceof TranslatableTextContent && ((TranslatableTextContent) textContent).getKey().equals(translationKey); + } + return false; + } + + private void closeServer(){ + NgrokLan.LOGGER.info("Closing LAN server..."); + PlayerManager playerManager = mc.getServer().getPlayerManager(); + List list = Lists.newArrayList(playerManager.getPlayerList()); + + for (ServerPlayerEntity serverPlayerEntity : list) { + GameProfile profile = serverPlayerEntity.getGameProfile(); + if(!mc.getServer().isHost(profile)){ + serverPlayerEntity.networkHandler.disconnect(Text.translatable("multiplayer.disconnect.server_shutdown")); + } + } + + mc.getServer().getNetworkIo().stop(); //stop listening to port + ((IntegratedServerAccessor) mc.getServer()).setLanPort(-1); + ((IntegratedServerAccessor) mc.getServer()).getLanPinger().interrupt(); + + //fix the window title + MinecraftClient.getInstance().updateWindowTitle(); + + if(NgrokLan.serverOpen){ //kill ngrok tunnel + NgrokLan.LOGGER.info("Closing Ngrok LAN, but world is open"); + NgrokLan.ngrokClient.kill(); + NgrokLan.serverOpen = false; + } + mc.inGameHud.getChatHud().addMessage(Text.translatable("text.info.ngroklan.closeMessage").formatted(Formatting.YELLOW)); + } + +} diff --git a/remappedSrc/alujjdnd/ngrok/lan/mixin/IntegratedServerAccessor.java b/remappedSrc/alujjdnd/ngrok/lan/mixin/IntegratedServerAccessor.java new file mode 100644 index 0000000..6daadcc --- /dev/null +++ b/remappedSrc/alujjdnd/ngrok/lan/mixin/IntegratedServerAccessor.java @@ -0,0 +1,15 @@ +package alujjdnd.ngrok.lan.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; +import net.minecraft.client.network.LanServerPinger; +import net.minecraft.server.integrated.IntegratedServer; + +@Mixin(IntegratedServer.class) +public interface IntegratedServerAccessor { + @Accessor + public void setLanPort(int lanPort); + + @Accessor + public LanServerPinger getLanPinger(); +} \ No newline at end of file diff --git a/remappedSrc/alujjdnd/ngrok/lan/mixin/OpenToLanScreenMixin.java b/remappedSrc/alujjdnd/ngrok/lan/mixin/OpenToLanScreenMixin.java index b489f9a..6de1acb 100644 --- a/remappedSrc/alujjdnd/ngrok/lan/mixin/OpenToLanScreenMixin.java +++ b/remappedSrc/alujjdnd/ngrok/lan/mixin/OpenToLanScreenMixin.java @@ -14,8 +14,9 @@ import net.minecraft.client.gui.screen.OpenToLanScreen; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.ButtonWidget; -import net.minecraft.client.util.NetworkUtils; -import net.minecraft.server.ServerConfigHandler; +import net.minecraft.server.OperatorList; +import net.minecraft.server.PlayerManager; +import net.minecraft.server.Whitelist; import net.minecraft.text.*; import net.minecraft.util.Formatting; import net.minecraft.world.GameMode; @@ -37,6 +38,9 @@ public class OpenToLanScreenMixin extends Screen { @Shadow private boolean allowCommands; + @Shadow + private int port; + protected OpenToLanScreenMixin(Text title) { super(title); } @@ -45,39 +49,37 @@ protected OpenToLanScreenMixin(Text title) { private void initWidgets(CallbackInfo info) { if (config.enabledCheckBox) { //if mod enabled in mod menu - this.addDrawableChild(new ButtonWidget(this.width / 2 - 155, this.height - 58, 150, 20, new TranslatableText("text.UI.ngroklan.LanButton"), (button) -> { - int localPort = NetworkUtils.findLocalPort(); // part of the minecraft Networkutils class, finds an available local port (this was from the openToLan class) + this.addDrawableChild(ButtonWidget.builder(Text.translatable("text.UI.ngroklan.LanButton"), (button) -> { + //localport was removed since OpenToLanScreen.class contains a new port variable that is automatically updated, I am shadowing it this.client.setScreen(null); // Removed all elements from the screen (this closes all menu windows) switch (config.regionSelect) { - case EU -> ngrokInit(localPort, Region.EU); - case AP -> ngrokInit(localPort, Region.AP); - case AU -> ngrokInit(localPort, Region.AU); - case SA -> ngrokInit(localPort, Region.SA); - case JP -> ngrokInit(localPort, Region.JP); - case IN -> ngrokInit(localPort, Region.IN); - default -> ngrokInit(localPort, Region.US); //US bundled here + case EU -> ngrokInit(port, Region.EU); + case AP -> ngrokInit(port, Region.AP); + case AU -> ngrokInit(port, Region.AU); + case SA -> ngrokInit(port, Region.SA); + case JP -> ngrokInit(port, Region.JP); + case IN -> ngrokInit(port, Region.IN); + default -> ngrokInit(port, Region.US); //US bundled here } - })); - + }).dimensions(this.width / 2 - 155, this.height - 52, 150, 20).build()); } } private void ngrokInit(int port, Region region) { - //Defines a new threaded function to oepn the Ngrok tunnel, so that the "Open to LAN" button does not hitch - this thread runs in a seperate process from the main game loop + //Defines a new threaded function to open the Ngrok tunnel, so that the "Open to LAN" button does not hitch - this thread runs in a seperate process from the main game loop Thread thread = new Thread(() -> { if (config.authToken.equals("AuthToken")) { // Check if authToken field has actually been changed, if not, print this text in chat - mc.inGameHud.getChatHud().addMessage(new TranslatableText("text.error.ngroklan.AuthTokenError")); - mc.inGameHud.getChatHud().addMessage(new TranslatableText("text.error.ngroklan.AuthTokenError").formatted(Formatting.RED)); + mc.inGameHud.getChatHud().addMessage(Text.translatable("text.error.ngroklan.AuthTokenError").formatted(Formatting.RED)); //\u00a7c + NgrokLan.LOGGER.error("Launched Lan UNSUCCESSFUL"); } else { try { NgrokLan.LOGGER.info("Launched Lan!"); - - mc.inGameHud.getChatHud().addMessage(new TranslatableText("text.info.ngroklan.startMessage").formatted(Formatting.YELLOW)); + mc.inGameHud.getChatHud().addMessage(Text.translatable("text.info.ngroklan.startMessage").formatted(Formatting.YELLOW)); // Java-ngrok wrapper code, to initiate the tunnel, with the authoken, region @@ -102,54 +104,50 @@ private void ngrokInit(int port, Region region) { var ngrok_url = tunnel.getPublicUrl().substring(6); // Print in chat the status of the tunnel, and the details copied to the clipboard - mc.inGameHud.getChatHud().addMessage(new TranslatableText("text.info.ngroklan.success").formatted(Formatting.GREEN)); + mc.inGameHud.getChatHud().addMessage(Text.translatable("text.info.ngroklan.success").formatted(Formatting.GREEN)); - Text copyText = Texts.bracketed((new LiteralText(ngrok_url)).styled((style) -> style.withColor(Formatting.YELLOW).withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, ngrok_url)).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TranslatableText("chat.copy.click"))).withInsertion(ngrok_url))); - mc.inGameHud.getChatHud().addMessage( new TranslatableText("text.info.ngroklan.ip", copyText)); + Text copyText = Texts.bracketed((Text.translatable(ngrok_url)).styled((style) -> style.withColor(Formatting.YELLOW).withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, ngrok_url)).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.translatable("chat.copy.click"))).withInsertion(ngrok_url))); + mc.inGameHud.getChatHud().addMessage(Text.translatable("text.info.ngroklan.ip", copyText)); mc.keyboard.setClipboard(ngrok_url); // This starts the LAN server and greys out the open to lan button - TranslatableText text; - + MutableText textStart; if (this.client.getServer().openToLan(this.gameMode, this.allowCommands, port)) { - mc.getServer().setOnlineMode(config.onlineCheckBox); - text = new TranslatableText("commands.publish.started", port); + mc.getServer().setOnlineMode(config.onlineCheckBox); //online mode (player auth) + textStart = Text.translatable("commands.publish.started", port); NgrokLan.serverOpen = true; - //TODO: make sure this works, I make a new thread that reads the json files to update the oplist and whitelist in the playermanager + //new thread that reads the json files to update the oplist and whitelist in the playermanager Thread thread2 = new Thread(() -> { - NgrokLan.LOGGER.info("debug THREAD2 STARTED"); - - boolean result = loadJson(); - if(!result){ - Text commandText = Texts.bracketed((new TranslatableText("text.info.ngroklan.reload.prompt")).styled((style) -> style.withColor(Formatting.YELLOW).withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/reloadngroklanlists")).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TranslatableText("text.info.ngroklan.reload.prompt"))))); - mc.inGameHud.getChatHud().addMessage( new TranslatableText("text.info.ngroklan.reload.message")); - mc.inGameHud.getChatHud().addMessage(commandText); + boolean loaded = loadJson(); + if(loaded){ + MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(Text.translatable("text.info.ngroklan.reload.success").styled(style -> style.withColor(Formatting.GREEN) )); } else{ - mc.inGameHud.getChatHud().addMessage( new TranslatableText("text.info.ngroklan.reload.success")); + Text commandText = Texts.bracketed((Text.translatable("text.info.ngroklan.reload.prompt")).styled((style) -> style.withColor(Formatting.RED).withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/reloadngroklanlists")).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.translatable("text.info.ngroklan.reload.prompt"))))); + mc.inGameHud.getChatHud().addMessage(Text.translatable("text.info.ngroklan.reload.message")); + mc.inGameHud.getChatHud().addMessage(commandText); } }); - NgrokLan.LOGGER.info("debug THREAD2 STARTING"); thread2.start(); } else { - text = new TranslatableText("commands.publish.failed"); + textStart = Text.translatable("commands.publish.failed"); NgrokLan.serverOpen = false; } - this.client.inGameHud.getChatHud().addMessage(text); + this.client.inGameHud.getChatHud().addMessage(textStart); this.client.updateWindowTitle(); } catch (Exception error) { error.printStackTrace(); - mc.inGameHud.getChatHud().addMessage(new LiteralText(error.getMessage())); - mc.inGameHud.getChatHud().addMessage(new TranslatableText("text.error.ngroklan.fail").formatted(Formatting.RED)); + mc.inGameHud.getChatHud().addMessage(Text.literal(error.getMessage())); + mc.inGameHud.getChatHud().addMessage(Text.translatable("text.error.ngroklan.fail").formatted(Formatting.RED)); //ngrokInitiated = false; throw new RuntimeException("Ngrok Service Failed to Start" + error.getMessage()); } @@ -162,41 +160,23 @@ private void ngrokInit(int port, Region region) { } private boolean loadJson(){ - NgrokLan.LOGGER.info("debug LOADING JSON"); - - int i; - boolean bl3 = false; + PlayerManager playerManager = this.client.getServer().getPlayerManager(); - for(i = 0; !bl3 && i <= 2; ++i) { - if (i > 0) { - NgrokLan.LOGGER.warn("Encountered a problem while converting the op list, retrying in a few seconds"); - this.sleepFiveSeconds(); - } - - bl3 = ServerConfigHandler.convertOperators(this.client.getServer()); - //fail is false - } + Whitelist whitelist = playerManager.getWhitelist(); + OperatorList opList = playerManager.getOpList(); - boolean bl4 = false; + try { + whitelist.load(); + opList.load(); - for(i = 0; !bl4 && i <= 2; ++i) { - if (i > 0) { - NgrokLan.LOGGER.warn("Encountered a problem while converting the whitelist, retrying in a few seconds"); - this.sleepFiveSeconds(); - } - bl4 = ServerConfigHandler.convertWhitelist(this.client.getServer()); - //fail is false + } catch (Exception e) { + return false; } - return bl3 && bl4; + return true; } - private void sleepFiveSeconds() { - try { - Thread.sleep(5000L); - } catch (InterruptedException var2) { - } - } + } diff --git a/remappedSrc/alujjdnd/ngrok/lan/mixin/WindowTitleMixin.java b/remappedSrc/alujjdnd/ngrok/lan/mixin/WindowTitleMixin.java index ba7bcd5..69f0c79 100644 --- a/remappedSrc/alujjdnd/ngrok/lan/mixin/WindowTitleMixin.java +++ b/remappedSrc/alujjdnd/ngrok/lan/mixin/WindowTitleMixin.java @@ -1,6 +1,7 @@ package alujjdnd.ngrok.lan.mixin; import alujjdnd.ngrok.lan.NgrokLan; +import net.minecraft.SharedConstants; import net.minecraft.client.MinecraftClient; import net.minecraft.client.resource.language.I18n; import org.spongepowered.asm.mixin.Mixin; @@ -12,9 +13,10 @@ public abstract class WindowTitleMixin { @Inject(method = "getWindowTitle", at = @At("HEAD"), cancellable = true) private void injected(CallbackInfoReturnable cir) { - StringBuilder stringBuilder2 = new StringBuilder(I18n.translate("text.title.ngroklan.window")); - if(NgrokLan.serverOpen){ + StringBuilder stringBuilder2 = new StringBuilder("Minecraft* "); + stringBuilder2.append(SharedConstants.getGameVersion().getName()); + stringBuilder2.append(I18n.translate("text.title.ngroklan.window")); cir.setReturnValue(stringBuilder2.toString()); } diff --git a/src/main/java/alujjdnd/ngrok/lan/NgrokLan.java b/src/main/java/alujjdnd/ngrok/lan/NgrokLan.java index efde24c..6986401 100644 --- a/src/main/java/alujjdnd/ngrok/lan/NgrokLan.java +++ b/src/main/java/alujjdnd/ngrok/lan/NgrokLan.java @@ -14,9 +14,6 @@ import me.shedaniel.autoconfig.AutoConfig; import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer; - - - public class NgrokLan implements ModInitializer { // This logger is used to write text to the console and the log file. // It is considered best practice to use your mod id as the logger's name. diff --git a/src/main/java/alujjdnd/ngrok/lan/command/LanWhitelistCommand.java b/src/main/java/alujjdnd/ngrok/lan/command/LanWhitelistCommand.java index 95209f2..fd14406 100644 --- a/src/main/java/alujjdnd/ngrok/lan/command/LanWhitelistCommand.java +++ b/src/main/java/alujjdnd/ngrok/lan/command/LanWhitelistCommand.java @@ -72,7 +72,9 @@ private static int executeAdd(ServerCommandSource source, Collection Text.translatable("commands.whitelist.add.success", Texts.toText(gameProfile)), true); + source.sendFeedback(() -> { + return Text.translatable("commands.whitelist.add.success", new Object[]{Text.literal(gameProfile.getName())}); + }, true); ++i; } } @@ -92,7 +94,9 @@ private static int executeRemove(ServerCommandSource source, Collection Text.translatable("commands.whitelist.remove.success", Texts.toText(gameProfile)), true); + source.sendFeedback(() -> { + return Text.translatable("commands.whitelist.remove.success", new Object[]{Text.literal(gameProfile.getName())}); + }, true); ++i; } } @@ -100,7 +104,7 @@ private static int executeRemove(ServerCommandSource source, Collection=0.14.21", "fabric": "*", - "minecraft": "~1.20.1", + "minecraft": "1.20.2", "java": ">=17" }, "suggests": {