Skip to content

Commit

Permalink
Added #getArgument(String,String) method and updated javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
Despical committed Apr 27, 2024
1 parent 105046b commit 7afbf63
Showing 1 changed file with 78 additions and 40 deletions.
118 changes: 78 additions & 40 deletions src/main/java/me/despical/commandframework/CommandArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
/**
* A utility class to use command arguments without external
* Bukkit parameters and includes some useful methods to improve
* code performance and quality.
* code quality and performance.
*
* @author Despical
* @since 1.0.0
Expand Down Expand Up @@ -67,14 +67,18 @@ public <T extends CommandSender> T getSender() {
}

/**
* @return base command.
* Retrieves the base command associated with this object.
*
* @return The base command.
*/
@NotNull
public Command getCommand() {
return command;
}

/**
* Retrieves the label associated with this object.
*
* @return label of the command.
*/
@NotNull
Expand All @@ -83,6 +87,8 @@ public String getLabel() {
}

/**
* Retrieves the array of arguments associated with this object.
*
* @return arguments of the command.
*/
@NotNull
Expand All @@ -93,71 +99,97 @@ public String[] getArguments() {
// SOME GETTER METHODS FOR COMMON PRIMITIVE TYPES //

/**
* @param i the index of desired argument.
* Retrieves the argument at the specified index.
*
* @param index the index of desired argument.
* @return indexed element or null if index out of bounds
*/
@Nullable
public String getArgument(int i) {
return arguments.length > i && i >= 0 ? arguments[i] : null;
public String getArgument(int index) {
return arguments.length > index && index >= 0 ? arguments[index] : null;
}

/**
* Returns the indexed element from the arguments array, or the {@code defaultValue}
* if and only if index is out the bounds.
*
* @param index the index of desired argument.
* @param defaultValue the default value to return if the index is out of bounds.
* @return the argument at the specified index, or the default value if the index is out of bounds.
*/
@NotNull
public String getArgument(int index, String defaultValue) {
return arguments.length > index && index >= 0 ? arguments[index] : defaultValue;
}

/**
* @param i the index of desired argument.
* Returns the integer value of the indexed element from the arguments array.
*
* @param index the index of desired argument.
* @return Integer if indexed element is primitive type of int
* or 0 if element is null.
*/
public int getArgumentAsInt(int i) {
return Utils.getInt(this.getArgument(i));
public int getArgumentAsInt(int index) {
return Utils.getInt(this.getArgument(index));
}

/**
* @param i the index of desired argument.
* Returns the double value of the indexed element from the arguments array.
*
* @param index the index of desired argument.
* @return Double if indexed element is primitive type of double
* or 0 if element is null.
*/
public double getArgumentAsDouble(int i) {
return Utils.getDouble(this.getArgument(i));
public double getArgumentAsDouble(int index) {
return Utils.getDouble(this.getArgument(index));
}

/**
* @param i the index of desired argument.
* Returns the float value of the indexed element from the arguments array.
*
* @param index the index of desired argument.
* @return Float if indexed element is primitive type of float
* or 0 if element is null.
*/
public float getArgumentAsFloat(int i) {
return Utils.getFloat(this.getArgument(i));
public float getArgumentAsFloat(int index) {
return Utils.getFloat(this.getArgument(index));
}

/**
* @param i the index of desired argument.
* Returns the long value of the indexed element from the arguments array.
*
* @param index the index of desired argument.
* @return Long if indexed element is primitive type of long
* or 0 if element is null.
*/
public long getArgumentAsLong(int i) {
return Utils.getLong(this.getArgument(i));
public long getArgumentAsLong(int index) {
return Utils.getLong(this.getArgument(index));
}

/**
* @param i the index of desired argument.
* Returns the boolean value of the indexed element from the arguments array.
*
* @param index the index of desired argument.
* @return Boolean if indexed element is primitive type of boolean
* or 0 if element is null.
*/
public boolean getArgumentAsBoolean(int i) {
return "true".equalsIgnoreCase(this.getArgument(i));
public boolean getArgumentAsBoolean(int index) {
return "true".equalsIgnoreCase(this.getArgument(index));
}

// ---------------------------------------------- //

/**
* Returns {@code true} if the arguments array is empty, otherwise {@code false}.
*
* @return true if arguments are empty, otherwise false.
*/
public boolean isArgumentsEmpty() {
return arguments.length == 0;
}

/**
* Sends message to sender without receiving command
* sender.
* Sends message to sender without receiving the command sender.
*
* @param message the message will be sent to sender.
*/
Expand All @@ -181,34 +213,40 @@ public void sendMessage(String message, Object... params) {
}

/**
* Checks if command sender is console.
* Returns {@code true} if, and only if, command sender is console.
*
* @return true if sender is console, otherwise false
* @return {@code true} if, and only if, command sender is console, otherwise
* {@code false}.
*/
public boolean isSenderConsole() {
return !isSenderPlayer();
}

/**
* Checks if command sender is player.
* Returns {@code true} if, and only if, command sender is player.
*
* @return true if sender is player, otherwise false
* @return {@code true} if, and only if, command sender is player, otherwise
* {@code false}.
*/
public boolean isSenderPlayer() {
return commandSender instanceof Player;
}

/**
* Checks if command sender has specified permission.
* Returns {@code true} if the command sender has required {@code permission} or, if
* {@code permission} is empty.
*
* @param permission the permission to check.
* @return true if sender has permission or {@code permission} is empty, otherwise false.
* @return {@code true} if the command sender has required {@code permission} or, if
* {@code permission} is empty, otherwise {@code false}.
*/
public boolean hasPermission(String permission) {
return permission.isEmpty() || commandSender.hasPermission(permission);
}

/**
* Returns the number of arguments passed to the command.
*
* @return length of the arguments array.
*/
public int getLength() {
Expand Down Expand Up @@ -236,7 +274,7 @@ public Optional<Player> getPlayer(String name) {
/**
* Gets player object from the server with given argument.
*
* @param i
* @param index
* the index of desired argument.
*
* @return player with the given name if online, otherwise
Expand All @@ -248,8 +286,8 @@ public Optional<Player> getPlayer(String name) {
* @see Optional#empty()
* @since 1.3.6
*/
public Optional<Player> getPlayer(int i) {
return this.getPlayer(this.getArgument(i));
public Optional<Player> getPlayer(int index) {
return this.getPlayer(this.getArgument(index));
}

/**
Expand Down Expand Up @@ -284,12 +322,12 @@ public String concatenateRangeOf(int from, int to) {
* Checks if the value obtained from the argument at the specified index is numeric,
* i.e., if it contains only digit characters (0-9).
*
* @param i The index of the argument from which the value is retrieved.
* @param index The index of the argument from which the value is retrieved.
* @return {@code true} if the value at the specified argument index is numeric, {@code false} otherwise.
* Returns {@code false} for null or empty values obtained from the argument.
*/
public boolean isNumeric(int i) {
return this.isNumeric(this.getArgument(i));
public boolean isNumeric(int index) {
return this.isNumeric(this.getArgument(index));
}

/**
Expand All @@ -310,13 +348,13 @@ public boolean isNumeric(String string) {
* Checks if the value obtained from the argument at the specified index can be successfully
* parsed into an integer using {@code Integer.parseInt}.
*
* @param i The index of the argument from which the value is retrieved.
* @param index The index of the argument from which the value is retrieved.
* @return {@code true} if the value at the specified argument index can be parsed into an integer,
* {@code false} otherwise. Returns {@code false} for null or empty values obtained from the argument.
* Also returns {@code false} for values that cannot be parsed into an integer.
*/
public boolean isInteger(int i) {
return this.isInteger(this.getArgument(i));
public boolean isInteger(int index) {
return this.isInteger(this.getArgument(index));
}

/**
Expand All @@ -339,13 +377,13 @@ public boolean isInteger(String string) {
* Checks if the value obtained from the argument at the specified index can be successfully
* parsed into a floating-point decimal using {@code Double.parseDouble}.
*
* @param i The index of the argument from which the value is retrieved.
* @param index The index of the argument from which the value is retrieved.
* @return {@code true} if the value at the specified argument index can be parsed into a floating-point decimal,
* {@code false} otherwise. Returns {@code false} for null or empty values obtained from the argument.
* Also returns {@code false} for values that cannot be parsed into a floating-point decimal.
*/
public boolean isFloatingDecimal(int i) {
return this.isFloatingDecimal(this.getArgument(i));
public boolean isFloatingDecimal(int index) {
return this.isFloatingDecimal(this.getArgument(index));
}

/**
Expand Down

0 comments on commit 7afbf63

Please sign in to comment.