Package ch.njol.skript.util
Class Utils
java.lang.Object
ch.njol.skript.util.Utils
Utility class.
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final boolean
static final boolean
protected static final Deque
<Utils.WordEnding> static final Random
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Adds 'a' or 'an' to the given string, depending on the first character of the string.static String
Adds 'a' or 'an' to the given string, depending on the first character of the string.static String
Adds 'A' or 'An' to the given string, depending on the first character of the string.static Class
<?> classForName
(String name) Gets class for name.static <T> int
findLastIndex
(List<T> list, Checker<T> checker) static double
getBlockHeight
(int type, byte data) Gets the collision height of solid or partially-solid blocks at the center of the block.static @Nullable String
static Class<?>[]
getClasses
(org.bukkit.plugin.Plugin plugin, String basePackage, String... subPackages) Loads classes of the plugin by package.static NonNullPair
<String, Boolean> getEnglishPlural
(String word) static @Nullable File
getFile
(org.bukkit.plugin.Plugin plugin) The first invocation of this method uses reflection to invoke the protected methodJavaPlugin.getFile()
to get the plugin's jar file.static Class
<?> getSuperType
(Class<?>... classes) static <Found,
Type extends Found>
Class<Found> highestDenominator
(Class<? super Found> bestGuess, @NotNull Class<? extends Type> @NotNull ... classes) Searches for the highest common denominator of the given types; in other words, the first supertype they all share.static <T> boolean
isEither
(T compared, T... types) static boolean
static String
static String
static @Nullable net.md_5.bungee.api.ChatColor
parseHexColor
(String hex) Tries to get aChatColor
from the given string.static int
Parses a number that was validated to be an integer but might still result in aNumberFormatException
when parsed withInteger.parseInt(String)
due to overflow.static long
Parses a number that was validated to be an integer but might still result in aNumberFormatException
when parsed withLong.parseLong(String)
due to overflow.static int
random
(int start, int end) Gets a random value between start (inclusive) and end (exclusive)static String
replaceChatStyles
(String message) Replaces <chat styles> in the messagestatic String
replaceEnglishChatStyles
(String message) Replaces english <chat styles> in the message.static CompletableFuture
<com.google.common.io.ByteArrayDataInput> sendPluginMessage
(String channel, String... data) Sends a plugin message using the first player fromBukkit.getOnlinePlayers()
.static CompletableFuture
<com.google.common.io.ByteArrayDataInput> sendPluginMessage
(String channel, Predicate<com.google.common.io.ByteArrayDataInput> messageVerifier, String... data) Sends a plugin message using the first player fromBukkit.getOnlinePlayers()
.static CompletableFuture
<com.google.common.io.ByteArrayDataInput> sendPluginMessage
(org.bukkit.entity.Player player, String channel, String... data) Sends a plugin message using the fromplayer
.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.static String
toEnglishPlural
(String word) Gets the english plural of a word.static String
toEnglishPlural
(String s, boolean p) Gets the plural of a word (or not if p is false)
-
Field Details
-
random
-
plurals
-
HEX_SUPPORTED
public static final boolean HEX_SUPPORTED -
COPY_SUPPORTED
public static final boolean COPY_SUPPORTED
-
-
Method Details
-
join
-
join
-
isEither
public static <T> boolean isEither(@Nullable T compared, @Nullable T... types) -
getAmount
-
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
The first invocation of this method uses reflection to invoke the protected methodJavaPlugin.getFile()
to get the plugin's jar file.- Returns:
- The jar file of the plugin.
-
getEnglishPlural
- Parameters:
word
- trimmed string- Returns:
- Pair of singular string + boolean whether it was plural
-
toEnglishPlural
Gets the english plural of a word.- Parameters:
word
-- Returns:
- The english plural of the given word
-
toEnglishPlural
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
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
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
Adds 'a' or 'an' to the given string, depending on the first character of the string.- Parameters:
s
- The string to add the article tocapA
- 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 theteleport 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 fromBukkit.getOnlinePlayers()
. The next plugin message to be received throughchannel
will be assumed to be the response.- Parameters:
channel
- the channel for this plugin messagedata
- 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 fromplayer
. The next plugin message to be received throughchannel
will be assumed to be the response.- Parameters:
player
- the player to send the plugin message throughchannel
- the channel for this plugin messagedata
- 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 fromBukkit.getOnlinePlayers()
.- Parameters:
channel
- the channel for this plugin messagemessageVerifier
- verifies that a plugin message is the response to the sent messagedata
- 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 throughchannel
- the channel for this plugin messagemessageVerifier
- verifies that a plugin message is the response to the sent messagedata
- 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
-
replaceChatStyles
Replaces <chat styles> in the message- Parameters:
message
-- Returns:
- message with localised chat styles converted to Minecraft's format
-
replaceEnglishChatStyles
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
Tries to get aChatColor
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
- See Also:
-
highestDenominator
@SafeVarargs public static <Found,Type extends Found> Class<Found> highestDenominator(Class<? super Found> bestGuess, @NotNull @NotNull Class<? extends Type> @NotNull ... classes) Searches for the highest common denominator of the given types; in other words, the first supertype they all share.Arbitrary Selection
Classes may have multiple highest common denominators: interfaces that they share which do not extend each other. This method selects a superclass first (where possible) but its selection of interfaces is quite random. For this reason, it is advised to specify a "best guess" class as the first parameter, which will be selected if it's appropriate. Note that if the "best guess" is not a real supertype, it can never be selected.- Type Parameters:
Found
- The highest common denominator foundType
- The input type spread- Parameters:
bestGuess
- The fallback class to guessclasses
- The types to check- Returns:
- The most appropriate common class of all provided
-
parseInt
Parses a number that was validated to be an integer but might still result in aNumberFormatException
when parsed withInteger.parseInt(String)
due to overflow. This method will returnInteger.MIN_VALUE
orInteger.MAX_VALUE
respectively if that happens.- Parameters:
s
-- Returns:
- The parsed integer,
Integer.MIN_VALUE
orInteger.MAX_VALUE
respectively
-
parseLong
Parses a number that was validated to be an integer but might still result in aNumberFormatException
when parsed withLong.parseLong(String)
due to overflow. This method will returnLong.MIN_VALUE
orLong.MAX_VALUE
respectively if that happens.- Parameters:
s
-- Returns:
- The parsed long,
Long.MIN_VALUE
orLong.MAX_VALUE
respectively
-
classForName
Gets class for name. Throws RuntimeException instead of checked one. Use this only when absolutely necessary.- Parameters:
name
- Class name.- Returns:
- The class.
-
findLastIndex
-
isInteger
-