Class Common

java.lang.Object
dev.demeng.pluginbase.Common

public final class Common extends Object
Commonly used methods and utilities.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The error message for players when an internal error occurs.
    static final boolean
    If the server software the plugin is running on is Spigot or a fork of Spigot.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static @NotNull String
    applyOperator(@Nullable String str, @Nullable UnaryOperator<String> operator)
     
    static @NotNull List<String>
    applyOperator(@Nullable List<String> list, @Nullable UnaryOperator<String> operator)
     
    static @NotNull org.bukkit.inventory.ItemStack
    applyOperator(@Nullable org.bukkit.inventory.ItemStack stack, @Nullable UnaryOperator<String> operator)
     
    static @Nullable Class<?>
    checkClass(@NotNull String className)
    Simple method to check if a class exists.
    static @Nullable Double
    checkDouble(@NotNull String str)
    Attempts to parse a string into a double.
    static @Nullable Float
    checkFloat(@NotNull String str)
    Attempts to parse a string into a float.
    static @Nullable Integer
    checkInt(@NotNull String str)
    Attempts to parse a string into an integer.
    static @Nullable Long
    checkLong(@NotNull String str)
    Attempts to parse a string into a long.
    static void
    error(@Nullable Throwable throwable, @NotNull String description, boolean disable, @NotNull org.bukkit.command.CommandSender... players)
    Reports an error in the plugin.
    static void
    forEachInt(String str, IntConsumer consumer)
    Parses a string sequence of integers and accepts a consumer for each integer.
    static @NotNull String
    formatDecimal(double d)
    Formats a decimal into a human-friendly string that rounds the double to 2 decimal places.
    static @NotNull String
    Gets the name of the plugin, as defined in plugin.yml.
    static <T> T
    getOrDefault(T nullable, T def)
    Returns the nullable value if not null, or the default value if it is null.
    static <T> T
    getOrError(T nullable, @NotNull String description, boolean disable)
    Returns the nullable value if not null, or throws a runtime exception with error if it is null.
    static int
    Gets the server's major Minecraft version.
    static @NotNull String
    Gets the version string, as defined in plugin.yml.
    static boolean
    hasPermission(@NotNull org.bukkit.command.CommandSender sender, @Nullable String permission)
    Checks if the specified command sender has the permission node.
    static boolean
    Checks if the server's major version is at least the specified version.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SPIGOT

      public static final boolean SPIGOT
      If the server software the plugin is running on is Spigot or a fork of Spigot. Used internally for some Spigot-only features or optimizations.
    • PLAYERS_ERROR_MESSAGE

      public static final String PLAYERS_ERROR_MESSAGE
      The error message for players when an internal error occurs.
  • Constructor Details

    • Common

      public Common()
  • Method Details

    • getName

      @NotNull public static @NotNull String getName()
      Gets the name of the plugin, as defined in plugin.yml.
      Returns:
      The name of the plugin
    • getVersion

      @NotNull public static @NotNull String getVersion()
      Gets the version string, as defined in plugin.yml.
      Returns:
      The version of the plugin
    • getServerMajorVersion

      public static int getServerMajorVersion()
      Gets the server's major Minecraft version.

      For example, if a server is running 1.16.4, this will return 16.

      Returns:
      The server's major Minecraft version.
    • isServerVersionAtLeast

      public static boolean isServerVersionAtLeast(int version)
      Checks if the server's major version is at least the specified version.
      Parameters:
      version - The minimum major version
      Returns:
      True if equal or greater to the provided version, false otherwise
    • checkInt

      @Nullable public static @Nullable Integer checkInt(@NotNull @NotNull String str)
      Attempts to parse a string into an integer.
      Parameters:
      str - The string to parse
      Returns:
      The parsed integer, or null if the string is not a valid integer
    • checkFloat

      @Nullable public static @Nullable Float checkFloat(@NotNull @NotNull String str)
      Attempts to parse a string into a float.
      Parameters:
      str - The string to parse
      Returns:
      The parsed float, or null if the string is not a valid float
    • checkLong

      @Nullable public static @Nullable Long checkLong(@NotNull @NotNull String str)
      Attempts to parse a string into a long.
      Parameters:
      str - The string to parse
      Returns:
      The parsed long, or null if the string is not a valid long
    • checkDouble

      @Nullable public static @Nullable Double checkDouble(@NotNull @NotNull String str)
      Attempts to parse a string into a double.
      Parameters:
      str - The string to parse
      Returns:
      The parsed double, or null if the string is not a valid double
    • applyOperator

      @NotNull public static @NotNull String applyOperator(@Nullable @Nullable String str, @Nullable @Nullable UnaryOperator<String> operator)
    • applyOperator

      @NotNull public static @NotNull List<String> applyOperator(@Nullable @Nullable List<String> list, @Nullable @Nullable UnaryOperator<String> operator)
    • applyOperator

      @NotNull public static @NotNull org.bukkit.inventory.ItemStack applyOperator(@Nullable @Nullable org.bukkit.inventory.ItemStack stack, @Nullable @Nullable UnaryOperator<String> operator)
    • checkClass

      @Nullable public static @Nullable Class<?> checkClass(@NotNull @NotNull String className)
      Simple method to check if a class exists.
      Parameters:
      className - The class's package and name (Example: dev.demeng.pluginbase.Validate)
      Returns:
      The actual class if the class exists, null otherwise
    • formatDecimal

      @NotNull public static @NotNull String formatDecimal(double d)
      Formats a decimal into a human-friendly string that rounds the double to 2 decimal places.
      Parameters:
      d - THe double to format
      Returns:
      The formatted double
    • getOrDefault

      @NotNull public static <T> T getOrDefault(@Nullable T nullable, @NotNull T def)
      Returns the nullable value if not null, or the default value if it is null.
      Type Parameters:
      T - The object type being checked and returned
      Parameters:
      nullable - The nullable value
      def - The default value
      Returns:
      The nullable if not null, default otherwise
    • getOrError

      @NotNull public static <T> T getOrError(@Nullable T nullable, @NotNull @NotNull String description, boolean disable)
      Returns the nullable value if not null, or throws a runtime exception with error if it is null.
      Type Parameters:
      T - The object type being checked and returned
      Parameters:
      nullable - The nullable value
      description - The error description if the value is null
      disable - If the plugin should disable if the value is null
      Returns:
      The nullable if not null
    • hasPermission

      public static boolean hasPermission(@NotNull @NotNull org.bukkit.command.CommandSender sender, @Nullable @Nullable String permission)
      Checks if the specified command sender has the permission node. If the permission node is null, empty, or equal to "none", the method will return true.
      Parameters:
      sender - The command sender to check
      permission - The permission to check
      Returns:
      True if the command sender has the permission, false otherwise
    • error

      public static void error(@Nullable @Nullable Throwable throwable, @NotNull @NotNull String description, boolean disable, @NotNull @NotNull org.bukkit.command.CommandSender... players)
      Reports an error in the plugin.
      Parameters:
      throwable - The throwable
      description - A brief description of the error
      disable - If the plugin should be disabled
      players - Any players associated with this error, a message will be sent to them
    • forEachInt

      public static void forEachInt(String str, IntConsumer consumer) throws IllegalArgumentException
      Parses a string sequence of integers and accepts a consumer for each integer. The sequence can either be a single integer (1), a range of integers (1-10), or a list of integers (1,2,3).
      Parameters:
      str - The integer sequence to parse
      consumer - The consumer to accept for each integer
      Throws:
      IllegalArgumentException - If the sequenece is invalid