From 1e732d62cb623e612a1fd75b0aa9f84e975dd5a1 Mon Sep 17 00:00:00 2001 From: games647 Date: Wed, 27 Jul 2022 10:06:46 +0200 Subject: [PATCH] Replace Field- and MethodUtils from breaking changes in ProtocolLib --- bukkit/pom.xml | 16 ++++++++++++-- .../fastlogin/bukkit/hook/CrazyLoginHook.java | 14 ++++--------- .../protocollib/SkinApplyListener.java | 21 +------------------ .../protocollib/VerifyResponseTask.java | 13 +++++++++--- .../fastlogin/bukkit/ReflectionTest.java | 7 +++++-- .../bukkit/hook/CrazyLoginHookTest.java | 7 +++++-- .../protocollib/VerifyResponseTaskTest.java | 11 +++++++--- 7 files changed, 47 insertions(+), 42 deletions(-) diff --git a/bukkit/pom.xml b/bukkit/pom.xml index 94c26cb4..2811991d 100644 --- a/bukkit/pom.xml +++ b/bukkit/pom.xml @@ -29,6 +29,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + + 4.1.77.Final + + com.github.games647 fastlogin @@ -371,14 +375,22 @@ io.netty netty-transport - 4.1.77.Final + ${nettyVersion} test io.netty netty-codec - 4.1.77.Final + ${nettyVersion} + test + + + + + commons-lang + commons-lang + 2.6 test diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHook.java index 5ab3bce6..2d6d88e6 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHook.java @@ -25,7 +25,8 @@ */ package com.github.games647.fastlogin.bukkit.hook; -import com.comphenix.protocol.reflect.FieldUtils; +import com.comphenix.protocol.reflect.accessors.Accessors; +import com.comphenix.protocol.reflect.accessors.FieldAccessor; import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.core.hooks.AuthPlugin; @@ -135,14 +136,7 @@ public class CrazyLoginHook implements AuthPlugin { } protected PlayerListener getListener() { - PlayerListener listener; - try { - listener = (PlayerListener) FieldUtils.readField(crazyLoginPlugin, "playerListener", true); - } catch (IllegalAccessException ex) { - plugin.getLog().error("Failed to get the listener instance for auto login", ex); - listener = null; - } - - return listener; + FieldAccessor accessor = Accessors.getFieldAccessor(crazyLoginPlugin.getClass(), PlayerListener.class, true); + return (PlayerListener) accessor.get(crazyLoginPlugin); } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/SkinApplyListener.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/SkinApplyListener.java index 3d4a807b..05a84e0c 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/SkinApplyListener.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/SkinApplyListener.java @@ -25,18 +25,12 @@ */ package com.github.games647.fastlogin.bukkit.listener.protocollib; -import com.comphenix.protocol.reflect.MethodUtils; -import com.comphenix.protocol.reflect.accessors.Accessors; -import com.comphenix.protocol.reflect.accessors.MethodAccessor; -import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedSignedProperty; import com.github.games647.craftapi.model.skin.Textures; import com.github.games647.fastlogin.bukkit.BukkitLoginSession; import com.github.games647.fastlogin.bukkit.FastLoginBukkit; -import java.lang.reflect.InvocationTargetException; - import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -46,9 +40,6 @@ import org.bukkit.event.player.PlayerLoginEvent.Result; public class SkinApplyListener implements Listener { - private static final Class GAME_PROFILE = MinecraftReflection.getGameProfileClass(); - private static final MethodAccessor GET_PROPERTIES = Accessors.getMethodAccessor(GAME_PROFILE, "getProperties"); - private final FastLoginBukkit plugin; public SkinApplyListener(FastLoginBukkit plugin) { @@ -78,16 +69,6 @@ public class SkinApplyListener implements Listener { WrappedGameProfile gameProfile = WrappedGameProfile.fromPlayer(player); WrappedSignedProperty skin = WrappedSignedProperty.fromValues(Textures.KEY, skinData, signature); - try { - gameProfile.getProperties().put(Textures.KEY, skin); - } catch (ClassCastException castException) { - //Cauldron, MCPC, Thermos, ... - Object map = GET_PROPERTIES.invoke(gameProfile.getHandle()); - try { - MethodUtils.invokeMethod(map, "put", new Object[]{Textures.KEY, skin.getHandle()}); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { - plugin.getLog().error("Error setting premium skin of: {}", player, ex); - } - } + gameProfile.getProperties().put(Textures.KEY, skin); } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTask.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTask.java index 1363cb34..b781648a 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTask.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTask.java @@ -30,8 +30,9 @@ import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.injector.temporary.TemporaryPlayerFactory; import com.comphenix.protocol.reflect.EquivalentConverter; -import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.FuzzyReflection; +import com.comphenix.protocol.reflect.accessors.Accessors; +import com.comphenix.protocol.reflect.accessors.FieldAccessor; import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftVersion; import com.comphenix.protocol.wrappers.BukkitConverters; @@ -184,7 +185,10 @@ public class VerifyResponseTask implements Runnable { try { Object networkManager = getNetworkManager(); //https://github.com/bergerkiller/CraftSource/blob/master/net.minecraft.server/NetworkManager.java#L69 - FieldUtils.writeField(networkManager, "spoofedUUID", premiumUUID, true); + + Class managerClass = networkManager.getClass(); + FieldAccessor accessor = Accessors.getFieldAccessorOrNull(managerClass, "spoofedUUID", UUID.class); + accessor.set(networkManager, premiumUUID); } catch (Exception exc) { plugin.getLog().error("Error setting premium uuid of {}", player, exc); } @@ -198,7 +202,10 @@ public class VerifyResponseTask implements Runnable { // ChannelInjector Class injectorClass = Class.forName("com.comphenix.protocol.injector.netty.Injector"); Object rawInjector = FuzzyReflection.getFieldValue(injectorContainer, injectorClass, true); - return FieldUtils.readField(rawInjector, "networkManager", true); + + Class rawInjectorClass = rawInjector.getClass(); + FieldAccessor accessor = Accessors.getFieldAccessorOrNull(rawInjectorClass, "networkManager", Object.class); + return accessor.get(rawInjector); } private boolean enableEncryption(SecretKey loginKey) throws IllegalArgumentException { diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/ReflectionTest.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/ReflectionTest.java index dae5c519..a9e319be 100644 --- a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/ReflectionTest.java +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/ReflectionTest.java @@ -25,9 +25,11 @@ */ package com.github.games647.fastlogin.bukkit; -import com.comphenix.protocol.reflect.FieldUtils; +import com.comphenix.protocol.reflect.accessors.Accessors; +import com.comphenix.protocol.reflect.accessors.FieldAccessor; import fr.xephi.authme.api.v3.AuthMeApi; +import fr.xephi.authme.process.Management; import org.junit.jupiter.api.Test; @@ -37,6 +39,7 @@ class ReflectionTest { @Test void testAuthMeManagementField() { - assertNotNull(FieldUtils.getField(AuthMeApi.class, "management", true)); + FieldAccessor accessor = Accessors.getFieldAccessor(AuthMeApi.class, Management.class, true); + assertNotNull(accessor.getField()); } } diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHookTest.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHookTest.java index ae53c1f6..41e5d542 100644 --- a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHookTest.java +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/hook/CrazyLoginHookTest.java @@ -25,9 +25,11 @@ */ package com.github.games647.fastlogin.bukkit.hook; -import com.comphenix.protocol.reflect.FieldUtils; +import com.comphenix.protocol.reflect.accessors.Accessors; +import com.comphenix.protocol.reflect.accessors.FieldAccessor; import de.st_ddt.crazylogin.CrazyLogin; +import de.st_ddt.crazylogin.listener.PlayerListener; import org.junit.jupiter.api.Test; @@ -37,7 +39,8 @@ class CrazyLoginHookTest { @Test void testPlayerListener() { - assertNotNull(FieldUtils.getField(CrazyLogin.class, "playerListener", true)); + FieldAccessor accessor = Accessors.getFieldAccessor(CrazyLogin.class, PlayerListener.class, true); + assertNotNull(accessor.getField()); } } diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTaskTest.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTaskTest.java index f751c5c0..bae46ece 100644 --- a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTaskTest.java +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTaskTest.java @@ -26,7 +26,8 @@ package com.github.games647.fastlogin.bukkit.listener.protocollib; import com.comphenix.protocol.injector.packet.PacketRegistry; -import com.comphenix.protocol.reflect.FieldUtils; +import com.comphenix.protocol.reflect.accessors.Accessors; +import com.comphenix.protocol.reflect.accessors.FieldAccessor; import com.comphenix.protocol.utility.MinecraftReflection; import java.util.Optional; @@ -42,6 +43,9 @@ import static org.mockito.Mockito.mockStatic; class VerifyResponseTaskTest { + private static final String NETTY_INJECTOR_CLASS = + "com.comphenix.protocol.injector.netty.channel.NettyChannelInjector"; + @Test void getNetworkManagerReflection() throws ClassNotFoundException { try ( @@ -56,8 +60,9 @@ class VerifyResponseTaskTest { registryMock.when(() -> PacketRegistry.tryGetPacketClass(any())).thenReturn(Optional.empty()); - Class injectorClass = Class.forName("com.comphenix.protocol.injector.netty.channel.NettyChannelInjector"); - assertNotNull(FieldUtils.getField(injectorClass, "networkManager", true)); + Class injectorClass = Class.forName(NETTY_INJECTOR_CLASS); + FieldAccessor accessor = Accessors.getFieldAccessorOrNull(injectorClass, "networkManager", Object.class); + assertNotNull(accessor.getField()); } } }