Class Utils

java.lang.Object
ch.njol.skript.util.Utils

public abstract class Utils extends Object
Utility class.
  • Field Details

    • random

      public static final Random random
    • HEX_SUPPORTED

      public static final boolean HEX_SUPPORTED
    • COPY_SUPPORTED

      public static final boolean COPY_SUPPORTED
  • Method Details

    • join

      public static String join(Object[] objects)
    • join

      public static String join(Iterable<?> objects)
    • isEither

      public static <T> boolean isEither(@Nullable T compared, @Nullable T... types)
    • getAmount

      public static Pair<String,Integer> getAmount(String s)
    • getClasses

      public static Class<?>[] getClasses(org.bukkit.plugin.Plugin plugin, String basePackage, String... subPackages) throws IOException
      Loads classes of the plugin by package. Useful for registering many syntax elements like Skript does it.
      Parameters:
      basePackage - The base package to add to all sub packages, e.g. "ch.njol.skript".
      subPackages - Which subpackages of the base package should be loaded, e.g. "expressions", "conditions", "effects". Subpackages of these packages will be loaded as well. Use an empty array to load all subpackages of the base package.
      Returns:
      This SkriptAddon
      Throws:
      IOException - If some error occurred attempting to read the plugin's jar file.
    • getFile

      public static @Nullable File getFile(org.bukkit.plugin.Plugin plugin)
      The first invocation of this method uses reflection to invoke the protected method JavaPlugin.getFile() to get the plugin's jar file.
      Returns:
      The jar file of the plugin.
    • getEnglishPlural

      public static NonNullPair<String,Boolean> getEnglishPlural(String s)
      Parameters:
      s - trimmed string
      Returns:
      Pair of singular string + boolean whether it was plural
    • toEnglishPlural

      public static String toEnglishPlural(String s)
      Gets the english plural of a word.
      Parameters:
      s -
      Returns:
      The english plural of the given word
    • toEnglishPlural

      public static String toEnglishPlural(String s, boolean p)
      Gets the plural of a word (or not if p is false)
      Parameters:
      s -
      p -
      Returns:
      The english plural of the given word, or the word itself if p is false.
    • a

      public static String a(String s)
      Adds 'a' or 'an' to the given string, depending on the first character of the string.
      Parameters:
      s - The string to add the article to
      Returns:
      The given string with an appended a/an and a space at the beginning
      See Also:
    • A

      public static String A(String s)
      Adds 'A' or 'An' to the given string, depending on the first character of the string.
      Parameters:
      s - The string to add the article to
      Returns:
      The given string with an appended A/An and a space at the beginning
      See Also:
    • a

      public static String a(String s, boolean capA)
      Adds 'a' or 'an' to the given string, depending on the first character of the string.
      Parameters:
      s - The string to add the article to
      capA - Whether to use a capital a or not
      Returns:
      The given string with an appended a/an (or A/An if capA is true) and a space at the beginning
      See Also:
    • getBlockHeight

      public static double getBlockHeight(int type, byte data)
      Gets the collision height of solid or partially-solid blocks at the center of the block. This is mostly for use in the teleport effect.

      This version operates on numeric ids, thus only working on Minecraft 1.12 or older.

      Parameters:
      type -
      Returns:
      The block's height at the center
    • sendPluginMessage

      public static CompletableFuture<com.google.common.io.ByteArrayDataInput> sendPluginMessage(String channel, String... data)
      Sends a plugin message using the first player from Bukkit.getOnlinePlayers(). The next plugin message to be received through channel will be assumed to be the response.
      Parameters:
      channel - the channel for this plugin message
      data - the data to add to the outgoing message
      Returns:
      a completable future for the message of the responding plugin message, if there is one. this completable future will complete exceptionally if no players are online.
    • sendPluginMessage

      public static CompletableFuture<com.google.common.io.ByteArrayDataInput> sendPluginMessage(org.bukkit.entity.Player player, String channel, String... data)
      Sends a plugin message using the from player. The next plugin message to be received through channel will be assumed to be the response.
      Parameters:
      player - the player to send the plugin message through
      channel - the channel for this plugin message
      data - the data to add to the outgoing message
      Returns:
      a completable future for the message of the responding plugin message, if there is one. this completable future will complete exceptionally if no players are online.
    • sendPluginMessage

      public static CompletableFuture<com.google.common.io.ByteArrayDataInput> sendPluginMessage(String channel, Predicate<com.google.common.io.ByteArrayDataInput> messageVerifier, String... data) throws IllegalStateException
      Sends a plugin message using the first player from Bukkit.getOnlinePlayers().
      Parameters:
      channel - the channel for this plugin message
      messageVerifier - verifies that a plugin message is the response to the sent message
      data - the data to add to the outgoing message
      Returns:
      a completable future for the message of the responding plugin message, if there is one. this completable future will complete exceptionally if the player is null.
      Throws:
      IllegalStateException - when there are no players online
    • sendPluginMessage

      public static CompletableFuture<com.google.common.io.ByteArrayDataInput> sendPluginMessage(org.bukkit.entity.Player player, String channel, Predicate<com.google.common.io.ByteArrayDataInput> messageVerifier, String... data)
      Sends a plugin message. Example usage using the "GetServers" bungee plugin message channel via an overload: Utils.sendPluginMessage("BungeeCord", r -> "GetServers".equals(r.readUTF()), "GetServers") .thenAccept(response -> Bukkit.broadcastMessage(response.readUTF()) // comma delimited server broadcast .exceptionally(ex -> { Skript.warning("Failed to get servers because there are no players online"); return null; });
      Parameters:
      player - the player to send the plugin message through
      channel - the channel for this plugin message
      messageVerifier - verifies that a plugin message is the response to the sent message
      data - the data to add to the outgoing message
      Returns:
      a completable future for the message of the responding plugin message, if there is one. this completable future will complete exceptionally if the player is null.
    • getChatStyle

      public static @Nullable String getChatStyle(String s)
    • replaceChatStyles

      public static String replaceChatStyles(String message)
      Replaces <chat styles> in the message
      Parameters:
      message -
      Returns:
      message with localised chat styles converted to Minecraft's format
    • replaceEnglishChatStyles

      public static String replaceEnglishChatStyles(String message)
      Replaces english <chat styles> in the message. This is used for messages in the language file as the language of colour codes is not well defined while the language is changing, and for some hardcoded messages.
      Parameters:
      message -
      Returns:
      message with english chat styles converted to Minecraft's format
    • parseHexColor

      public static @Nullable net.md_5.bungee.api.ChatColor parseHexColor(String hex)
      Tries to get a ChatColor from the given string.
      Parameters:
      hex - The hex code to parse.
      Returns:
      The ChatColor, or null if it couldn't be parsed.
    • random

      public static int random(int start, int end)
      Gets a random value between start (inclusive) and end (exclusive)
      Parameters:
      start -
      end -
      Returns:
      start + random.nextInt(end - start)
    • getSuperType

      public static Class<?> getSuperType(Class<?>... cs)
    • parseInt

      public static int parseInt(String s)
      Parses a number that was validated to be an integer but might still result in a NumberFormatException when parsed with Integer.parseInt(String) due to overflow. This method will return Integer.MIN_VALUE or Integer.MAX_VALUE respectively if that happens.
      Parameters:
      s -
      Returns:
      The parsed integer, Integer.MIN_VALUE or Integer.MAX_VALUE respectively
    • parseLong

      public static long parseLong(String s)
      Parses a number that was validated to be an integer but might still result in a NumberFormatException when parsed with Long.parseLong(String) due to overflow. This method will return Long.MIN_VALUE or Long.MAX_VALUE respectively if that happens.
      Parameters:
      s -
      Returns:
      The parsed long, Long.MIN_VALUE or Long.MAX_VALUE respectively
    • classForName

      public static Class<?> classForName(String name)
      Gets class for name. Throws RuntimeException instead of checked one. Use this only when absolutely necessary.
      Parameters:
      name - Class name.
      Returns:
      The class.
    • findLastIndex

      public static <T> int findLastIndex(List<T> list, Checker<T> checker)
      Finds the index of the last in a List that matches the given Checker.
      Parameters:
      list - the List to search.
      checker - the Checker to match elements against.
      Returns:
      the index of the element found, or -1 if no matching element was found.