handle) {
return new CallableVersionHandler<>(version, handle);
}
@@ -288,6 +307,20 @@ public final class ReflectionUtils {
return MINOR_NUMBER >= minorNumber;
}
+ /**
+ * Checks whether the server version is equal or greater than the given version.
+ *
+ * @param minorNumber the minor version to compare the server version with.
+ * @param patchNumber the patch number to compare the server version with.
+ * @return true if the version is equal or newer, otherwise false.
+ * @see #MINOR_NUMBER
+ * @see #PATCH_NUMBER
+ * @since 7.1.0
+ */
+ public static boolean supports(int minorNumber, int patchNumber) {
+ return (MINOR_NUMBER == minorNumber && supportsPatch(patchNumber)) || MINOR_NUMBER > minorNumber;
+ }
+
/**
* Checks whether the server version is equal or greater than the given version.
*
@@ -300,36 +333,28 @@ public final class ReflectionUtils {
return PATCH_NUMBER >= patchNumber;
}
- /**
- * Checks whether the server version is equal or greater than the given version.
- * If minorNumber matches, it will check if patchNumber is equal or greater,
- * if minorNumber does not match, it will check if minorNumber is greater.
- *
- * @param minorNumber the minor version to compare the server version with.
- * @param patchNumber the patch version to compare the server version with.
- * @see #MINOR_NUMBER
- * @see #PATCH_NUMBER
- */
- public static boolean supports(int minorNumber, int patchNumber) {
- return (MINOR_NUMBER == minorNumber && supportsPatch(patchNumber)) || MINOR_NUMBER > minorNumber;
- }
-
/**
* Get a NMS (net.minecraft.server) class which accepts a package for 1.17 compatibility.
*
- * @param newPackage the 1.17 package name.
- * @param name the name of the class.
+ * @param packageName the 1.17+ package name of this class.
+ * @param name the name of the class.
* @return the NMS class or null if not found.
* @since 4.0.0
*/
@Nullable
- public static Class> getNMSClass(@Nonnull String newPackage, @Nonnull String name) {
- if (supports(17)) name = newPackage + '.' + name;
- return getNMSClass(name);
+ public static Class> getNMSClass(@Nullable String packageName, @Nonnull String name) {
+ if (packageName != null && supports(17)) name = packageName + '.' + name;
+
+ try {
+ return Class.forName(NMS_PACKAGE + name);
+ } catch (ClassNotFoundException ex) {
+ ex.printStackTrace();
+ return null;
+ }
}
/**
- * Get a NMS (net.minecraft.server) class.
+ * Get a NMS {@link #NMS_PACKAGE} class.
*
* @param name the name of the class.
* @return the NMS class or null if not found.
@@ -337,12 +362,7 @@ public final class ReflectionUtils {
*/
@Nullable
public static Class> getNMSClass(@Nonnull String name) {
- try {
- return Class.forName(NMS_PACKAGE + name);
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- return null;
- }
+ return getNMSClass(null, name);
}
/**
@@ -437,6 +457,10 @@ public final class ReflectionUtils {
}
}
+ /**
+ * @deprecated Use {@link #toArrayClass(Class)} instead.
+ */
+ @Deprecated
public static Class> getArrayClass(String clazz, boolean nms) {
clazz = "[L" + (nms ? NMS_PACKAGE : CRAFTBUKKIT_PACKAGE) + clazz + ';';
try {
@@ -447,6 +471,15 @@ public final class ReflectionUtils {
}
}
+ /**
+ * Gives an array version of a class. For example if you wanted {@code EntityPlayer[]} you'd use:
+ * {@code
+ * Class EntityPlayer = ReflectionUtils.getNMSClass("...", "EntityPlayer");
+ * Class EntityPlayerArray = ReflectionUtils.toArrayClass(EntityPlayer);
+ * }
+ *
+ * @param clazz the class to get the array version of. You could use for multi-dimensions arrays too.
+ */
public static Class> toArrayClass(Class> clazz) {
try {
return Class.forName("[L" + clazz.getName() + ';');
@@ -457,26 +490,39 @@ public final class ReflectionUtils {
}
public static final class VersionHandler {
- private int version;
+ private int version, patch;
private T handle;
private VersionHandler(int version, T handle) {
- if (supports(version)) {
+ this(version, 0, handle);
+ }
+
+ private VersionHandler(int version, int patch, T handle) {
+ if (supports(version) && supportsPatch(patch)) {
this.version = version;
+ this.patch = patch;
this.handle = handle;
}
}
public VersionHandler v(int version, T handle) {
- if (version == this.version)
- throw new IllegalArgumentException("Cannot have duplicate version handles for version: " + version);
- if (version > this.version && supports(version)) {
+ return v(version, 0, handle);
+ }
+
+ public VersionHandler v(int version, int patch, T handle) {
+ if (version == this.version && patch == this.patch)
+ throw new IllegalArgumentException("Cannot have duplicate version handles for version: " + version + '.' + patch);
+ if (version > this.version && supports(version) && patch >= this.patch && supportsPatch(patch)) {
this.version = version;
+ this.patch = patch;
this.handle = handle;
}
return this;
}
+ /**
+ * If none of the previous version checks matched, it'll return this object.
+ */
public T orElse(T handle) {
return this.version == 0 ? handle : this.handle;
}
@@ -512,4 +558,4 @@ public final class ReflectionUtils {
}
}
}
-}
\ No newline at end of file
+}