Skip to content

Commit

Permalink
Call Mojang's code instead of copying it
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 12, 2024
1 parent a0e4689 commit a73a96b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
14 changes: 14 additions & 0 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -226,6 +231,7 @@ 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,6 +871,14 @@ private static Path getGameDir() {
//#endif
}

public static ChannelInitializer<Channel> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/world-host.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"MixinCommands",
"MixinLevelSummary",
"MixinPublishCommand",
"MixinServerConnectionListener_1",
"ServerConnectionListenerAccessor"
],
"client": [
Expand Down

0 comments on commit a73a96b

Please sign in to comment.