fix: check for return types (optional) (#51)

This commit is contained in:
Pierre Maurice Schwang 2022-08-28 00:11:39 +02:00 committed by GitHub
parent 0122727cd0
commit 61d07dec61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 2 deletions

View File

@ -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) {

View File

@ -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(

View File

@ -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() + "[", "]")

View File

@ -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<String, Field> 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);
}