Skip to content

Commit

Permalink
Move channelInitializerConstructor and createChannelInitializer to Pr…
Browse files Browse the repository at this point in the history
…oxyChannels
  • Loading branch information
Gaming32 committed Jun 12, 2024
1 parent 377924f commit 5756141
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
14 changes: 0 additions & 14 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
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.Long2ObjectMaps;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
Expand All @@ -43,7 +41,6 @@
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.io.function.IOFunction;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -62,7 +59,6 @@
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;
Expand Down Expand Up @@ -218,7 +214,6 @@ public class WorldHost
public static boolean shareWorldOnLoad;

public static SocketAddress proxySocketAddress;
public static Constructor<? extends ChannelInitializer<Channel>> channelInitializerConstructor;

//#if FABRIC
@Override
Expand Down Expand Up @@ -865,15 +860,6 @@ private static Path getGameDir() {
//#endif
}

public static ChannelInitializer<Channel> createChannelInitializer(ServerConnectionListener listener) {
try {
return channelInitializerConstructor.newInstance(listener);
} catch (ReflectiveOperationException e) {
// TODO: UncheckedReflectiveOperationException when 1.20.4+ becomes the minimum
throw new RuntimeException(e);
}
}

//#if FORGELIKE
//#if MC >= 1.20.5
//$$ @EventBusSubscriber(modid = MOD_ID, bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.gaming32.worldhost.mixin;

import io.github.gaming32.worldhost.WorldHost;
import io.github.gaming32.worldhost.proxy.ProxyChannels;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import net.minecraft.server.network.ServerConnectionListener;
Expand All @@ -13,9 +13,9 @@
public abstract class MixinServerConnectionListener_1 extends ChannelInitializer<Channel> {
@Inject(method = "<init>", at = @At("TAIL"))
private void storeClass(ServerConnectionListener this$0, CallbackInfo ci) throws NoSuchMethodException {
if (WorldHost.channelInitializerConstructor == null) {
WorldHost.channelInitializerConstructor = getClass().getDeclaredConstructor(ServerConnectionListener.class);
WorldHost.channelInitializerConstructor.setAccessible(true);
if (ProxyChannels.channelInitializerConstructor == null) {
ProxyChannels.channelInitializerConstructor = getClass().getDeclaredConstructor(ServerConnectionListener.class);
ProxyChannels.channelInitializerConstructor.setAccessible(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
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.local.LocalAddress;
import io.netty.channel.local.LocalServerChannel;
import net.minecraft.server.network.ServerConnectionListener;

import java.lang.reflect.Constructor;
import java.net.SocketAddress;

public class ProxyChannels {
public static Constructor<? extends ChannelInitializer<Channel>> channelInitializerConstructor;

public static SocketAddress startProxyChannel(ServerConnectionListener listener) {
final ServerConnectionListenerAccessor accessor = (ServerConnectionListenerAccessor)listener;
ChannelFuture channel;
synchronized (accessor.getChannels()) {
channel = new ServerBootstrap()
.channel(LocalServerChannel.class)
.childHandler(WorldHost.createChannelInitializer(listener))
.childHandler(createChannelInitializer(listener))
.group(ServerConnectionListener.SERVER_EVENT_GROUP.get())
.localAddress(LocalAddress.ANY)
.bind()
Expand All @@ -26,4 +30,13 @@ public static SocketAddress startProxyChannel(ServerConnectionListener listener)
}
return channel.channel().localAddress();
}

public static ChannelInitializer<Channel> createChannelInitializer(ServerConnectionListener listener) {
try {
return channelInitializerConstructor.newInstance(listener);
} catch (ReflectiveOperationException e) {
// TODO: UncheckedReflectiveOperationException when 1.20.4+ becomes the minimum
throw new RuntimeException(e);
}
}
}

0 comments on commit 5756141

Please sign in to comment.