From 61d07dec614db7417d50cb6e49485102e35e1fc1 Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Sun, 28 Aug 2022 00:11:39 +0200 Subject: [PATCH] fix: check for return types (optional) (#51) --- .../legacysupport/injector/NMSReflection.java | 12 ++++++++++++ .../legacysupport/listener/SoundListener.java | 6 ++++-- .../legacysupport/reflection/MethodSignature.java | 13 +++++++++++++ .../legacysupport/reflection/ReflectionAPI.java | 4 ++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/gerrygames/viarewind/legacysupport/injector/NMSReflection.java b/src/main/java/de/gerrygames/viarewind/legacysupport/injector/NMSReflection.java index 4c39a35..1341b6b 100644 --- a/src/main/java/de/gerrygames/viarewind/legacysupport/injector/NMSReflection.java +++ b/src/main/java/de/gerrygames/viarewind/legacysupport/injector/NMSReflection.java @@ -27,6 +27,18 @@ public class NMSReflection { protocolVersion; } + public static Class getBlockDataClass() { + try { + if (getProtocolVersion() >= PROTOCOL_1_17) { + return Class.forName("net.minecraft.world.level.block.state.IBlockData"); + } + return getLegacyNMSClass("IBlockData"); + } catch (ClassNotFoundException ex) { + ex.printStackTrace(); + } + return null; + } + public static Class getBlockPositionClass() { try { if (getProtocolVersion() >= PROTOCOL_1_17) { diff --git a/src/main/java/de/gerrygames/viarewind/legacysupport/listener/SoundListener.java b/src/main/java/de/gerrygames/viarewind/legacysupport/listener/SoundListener.java index a3d77b6..c2157e1 100644 --- a/src/main/java/de/gerrygames/viarewind/legacysupport/listener/SoundListener.java +++ b/src/main/java/de/gerrygames/viarewind/legacysupport/listener/SoundListener.java @@ -31,6 +31,8 @@ public class SoundListener implements Listener { } catch (ClassNotFoundException ignored) {} } + private static final Class I_BLOCK_DATA = NMSReflection.getBlockDataClass(); + public SoundListener() { try { Class.forName("org.bukkit.event.entity.EntityPickupItemEvent"); @@ -102,8 +104,8 @@ public class SoundListener implements Listener { } Method getTypeMethod = ReflectionAPI.pickMethod( nmsWorld.getClass(), - new MethodSignature("getType", blockPositionClass), - new MethodSignature("a_", blockPositionClass) // 1.18.2 + new MethodSignature("getType", blockPositionClass).withReturnType(I_BLOCK_DATA), + new MethodSignature("a_", blockPositionClass).withReturnType(I_BLOCK_DATA) // 1.18.2 ); Object blockData = getTypeMethod.invoke(nmsWorld, blockPosition); Method getBlock = ReflectionAPI.pickMethod( diff --git a/src/main/java/de/gerrygames/viarewind/legacysupport/reflection/MethodSignature.java b/src/main/java/de/gerrygames/viarewind/legacysupport/reflection/MethodSignature.java index c85e3d0..c468baa 100644 --- a/src/main/java/de/gerrygames/viarewind/legacysupport/reflection/MethodSignature.java +++ b/src/main/java/de/gerrygames/viarewind/legacysupport/reflection/MethodSignature.java @@ -1,6 +1,7 @@ package de.gerrygames.viarewind.legacysupport.reflection; import java.util.Arrays; +import java.util.Objects; import java.util.StringJoiner; public class MethodSignature { @@ -8,6 +9,8 @@ public class MethodSignature { private final String name; private final Class[] parameterTypes; + private Class returnType; + public MethodSignature(String name, Class... parameterTypes) { this.name = name; this.parameterTypes = parameterTypes; @@ -21,6 +24,16 @@ public class MethodSignature { return parameterTypes; } + public Class returnType() { + return returnType; + } + + public MethodSignature withReturnType(Class returnType) { + Objects.requireNonNull(returnType); + this.returnType = returnType; + return this; + } + @Override public String toString() { return new StringJoiner(", ", MethodSignature.class.getSimpleName() + "[", "]") diff --git a/src/main/java/de/gerrygames/viarewind/legacysupport/reflection/ReflectionAPI.java b/src/main/java/de/gerrygames/viarewind/legacysupport/reflection/ReflectionAPI.java index 479cae3..8b97bb1 100644 --- a/src/main/java/de/gerrygames/viarewind/legacysupport/reflection/ReflectionAPI.java +++ b/src/main/java/de/gerrygames/viarewind/legacysupport/reflection/ReflectionAPI.java @@ -10,6 +10,7 @@ import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.Objects; public class ReflectionAPI { private static Map fields = new HashMap<>(); @@ -41,6 +42,9 @@ public class ReflectionAPI { for (MethodSignature signature : signatures) { try { Method method = depth.getDeclaredMethod(signature.name(), signature.parameterTypes()); + if (signature.returnType() != null && !Objects.equals(method.getReturnType(), signature.returnType())) { + continue; + } if (!method.isAccessible()) { method.setAccessible(true); }