From 747dbdcf3ddd38f0115ce350bc5b3194b4693af1 Mon Sep 17 00:00:00 2001 From: GliczDev <67753196+GliczDev@users.noreply.github.com> Date: Fri, 1 Aug 2025 00:48:00 +0200 Subject: [PATCH 1/2] Rework CommandArguments --- commandapi-core/pom.xml | 13 + .../executors/CommandArguments.java | 431 +++++++----------- 2 files changed, 186 insertions(+), 258 deletions(-) diff --git a/commandapi-core/pom.xml b/commandapi-core/pom.xml index 0aae7c12fc..5ee414cb9a 100644 --- a/commandapi-core/pom.xml +++ b/commandapi-core/pom.xml @@ -66,6 +66,19 @@ ${project.version} provided + + + + org.jspecify + jspecify + 1.0.0 + + + org.jetbrains + annotations + 24.1.0 + provided + diff --git a/commandapi-core/src/main/java/dev/jorel/commandapi/executors/CommandArguments.java b/commandapi-core/src/main/java/dev/jorel/commandapi/executors/CommandArguments.java index 809577f93c..eaf621ea8b 100644 --- a/commandapi-core/src/main/java/dev/jorel/commandapi/executors/CommandArguments.java +++ b/commandapi-core/src/main/java/dev/jorel/commandapi/executors/CommandArguments.java @@ -8,9 +8,10 @@ import java.util.Optional; import java.util.function.Supplier; -import javax.annotation.Nullable; - import dev.jorel.commandapi.arguments.AbstractArgument; +import org.jetbrains.annotations.Contract; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; /** * This class stores the arguments for this command @@ -21,6 +22,7 @@ * @param rawArgsMap The raw arguments for this command mapped to their node names. This is an ordered map * @param fullInput The command string a player has entered (including the /) */ +@NullMarked @SuppressWarnings("unchecked") public record CommandArguments( @@ -90,112 +92,125 @@ public int count() { // https://kotlinlang.org/docs/operator-overloading.html /** - * Returns an argument by its position + * Returns an argument by its index * - * @param index The position of this argument - * @return An argument which is placed at the given index, or {@code null} if the provided index does not point to an argument. + * @param index The index of the argument + * @param The expected type of the argument + * @return The argument at the given index + * @throws NullPointerException If no argument is found for the index */ - @Nullable - public Object get(int index) { - if (args.length <= index) { - return null; - } else { - return args[index]; + public T get(int index) { + if (index < 0 || index >= args.length) { + throw new NullPointerException("No argument found at index " + index); } + + return (T) args[index]; } /** * Returns an argument by its node name * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @return An argument which has the given node name. Can be {@code null} if nodeName was not found. + * @param nodeName The node name of the argument + * @param The expected type of the argument + * @return The argument with given node name + * @throws NullPointerException If no argument is found for the given node name */ - @Nullable - public Object get(String nodeName) { - return argsMap.get(nodeName); + public T get(String nodeName) { + return Objects.requireNonNull( + (T) argsMap.get(nodeName), + "No argument found for name " + nodeName + ); } /** * Returns an argument by its index * - * @param index The position of this argument - * @param defaultValue The Object returned if the argument is not existent - * @return An argument which is placed at the given index, or the provided default value + * @param index The index of the argument + * @param The expected type of the argument + * @param defaultValue The value returned when no argument is found for the index + * @return The argument at the given index, or the default value */ - public Object getOrDefault(int index, Object defaultValue) { - if (args.length <= index) { + @Contract("_, !null -> !null") + public @Nullable T getOrDefault(int index, @Nullable T defaultValue) { + if (index < 0 || index >= args.length) { return defaultValue; - } else { - return args[index]; } + + return (T) args[index]; } /** * Returns an argument by its node name * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @param defaultValue The Object returned if the argument was not found. - * @return The argument with the specified node name or the provided default value + * @param nodeName The node name of the argument + * @param The expected type of the argument + * @param defaultValue The value returned when no argument is found for the node name + * @return The argument with given node name, or the default value */ - public Object getOrDefault(String nodeName, Object defaultValue) { - return argsMap.getOrDefault(nodeName, defaultValue); + @Contract("_, !null -> !null") + public @Nullable T getOrDefault(String nodeName, @Nullable T defaultValue) { + T value = (T) argsMap.get(nodeName); + return value != null ? value : defaultValue; } /** * Returns an argument by its index * - * @param index The position of this argument - * @param defaultValue The Object returned if the argument is not existent - * @return An argument which is placed at the given index, or the provided default value + * @param index The index of the argument + * @param The expected type of the argument + * @param defaultValue The value returned when no argument is found for the index + * @return The argument at the given index, or the default value */ - public Object getOrDefault(int index, Supplier defaultValue) { - if (args.length <= index) { + public @Nullable T getOrDefault(int index, Supplier<@Nullable T> defaultValue) { + if (index < 0 || index >= args.length) { return defaultValue.get(); - } else { - return args[index]; } + + return (T) args[index]; } /** * Returns an argument by its node name * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @param defaultValue The Object returned if the argument was not found. - * @return The argument with the specified node name or the provided default value + * @param nodeName The node name of the argument + * @param The expected type of the argument + * @param defaultValue The value returned when no argument is found for the node name + * @return The argument with the given node name, or the default value */ - public Object getOrDefault(String nodeName, Supplier defaultValue) { - return argsMap.getOrDefault(nodeName, defaultValue.get()); + public @Nullable T getOrDefault(String nodeName, Supplier<@Nullable T> defaultValue) { + T value = (T) argsMap.get(nodeName); + return value != null ? value : defaultValue.get(); } /** - * Returns an Optional holding the argument by its index + * Returns an {@link Optional} holding the argument by its index * - * @param index The position of this argument - * @return An optional holding the argument which is placed at the given index, or an empty optional if index is invalid + * @param index The index of the argument + * @param The expected type of the argument + * @return An optional holding the argument at the given index, or an empty optional if no argument was found */ - public Optional getOptional(int index) { - if (args.length <= index) { + public Optional getOptional(int index) { + if (index < 0 || index >= args.length) { return Optional.empty(); - } else { - return Optional.of(args[index]); } + + return Optional.of((T) args[index]); } /** - * Returns an argument by its node name + * Returns an {@link Optional} holding the argument by its node name * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @return An optional holding the argument with the specified node name or an empty optional if the node name was not found + * @param nodeName The node name of the argument + * @param The expected type of the argument + * @return An optional holding the argument with the given node name, or an empty optional if no argument was found */ - public Optional getOptional(String nodeName) { - if (!argsMap.containsKey(nodeName)) { - return Optional.empty(); - } - return Optional.of(argsMap.get(nodeName)); + public Optional getOptional(String nodeName) { + return Optional.ofNullable((T) argsMap.get(nodeName)); } /** - * Returns a raw argument by its position + * Returns a raw argument by its index + * *

* A raw argument is the {@link String} form of an argument as written in a command. For example take this command: *

@@ -203,20 +218,20 @@ public Optional getOptional(String nodeName) { *

* When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * - * @param index The position of this argument - * @return An argument which is placed at the given index, or {@code null} if the provided index does not point to an argument. + * @return The raw argument at the given index + * @throws NullPointerException If no argument is found for the index */ - @Nullable public String getRaw(int index) { - if (rawArgs.length <= index) { - return null; - } else { - return rawArgs[index]; + if (index < 0 || index >= args.length) { + throw new NullPointerException("No argument found at index " + index); } + + return rawArgs[index]; } /** * Returns a raw argument by its node name + * *

* A raw argument is the {@link String} form of an argument as written in a command. For example take this command: *

@@ -224,16 +239,20 @@ public String getRaw(int index) { *

* When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @return A raw argument which has the given node name. Can be {@code null} if nodeName was not found. + * @param nodeName The node name of the argument + * @return The raw argument with given node name + * @throws NullPointerException If no argument is found for the given node name */ - @Nullable public String getRaw(String nodeName) { - return rawArgsMap.get(nodeName); + return Objects.requireNonNull( + rawArgsMap.get(nodeName), + "No argument found for name " + nodeName + ); } /** * Returns a raw argument by its index + * *

* A raw argument is the {@link String} form of an argument as written in a command. For example take this command: *

@@ -241,20 +260,22 @@ public String getRaw(String nodeName) { *

* When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * - * @param index The position of this argument - * @param defaultValue The String returned if the raw argument is not existent - * @return A raw argument which is placed at the given index, or the provided default value + * @param index The index of the argument + * @param defaultValue The value returned when no argument is found for the index + * @return The raw argument at the given index, or the default value */ - public String getOrDefaultRaw(int index, String defaultValue) { - if (rawArgs.length <= index) { + @Contract("_, !null -> !null") + public @Nullable String getOrDefaultRaw(int index, @Nullable String defaultValue) { + if (index < 0 || index >= rawArgs.length) { return defaultValue; - } else { - return rawArgs[index]; } + + return rawArgs[index]; } /** * Returns a raw argument by its node name + * *

* A raw argument is the {@link String} form of an argument as written in a command. For example take this command: *

@@ -262,16 +283,19 @@ public String getOrDefaultRaw(int index, String defaultValue) { *

* When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @param defaultValue The String returned if the raw argument was not found. - * @return A raw argument with the specified node name or the provided default value + * @param nodeName The node name of the argument + * @param defaultValue The value returned when no argument is found for the node name + * @return The raw argument with given node name, or the default value */ - public String getOrDefaultRaw(String nodeName, String defaultValue) { - return rawArgsMap.getOrDefault(nodeName, defaultValue); + @Contract("_, !null -> !null") + public @Nullable String getOrDefaultRaw(String nodeName, @Nullable String defaultValue) { + String value = rawArgsMap.get(nodeName); + return value != null ? value : defaultValue; } /** * Returns a raw argument by its index + * *

* A raw argument is the {@link String} form of an argument as written in a command. For example take this command: *

@@ -279,20 +303,21 @@ public String getOrDefaultRaw(String nodeName, String defaultValue) { *

* When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * - * @param index The position of this argument - * @param defaultValue The String returned if the raw argument is not existent - * @return A raw argument which is placed at the given index, or the provided default value + * @param index The index of the argument + * @param defaultValue The value returned when no argument is found for the index + * @return The raw argument at the given index, or the default value */ - public String getOrDefaultRaw(int index, Supplier defaultValue) { - if (rawArgs.length <= index) { + public @Nullable String getOrDefaultRaw(int index, Supplier<@Nullable String> defaultValue) { + if (index < 0 || index >= rawArgs.length) { return defaultValue.get(); - } else { - return rawArgs[index]; } + + return rawArgs[index]; } /** * Returns a raw argument by its node name + * *

* A raw argument is the {@link String} form of an argument as written in a command. For example take this command: *

@@ -300,16 +325,18 @@ public String getOrDefaultRaw(int index, Supplier defaultValue) { *

* When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * - * @param nodeName The node name of this raw argument. This was set when initializing an argument - * @param defaultValue The String returned if the raw argument was not found. - * @return A raw argument with the specified node name or the provided default value + * @param nodeName The node name of the argument + * @param defaultValue The value returned when no argument is found for the node name + * @return The raw argument with the given node name, or the default value */ - public String getOrDefaultRaw(String nodeName, Supplier defaultValue) { - return rawArgsMap.getOrDefault(nodeName, defaultValue.get()); + public @Nullable String getOrDefaultRaw(String nodeName, Supplier<@Nullable String> defaultValue) { + String value = rawArgsMap.get(nodeName); + return value != null ? value : defaultValue.get(); } /** * Returns an {@link Optional} holding the raw argument by its index + * *

* A raw argument is the {@link String} form of an argument as written in a command. For example take this command: *

@@ -317,19 +344,20 @@ public String getOrDefaultRaw(String nodeName, Supplier defaultValue) { *

* When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * - * @param index The position of this argument - * @return An optional holding the raw argument which is placed at the given index, or an empty optional if index is invalid + * @param index The index of the argument + * @return An optional holding the raw argument at the given index, or an empty optional if no argument was found */ public Optional getRawOptional(int index) { - if (rawArgs.length <= index) { + if (index < 0 || index >= args.length) { return Optional.empty(); - } else { - return Optional.of(rawArgs[index]); } + + return Optional.of(rawArgs[index]); } /** - * Returns an {@link Optional} holding the raw argument by its node name + * Returns an {@link Optional} holding the raw argument by its node name + * *

* A raw argument is the {@link String} form of an argument as written in a command. For example take this command: *

@@ -337,114 +365,11 @@ public Optional getRawOptional(int index) { *

* When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @return An optional holding the argument with the specified node name or an empty optional if the node name was not found + * @param nodeName The node name of the argument + * @return An optional holding the raw argument with the given node name, or an empty optional if no argument was found */ public Optional getRawOptional(String nodeName) { - if (!rawArgsMap.containsKey(nodeName)) { - return Optional.empty(); - } - return Optional.of(rawArgsMap.get(nodeName)); - } - - /** Unchecked methods. These are the same as the methods above, but use - * unchecked generics to conform to the type they are declared as. In Java, - * the normal methods (checked) require casting: - * - * CommandArguments args = ...; - * String myString = (String) args.get("target"); - * - * However, these unchecked methods don't require casting: - * - * CommandArguments args = ...; - * String myString = args.getUnchecked("target"); - * - */ - - /** - * Returns an argument by its position - * - * @param index The position of this argument - * @return An argument which is placed at the given index, or {@code null} if the provided index does not point to an argument. - */ - @Nullable - public T getUnchecked(int index) { - return (T) get(index); - } - - /** - * Returns an argument by its node name - * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @return An argument which has the given node name. Can be null if nodeName was not found. - */ - @Nullable - public T getUnchecked(String nodeName) { - return (T) get(nodeName); - } - - /** - * Returns an argument by its index - * - * @param index The position of this argument - * @param defaultValue The Object returned if the argument is not existent - * @return An argument which is placed at the given index, or the provided default value - */ - public T getOrDefaultUnchecked(int index, T defaultValue) { - return (T) getOrDefault(index, defaultValue); - } - - /** - * Returns an argument by its node name - * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @param defaultValue The Object returned if the argument was not found. - * @return The argument with the specified node name or the provided default value - */ - public T getOrDefaultUnchecked(String nodeName, T defaultValue) { - return (T) getOrDefault(nodeName, defaultValue); - } - - /** - * Returns an argument by its index - * - * @param index The position of this argument - * @param defaultValue The Object returned if the argument is not existent - * @return An argument which is placed at the given index, or the provided default value - */ - public T getOrDefaultUnchecked(int index, Supplier defaultValue) { - return (T) getOrDefault(index, defaultValue); - } - - /** - * Returns an argument by its node name - * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @param defaultValue The Object returned if the argument was not found. - * @return The argument with the specified node name or the provided default value - */ - public T getOrDefaultUnchecked(String nodeName, Supplier defaultValue) { - return (T) getOrDefault(nodeName, defaultValue); - } - - /** - * Returns an Optional holding the argument at its index - * - * @param index The position of this argument - * @return An optional holding the argument at its index, or an empty optional if the index was invalid - */ - public Optional getOptionalUnchecked(int index) { - return (Optional) getOptional(index); - } - - /** - * Returns an Optional holding the argument by its node name - * - * @param nodeName The node name of this argument. This was set when initializing an argument - * @return An optional holding the argument with the specified node name, or an empty optional if the index was invalid - */ - public Optional getOptionalUnchecked(String nodeName) { - return (Optional) getOptional(nodeName); + return Optional.ofNullable(rawArgsMap.get(nodeName)); } /***************************************** @@ -452,123 +377,113 @@ public Optional getOptionalUnchecked(String nodeName) { *****************************************/ /** - * Returns an argument purely based on its CommandAPI representation. This also attempts to directly cast the argument to the type represented by {@link dev.jorel.commandapi.arguments.AbstractArgument#getPrimitiveType()} + * Returns an argument by its argument instance * * @param argumentType The argument instance used to create the argument - * @return The argument represented by the CommandAPI argument, or null if the argument was not found. + * @param The expected type of the argument + * @return The argument represented by the argument instance + * @throws NullPointerException If no argument is found for the argument instance */ - @Nullable public T getByArgument(AbstractArgument argumentType) { return castArgument(get(argumentType.getNodeName()), argumentType.getPrimitiveType(), argumentType.getNodeName()); } /** - * Returns an argument purely based on its CommandAPI representation or a default value if the argument wasn't found. - *

- * If the argument was found, this also attempts to directly cast the argument to the type represented by {@link dev.jorel.commandapi.arguments.AbstractArgument#getPrimitiveType()} + * Returns an argument by its argument instance * * @param argumentType The argument instance used to create the argument - * @param defaultValue The default value to return if the argument wasn't found - * @return The argument represented by the CommandAPI argument, or the default value if the argument was not found. + * @param The expected type of the argument + * @param defaultValue The value returned when no argument is found for the argument instance + * @return The argument represented by the argument instance, or the default value */ - public T getByArgumentOrDefault(AbstractArgument argumentType, T defaultValue) { - T argument = getByArgument(argumentType); - return (argument != null) ? argument : defaultValue; + @Contract("_, !null -> !null") + public @Nullable T getByArgumentOrDefault(AbstractArgument argumentType, @Nullable T defaultValue) { + return castArgument(getOrDefault(argumentType.getNodeName(), defaultValue), argumentType.getPrimitiveType(), argumentType.getNodeName()); } /** - * Returns an Optional holding the provided argument. This Optional can be empty if the argument was not given when running the command. - *

- * This attempts to directly cast the argument to the type represented by {@link dev.jorel.commandapi.arguments.AbstractArgument#getPrimitiveType()} + * Returns an {@link Optional} holding the argument by its argument instance * * @param argumentType The argument instance used to create the argument - * @return An Optional holding the argument, or an empty Optional if the argument was not found. + * @param The expected type of the argument + * @return An optional holding the argument represented by the argument instance, or an empty optional if no argument was found */ public Optional getOptionalByArgument(AbstractArgument argumentType) { - return Optional.ofNullable(getByArgument(argumentType)); + return Optional.ofNullable(getByArgumentOrDefault(argumentType, null)); } /** - * Returns an argument based on its node name. This also attempts to directly cast the argument to the type represented by the {@code argumentType} parameter. + * Returns an argument by its node name and type * * @param nodeName The node name of the argument - * @param argumentType The class that represents the argument - * @return The argument with the given node name, or null if the argument was not found. + * @param argumentType The expected type of the argument + * @return The argument with given node name */ - @Nullable public T getByClass(String nodeName, Class argumentType) { return castArgument(get(nodeName), argumentType, nodeName); } /** - * Returns an argument based on its node name or a default value if the argument wasn't found. - *

- * If the argument was found, this method attempts to directly cast the argument to the type represented by the {@code argumentType} parameter. + * Returns an argument by its node name and type * * @param nodeName The node name of the argument - * @param argumentType The class that represents the argument - * @param defaultValue The default value to return if the argument wasn't found - * @return The argument with the given node name, or the default value if the argument was not found. + * @param argumentType The expected type of the argument + * @param defaultValue The value returned when no argument is found for the node name + * @return The argument with given node name, or the default value */ - public T getByClassOrDefault(String nodeName, Class argumentType, T defaultValue) { - T argument = getByClass(nodeName, argumentType); - return (argument != null) ? argument : defaultValue; + @Contract("_, _, !null -> !null") + public @Nullable T getByClassOrDefault(String nodeName, Class argumentType, @Nullable T defaultValue) { + return castArgument(getOrDefault(nodeName, defaultValue), argumentType, nodeName); } /** - * Returns an Optional holding the argument with the given node name. This Optional can be empty if the argument was not given when running the command. - *

- * This attempts to directly cast the argument to the type represented by the {@code argumentType} parameter. + * Returns an {@link Optional} holding the argument by its node name and type * * @param nodeName The node name of the argument - * @param argumentType The class that represents the argument - * @return An Optional holding the argument, or an empty Optional if the argument was not found. + * @param argumentType The expected type of the argument + * @return An optional holding the argument with the given node name, or an empty optional if no argument was found */ public Optional getOptionalByClass(String nodeName, Class argumentType) { - return Optional.ofNullable(getByClass(nodeName, argumentType)); + return Optional.ofNullable(getByClassOrDefault(nodeName, argumentType, null)); } /** - * Returns an argument based on its index. This also attempts to directly cast the argument to the type represented by the {@code argumentType} parameter. + * Returns an argument by its index and type * * @param index The index of the argument - * @param argumentType The class that represents the argument - * @return The argument at the given index, or null if the argument was not found. + * @param argumentType The expected type of the argument + * @return The argument at the given index */ - @Nullable public T getByClass(int index, Class argumentType) { return castArgument(get(index), argumentType, index); } /** - * Returns an argument based on its index or a default value if the argument wasn't found. - *

- * If the argument was found, this method attempts to directly cast the argument to the type represented by the {@code argumentType} parameter. + * Returns an argument by its index and type * * @param index The index of the argument - * @param argumentType The class that represents the argument - * @param defaultValue The default value to return if the argument wasn't found - * @return The argument at the given index, or the default value if the argument was not found. + * @param argumentType The expected type of the argument + * @param defaultValue The value returned when no argument is found for the index + * @return The argument at the given index, or the default value */ - public T getByClassOrDefault(int index, Class argumentType, T defaultValue) { - T argument = getByClass(index, argumentType); - return (argument != null) ? argument : defaultValue; + @Contract("_, _, !null -> !null") + public @Nullable T getByClassOrDefault(int index, Class argumentType, @Nullable T defaultValue) { + return castArgument(getOrDefault(index, defaultValue), argumentType, index); } /** - * Returns an Optional holding the argument at the given index. This Optional can be empty if the argument was not given when running the command. - *

- * This attempts to directly cast the argument to the type represented by the {@code argumentType} parameter. + * Returns an {@link Optional} holding the argument by its index and type * * @param index The index of the argument - * @param argumentType The class that represents the argument - * @return An Optional holding the argument, or an empty Optional if the argument was not found. + * @param argumentType The expected type of the argument + * @return An optional holding the argument at the given index, or an empty optional if no argument was found */ public Optional getOptionalByClass(int index, Class argumentType) { - return Optional.ofNullable(getByClass(index, argumentType)); + return Optional.ofNullable(getByClassOrDefault(index, argumentType, null)); } - private T castArgument(Object argument, Class argumentType, Object argumentNameOrIndex) { + @Contract("!null, _, _ -> !null") + private @Nullable T castArgument(@Nullable Object argument, Class argumentType, Object argumentNameOrIndex) { if (argument == null) { return null; } From 0bc44fcd2a7270ec507fb5b78182f8ca27e6d3a4 Mon Sep 17 00:00:00 2001 From: GliczDev <67753196+GliczDev@users.noreply.github.com> Date: Sat, 2 Aug 2025 01:01:46 +0200 Subject: [PATCH 2/2] Resolve changes request --- .../executors/CommandArguments.java | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/commandapi-core/src/main/java/dev/jorel/commandapi/executors/CommandArguments.java b/commandapi-core/src/main/java/dev/jorel/commandapi/executors/CommandArguments.java index eaf621ea8b..eecc7db637 100644 --- a/commandapi-core/src/main/java/dev/jorel/commandapi/executors/CommandArguments.java +++ b/commandapi-core/src/main/java/dev/jorel/commandapi/executors/CommandArguments.java @@ -97,11 +97,11 @@ public int count() { * @param index The index of the argument * @param The expected type of the argument * @return The argument at the given index - * @throws NullPointerException If no argument is found for the index + * @throws IllegalArgumentException If no argument is found for the index */ public T get(int index) { if (index < 0 || index >= args.length) { - throw new NullPointerException("No argument found at index " + index); + throw new IllegalArgumentException("No argument found at index " + index); } return (T) args[index]; @@ -113,13 +113,15 @@ public T get(int index) { * @param nodeName The node name of the argument * @param The expected type of the argument * @return The argument with given node name - * @throws NullPointerException If no argument is found for the given node name + * @throws IllegalArgumentException If no argument is found for the given node name */ public T get(String nodeName) { - return Objects.requireNonNull( - (T) argsMap.get(nodeName), - "No argument found for name " + nodeName - ); + T value = (T) argsMap.get(nodeName); + if (value == null) { + throw new IllegalArgumentException("No argument found for name " + nodeName); + } + + return value; } /** @@ -149,8 +151,7 @@ public T get(String nodeName) { */ @Contract("_, !null -> !null") public @Nullable T getOrDefault(String nodeName, @Nullable T defaultValue) { - T value = (T) argsMap.get(nodeName); - return value != null ? value : defaultValue; + return (T) argsMap.getOrDefault(nodeName, defaultValue); } /** @@ -219,11 +220,11 @@ public Optional getOptional(String nodeName) { * When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * * @return The raw argument at the given index - * @throws NullPointerException If no argument is found for the index + * @throws IllegalArgumentException If no argument is found for the index */ public String getRaw(int index) { if (index < 0 || index >= args.length) { - throw new NullPointerException("No argument found at index " + index); + throw new IllegalArgumentException("No argument found at index " + index); } return rawArgs[index]; @@ -244,10 +245,12 @@ public String getRaw(int index) { * @throws NullPointerException If no argument is found for the given node name */ public String getRaw(String nodeName) { - return Objects.requireNonNull( - rawArgsMap.get(nodeName), - "No argument found for name " + nodeName - ); + String value = rawArgsMap.get(nodeName); + if (value == null) { + throw new IllegalArgumentException("No argument found for name " + nodeName); + } + + return value; } /** @@ -261,7 +264,7 @@ public String getRaw(String nodeName) { * When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * * @param index The index of the argument - * @param defaultValue The value returned when no argument is found for the index + * @param defaultValue The String returned when no argument is found for the index * @return The raw argument at the given index, or the default value */ @Contract("_, !null -> !null") @@ -284,13 +287,12 @@ public String getRaw(String nodeName) { * When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * * @param nodeName The node name of the argument - * @param defaultValue The value returned when no argument is found for the node name + * @param defaultValue The String returned when no argument is found for the node name * @return The raw argument with given node name, or the default value */ @Contract("_, !null -> !null") public @Nullable String getOrDefaultRaw(String nodeName, @Nullable String defaultValue) { - String value = rawArgsMap.get(nodeName); - return value != null ? value : defaultValue; + return rawArgsMap.getOrDefault(nodeName, defaultValue); } /** @@ -304,7 +306,7 @@ public String getRaw(String nodeName) { * When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * * @param index The index of the argument - * @param defaultValue The value returned when no argument is found for the index + * @param defaultValue The String returned when no argument is found for the index * @return The raw argument at the given index, or the default value */ public @Nullable String getOrDefaultRaw(int index, Supplier<@Nullable String> defaultValue) { @@ -326,7 +328,7 @@ public String getRaw(String nodeName) { * When using this method to access these arguments, {@code @e} and {@code 15.3} will be available as {@link String}s and not as a {@link Collection} and {@link Double} * * @param nodeName The node name of the argument - * @param defaultValue The value returned when no argument is found for the node name + * @param defaultValue The String returned when no argument is found for the node name * @return The raw argument with the given node name, or the default value */ public @Nullable String getOrDefaultRaw(String nodeName, Supplier<@Nullable String> defaultValue) {