diff --git a/patches/minecraft/net/minecraft/client/gui/screens/ConnectScreen.java.patch b/patches/minecraft/net/minecraft/client/gui/screens/ConnectScreen.java.patch new file mode 100644 index 0000000000..b7e970fe98 --- /dev/null +++ b/patches/minecraft/net/minecraft/client/gui/screens/ConnectScreen.java.patch @@ -0,0 +1,11 @@ +--- a/net/minecraft/client/gui/screens/ConnectScreen.java ++++ b/net/minecraft/client/gui/screens/ConnectScreen.java +@@ -72,6 +_,8 @@ + } + + if (!optional.isPresent()) { ++ ConnectScreen.f_95683_.error("Couldn't connect to server: Unknown host \"{}\"", p_249536_.m_171863_()); ++ net.minecraftforge.network.DualStackUtils.logInitialPreferences(); + p_251955_.execute(() -> { + p_251955_.m_91152_(new DisconnectedScreen(ConnectScreen.this.f_95686_, CommonComponents.f_130661_, ConnectScreen.f_169260_)); + }); diff --git a/src/main/java/net/minecraftforge/common/MinecraftForge.java b/src/main/java/net/minecraftforge/common/MinecraftForge.java index 352c3abaf8..02af3eb431 100644 --- a/src/main/java/net/minecraftforge/common/MinecraftForge.java +++ b/src/main/java/net/minecraftforge/common/MinecraftForge.java @@ -10,6 +10,7 @@ import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.eventbus.api.BusBuilder; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.loading.FMLEnvironment; +import net.minecraftforge.network.DualStackUtils; import net.minecraftforge.versions.forge.ForgeVersion; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -42,6 +43,7 @@ public class MinecraftForge UsernameCache.load(); TierSortingRegistry.init(); if (FMLEnvironment.dist == Dist.CLIENT) ClientCommandHandler.init(); + DualStackUtils.initialise(); } /* diff --git a/src/main/java/net/minecraftforge/network/DualStackUtils.java b/src/main/java/net/minecraftforge/network/DualStackUtils.java index bec3e72a62..1786b7e259 100644 --- a/src/main/java/net/minecraftforge/network/DualStackUtils.java +++ b/src/main/java/net/minecraftforge/network/DualStackUtils.java @@ -5,12 +5,16 @@ package net.minecraftforge.network; +import com.mojang.logging.LogUtils; import net.minecraft.client.multiplayer.resolver.ResolvedServerAddress; import net.minecraft.client.multiplayer.resolver.ServerAddress; import net.minecraft.client.multiplayer.resolver.ServerNameResolver; import net.minecraft.util.HttpUtil; +import org.jetbrains.annotations.ApiStatus; +import org.slf4j.Logger; import javax.annotation.Nullable; +import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -19,6 +23,19 @@ import java.util.Optional; public class DualStackUtils { + private static final String INITIAL_PREFER_IPv4_STACK = System.getProperty("java.net.preferIPv4Stack") == null ? "false" : System.getProperty("java.net.preferIPv4Stack"); + private static final String INITIAL_PREFER_IPv6_ADDRESSES = System.getProperty("java.net.preferIPv6Addresses") == null ? "false" : System.getProperty("java.net.preferIPv6Addresses"); + + private static final Logger LOGGER = LogUtils.getLogger(); + + /** + * Called by {@link net.minecraftforge.common.MinecraftForge} to load this class so that the initial network + * property constants are set before any of the other methods in this class are called. This is so we can + * distinguish what Java's read once on JVM start vs what we've set for Netty. + */ + @ApiStatus.Internal + public static void initialise() {} + /** * Resolve the address and see if Java and the OS return an IPv6 or IPv4 one, then let Netty know * accordingly (it doesn't understand the {@code java.net.preferIPv6Addresses=system} property). @@ -45,14 +62,49 @@ public class DualStackUtils */ public static boolean checkIPv6(final InetAddress inetAddress) { - if (inetAddress instanceof Inet6Address) + // only log debug messages if we're not in the server pinger thread, as otherwise it's unclear which IP + // corresponds to which server as soon as you have more than one server in the multiplayer server list + final String currentThreadName = Thread.currentThread().getName(); + final boolean shouldLogDebug = !currentThreadName.contains("Server Pinger #"); + + if (inetAddress instanceof Inet6Address addr) { + if (shouldLogDebug) + LOGGER.debug("Detected IPv6 address: \"" + addr.getHostAddress() + "\""); + System.setProperty("java.net.preferIPv4Stack", "false"); System.setProperty("java.net.preferIPv6Addresses", "true"); return true; } + else if (inetAddress instanceof Inet4Address addr) + { + if (shouldLogDebug) + LOGGER.debug("Detected IPv4 address: \"" + addr.getHostAddress() + "\""); + + System.setProperty("java.net.preferIPv4Stack", "true"); + System.setProperty("java.net.preferIPv6Addresses", "false"); + return false; + } else { + if (shouldLogDebug) { + final String addr = inetAddress == null ? "null" : "\"" + inetAddress.getHostAddress() + "\""; + LOGGER.debug("Unable to determine IP version of address: " + addr); + } + + if (INITIAL_PREFER_IPv4_STACK.equalsIgnoreCase("false") && INITIAL_PREFER_IPv6_ADDRESSES.equalsIgnoreCase("true")) + { + if (shouldLogDebug) + LOGGER.debug("Assuming IPv6 as Java was explicitly told to prefer it..."); + + System.setProperty("java.net.preferIPv4Stack", "false"); + System.setProperty("java.net.preferIPv6Addresses", "true"); + return true; + } + + if (shouldLogDebug) + LOGGER.debug("Assuming IPv4..."); + System.setProperty("java.net.preferIPv4Stack", "true"); System.setProperty("java.net.preferIPv6Addresses", "false"); return false; @@ -81,8 +133,21 @@ public class DualStackUtils } } + /** + * Used for the "Open to LAN" feature. + * @return The multicast group to use for LAN discovery - IPv6 if available, IPv4 otherwise. + */ public static String getMulticastGroup() { if (checkIPv6(getLocalAddress())) return "FF75:230::60"; else return "224.0.2.60"; } + + /** + * Logs the initial values of the {@code java.net.preferIPv4Stack} and {@code java.net.preferIPv6Addresses} system + * properties that Java has read on JVM start. Useful for debugging hostname lookup failures. + */ + public static void logInitialPreferences() { + LOGGER.debug("Initial IPv4 stack preference: " + INITIAL_PREFER_IPv4_STACK); + LOGGER.debug("Initial IPv6 addresses preference: " + INITIAL_PREFER_IPv6_ADDRESSES); + } }