From 45c28d47c89fbdf9f0be73b3185498d74ac90e7d Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Wed, 5 Dec 2012 23:47:55 +0100 Subject: [PATCH] Create a new dedicated Minecraft reflection class. --- .../protocol/injector/EntityUtilities.java | 3 ++- .../comphenix/protocol/injector/PacketInjector.java | 4 +++- .../protocol/injector/player/PlayerInjector.java | 8 +++----- .../comphenix/protocol/reflect/FuzzyReflection.java | 5 ----- .../protocol/utility/MinecraftReflection.java | 13 +++++++++++++ 5 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java index ef9c9e8c..806c03e1 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/EntityUtilities.java @@ -39,6 +39,7 @@ import org.bukkit.entity.Player; import com.comphenix.protocol.reflect.FieldAccessException; import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.FuzzyReflection; +import com.comphenix.protocol.utility.MinecraftReflection; import com.google.common.collect.Lists; /** @@ -193,7 +194,7 @@ class EntityUtilities { // The Minecraft field that's NOT filled in by the constructor trackedEntitiesField = FuzzyReflection.fromObject(tracker, true). - getFieldByType(FuzzyReflection.MINECRAFT_OBJECT, ignoredTypes); + getFieldByType(MinecraftReflection.MINECRAFT_OBJECT, ignoredTypes); } // Read the entity hashmap diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketInjector.java index 8d3374dc..6f6fb19a 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketInjector.java @@ -37,6 +37,7 @@ import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.injector.player.PlayerInjectionHandler; import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.FuzzyReflection; +import com.comphenix.protocol.utility.MinecraftReflection; /** * This class is responsible for adding or removing proxy objects that intercepts recieved packets. @@ -92,7 +93,8 @@ class PacketInjector { private void initialize() throws IllegalAccessException { if (intHashMap == null) { // We're looking for the first static field with a Minecraft-object. This should be a IntHashMap. - Field intHashMapField = FuzzyReflection.fromClass(Packet.class, true).getFieldByType(FuzzyReflection.MINECRAFT_OBJECT); + Field intHashMapField = FuzzyReflection.fromClass(Packet.class, true). + getFieldByType(MinecraftReflection.MINECRAFT_OBJECT); try { intHashMap = FieldUtils.readField(intHashMapField, (Object) null, true); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java index 6eda9848..7739b68f 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java @@ -25,9 +25,6 @@ import java.lang.reflect.Method; import java.net.Socket; import java.net.SocketAddress; -import net.minecraft.server.EntityPlayer; -import net.minecraft.server.NetLoginHandler; -import net.minecraft.server.Packet; import net.sf.cglib.proxy.Factory; import org.bukkit.craftbukkit.entity.CraftPlayer; @@ -45,6 +42,7 @@ import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.VolatileField; +import com.comphenix.protocol.utility.MinecraftReflection; abstract class PlayerInjector { @@ -122,7 +120,7 @@ abstract class PlayerInjector { * @param player - the player to retrieve. * @return Notch player object. */ - protected EntityPlayer getEntityPlayer(Player player) { + protected Object getEntityPlayer(Player player) { CraftPlayer craft = (CraftPlayer) player; return craft.getHandle(); } @@ -379,7 +377,7 @@ abstract class PlayerInjector { try { // Well, that sucks. Try just Minecraft objects then. netHandlerField = FuzzyReflection.fromClass(networkManager.getClass(), true). - getFieldByType(FuzzyReflection.MINECRAFT_OBJECT); + getFieldByType(MinecraftReflection.MINECRAFT_OBJECT); } catch (RuntimeException e2) { throw new IllegalAccessException("Cannot locate net handler. " + e2.getMessage()); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java index ae986fdb..4c8cd1bf 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java @@ -32,11 +32,6 @@ import java.util.regex.Pattern; * @author Kristian */ public class FuzzyReflection { - - /** - * Matches a Minecraft object. - */ - public static final String MINECRAFT_OBJECT = "net\\.minecraft(\\.\\w+)+"; // The class we're actually representing private Class source; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java new file mode 100644 index 00000000..80f73190 --- /dev/null +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java @@ -0,0 +1,13 @@ +package com.comphenix.protocol.utility; + +/** + * Methods and constants specifically used in conjuction with reflecting Minecraft object. + * @author Kristian + * + */ +public class MinecraftReflection { + /** + * Matches a Minecraft object. + */ + public static final String MINECRAFT_OBJECT = "net\\.minecraft(\\.\\w+)+"; +}