diff --git a/src/main/java/xyz/srnyx/annoyingapi/command/AnnoyingSender.java b/src/main/java/xyz/srnyx/annoyingapi/command/AnnoyingSender.java index 135b430..580035e 100644 --- a/src/main/java/xyz/srnyx/annoyingapi/command/AnnoyingSender.java +++ b/src/main/java/xyz/srnyx/annoyingapi/command/AnnoyingSender.java @@ -176,6 +176,46 @@ public Optional getPlayerOptional() { return Optional.ofNullable(getPlayerOrNull()); } + /** + * Gets the argument at the specified index + * + * @param index the argument index + * + * @return the argument at the specified index + */ + @Nullable + public String getArgument(int index) { + return args == null || args.length <= index ? null : args[index]; + } + + /** + * Gets the argument at the specified index after applying the specified function + *
If it's {@code null} before/after the function, send the invalid argument message + *
Example usage: + *
{@code
+     * final Player target = sender.getArgument(2, Bukkit::getPlayer);
+     * if (target == null) return;
+     * }
+ * + * @param index the argument index + * @param function the function to apply to the argument + * + * @return the argument at the specified index after applying the specified function + * + * @param the type of the argument + */ + @Nullable + public T getArgument(int index, @NotNull Function function) { + final String argument = getArgument(index); + if (argument == null) { + invalidArgumentByIndex(index); + return null; + } + final T value = function.apply(argument); + if (value == null) invalidArgument(argument); + return value; + } + /** * Gets the argument at the specified index as an {@link Optional} * @@ -184,13 +224,13 @@ public Optional getPlayerOptional() { * @return the argument at the specified index as an {@link Optional} */ @NotNull - public Optional getArgument(int index) { - return args == null || args.length <= index ? Optional.empty() : Optional.ofNullable(args[index]); + public Optional getArgumentOptional(int index) { + return Optional.ofNullable(getArgument(index)); } /** * Gets the argument at the specified index as an {@link Optional} after applying the specified function - *
If it's empty after the function, send the invalid argument message + *
If it's {@link Optional#empty() empty} before/after the function, send the invalid argument message *
Example usage: *
{@code
      * final Player target = sender.getArgument(2, Bukkit::getPlayer).orElse(null);
@@ -205,12 +245,10 @@ public Optional getArgument(int index) {
      * @param            the type of the argument
      */
     @NotNull
-    public  Optional getArgument(int index, @NotNull Function function) {
-        return getArgument(index).map(argument -> {
-            final T value = function.apply(argument);
-            if (value == null) invalidArgument(argument);
-            return value;
-        });
+    public  Optional getArgumentOptional(int index, @NotNull Function function) {
+        final Optional optional = getArgumentOptional(index).map(function);
+        if (!optional.isPresent()) invalidArgumentByIndex(index);
+        return optional;
     }
 
     /**