Skip to content

Commit

Permalink
Try to improve mod compatibility with port replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Aug 22, 2024
1 parent 78722e7 commit f998453
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ public static int getChatLength() {
* @param version The protocol version
* @return The server address with the replaced default port
*/
public static ServerAddress replaceDefaultPort(final String address, final ProtocolVersion version) {
public static String 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 + ":" + ProtocolConstants.BEDROCK_DEFAULT_PORT);
return address + ":" + ProtocolConstants.BEDROCK_DEFAULT_PORT;
} else {
return ServerAddress.parse(address);
return address;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public static Vec3d getMountedHeightOffset(final Entity entity, final Entity pas
if (passenger instanceof AnimalEntity) xOffset += 0.2F;
}


return new Vec3d(xOffset, yOffset, 0F).rotateY(-(float) (Math.PI / 2));
}
} else if (entity instanceof CamelEntity camelEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

package de.florianmichael.viafabricplus.injection.mixin.base.integration;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
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.IServerInfo;
import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator;
Expand All @@ -34,7 +37,6 @@
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)
Expand All @@ -56,15 +58,17 @@ private void addProtocolSelectionButton(CallbackInfo ci) {
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());
@WrapOperation(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, Operation<ServerAddress> original, @Local(argsOnly = true) ServerInfo entry) {
final IServerInfo mixinServerInfo = (IServerInfo) entry;

ProtocolVersion version;
if (mixinServerInfo.viaFabricPlus$passedDirectConnectScreen()) {
version = ProtocolTranslator.getTargetVersion();
} else {
// Otherwise the forced version is used
return ClientsideFixes.replaceDefaultPort(address, ((IServerInfo) entry).viaFabricPlus$forcedVersion());
version = mixinServerInfo.viaFabricPlus$forcedVersion();
}
return original.call(ClientsideFixes.replaceDefaultPort(address, version));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@
import net.minecraft.util.profiler.MultiValueDebugSampleLogImpl;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

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) {
@WrapOperation(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, Operation<ServerAddress> original, @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());
return original.call(ClientsideFixes.replaceDefaultPort(address, ((IServerInfo) entry).viaFabricPlus$forcedVersion()));
}

@WrapOperation(method = "add", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;connect(Ljava/net/InetSocketAddress;ZLnet/minecraft/util/profiler/MultiValueDebugSampleLogImpl;)Lnet/minecraft/network/ClientConnection;"))
Expand Down

0 comments on commit f998453

Please sign in to comment.