Package ch.njol.skript.util
Class Utils
java.lang.Object
ch.njol.skript.util.Utils
Utility class.
-
Field Summary
Fields -
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 Class<?>classForName(String name)Gets class for name.static <T> intfindLastIndex(List<T> list, Checker<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 StringgetChatStyle(String s)static NonNullPair<String,Boolean>static Class<?>getSuperType(Class<?>... cs)static <T extends org.bukkit.entity.Entity>
@Nullable TgetTarget(org.bukkit.entity.LivingEntity entity, @Nullable EntityData<T> type)Gets an entity's target.static <T> booleanisEither(@Nullable T compared, @Nullable T... types)static Stringstatic Stringstatic @Nullable net.md_5.bungee.api.ChatColorparseHexColor(String hex)Tries to get aChatColorfrom the given string.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 intrandom(int start, int end)Gets a random value between start (inclusive) and end (exclusive)static StringreplaceChatStyles(String message)Replaces <chat styles> in the messagestatic StringreplaceEnglishChatStyles(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 StringGets 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
-
HEX_SUPPORTED
public static final boolean HEX_SUPPORTED
-
-
Method Details
-
join
-
join
-
isEither
public static <T> boolean isEither(@Nullable T compared, @Nullable T... types) -
getTarget
public static <T extends org.bukkit.entity.Entity> @Nullable T getTarget(org.bukkit.entity.LivingEntity entity, @Nullable EntityData<T> type)Gets an entity's target.- Parameters:
entity- The entity to get the target oftype- Can be null for any entity- Returns:
- The entity's target
-
getAmount
-
getEnglishPlural
- Parameters:
s- trimmed string- Returns:
- Pair of singular string + boolean whether it was plural
-
toEnglishPlural
Gets the english plural of a word.- Parameters:
s-- 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(String),a(String, boolean)
-
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(String),a(String, boolean)
-
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:
a(String)
-
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 throughchannelwill 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 throughchannelwill 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 IllegalStateExceptionSends 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 aChatColorfrom 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
-
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
Gets class for name. Throws RuntimeException instead of checked one. Use this only when absolutely necessary.- Parameters:
name- Class name.- Returns:
- The class.
-
findLastIndex
-