diff --git a/src/main/java/io/github/gaming32/worldhost/WorldHost.java b/src/main/java/io/github/gaming32/worldhost/WorldHost.java index c1ee8b9..3bb44e3 100644 --- a/src/main/java/io/github/gaming32/worldhost/WorldHost.java +++ b/src/main/java/io/github/gaming32/worldhost/WorldHost.java @@ -17,6 +17,8 @@ import io.github.gaming32.worldhost.upnp.GatewayFinder; import io.github.gaming32.worldhost.versions.Components; import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2IntAVLTreeMap; @@ -37,8 +39,10 @@ import net.minecraft.network.protocol.status.ClientboundStatusResponsePacket; import net.minecraft.network.protocol.status.ServerStatus; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.network.ServerConnectionListener; import net.minecraft.server.players.GameProfileCache; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.exception.UncheckedReflectiveOperationException; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; @@ -53,6 +57,7 @@ import java.io.InputStreamReader; import java.io.IOException; import java.io.UncheckedIOException; +import java.lang.reflect.Constructor; import java.net.InetAddress; import java.net.SocketAddress; import java.net.URI; @@ -226,6 +231,7 @@ public class WorldHost public static boolean shareWorldOnLoad; public static SocketAddress proxySocketAddress; + public static Constructor> channelInitializerConstructor; //#if FABRIC @Override @@ -865,6 +871,14 @@ private static Path getGameDir() { //#endif } + public static ChannelInitializer createChannelInitializer(ServerConnectionListener listener) { + try { + return channelInitializerConstructor.newInstance(listener); + } catch (ReflectiveOperationException e) { + throw new UncheckedReflectiveOperationException(e); + } + } + //#if FORGELIKE //#if MC >= 1.20.5 //$$ @EventBusSubscriber(modid = MOD_ID, bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) diff --git a/src/main/java/io/github/gaming32/worldhost/mixin/MixinServerConnectionListener_1.java b/src/main/java/io/github/gaming32/worldhost/mixin/MixinServerConnectionListener_1.java new file mode 100644 index 0000000..ba5b932 --- /dev/null +++ b/src/main/java/io/github/gaming32/worldhost/mixin/MixinServerConnectionListener_1.java @@ -0,0 +1,20 @@ +package io.github.gaming32.worldhost.mixin; + +import io.github.gaming32.worldhost.WorldHost; +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import net.minecraft.server.network.ServerConnectionListener; +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; + +@Mixin(targets = "net.minecraft.server.network.ServerConnectionListener$1") +public abstract class MixinServerConnectionListener_1 extends ChannelInitializer { + @Inject(method = "", at = @At("TAIL")) + private void storeClass(ServerConnectionListener this$0, CallbackInfo ci) throws NoSuchMethodException { + if (WorldHost.channelInitializerConstructor == null) { + WorldHost.channelInitializerConstructor = getClass().getDeclaredConstructor(ServerConnectionListener.class); + } + } +} diff --git a/src/main/java/io/github/gaming32/worldhost/proxy/ProxyChannels.java b/src/main/java/io/github/gaming32/worldhost/proxy/ProxyChannels.java index 88f235e..dad266a 100644 --- a/src/main/java/io/github/gaming32/worldhost/proxy/ProxyChannels.java +++ b/src/main/java/io/github/gaming32/worldhost/proxy/ProxyChannels.java @@ -1,20 +1,12 @@ package io.github.gaming32.worldhost.proxy; +import io.github.gaming32.worldhost.WorldHost; import io.github.gaming32.worldhost.mixin.ServerConnectionListenerAccessor; import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelPipeline; import io.netty.channel.local.LocalAddress; import io.netty.channel.local.LocalServerChannel; -import io.netty.handler.timeout.ReadTimeoutHandler; -import net.minecraft.network.Connection; -import net.minecraft.network.RateKickingConnection; -import net.minecraft.network.protocol.PacketFlow; -import net.minecraft.server.network.LegacyQueryHandler; import net.minecraft.server.network.ServerConnectionListener; -import net.minecraft.server.network.ServerHandshakePacketListenerImpl; import java.net.SocketAddress; @@ -25,25 +17,7 @@ public static SocketAddress startProxyChannel(ServerConnectionListener listener) synchronized (accessor.getChannels()) { channel = new ServerBootstrap() .channel(LocalServerChannel.class) - .childHandler(new ChannelInitializer<>() { - @Override - protected void initChannel(Channel ch) { - final ChannelPipeline pipeline = ch.pipeline().addLast("timeout", new ReadTimeoutHandler(30)); - if (listener.getServer().repliesToStatus()) { - pipeline.addLast("legacy_query", new LegacyQueryHandler(listener.getServer())); - } - Connection.configureSerialization(pipeline, PacketFlow.SERVERBOUND, false, null); - final int rateLimit = listener.getServer().getRateLimitPacketsPerSecond(); - final Connection connection = rateLimit > 0 - ? new RateKickingConnection(rateLimit) - : new Connection(PacketFlow.SERVERBOUND); - listener.getConnections().add(connection); - connection.configurePacketHandler(pipeline); - connection.setListenerForServerboundHandshake( - new ServerHandshakePacketListenerImpl(listener.getServer(), connection) - ); - } - }) + .childHandler(WorldHost.createChannelInitializer(listener)) .group(ServerConnectionListener.SERVER_EVENT_GROUP.get()) .localAddress(LocalAddress.ANY) .bind() diff --git a/src/main/resources/world-host.mixins.json b/src/main/resources/world-host.mixins.json index 8870706..6bc25e5 100644 --- a/src/main/resources/world-host.mixins.json +++ b/src/main/resources/world-host.mixins.json @@ -7,6 +7,7 @@ "MixinCommands", "MixinLevelSummary", "MixinPublishCommand", + "MixinServerConnectionListener_1", "ServerConnectionListenerAccessor" ], "client": [