Class Utils
java.lang.Object
ch.njol.skript.util.Utils
Utility class.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordStores the result ofisPlural(String).protected static final record -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Deque<Utils.WordEnding> static final Random -
Method Summary
Modifier and TypeMethodDescriptionstatic StringAdds 'a' or 'an' to the given string, depending on the first character of the string.static StringAdds 'a' or 'an' to the given string, depending on the first character of the string.static StringAdds 'A' or 'An' to the given string, depending on the first character of the string.static voidaddPluralOverride(String singular, String plural) Adds a singular/plural word override for the given words.static Class<?> classForName(String name) Gets class for name.static <T> intfindLastIndex(List<T> list, Predicate<T> checker) static doublegetBlockHeight(int type, byte data) Gets the collision height of solid or partially-solid blocks at the center of the block.static @Nullable StringDeprecated, for removal: This API element is subject to removal in a future version.static Class<?>[]getClasses(org.bukkit.plugin.Plugin plugin, String basePackage, String... subPackages) Deprecated, for removal: This API element is subject to removal in a future version.UseClassLoaderinstead.static Class<?> getComponentType(Class<?> cls) static NonNullPair<String, Boolean> getEnglishPlural(String word) Deprecated, for removal: This API element is subject to removal in a future version.UseisPlural(String)instead.static @Nullable FilegetFile(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> booleanisEither(T compared, T... types) static booleanstatic Utils.PluralResultReturns whether a word is plural.static booleanisValidUUID(String uuid) Checks if the provided string is a validUUID.static Stringstatic Stringstatic intloadedRemovedClassWarning(Class<?> source) Prints a warning about the loading/use of a class that has been deprecated or removed.static @Nullable net.md_5.bungee.api.ChatColorparseHexColor(String string) Deprecated, for removal: This API element is subject to removal in a future version.SeeTextComponentParser.static intParses a number that was validated to be an integer but might still result in aNumberFormatExceptionwhen parsed withInteger.parseInt(String)due to overflow.static longParses a number that was validated to be an integer but might still result in aNumberFormatExceptionwhen parsed withLong.parseLong(String)due to overflow.static @Nullable StringparseUnicode(String string) Deprecated, for removal: This API element is subject to removal in a future version.SeeTextComponentParser.static intrandom(int start, int end) Gets a random value between start (inclusive) and end (exclusive)static @NotNull StringreplaceChatStyles(String message) Deprecated, for removal: This API element is subject to removal in a future version.SeeTextComponentParser.static @NotNull StringreplaceEnglishChatStyles(String message) Deprecated, for removal: This API element is subject to removal in a future version.SeeTextComponentParser.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 StringtoEnglishPlural(String word) Gets the english plural of a word.static StringtoEnglishPlural(String s, boolean p) Gets the plural of a word (or not if p is false)
-
Field Details
-
random
-
plurals
-
-
Method Details
-
join
-
join
-
isEither
public static <T> boolean isEither(@Nullable T compared, @Nullable T... types) -
getAmount
-
getClasses
@Deprecated(since="2.10.0", forRemoval=true) public static Class<?>[] getClasses(org.bukkit.plugin.Plugin plugin, String basePackage, String... subPackages) throws IOException Deprecated, for removal: This API element is subject to removal in a future version.UseClassLoaderinstead.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
@Deprecated(forRemoval=true, since="2.14") public static NonNullPair<String,Boolean> getEnglishPlural(String word) Deprecated, for removal: This API element is subject to removal in a future version.UseisPlural(String)instead. -
isPlural
Returns whether a word is plural. If it is,updatedcontains the single variant of the word. Otherwise,updated == word.- Parameters:
word- The word to check.- Returns:
- A pair with the updated word and a boolean indicating whether it was plural.
-
addPluralOverride
Adds a singular/plural word override for the given words. This is inserted first in the list of words to be checked: it will always be matched and will override all other plurality rules. This will only match the wordexactly, and will not apply to derivations of the word.- Parameters:
singular- The singular form of the wordplural- The plural form of the word
-
toEnglishPlural
-
toEnglishPlural
-
a
-
A
-
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 through
channelwill 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 through
channelwill 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
@Deprecated(since="2.15", forRemoval=true) @Nullable public static @Nullable String getChatStyle(String s) Deprecated, for removal: This API element is subject to removal in a future version.SeeTextComponentParser. -
replaceChatStyles
@Deprecated(since="2.15", forRemoval=true) @NotNull public static @NotNull String replaceChatStyles(String message) Deprecated, for removal: This API element is subject to removal in a future version.SeeTextComponentParser. -
replaceEnglishChatStyles
@Deprecated(since="2.15", forRemoval=true) @NotNull public static @NotNull String replaceEnglishChatStyles(String message) Deprecated, for removal: This API element is subject to removal in a future version.SeeTextComponentParser. -
parseUnicode
@Deprecated(since="2.15", forRemoval=true) @Nullable public static @Nullable String parseUnicode(String string) Deprecated, for removal: This API element is subject to removal in a future version.SeeTextComponentParser. -
parseHexColor
@Deprecated(since="2.15", forRemoval=true) @Nullable public static @Nullable net.md_5.bungee.api.ChatColor parseHexColor(String string) Deprecated, for removal: This API element is subject to removal in a future version.SeeTextComponentParser. -
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
-
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 aNumberFormatExceptionwhen parsed withInteger.parseInt(String)due to overflow. This method will returnInteger.MIN_VALUEorInteger.MAX_VALUErespectively if that happens.- Parameters:
s-- Returns:
- The parsed integer,
Integer.MIN_VALUEorInteger.MAX_VALUErespectively
-
parseLong
Parses a number that was validated to be an integer but might still result in aNumberFormatExceptionwhen parsed withLong.parseLong(String)due to overflow. This method will returnLong.MIN_VALUEorLong.MAX_VALUErespectively if that happens.- Parameters:
s-- Returns:
- The parsed long,
Long.MIN_VALUEorLong.MAX_VALUErespectively
-
classForName
-
findLastIndex
-
isInteger
-
loadedRemovedClassWarning
Prints a warning about the loading/use of a class that has been deprecated or removed. This is a fairly-unsafe method and should only be used during class-loading.- Parameters:
source- The class about which to print the warning. This MUST be the class calling this method.- Returns:
- 0 (for use by interfaces)
-
isValidUUID
-
getComponentType
-
TextComponentParser.