|
9 | 9 | import io.netty.channel.ChannelPipeline;
|
10 | 10 | import io.netty.channel.ChannelPromise;
|
11 | 11 |
|
| 12 | +import java.lang.reflect.Field; |
12 | 13 | import java.util.Collections;
|
13 | 14 | import java.util.List;
|
14 | 15 | import java.util.Map;
|
|
44 | 45 | public abstract class TinyProtocol {
|
45 | 46 | private static final AtomicInteger ID = new AtomicInteger(0);
|
46 | 47 |
|
| 48 | + // Required Minecraft classes |
| 49 | + private static final Class<?> entityPlayerClass = Reflection.getClass("{nms}.EntityPlayer", "net.minecraft.server.level.EntityPlayer"); |
| 50 | + private static final Class<?> playerConnectionClass = Reflection.getClass("{nms}.PlayerConnection", "net.minecraft.server.network.PlayerConnection"); |
| 51 | + private static final Class<?> networkManagerClass = Reflection.getClass("{nms}.NetworkManager", "net.minecraft.network.NetworkManager"); |
| 52 | + |
47 | 53 | // Used in order to lookup a channel
|
48 | 54 | private static final MethodInvoker getPlayerHandle = Reflection.getMethod("{obc}.entity.CraftPlayer", "getHandle");
|
49 |
| - private static final FieldAccessor<Object> getConnection = Reflection.getField("{nms}.EntityPlayer", "playerConnection", Object.class); |
50 |
| - private static final FieldAccessor<Object> getManager = Reflection.getField("{nms}.PlayerConnection", "networkManager", Object.class); |
51 |
| - private static final FieldAccessor<Channel> getChannel = Reflection.getField("{nms}.NetworkManager", Channel.class, 0); |
| 55 | + private static final FieldAccessor<?> getConnection = Reflection.getField(entityPlayerClass, null, playerConnectionClass); |
| 56 | + private static final FieldAccessor<?> getManager = Reflection.getField(playerConnectionClass, null, networkManagerClass); |
| 57 | + private static final FieldAccessor<Channel> getChannel = Reflection.getField(networkManagerClass, Channel.class, 0); |
52 | 58 |
|
53 | 59 | // Looking up ServerConnection
|
54 |
| - private static final Class<Object> minecraftServerClass = Reflection.getUntypedClass("{nms}.MinecraftServer"); |
55 |
| - private static final Class<Object> serverConnectionClass = Reflection.getUntypedClass("{nms}.ServerConnection"); |
| 60 | + private static final Class<Object> minecraftServerClass = Reflection.getUntypedClass("{nms}.MinecraftServer", "net.minecraft.server.MinecraftServer"); |
| 61 | + private static final Class<Object> serverConnectionClass = Reflection.getUntypedClass("{nms}.ServerConnection", "net.minecraft.server.network.ServerConnection"); |
56 | 62 | private static final FieldAccessor<Object> getMinecraftServer = Reflection.getField("{obc}.CraftServer", minecraftServerClass, 0);
|
57 | 63 | private static final FieldAccessor<Object> getServerConnection = Reflection.getField(minecraftServerClass, serverConnectionClass, 0);
|
58 |
| - private static final MethodInvoker getNetworkMarkers = Reflection.getTypedMethod(serverConnectionClass, null, List.class, serverConnectionClass); |
59 | 64 |
|
60 | 65 | // Packets we have to intercept
|
61 |
| - private static final Class<?> PACKET_LOGIN_IN_START = Reflection.getMinecraftClass("PacketLoginInStart"); |
| 66 | + private static final Class<?> PACKET_LOGIN_IN_START = Reflection.getClass("{nms}.PacketLoginInStart", "net.minecraft.network.protocol.login.PacketLoginInStart"); |
62 | 67 | private static final FieldAccessor<GameProfile> getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START, GameProfile.class, 0);
|
63 | 68 |
|
64 | 69 | // Speedup channel lookup
|
@@ -199,8 +204,22 @@ private void registerChannelHandler() {
|
199 | 204 | Object serverConnection = getServerConnection.get(mcServer);
|
200 | 205 | boolean looking = true;
|
201 | 206 |
|
| 207 | + try { |
| 208 | + Field field = Reflection.getParameterizedField(serverConnectionClass, List.class, networkManagerClass); |
| 209 | + field.setAccessible(true); |
| 210 | + |
| 211 | + networkManagers = (List<Object>) field.get(serverConnection); |
| 212 | + } catch (Exception ex) { |
| 213 | + plugin.getLogger().info("Encountered an exception checking list fields" + ex); |
| 214 | + MethodInvoker method = Reflection.getTypedMethod(serverConnectionClass, null, List.class, serverConnectionClass); |
| 215 | + |
| 216 | + networkManagers = (List<Object>) method.invoke(null, serverConnection); |
| 217 | + } |
| 218 | + |
| 219 | + if (networkManagers == null) { |
| 220 | + throw new IllegalArgumentException("Failed to obtain list of network managers"); |
| 221 | + } |
202 | 222 | // We need to synchronize against this list
|
203 |
| - networkManagers = (List<Object>) getNetworkMarkers.invoke(null, serverConnection); |
204 | 223 | createServerChannelHandler();
|
205 | 224 |
|
206 | 225 | // Find the correct list, or implicitly throw an exception
|
|
0 commit comments