Class ReflectionUtils
Caches the packet related methods and is asynchronous.
This class does not handle null checks as most of the requests are from the other utility classes that already handle null checks.
Clientbound Packets are considered fake updates to the client without changing the actual data. Since all the data is handled by the server.
A useful resource used to compare mappings is Mini's Mapping Viewer
- Version:
- 7.1.0.0.1
- Author:
- Crypto Morin
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
static final class
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Mojang remapped their NMS in 1.17: Spigot Threadstatic final int
The raw minor version number.static final String
Mojang remapped their NMS in 1.17: Spigot Threadstatic final String
We use reflection mainly to avoid writing a new class for version barrier.static final int
The raw patch version number. -
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>
getArrayClass
(String clazz, boolean nms) Deprecated.static Object
getConnection
(org.bukkit.entity.Player player) static Class<?>
getCraftClass
(String name) Get a CraftBukkit (org.bukkit.craftbukkit) class.static Object
getHandle
(org.bukkit.entity.Player player) static Integer
getLatestPatchNumberOf
(int minorVersion) Gets the latest known patch number of the given minor version.static Class<?>
getNMSClass
(String name) Get a NMSNMS_PACKAGE
class.static Class<?>
getNMSClass
(String packageName, String name) Get a NMS (net.minecraft.server) class which accepts a package for 1.17 compatibility.static String
Gets the full version information of the server.static CompletableFuture<Void>
sendPacket
(org.bukkit.entity.Player player, Object... packets) Sends a packet to the player asynchronously if they're online.static void
sendPacketSync
(org.bukkit.entity.Player player, Object... packets) Sends a packet to the player synchronously if they're online.static boolean
supports
(int minorNumber) Checks whether the server version is equal or greater than the given version.static boolean
supports
(int minorNumber, int patchNumber) Checks whether the server version is equal or greater than the given version.static boolean
supportsPatch
(int patchNumber) Checks whether the server version is equal or greater than the given version.static Class<?>
toArrayClass
(Class<?> clazz) Gives an array version of a class.static <T> ReflectionUtils.VersionHandler<T>
v
(int version, int patch, T handle) Overload for#v(int, T)
that supports patch versionsstatic <T> ReflectionUtils.CallableVersionHandler<T>
static <T> ReflectionUtils.VersionHandler<T>
v
(int version, T handle) Gives thehandle
object if the server version is equal or greater than the given version.
-
Field Details
-
NMS_VERSION
We use reflection mainly to avoid writing a new class for version barrier. The version barrier is for NMS that uses the Minecraft version as the main package name.E.g. EntityPlayer in 1.15 is in the class
net.minecraft.server.v1_15_R1
but in 1.14 it's innet.minecraft.server.v1_14_R1
In order to maintain cross-version compatibility we cannot import these classes.Performance is not a concern for these specific statically initialized values.
-
MINOR_NUMBER
public static final int MINOR_NUMBERThe raw minor version number. E.g.v1_17_R1
to17
- Since:
- 4.0.0
- See Also:
-
PATCH_NUMBER
public static final int PATCH_NUMBERThe raw patch version number. Refers to the major.minor.patch version scheme. E.g.v1.20.4
to4
v1.18.2
to2
v1.19.1
to1
I'd not recommend developers to support individual patches at all. You should always support the latest patch. For example, between v1.14.0, v1.14.1, v1.14.2, v1.14.3 and v1.14.4 you should only support v1.14.4
This can be used to warn server owners when your plugin will break on older patches.
- Since:
- 7.0.0
- See Also:
-
CRAFTBUKKIT_PACKAGE
Mojang remapped their NMS in 1.17: Spigot Thread -
NMS_PACKAGE
Mojang remapped their NMS in 1.17: Spigot Thread
-
-
Method Details
-
getVersionInformation
Gets the full version information of the server. Useful for including in errors.- Since:
- 7.0.0
-
getLatestPatchNumberOf
Gets the latest known patch number of the given minor version. For example: 1.14 -> 4, 1.17 -> 10 The latest version is expected to get newer patches, so make sure to account for unexpected results.- Parameters:
minorVersion
- the minor version to get the patch number of.- Returns:
- the patch number of the given minor version if recognized, otherwise null.
- Since:
- 7.0.0
-
v
Gives thehandle
object if the server version is equal or greater than the given version. This method is purely for readability and should be always used withReflectionUtils.VersionHandler.orElse(Object)
.- Since:
- 5.0.0
- See Also:
-
v
Overload for#v(int, T)
that supports patch versions- Since:
- 9.5.0
-
v
-
supports
public static boolean supports(int minorNumber) Checks whether the server version is equal or greater than the given version.- Parameters:
minorNumber
- the version to compare the server version with.- Returns:
- true if the version is equal or newer, otherwise false.
- Since:
- 4.0.0
- See Also:
-
supports
public static boolean supports(int minorNumber, int patchNumber) Checks whether the server version is equal or greater than the given version.- Parameters:
minorNumber
- the minor version to compare the server version with.patchNumber
- the patch number to compare the server version with.- Returns:
- true if the version is equal or newer, otherwise false.
- Since:
- 7.1.0
- See Also:
-
supportsPatch
public static boolean supportsPatch(int patchNumber) Checks whether the server version is equal or greater than the given version.- Parameters:
patchNumber
- the version to compare the server version with.- Returns:
- true if the version is equal or newer, otherwise false.
- Since:
- 7.0.0
- See Also:
-
getNMSClass
Get a NMS (net.minecraft.server) class which accepts a package for 1.17 compatibility.- Parameters:
packageName
- the 1.17+ package name of this class.name
- the name of the class.- Returns:
- the NMS class or null if not found.
- Since:
- 4.0.0
-
getNMSClass
Get a NMSNMS_PACKAGE
class.- Parameters:
name
- the name of the class.- Returns:
- the NMS class or null if not found.
- Since:
- 1.0.0
-
sendPacket
@Nonnull public static CompletableFuture<Void> sendPacket(@Nonnull org.bukkit.entity.Player player, @Nonnull Object... packets) Sends a packet to the player asynchronously if they're online. Packets are thread-safe.- Parameters:
player
- the player to send the packet to.packets
- the packets to send.- Returns:
- the async thread handling the packet.
- Since:
- 1.0.0
- See Also:
-
sendPacketSync
public static void sendPacketSync(@Nonnull org.bukkit.entity.Player player, @Nonnull Object... packets) Sends a packet to the player synchronously if they're online.- Parameters:
player
- the player to send the packet to.packets
- the packets to send.- Since:
- 2.0.0
- See Also:
-
getHandle
-
getConnection
-
getCraftClass
Get a CraftBukkit (org.bukkit.craftbukkit) class.- Parameters:
name
- the name of the class to load.- Returns:
- the CraftBukkit class or null if not found.
- Since:
- 1.0.0
-
getArrayClass
Deprecated.UsetoArrayClass(Class)
instead. -
toArrayClass
Gives an array version of a class. For example if you wantedEntityPlayer[]
you'd use:Class EntityPlayer = ReflectionUtils.getNMSClass("...", "EntityPlayer"); Class EntityPlayerArray = ReflectionUtils.toArrayClass(EntityPlayer);
- Parameters:
clazz
- the class to get the array version of. You could use for multi-dimensions arrays too.
-
toArrayClass(Class)
instead.