Archiviert
13
0

Improve Minecraft class detection

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-04-02 13:55:18 +02:00
Ursprung a43428c2c4
Commit 9a34036d14

Datei anzeigen

@ -81,6 +81,9 @@ public class MinecraftReflection {
private static Constructor<?> craftNMSConstructor; private static Constructor<?> craftNMSConstructor;
private static Constructor<?> craftBukkitConstructor; private static Constructor<?> craftBukkitConstructor;
// Matches classes
private static AbstractFuzzyMatcher<Class<?>> fuzzyMatcher;
// New in 1.4.5 // New in 1.4.5
private static Method craftNMSMethod; private static Method craftNMSMethod;
private static Method craftBukkitMethod; private static Method craftBukkitMethod;
@ -88,7 +91,12 @@ public class MinecraftReflection {
// net.minecraft.server // net.minecraft.server
private static Class<?> itemStackArrayClass; private static Class<?> itemStackArrayClass;
/**
* Whether or not we're currently initializing the reflection handler.
*/
private static boolean initializing;
private MinecraftReflection() { private MinecraftReflection() {
// No need to make this constructable. // No need to make this constructable.
} }
@ -108,7 +116,9 @@ public class MinecraftReflection {
* @return A matcher for Minecraft objects. * @return A matcher for Minecraft objects.
*/ */
public static AbstractFuzzyMatcher<Class<?>> getMinecraftObjectMatcher() { public static AbstractFuzzyMatcher<Class<?>> getMinecraftObjectMatcher() {
return FuzzyMatchers.matchRegex(getMinecraftObjectRegex(), 50); if (fuzzyMatcher == null)
fuzzyMatcher = FuzzyMatchers.matchRegex(getMinecraftObjectRegex(), 50);
return fuzzyMatcher;
} }
/** /**
@ -119,6 +129,9 @@ public class MinecraftReflection {
// Speed things up // Speed things up
if (MINECRAFT_FULL_PACKAGE != null) if (MINECRAFT_FULL_PACKAGE != null)
return MINECRAFT_FULL_PACKAGE; return MINECRAFT_FULL_PACKAGE;
if (initializing)
throw new IllegalStateException("Already initializing minecraft package!");
initializing = true;
Server craftServer = Bukkit.getServer(); Server craftServer = Bukkit.getServer();
@ -144,16 +157,16 @@ public class MinecraftReflection {
MINECRAFT_PREFIX_PACKAGE = MINECRAFT_FULL_PACKAGE; MINECRAFT_PREFIX_PACKAGE = MINECRAFT_FULL_PACKAGE;
// The package is usualy flat, so go with that assumtion // The package is usualy flat, so go with that assumtion
DYNAMIC_PACKAGE_MATCHER = String matcher =
(MINECRAFT_PREFIX_PACKAGE.length() > 0 ? (MINECRAFT_PREFIX_PACKAGE.length() > 0 ?
Pattern.quote(MINECRAFT_PREFIX_PACKAGE + ".") : "") + "\\w+"; Pattern.quote(MINECRAFT_PREFIX_PACKAGE + ".") : "") + "\\w+";
// We'll still accept the default location, however // We'll still accept the default location, however
DYNAMIC_PACKAGE_MATCHER = "(" + DYNAMIC_PACKAGE_MATCHER + ")|(" + MINECRAFT_OBJECT + ")"; setDynamicPackageMatcher("(" + matcher + ")|(" + MINECRAFT_OBJECT + ")");
} else { } else {
// Use the standard matcher // Use the standard matcher
DYNAMIC_PACKAGE_MATCHER = MINECRAFT_OBJECT; setDynamicPackageMatcher(MINECRAFT_OBJECT);
} }
return MINECRAFT_FULL_PACKAGE; return MINECRAFT_FULL_PACKAGE;
@ -162,13 +175,27 @@ public class MinecraftReflection {
throw new RuntimeException("Security violation. Cannot get handle method.", e); throw new RuntimeException("Security violation. Cannot get handle method.", e);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
throw new IllegalStateException("Cannot find getHandle() method on server. Is this a modified CraftBukkit version?", e); throw new IllegalStateException("Cannot find getHandle() method on server. Is this a modified CraftBukkit version?", e);
} finally {
initializing = false;
} }
} else { } else {
initializing = false;
throw new IllegalStateException("Could not find Bukkit. Is it running?"); throw new IllegalStateException("Could not find Bukkit. Is it running?");
} }
} }
/**
* Update the dynamic package matcher.
* @param regex - the Minecraft package regex.
*/
private static void setDynamicPackageMatcher(String regex) {
DYNAMIC_PACKAGE_MATCHER = regex;
// Ensure that the matcher is regenerated
fuzzyMatcher = null;
}
// Patch for Libigot // Patch for Libigot
private static void handleLibigot() { private static void handleLibigot() {
try { try {
@ -193,7 +220,7 @@ public class MinecraftReflection {
CRAFTBUKKIT_PACKAGE = craftBukkitPackage; CRAFTBUKKIT_PACKAGE = craftBukkitPackage;
// Standard matcher // Standard matcher
DYNAMIC_PACKAGE_MATCHER = MINECRAFT_OBJECT; setDynamicPackageMatcher(MINECRAFT_OBJECT);
} }
/** /**
@ -261,8 +288,7 @@ public class MinecraftReflection {
if (clazz == null) if (clazz == null)
throw new IllegalArgumentException("Class cannot be NULL."); throw new IllegalArgumentException("Class cannot be NULL.");
// Doesn't matter if we don't check for the version here return getMinecraftObjectMatcher().isMatch(clazz, null);
return clazz.getName().startsWith(MINECRAFT_PREFIX_PACKAGE);
} }
/** /**