mirror of
https://github.com/ViaVersion/ViaRewind-Legacy-Support.git
synced 2024-11-14 10:15:31 +01:00
fix: check for return types (optional) (#51)
This commit is contained in:
parent
0122727cd0
commit
61d07dec61
@ -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) {
|
||||
|
@ -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(
|
||||
|
@ -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() + "[", "]")
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user