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