From c00117a0ade641773387fd75c47f0e08f047a537 Mon Sep 17 00:00:00 2001 From: asofold Date: Sat, 13 Jul 2013 09:52:06 +0200 Subject: [PATCH] Check some method signatures with MCAccess implementations. Minimum safeguard: Check the getMinX etc. methods for proper signatures, to avoid use with mods that have incomplete CraftBukkit mappings. --- .../nocheatplus/utilities/ReflectionUtil.java | 25 +++++++++++++++++++ .../compat/cb2511/MCAccessCB2511.java | 2 ++ .../compat/cb2512/MCAccessCB2512.java | 2 ++ .../compat/cb2545/MCAccessCB2545.java | 2 ++ .../compat/cb2602/MCAccessCB2602.java | 2 ++ .../compat/cb2645/MCAccessCB2645.java | 2 ++ .../compat/cb2691/MCAccessCB2691.java | 2 ++ .../compat/cb2763/MCAccessCB2763.java | 2 ++ .../compat/cb2794/MCAccessCB2794.java | 2 ++ .../compat/cbdev/MCAccessCBDev.java | 3 +++ 10 files changed, 44 insertions(+) diff --git a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ReflectionUtil.java b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ReflectionUtil.java index 6aa7c16e..9e484225 100644 --- a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ReflectionUtil.java +++ b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ReflectionUtil.java @@ -26,6 +26,31 @@ public class ReflectionUtil { } } + /** + * Check for the given names if the method returns the desired type of result (exact check). + * @param methodNames + * @param returnType + */ + public static void checkMethodReturnTypesNoArgs(Class objClass, String[] methodNames, Class returnType){ + // TODO: Add check: boolean isStatic. + try { + for (String methodName : methodNames){ + Method m = objClass.getMethod(methodName); + if (m.getParameterTypes().length != 0){ + throw new RuntimeException("Expect method without arguments for " + objClass.getName() + "." + methodName); + } + if (m.getReturnType() != returnType){ + throw new RuntimeException("Wrong return type for: " + objClass.getName() + "." + methodName); + } + } + } catch (SecurityException e) { + // Let this one pass. + //throw new RuntimeException(e); + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + /** * Dirty method to call a declared method with a generic parameter type. Does try.catch and return null insome cases for method invocation. Purpose for this is generic factory registration, having methods with type Object alongside methods with more specialized types. * @param obj diff --git a/NCPCompatCB2511/src/main/java/fr/neatmonster/nocheatplus/compat/cb2511/MCAccessCB2511.java b/NCPCompatCB2511/src/main/java/fr/neatmonster/nocheatplus/compat/cb2511/MCAccessCB2511.java index 0e910432..1bb55af7 100644 --- a/NCPCompatCB2511/src/main/java/fr/neatmonster/nocheatplus/compat/cb2511/MCAccessCB2511.java +++ b/NCPCompatCB2511/src/main/java/fr/neatmonster/nocheatplus/compat/cb2511/MCAccessCB2511.java @@ -33,6 +33,8 @@ public class MCAccessCB2511 implements MCAccess { getCommandMap(); ReflectionUtil.checkMembers("net.minecraft.server.", new String[]{"Entity" , "dead"}); ReflectionUtil.checkMembers("net.minecraft.server.", new String[]{"EntityPlayer" , "netServerHandler"}); + ReflectionUtil.checkMethodReturnTypesNoArgs(net.minecraft.server.Block.class, + new String[]{"v", "w", "x", "y", "z", "A"}, double.class); } @Override diff --git a/NCPCompatCB2512/src/main/java/fr/neatmonster/nocheatplus/compat/cb2512/MCAccessCB2512.java b/NCPCompatCB2512/src/main/java/fr/neatmonster/nocheatplus/compat/cb2512/MCAccessCB2512.java index ef18d971..7dfa8bb2 100644 --- a/NCPCompatCB2512/src/main/java/fr/neatmonster/nocheatplus/compat/cb2512/MCAccessCB2512.java +++ b/NCPCompatCB2512/src/main/java/fr/neatmonster/nocheatplus/compat/cb2512/MCAccessCB2512.java @@ -31,6 +31,8 @@ public class MCAccessCB2512 implements MCAccess{ public MCAccessCB2512(){ getCommandMap(); ReflectionUtil.checkMembers("net.minecraft.server.v1_4_5.", new String[]{"Entity" , "dead"}); + ReflectionUtil.checkMethodReturnTypesNoArgs(net.minecraft.server.v1_4_5.Block.class, + new String[]{"v", "w", "x", "y", "z", "A"}, double.class); } @Override diff --git a/NCPCompatCB2545/src/main/java/fr/neatmonster/nocheatplus/compat/cb2545/MCAccessCB2545.java b/NCPCompatCB2545/src/main/java/fr/neatmonster/nocheatplus/compat/cb2545/MCAccessCB2545.java index 42d16926..979a916e 100644 --- a/NCPCompatCB2545/src/main/java/fr/neatmonster/nocheatplus/compat/cb2545/MCAccessCB2545.java +++ b/NCPCompatCB2545/src/main/java/fr/neatmonster/nocheatplus/compat/cb2545/MCAccessCB2545.java @@ -31,6 +31,8 @@ public class MCAccessCB2545 implements MCAccess{ public MCAccessCB2545(){ getCommandMap(); ReflectionUtil.checkMembers("net.minecraft.server.v1_4_6.", new String[]{"Entity" , "dead"}); + ReflectionUtil.checkMethodReturnTypesNoArgs(net.minecraft.server.v1_4_6.Block.class, + new String[]{"v", "w", "x", "y", "z", "A"}, double.class); } @Override diff --git a/NCPCompatCB2602/src/main/java/fr/neatmonster/nocheatplus/compat/cb2602/MCAccessCB2602.java b/NCPCompatCB2602/src/main/java/fr/neatmonster/nocheatplus/compat/cb2602/MCAccessCB2602.java index da25b899..739b2511 100644 --- a/NCPCompatCB2602/src/main/java/fr/neatmonster/nocheatplus/compat/cb2602/MCAccessCB2602.java +++ b/NCPCompatCB2602/src/main/java/fr/neatmonster/nocheatplus/compat/cb2602/MCAccessCB2602.java @@ -31,6 +31,8 @@ public class MCAccessCB2602 implements MCAccess{ public MCAccessCB2602(){ getCommandMap(); ReflectionUtil.checkMembers("net.minecraft.server.v1_4_R1.", new String[]{"Entity" , "dead"}); + ReflectionUtil.checkMethodReturnTypesNoArgs(net.minecraft.server.v1_4_R1.Block.class, + new String[]{"v", "w", "x", "y", "z", "A"}, double.class); } @Override diff --git a/NCPCompatCB2645/src/main/java/fr/neatmonster/nocheatplus/compat/cb2645/MCAccessCB2645.java b/NCPCompatCB2645/src/main/java/fr/neatmonster/nocheatplus/compat/cb2645/MCAccessCB2645.java index c1a0a8df..4635948e 100644 --- a/NCPCompatCB2645/src/main/java/fr/neatmonster/nocheatplus/compat/cb2645/MCAccessCB2645.java +++ b/NCPCompatCB2645/src/main/java/fr/neatmonster/nocheatplus/compat/cb2645/MCAccessCB2645.java @@ -31,6 +31,8 @@ public class MCAccessCB2645 implements MCAccess{ public MCAccessCB2645(){ getCommandMap(); ReflectionUtil.checkMembers("net.minecraft.server.v1_5_R1.", new String[]{"Entity" , "dead"}); + ReflectionUtil.checkMethodReturnTypesNoArgs(net.minecraft.server.v1_5_R1.Block.class, + new String[]{"u", "v", "w", "x", "y", "z"}, double.class); } @Override diff --git a/NCPCompatCB2691/src/main/java/fr/neatmonster/nocheatplus/compat/cb2691/MCAccessCB2691.java b/NCPCompatCB2691/src/main/java/fr/neatmonster/nocheatplus/compat/cb2691/MCAccessCB2691.java index e4a4497c..760d523c 100644 --- a/NCPCompatCB2691/src/main/java/fr/neatmonster/nocheatplus/compat/cb2691/MCAccessCB2691.java +++ b/NCPCompatCB2691/src/main/java/fr/neatmonster/nocheatplus/compat/cb2691/MCAccessCB2691.java @@ -31,6 +31,8 @@ public class MCAccessCB2691 implements MCAccess{ public MCAccessCB2691(){ getCommandMap(); ReflectionUtil.checkMembers("net.minecraft.server.v1_5_R2.", new String[]{"Entity" , "dead"}); + ReflectionUtil.checkMethodReturnTypesNoArgs(net.minecraft.server.v1_5_R2.Block.class, + new String[]{"u", "v", "w", "x", "y", "z"}, double.class); } @Override diff --git a/NCPCompatCB2763/src/main/java/fr/neatmonster/nocheatplus/compat/cb2763/MCAccessCB2763.java b/NCPCompatCB2763/src/main/java/fr/neatmonster/nocheatplus/compat/cb2763/MCAccessCB2763.java index 41048bc0..021a4b4a 100644 --- a/NCPCompatCB2763/src/main/java/fr/neatmonster/nocheatplus/compat/cb2763/MCAccessCB2763.java +++ b/NCPCompatCB2763/src/main/java/fr/neatmonster/nocheatplus/compat/cb2763/MCAccessCB2763.java @@ -31,6 +31,8 @@ public class MCAccessCB2763 implements MCAccess{ public MCAccessCB2763(){ getCommandMap(); ReflectionUtil.checkMembers("net.minecraft.server.v1_5_R3.", new String[]{"Entity" , "dead"}); + ReflectionUtil.checkMethodReturnTypesNoArgs(net.minecraft.server.v1_5_R3.Block.class, + new String[]{"u", "v", "w", "x", "y", "z"}, double.class); } @Override diff --git a/NCPCompatCB2794/src/main/java/fr/neatmonster/nocheatplus/compat/cb2794/MCAccessCB2794.java b/NCPCompatCB2794/src/main/java/fr/neatmonster/nocheatplus/compat/cb2794/MCAccessCB2794.java index 9763d0ba..494c840f 100644 --- a/NCPCompatCB2794/src/main/java/fr/neatmonster/nocheatplus/compat/cb2794/MCAccessCB2794.java +++ b/NCPCompatCB2794/src/main/java/fr/neatmonster/nocheatplus/compat/cb2794/MCAccessCB2794.java @@ -31,6 +31,8 @@ public class MCAccessCB2794 implements MCAccess{ public MCAccessCB2794(){ getCommandMap(); ReflectionUtil.checkMembers("net.minecraft.server.v1_6_R1.", new String[]{"Entity" , "dead"}); + ReflectionUtil.checkMethodReturnTypesNoArgs(net.minecraft.server.v1_6_R1.Block.class, + new String[]{"u", "v", "w", "x", "y", "z"}, double.class); } @Override diff --git a/NCPCompatCBDev/src/main/java/fr/neatmonster/nocheatplus/compat/cbdev/MCAccessCBDev.java b/NCPCompatCBDev/src/main/java/fr/neatmonster/nocheatplus/compat/cbdev/MCAccessCBDev.java index 50176188..1d6f1246 100644 --- a/NCPCompatCBDev/src/main/java/fr/neatmonster/nocheatplus/compat/cbdev/MCAccessCBDev.java +++ b/NCPCompatCBDev/src/main/java/fr/neatmonster/nocheatplus/compat/cbdev/MCAccessCBDev.java @@ -31,6 +31,9 @@ public class MCAccessCBDev implements MCAccess{ public MCAccessCBDev(){ getCommandMap(); ReflectionUtil.checkMembers("net.minecraft.server.v1_6_R2.", new String[]{"Entity" , "dead"}); + ReflectionUtil.checkMethodReturnTypesNoArgs(net.minecraft.server.v1_6_R2.Block.class, + new String[]{"u", "v", "w", "x", "y", "z"}, double.class); + // TODO: Nail it down further. } @Override