From f8992718819b4b3af464b42c958a9d805d0ac1f1 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Fri, 22 Mar 2024 05:04:18 +0100 Subject: [PATCH] Added setting to replace the default port when bedrock is selected --- .../viafabricplus/fixes/ClientsideFixes.java | 28 ++++++++++++++++++- .../integration/MixinMultiplayerScreen.java | 10 +++++++ .../MixinMultiplayerServerListPinger.java | 9 +++++- .../settings/impl/BedrockSettings.java | 1 + .../assets/viafabricplus/lang/en_us.json | 1 + src/main/resources/viafabricplus.mixins.json | 4 +-- 6 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java b/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java index a3590999..f4e8ff83 100644 --- a/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java +++ b/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java @@ -28,13 +28,16 @@ import de.florianmichael.viafabricplus.fixes.versioned.classic.CPEAdditions; import de.florianmichael.viafabricplus.fixes.versioned.classic.GridItemSelectionScreen; import de.florianmichael.viafabricplus.fixes.versioned.visual.ArmorHudEmulation1_8; import de.florianmichael.viafabricplus.fixes.versioned.visual.FootStepParticle1_12_2; -import de.florianmichael.viafabricplus.injection.ViaFabricPlusMixinPlugin; import de.florianmichael.viafabricplus.injection.access.IClientConnection; +import de.florianmichael.viafabricplus.injection.access.IServerInfo; import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; +import de.florianmichael.viafabricplus.settings.impl.BedrockSettings; import net.minecraft.block.*; import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.FontStorage; import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.client.network.ServerAddress; +import net.minecraft.client.network.ServerInfo; import net.minecraft.network.PacketByteBuf; import net.minecraft.registry.Registries; import net.raphimc.viabedrock.api.BedrockProtocolVersion; @@ -43,6 +46,7 @@ import net.raphimc.vialegacy.protocols.classic.protocolc0_28_30toc0_28_30cpe.dat import net.raphimc.vialegacy.protocols.classic.protocolc0_28_30toc0_28_30cpe.storage.ExtensionProtocolMetadataStorage; import java.util.Map; +import java.util.Objects; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; @@ -141,6 +145,28 @@ public class ClientsideFixes { } } + /** + * Replaces the default port when parsing a server address if the default port should be replaced + * + * @param info The server info + * @return The server address with the replaced default port + */ + public static ServerAddress replaceDefaultPort(final ServerInfo info) { + ProtocolVersion targetVersion = ((IServerInfo) info).viaFabricPlus$forcedVersion(); + if (targetVersion == null) { + targetVersion = ProtocolTranslator.getTargetVersion(); + } + final boolean isBedrock = Objects.equals(targetVersion, BedrockProtocolVersion.bedrockLatest); + + // If the default port for this entry should be replaced, check if the address already contains a port + // We can't just replace vanilla's default port because a bedrock server might be running on the same port + if (BedrockSettings.global().replaceDefaultPort.getValue() && isBedrock && !info.address.contains(":")) { + return ServerAddress.parse(info.address + ":" + 19132); + } else { + return ServerAddress.parse(info.address); + } + } + /** * Executes a sync task and returns the uuid of the task * diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerScreen.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerScreen.java index ecec0977..08de92ff 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerScreen.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/integration/MixinMultiplayerScreen.java @@ -19,15 +19,20 @@ package de.florianmichael.viafabricplus.injection.mixin.base.integration; +import com.llamalad7.mixinextras.sugar.Local; +import de.florianmichael.viafabricplus.fixes.ClientsideFixes; import de.florianmichael.viafabricplus.screen.base.ProtocolSelectionScreen; import de.florianmichael.viafabricplus.settings.impl.GeneralSettings; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.network.ServerAddress; +import net.minecraft.client.network.ServerInfo; import net.minecraft.text.Text; 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.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(MultiplayerScreen.class) @@ -49,4 +54,9 @@ public abstract class MixinMultiplayerScreen extends Screen { this.addDrawableChild(GeneralSettings.withOrientation(builder, buttonPosition, width, height).build()); } + @Redirect(method = "connect(Lnet/minecraft/client/network/ServerInfo;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ServerAddress;parse(Ljava/lang/String;)Lnet/minecraft/client/network/ServerAddress;")) + private ServerAddress replaceDefaultPort(String address, @Local(argsOnly = true) ServerInfo entry) { + return ClientsideFixes.replaceDefaultPort(entry); + } + } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerServerListPinger.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerServerListPinger.java index 9915af4a..b20f4f8a 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerServerListPinger.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/perserverversion/MixinMultiplayerServerListPinger.java @@ -21,9 +21,11 @@ package de.florianmichael.viafabricplus.injection.mixin.base.perserverversion; import com.llamalad7.mixinextras.sugar.Local; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import de.florianmichael.viafabricplus.fixes.ClientsideFixes; import de.florianmichael.viafabricplus.injection.access.IPerformanceLog; import de.florianmichael.viafabricplus.injection.access.IServerInfo; import net.minecraft.client.network.MultiplayerServerListPinger; +import net.minecraft.client.network.ServerAddress; import net.minecraft.client.network.ServerInfo; import net.minecraft.network.ClientConnection; import net.minecraft.util.profiler.PerformanceLog; @@ -36,8 +38,13 @@ import java.net.InetSocketAddress; @Mixin(MultiplayerServerListPinger.class) public abstract class MixinMultiplayerServerListPinger { + @Redirect(method = "add", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ServerAddress;parse(Ljava/lang/String;)Lnet/minecraft/client/network/ServerAddress;")) + private ServerAddress replaceDefaultPort(String address, @Local(argsOnly = true) ServerInfo entry) { + return ClientsideFixes.replaceDefaultPort(entry); + } + @Redirect(method = "add", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;connect(Ljava/net/InetSocketAddress;ZLnet/minecraft/util/profiler/PerformanceLog;)Lnet/minecraft/network/ClientConnection;")) - private ClientConnection setForcedVersion(InetSocketAddress address, boolean useEpoll, PerformanceLog packetSizeLog, @Local ServerInfo serverInfo) { + private ClientConnection setForcedVersion(InetSocketAddress address, boolean useEpoll, PerformanceLog packetSizeLog, @Local(argsOnly = true) ServerInfo serverInfo) { final ProtocolVersion forcedVersion = ((IServerInfo) serverInfo).viaFabricPlus$forcedVersion(); if (forcedVersion != null && !((IServerInfo) serverInfo).viaFabricPlus$passedDirectConnectScreen()) { diff --git a/src/main/java/de/florianmichael/viafabricplus/settings/impl/BedrockSettings.java b/src/main/java/de/florianmichael/viafabricplus/settings/impl/BedrockSettings.java index c8cf776e..c53a34bb 100644 --- a/src/main/java/de/florianmichael/viafabricplus/settings/impl/BedrockSettings.java +++ b/src/main/java/de/florianmichael/viafabricplus/settings/impl/BedrockSettings.java @@ -55,6 +55,7 @@ public class BedrockSettings extends SettingGroup { } }; public final BooleanSetting openPromptGUIToConfirmTransfer = new BooleanSetting(this, Text.translatable("bedrock_settings.viafabricplus.confirm_transfer_server_prompt"), true); + public final BooleanSetting replaceDefaultPort = new BooleanSetting(this, Text.translatable("bedrock_settings.viafabricplus.replace_default_port"), true); public BedrockSettings() { super(Text.translatable("setting_group_name.viafabricplus.bedrock")); diff --git a/src/main/resources/assets/viafabricplus/lang/en_us.json b/src/main/resources/assets/viafabricplus/lang/en_us.json index 8ef6de8c..05934ab2 100644 --- a/src/main/resources/assets/viafabricplus/lang/en_us.json +++ b/src/main/resources/assets/viafabricplus/lang/en_us.json @@ -48,6 +48,7 @@ "bedrock_settings.viafabricplus.confirm_transfer_server_prompt": "Open prompt GUI to confirm transferring to other servers", "bedrock_settings.viafabricplus.click_to_set_bedrock_account": "Click to set account for Bedrock edition", + "bedrock_settings.viafabricplus.replace_default_port": "Replace default port in server list", "debug_settings.viafabricplus.queue_config_packets": "Queue config packets", "debug_settings.viafabricplus.disable_sequencing": "Disable sequencing", diff --git a/src/main/resources/viafabricplus.mixins.json b/src/main/resources/viafabricplus.mixins.json index 0b8368e0..f683f3e2 100644 --- a/src/main/resources/viafabricplus.mixins.json +++ b/src/main/resources/viafabricplus.mixins.json @@ -129,6 +129,7 @@ "fixes.minecraft.item.MixinItemPlacementContext", "fixes.minecraft.item.MixinItemRenderer", "fixes.minecraft.item.MixinItemStack", + "fixes.minecraft.item.MixinMiningToolItem", "fixes.minecraft.item.MixinPickaxeItem", "fixes.minecraft.item.MixinShearsItem", "fixes.minecraft.item.MixinShovelItem", @@ -199,8 +200,7 @@ "vialegacy.MixinExtensionProtocolMetadataStorage", "vialegacy.MixinViaLegacyConfig", "viaversion.MixinConfig", - "viaversion.MixinProtocolVersion", - "fixes.minecraft.item.MixinMiningToolItem" + "viaversion.MixinProtocolVersion" ], "injectors": { "defaultRequire": 1