Class ReflectionUtils

java.lang.Object
xyz.xenondevs.particle.utils.ReflectionUtils

public final class ReflectionUtils extends Object
Since:
30.08.2018
Author:
ByteZ
See Also:
  • Field Details

    • MINECRAFT_VERSION

      public static final double MINECRAFT_VERSION
      The current Minecraft version as an int.
    • PLAYER_CONNECTION_CACHE

      public static final PlayerConnectionCache PLAYER_CONNECTION_CACHE
      A cache for playerconnections.
  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • getPlugin

      public static org.bukkit.plugin.Plugin getPlugin()
      Gets the plugin ParticleLib uses to register event Listeners and start tasks.
      Returns:
      the plugin ParticleLib should use
    • setPlugin

      public static void setPlugin(org.bukkit.plugin.Plugin plugin)
      Sets the plugin ParticleLib uses to register event Listeners and start tasks.
      Parameters:
      plugin - the plugin ParticleLib should use
    • getClassSafe

      public static Class<?> getClassSafe(String path)
      Gets a class but returns null instead of throwing a ClassNotFoundException.
      Parameters:
      path - the path of the class
      Returns:
      the class. If the class isn't found null
    • getNMSPath

      public static String getNMSPath(String path)
      Gets the nms path of a class without depending on versions

      e.g. getNMSPath("Block") = "net.minecraft.server.v1_14_R1.Block" getNMSPath("Entity") = "net.minecraft.server.v1_12_R1.Entity"

      Parameters:
      path - the path that should be added to the nms path
      Returns:
      the nms path
    • getNMSClass

      public static Class<?> getNMSClass(String path)
      Directly gets the class object over the path
      Parameters:
      path - the path of the class
      Returns:
      the class. If the class isn't found null
    • getCraftBukkitPath

      public static String getCraftBukkitPath(String path)
      Gets the craftbukkit path of a class without depending on versions

      e.g. getCraftBukkitPath("CraftChunk") = "org.bukkit.craftbukkit.v1_15_R1.CraftChunk" getCraftBukkitPath("event.CraftEventFactory") = "org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory"

      Parameters:
      path - the path that should be added to the craftbukkit path
      Returns:
      the craftbukkit path
    • getCraftBukkitClass

      public static Class<?> getCraftBukkitClass(String path)
      Method to directly get the class object over the path
      Parameters:
      path - the path of the class
      Returns:
      the class. If the class isn't found null
    • getMethodOrNull

      public static Method getMethodOrNull(Class targetClass, String methodName, Class<?>... parameterTypes)
      Method to not get disturbed by the forced try catch block
      Parameters:
      targetClass - the Class the Method is in
      methodName - the name of the target Method
      parameterTypes - the parameterTypes of the Method
      Returns:
      if found the target Method. If not found null.
    • getFieldOrNull

      public static Field getFieldOrNull(Class targetClass, String fieldName, boolean declared)
      Method to not get disturbed by the forced try catch block
      Parameters:
      targetClass - the Class the Field is in
      fieldName - the name of the target Field
      declared - defines if the target Field is private
      Returns:
      if found the target Field. If not found null.
    • getConstructorOrNull

      public static Constructor getConstructorOrNull(Class targetClass, Class... parameterTypes)
      Gets a constructor without throwing exceptions
      Parameters:
      targetClass - the Class the Constructor is in
      parameterTypes - the parameterTypes of the Constructor
      Returns:
      if found the target Constructor. If not found null.
    • existsClass

      public static boolean existsClass(String path)
      Checks if a class exists
      Parameters:
      path - the path of the class that should be checked
      Returns:
      true if the defined class exists
    • readField

      public static Object readField(Class targetClass, String fieldName, Object object)
      Gets the specified Field over the specified fieldName and the given targetClass. Then reads the specified Field from the specified Object. When the Field is static set the object to null.
      Parameters:
      targetClass - the Class of the field
      fieldName - the name of the searched Field
      object - the Object from which the specified Fields value is to be extracted.
      Returns:
      the extracted value of the specified Field in the specified Object.
    • readField

      public static <T> T readField(Field field, Object object)
      Reads the specified Field from the specified Object. When the Field is static set the object to null.
      Parameters:
      field - the Field from which the value should be extracted.
      object - the Object from which the specified Fields value is to be extracted.
      Returns:
      the extracted value of the specified Field in the specified Object.
    • readDeclaredField

      public static Object readDeclaredField(Class targetClass, String fieldName, Object object)
      Gets the specified declared Field over the specified fieldName and the given targetClass. Then reads the specified Field from the specified Object. When the Field is static set the object to null.
      Parameters:
      targetClass - the Class of the field
      fieldName - the name of the searched Field
      object - the Object from which the specified Fields value is to be extracted.
      Returns:
      the extracted value of the specified Field in the specified Object.
    • readDeclaredField

      public static <T> T readDeclaredField(Field field, Object object)
      Reads the declared specified Field from the specified Object. When the Field is static set the object to null.
      Parameters:
      field - the Field from which the value should be extracted.
      object - the Object from which the specified Fields value is to be extracted.
      Returns:
      the extracted value of the specified Field in the specified Object.
    • writeDeclaredField

      public static void writeDeclaredField(Class targetClass, String fieldName, Object object, Object value)
      Gets the specified declared Field over the specified fieldName and the given targetClass. Then writes the specified value into the specified Field in the given Object.
      Parameters:
      targetClass - the Class of the field.
      fieldName - the name of the searched Field.
      object - the Object whose Fields value should be changed.
      value - the value which should be set in the specified Field.
    • writeDeclaredField

      public static void writeDeclaredField(Field field, Object object, Object value)
      Writes a value to the specified declared Field in the given Object.
      Parameters:
      field - the Field which should be changed.
      object - the Object whose Fields value should be changed.
      value - the value which should be set in the specified Field.
    • writeField

      public static void writeField(Class targetClass, String fieldName, Object object, Object value)
      Gets the specified Field over the specified fieldName and the given targetClass. Then writes the specified value into the specified Field in the given Object.
      Parameters:
      targetClass - the Class of the field.
      fieldName - the name of the searched Field.
      object - the Object whose Fields value should be changed.
      value - the value which should be set in the specified Field.
    • writeField

      public static void writeField(Field field, Object object, Object value)
      Writes a value to the specified Field in the given Object.
      Parameters:
      field - the Field which should be changed.
      object - the Object whose Fields value should be changed.
      value - the value which should be set in the specified Field.
    • getNetMinecraftServerPackagePath

      public static String getNetMinecraftServerPackagePath()
      Returns:
      the nms path
    • getCraftBukkitPackagePath

      public static String getCraftBukkitPackagePath()
      Returns:
      the craftbukkit path
    • getMinecraftKey

      public static Object getMinecraftKey(String key)
      Creates a new MinecraftKey with the given data.
      Parameters:
      key - the data that should be used in the constructor of the key.
      Returns:
      the new MinecraftKey
    • createVector3fa

      public static Object createVector3fa(float x, float y, float z)
      Creates a new Vector3fa instance.
      Parameters:
      x - x value of the vector.
      y - y value of the vector.
      z - z value of the vector.
      Returns:
      a Vector3fa instance with the specified coordinates.
    • createBlockPosition

      public static Object createBlockPosition(org.bukkit.Location location)
      Creates a new BlockPosition.
      Parameters:
      location - the Location of the block.
      Returns:
      the BlockPosition of the location
    • getEntityHandle

      public static Object getEntityHandle(org.bukkit.entity.Entity entity)
      Gets the Entity instance of a CraftEntity
      Parameters:
      entity - the CraftEntity
      Returns:
      the Entity instance of the defined CraftEntity or null if either the given parameter is invalid or an error occurs.
    • getPlayerHandle

      public static Object getPlayerHandle(org.bukkit.entity.Player player)
      Gets the EntityPlayer instance of a CraftPlayer
      Parameters:
      player - the CraftPlayer
      Returns:
      the EntityPlayer instance of the defined CraftPlayer or null if either the given parameter is invalid or an error occurs.
    • getPlayerConnection

      public static Object getPlayerConnection(org.bukkit.entity.Player target)
      Gets the PlayerConnection of a Player
      Parameters:
      target - the target Player
      Returns:
      the PlayerConnection of the specified target Player
    • sendPacket

      public static void sendPacket(org.bukkit.entity.Player player, Object packet)
      Sends a packet to a defined player.
      Parameters:
      player - the player that should receive the packet
      packet - the packet that should be sent
    • getResourceStreamSafe

      public static InputStream getResourceStreamSafe(String resource)
      Gets the InputStream of a resource.
      Parameters:
      resource - the name of the resource
      Returns:
      the InputStream of the resource