mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-12-29 04:27:38 +01:00
Fix class not found exception on versions < 1.13
This commit is contained in:
parent
778031c105
commit
e827a3dd1f
@ -96,9 +96,9 @@ public class ImmutableDetector implements Cloner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MinecraftReflection.getBlockClass().isAssignableFrom(type)
|
if (MinecraftReflection.is(MinecraftReflection.getBlockClass(), type)
|
||||||
|| MinecraftReflection.getMinecraftClass("Item").isAssignableFrom(type)
|
|| MinecraftReflection.is(MinecraftReflection.getItemClass(), type)
|
||||||
|| MinecraftReflection.getMinecraftClass("FluidType").isAssignableFrom(type)) {
|
|| MinecraftReflection.is(MinecraftReflection.getFluidTypeClass(), type)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,8 +17,8 @@
|
|||||||
package com.comphenix.protocol.utility;
|
package com.comphenix.protocol.utility;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import com.google.common.base.Optional;
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
@ -51,7 +51,7 @@ class CachedPackage {
|
|||||||
*/
|
*/
|
||||||
public void setPackageClass(String className, Class<?> clazz) {
|
public void setPackageClass(String className, Class<?> clazz) {
|
||||||
if (clazz != null) {
|
if (clazz != null) {
|
||||||
cache.put(className, Optional.<Class<?>> of(clazz));
|
cache.put(className, Optional.of(clazz));
|
||||||
} else {
|
} else {
|
||||||
cache.remove(className);
|
cache.remove(className);
|
||||||
}
|
}
|
||||||
@ -63,32 +63,21 @@ class CachedPackage {
|
|||||||
* @return Class object.
|
* @return Class object.
|
||||||
* @throws RuntimeException If we are unable to find the given class.
|
* @throws RuntimeException If we are unable to find the given class.
|
||||||
*/
|
*/
|
||||||
public Class<?> getPackageClass(String className) {
|
public Optional<Class<?>> getPackageClass(String className) {
|
||||||
Preconditions.checkNotNull(className, "className cannot be null!");
|
Preconditions.checkNotNull(className, "className cannot be null!");
|
||||||
|
|
||||||
// See if we've already looked it up
|
Optional<Class<?>> result = cache.get(className);
|
||||||
if (cache.containsKey(className)) {
|
if (result == null) {
|
||||||
Optional<Class<?>> result = cache.get(className);
|
try {
|
||||||
if (!result.isPresent()) {
|
Class<?> clazz = source.loadClass(combine(packageName, className));
|
||||||
throw new RuntimeException("Cannot find class " + className);
|
result = Optional.ofNullable(clazz);
|
||||||
|
cache.put(className, result);
|
||||||
|
} catch (ClassNotFoundException ex) {
|
||||||
|
cache.put(className, Optional.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return result;
|
||||||
// Try looking it up
|
|
||||||
Class<?> clazz = source.loadClass(combine(packageName, className));
|
|
||||||
if (clazz == null) {
|
|
||||||
throw new IllegalArgumentException("Source " + source + " returned null for " + className);
|
|
||||||
}
|
|
||||||
|
|
||||||
cache.put(className, Optional.<Class<?>> of(clazz));
|
|
||||||
return clazz;
|
|
||||||
} catch (ClassNotFoundException ex) {
|
|
||||||
cache.put(className, Optional.<Class<?>> absent());
|
|
||||||
throw new RuntimeException("Cannot find class " + className, ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -428,6 +428,17 @@ public class MinecraftReflection {
|
|||||||
return clazz.isAssignableFrom(object.getClass());
|
return clazz.isAssignableFrom(object.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Equivalent to {@link #is(Class, Object)} but we don't call getClass again
|
||||||
|
*/
|
||||||
|
public static boolean is(Class<?> clazz, Class<?> test) {
|
||||||
|
if (clazz == null || test == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return clazz.isAssignableFrom(test);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if a given object is a ChunkPosition.
|
* Determine if a given object is a ChunkPosition.
|
||||||
* @param obj - the object to test.
|
* @param obj - the object to test.
|
||||||
@ -1137,6 +1148,14 @@ public class MinecraftReflection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Class<?> getItemClass() {
|
||||||
|
return getNullableNMS("Item");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Class<?> getFluidTypeClass() {
|
||||||
|
return getNullableNMS("FluidType");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the WorldType class.
|
* Retrieve the WorldType class.
|
||||||
* @return The WorldType class.
|
* @return The WorldType class.
|
||||||
@ -1985,7 +2004,8 @@ public class MinecraftReflection {
|
|||||||
public static Class<?> getCraftBukkitClass(String className) {
|
public static Class<?> getCraftBukkitClass(String className) {
|
||||||
if (craftbukkitPackage == null)
|
if (craftbukkitPackage == null)
|
||||||
craftbukkitPackage = new CachedPackage(getCraftBukkitPackage(), getClassSource());
|
craftbukkitPackage = new CachedPackage(getCraftBukkitPackage(), getClassSource());
|
||||||
return craftbukkitPackage.getPackageClass(className);
|
return craftbukkitPackage.getPackageClass(className)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Failed to find CraftBukkit class: " + className));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1997,7 +2017,14 @@ public class MinecraftReflection {
|
|||||||
public static Class<?> getMinecraftClass(String className) {
|
public static Class<?> getMinecraftClass(String className) {
|
||||||
if (minecraftPackage == null)
|
if (minecraftPackage == null)
|
||||||
minecraftPackage = new CachedPackage(getMinecraftPackage(), getClassSource());
|
minecraftPackage = new CachedPackage(getMinecraftPackage(), getClassSource());
|
||||||
return minecraftPackage.getPackageClass(className);
|
return minecraftPackage.getPackageClass(className)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Failed to find NMS class: " + className));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Class<?> getNullableNMS(String className) {
|
||||||
|
if (minecraftPackage == null)
|
||||||
|
minecraftPackage = new CachedPackage(getMinecraftPackage(), getClassSource());
|
||||||
|
return minecraftPackage.getPackageClass(className).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2083,7 +2110,8 @@ public class MinecraftReflection {
|
|||||||
public static Class<?> getMinecraftLibraryClass(String className) {
|
public static Class<?> getMinecraftLibraryClass(String className) {
|
||||||
if (libraryPackage == null)
|
if (libraryPackage == null)
|
||||||
libraryPackage = new CachedPackage("", getClassSource());
|
libraryPackage = new CachedPackage("", getClassSource());
|
||||||
return libraryPackage.getPackageClass(className);
|
return libraryPackage.getPackageClass(className)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Failed to find class: " + className));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user