diff --git a/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java b/src/main/java/de/florianmichael/viafabricplus/fixes/ClientsideFixes.java index a3590999..65dd01a3 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,23 @@ public class ClientsideFixes { } } + /** + * Replaces the default port when parsing a server address if the default port should be replaced + * + * @param address The original address of the server + * @param version The protocol version + * @return The server address with the replaced default port + */ + public static ServerAddress replaceDefaultPort(final String address, final ProtocolVersion version) { + // 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() && Objects.equals(version, BedrockProtocolVersion.bedrockLatest) && !address.contains(":")) { + return ServerAddress.parse(address + ":" + 19132); + } else { + return ServerAddress.parse(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..b75deba1 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,22 @@ package de.florianmichael.viafabricplus.injection.mixin.base.integration; +import com.llamalad7.mixinextras.sugar.Local; +import de.florianmichael.viafabricplus.fixes.ClientsideFixes; +import de.florianmichael.viafabricplus.injection.access.IServerInfo; +import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; 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 +56,15 @@ 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) { + if (((IServerInfo) entry).viaFabricPlus$passedDirectConnectScreen()) { + // If the user has already passed the direct connect screen, we use the target version + return ClientsideFixes.replaceDefaultPort(address, ProtocolTranslator.getTargetVersion()); + } else { + // Otherwise the forced version is used + return ClientsideFixes.replaceDefaultPort(address, ((IServerInfo) entry).viaFabricPlus$forcedVersion()); + } + } + } 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..99ba5e6c 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,14 @@ 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) { + // Replace port when pinging the server and the forced version is set + return ClientsideFixes.replaceDefaultPort(address, ((IServerInfo) entry).viaFabricPlus$forcedVersion()); + } + @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