From 70347779c2d2345b7c3c574ae5dd5f2ec4508370 Mon Sep 17 00:00:00 2001 From: Sekwah Date: Fri, 19 Oct 2018 04:26:54 +0100 Subject: [PATCH] Making network manager --- .../advancedportals/compat/CraftBukkit.java | 14 ++----------- .../injector/PacketInjector.java | 20 ++++++++++++++++++- .../reflection/ReflectionHelper.java | 9 +++++++++ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/sekwah/advancedportals/compat/CraftBukkit.java b/src/main/java/com/sekwah/advancedportals/compat/CraftBukkit.java index 19134f65..fcef78da 100644 --- a/src/main/java/com/sekwah/advancedportals/compat/CraftBukkit.java +++ b/src/main/java/com/sekwah/advancedportals/compat/CraftBukkit.java @@ -54,7 +54,7 @@ public class CraftBukkit { this.plugin.getLogger().info("Bukkit version detected " + bukkitImpl); Class chatBaseComponent = Class.forName(minecraftPackage + "IChatBaseComponent"); // string to packet methods - Class chatSerialClass = this.findClass(chatBaseComponent, "ChatSerializer"); + Class chatSerialClass = ReflectionHelper.findClass(chatBaseComponent, "ChatSerializer"); Class chatMessageTypeClass = Class.forName(minecraftPackage + "ChatMessageType"); @@ -92,7 +92,7 @@ public class CraftBukkit { plugin.getLogger().warning("Attempting to use backup porekit locations"); // Fall back on your Porekit Class textBaseComponent = Class.forName("net.minecraft.util.text.ITextComponent"); // string to packet methods - this.serializeMessage = this.findClass(textBaseComponent, "Serializer").getMethod("func_150699_a", String.class); // md: jsonToComponent + this.serializeMessage = ReflectionHelper.findClass(textBaseComponent, "Serializer").getMethod("func_150699_a", String.class); // md: jsonToComponent this.chatPacketConstructor = Class.forName("net.minecraft.network.play.server.SPacketChat").getConstructor(textBaseComponent, byte.class); this.playerGetHandle = Class.forName("blue.lapis.pore.impl.entity.PorePlayer").getMethod("getHandle"); @@ -125,16 +125,6 @@ public class CraftBukkit { } - public Class findClass(Class classObj, String className){ - for(Class classes : classObj.getDeclaredClasses()){ - if(classes.getSimpleName().equals(className)){ - return classes; - } - } - return null; - } - - /** * Blocks the beacon from showing * @param block diff --git a/src/main/java/com/sekwah/advancedportals/injector/PacketInjector.java b/src/main/java/com/sekwah/advancedportals/injector/PacketInjector.java index cd74bcbc..6272ff36 100644 --- a/src/main/java/com/sekwah/advancedportals/injector/PacketInjector.java +++ b/src/main/java/com/sekwah/advancedportals/injector/PacketInjector.java @@ -1,14 +1,32 @@ package com.sekwah.advancedportals.injector; import com.sekwah.advancedportals.AdvancedPortalsPlugin; +import com.sekwah.advancedportals.reflection.ReflectionHelper; +import io.netty.channel.Channel; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; public class PacketInjector { + private Method getPlayerHandle; + private Field playerConnection; + private Field networkManager; + private Field networkChannel; + public PacketInjector(AdvancedPortalsPlugin plugin, String bukkitImpl) { String craftBukkitPackage = "org.bukkit.craftbukkit." + bukkitImpl + "."; String minecraftPackage = "net.minecraft.server." + bukkitImpl + "."; - + try { + getPlayerHandle = Class.forName(craftBukkitPackage + "entity.CraftPlayer").getMethod("getHandle"); + playerConnection = Class.forName(minecraftPackage + "EntityPlayer").getField("playerConnection"); + networkManager = Class.forName(minecraftPackage + "PlayerConnection").getField("networkManager"); + networkChannel = ReflectionHelper.getFieldByType(Class.forName(minecraftPackage + "NetworkManager"), Channel.class, true); + + } catch (NoSuchMethodException | ClassNotFoundException | NoSuchFieldException e) { + e.printStackTrace(); + } } } diff --git a/src/main/java/com/sekwah/advancedportals/reflection/ReflectionHelper.java b/src/main/java/com/sekwah/advancedportals/reflection/ReflectionHelper.java index 4a7a8d4d..d0db79d2 100644 --- a/src/main/java/com/sekwah/advancedportals/reflection/ReflectionHelper.java +++ b/src/main/java/com/sekwah/advancedportals/reflection/ReflectionHelper.java @@ -14,4 +14,13 @@ public class ReflectionHelper { } return null; } + + public static Class findClass(Class classObj, String className){ + for(Class classes : classObj.getDeclaredClasses()){ + if(classes.getSimpleName().equals(className)){ + return classes; + } + } + return null; + } }