Package me.despical.commandframework
Class CommandArguments
java.lang.Object
me.despical.commandframework.CommandArguments
A utility class to use command arguments without external
Bukkit parameters and includes some useful methods to improve
code quality and performance.
- Since:
- 1.0.0
- Author:
- Despical
-
Method Summary
Modifier and TypeMethodDescriptionbooleanThis method checks if the current command sender has a cooldown on the command that is associated with currentCommandArgumentsobject.Concatenates all arguments into a singleStringobject.concatRangeOf(int from, int to) Concatenates a range of elements from the specified array into a single string, using a space as the delimiter.findOption(@NotNull String option) @Nullable StringgetArgument(int index) Retrieves the argument at the specified index.@NotNull StringgetArgument(int index, String defaultValue) Returns the indexed element from the arguments array, or thedefaultValueif and only if index is out the bounds.booleangetArgumentAsBoolean(int index) Returns the boolean value of the indexed element from the arguments array.doublegetArgumentAsDouble(int index) Returns the double value of the indexed element from the arguments array.floatgetArgumentAsFloat(int index) Returns the float value of the indexed element from the arguments array.intgetArgumentAsInt(int index) Returns the integer value of the indexed element from the arguments array.longgetArgumentAsLong(int index) Returns the long value of the indexed element from the arguments array.@NotNull String[]Retrieves the array of arguments associated with this object.@NotNull org.bukkit.command.CommandRetrieves the Bukkit command associated with this object.Retrieves the base command associated with this object.@NotNull StringgetLabel()Retrieves the label associated with this object.intReturns the number of arguments passed to the command.Optional<org.bukkit.entity.Player> getPlayer(int index) Gets player object from the server with given argument.Optional<org.bukkit.entity.Player> Gets player object from the server with givenname.<T extends org.bukkit.command.CommandSender>
TDo not try to cast objects except subclasses ofCommandSenderotherwiseClassCastExceptionwill occur.booleanhasPermission(String permission) Returnstrueif the command sender has requiredpermissionor, ifpermissionis empty.booleanReturnstrueif the arguments array is empty, otherwisefalse.booleanisFlagPresent(@NotNull String flag) booleanisFloatingDecimal(int index) Checks if the value obtained from the argument at the specified index can be successfully parsed into a floating-point decimal usingDouble.parseDouble.booleanisFloatingDecimal(String string) Checks if the given string can be successfully parsed into a floating decimal usingDouble.parseDouble.booleanisInteger(int index) Checks if the value obtained from the argument at the specified index can be successfully parsed into an integer usingInteger.parseInt.booleanChecks if the given string can be successfully parsed into an integer usingInteger.parseInt.booleanisNumeric(int index) Checks if the value obtained from the argument at the specified index is numeric, i.e., if it contains only digit characters (0-9).booleanChecks if the given string is numeric, i.e., if it contains only digit characters (0-9).booleanReturnstrueif, and only if, command sender is console.booleanReturnstrueif, and only if, command sender is player.voidsendMessage(String message) Sends message to sender without receiving the command sender.voidsendMessage(String message, Object... params) Sends message to sender without receiving command sender with the given parameters.booleansendMessage(Message message) Sends the specifiedMessageto command sender associated with this object.voidsendMessage(net.kyori.adventure.text.Component component) Sends the specifiedComponentto command sender associated with this object.
-
Method Details
-
getSender
@NotNull public <T extends org.bukkit.command.CommandSender> T getSender()Do not try to cast objects except subclasses ofCommandSenderotherwiseClassCastExceptionwill occur. Also casting forPlayerorConsoleCommandSenderisn't needed.- Type Parameters:
T-CommandSender- Returns:
- sender of command as Player or CommandSender
-
getCommand
Retrieves the base command associated with this object.- Returns:
- the base command.
- Since:
- 1.4.8
-
getBukkitCommand
@NotNull public @NotNull org.bukkit.command.Command getBukkitCommand()Retrieves the Bukkit command associated with this object.- Returns:
- the base command as a Bukkit command.
-
getLabel
Retrieves the label associated with this object.- Returns:
- label of the command.
-
getArguments
Retrieves the array of arguments associated with this object.- Returns:
- arguments of the command.
-
getArgument
Retrieves the argument at the specified index.- Parameters:
index- the index of desired argument.- Returns:
- indexed element or null if index out of bounds
-
getArgument
Returns the indexed element from the arguments array, or thedefaultValueif and only if index is out the bounds.- Parameters:
index- the index of desired argument.defaultValue- the default value to return if the index is out of bounds.- Returns:
- the argument at the specified index, or the default value if the index is out of bounds.
-
getArgumentAsInt
public int getArgumentAsInt(int index) Returns the integer value of the indexed element from the arguments array.- Parameters:
index- the index of desired argument.- Returns:
- Integer if indexed element is primitive type of int or 0 if element is null.
-
getArgumentAsDouble
public double getArgumentAsDouble(int index) Returns the double value of the indexed element from the arguments array.- Parameters:
index- the index of desired argument.- Returns:
- Double if indexed element is primitive type of double or 0 if element is null.
-
getArgumentAsFloat
public float getArgumentAsFloat(int index) Returns the float value of the indexed element from the arguments array.- Parameters:
index- the index of desired argument.- Returns:
- Float if indexed element is primitive type of float or 0 if element is null.
-
getArgumentAsLong
public long getArgumentAsLong(int index) Returns the long value of the indexed element from the arguments array.- Parameters:
index- the index of desired argument.- Returns:
- Long if indexed element is primitive type of long or 0 if element is null.
-
getArgumentAsBoolean
public boolean getArgumentAsBoolean(int index) Returns the boolean value of the indexed element from the arguments array.- Parameters:
index- the index of desired argument.- Returns:
- Boolean if indexed element is primitive type of boolean or 0 if element is null.
-
isArgumentsEmpty
public boolean isArgumentsEmpty()Returnstrueif the arguments array is empty, otherwisefalse.- Returns:
- true if arguments are empty, otherwise false.
-
sendMessage
Sends message to sender without receiving the command sender.- Parameters:
message- the message will be sent to sender.
-
sendMessage
Sends message to sender without receiving command sender with the given parameters.- Parameters:
message- the message will be sent to sender.params- the parameters to format the message.
-
sendMessage
Sends the specifiedMessageto command sender associated with this object.- Parameters:
message- theMessageobject to be sent.- See Also:
-
sendMessage
public void sendMessage(net.kyori.adventure.text.Component component) Sends the specifiedComponentto command sender associated with this object.- Parameters:
component- theComponentobject to be sent.
-
isSenderConsole
public boolean isSenderConsole()Returnstrueif, and only if, command sender is console.- Returns:
trueif, and only if, command sender is console, otherwisefalse.
-
isSenderPlayer
public boolean isSenderPlayer()Returnstrueif, and only if, command sender is player.- Returns:
trueif, and only if, command sender is player, otherwisefalse.
-
hasPermission
Returnstrueif the command sender has requiredpermissionor, ifpermissionis empty.- Parameters:
permission- the permission to check.- Returns:
trueif the command sender has requiredpermissionor, ifpermissionis empty, otherwisefalse.
-
getLength
public int getLength()Returns the number of arguments passed to the command.- Returns:
- length of the arguments array.
-
getPlayer
Gets player object from the server with givenname.- Parameters:
name- the name of player.- Returns:
- player with the given name if online, otherwise empty optional.
- Throws:
IllegalArgumentException- if thenameis null.- Since:
- 1.3.6
- See Also:
-
getPlayer
Gets player object from the server with given argument.- Parameters:
index- the index of desired argument.- Returns:
- player with the given name if online, otherwise empty optional.
- Throws:
IllegalArgumentException- if given index is out of bounds.- Since:
- 1.3.6
- See Also:
-
concatArguments
Concatenates all arguments into a singleStringobject.- Returns:
- all arguments as a single String object.
- Since:
- 1.3.8
-
concatRangeOf
Concatenates a range of elements from the specified array into a single string, using a space as the delimiter.- Parameters:
from- the starting index (inclusive) of the range.to- the ending index (exclusive) of the range.- Returns:
- a string containing the concatenated elements within the specified range, separated by a space.
- Throws:
ArrayIndexOutOfBoundsException- iffromis negative,tois greater than the length of the array, orfromis greater thanto.- Since:
- 1.3.8
-
isNumeric
public boolean isNumeric(int index) Checks if the value obtained from the argument at the specified index is numeric, i.e., if it contains only digit characters (0-9).- Parameters:
index- The index of the argument from which the value is retrieved.- Returns:
trueif the value at the specified argument index is numeric,falseotherwise. Returnsfalsefor null or empty values obtained from the argument.
-
isNumeric
Checks if the given string is numeric, i.e., if it contains only digit characters (0-9).- Parameters:
string- The input string to be checked for numeric content.- Returns:
trueif the input string is numeric,falseotherwise. Returnsfalsefor null or empty strings.
-
isInteger
public boolean isInteger(int index) Checks if the value obtained from the argument at the specified index can be successfully parsed into an integer usingInteger.parseInt.- Parameters:
index- The index of the argument from which the value is retrieved.- Returns:
trueif the value at the specified argument index can be parsed into an integer,falseotherwise. Returnsfalsefor null or empty values obtained from the argument. Also returnsfalsefor values that cannot be parsed into an integer.
-
isInteger
Checks if the given string can be successfully parsed into an integer usingInteger.parseInt.- Parameters:
string- The input string to be checked for its ability to be parsed into an integer.- Returns:
trueif the string can be parsed into an integer,falseotherwise. Returnsfalsefor null strings or strings that cannot be parsed into an integer.
-
isFloatingDecimal
public boolean isFloatingDecimal(int index) Checks if the value obtained from the argument at the specified index can be successfully parsed into a floating-point decimal usingDouble.parseDouble.- Parameters:
index- The index of the argument from which the value is retrieved.- Returns:
trueif the value at the specified argument index can be parsed into a floating-point decimal,falseotherwise. Returnsfalsefor null or empty values obtained from the argument. Also returnsfalsefor values that cannot be parsed into a floating-point decimal.
-
isFloatingDecimal
Checks if the given string can be successfully parsed into a floating decimal usingDouble.parseDouble. Supports primitive types such asInteger,Float,Double,Long, etc.- Parameters:
string- The input string to be checked for its ability to be parsed into a decimal.- Returns:
trueif the string can be parsed into a decimal,falseotherwise. Returnsfalsefor null strings or strings that cannot be parsed into a decimal.
-
checkCooldown
public boolean checkCooldown()This method checks if the current command sender has a cooldown on the command that is associated with currentCommandArgumentsobject.For example,
Note that execution will be stopped if this method returns@Command(name = "test") @Cooldown(cooldown = 5) public void testCommand(CommandArguments args) { if (args.getLength() != 1) { // if sender has cooldown, execution will be end here args.checkCooldown(); } args.sendMessage("Test command successfully executed."); }true.- Returns:
trueif the sender has a cooldown on this command
-
getOption
-
findOption
-
isFlagPresent
-