Skip to content

Commit 6023e6a

Browse files
created method getArgumentChecked in Arguments to avoid constant unwrapping of arguments
1 parent c09df92 commit 6023e6a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/main/java/com/datasiqn/commandcore/CommandCore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static CommandCore getInstance() {
176176
.description("Shows the help menu")
177177
.then(ArgumentBuilder.argument(ArgumentType.COMMAND, "command")
178178
.executes(context -> {
179-
Command cmd = context.getArguments().get(0, ArgumentType.COMMAND).unwrap();
179+
Command cmd = context.getArguments().get(0, ArgumentType.COMMAND);
180180
String commandName = context.getArguments().getString(0);
181181
if (!context.getSource().hasPermission(cmd.getPermissionString())) {
182182
context.getSource().sendMessage(ChatColor.RED + "No help for " + commandName);

src/main/java/com/datasiqn/commandcore/argument/Arguments.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.datasiqn.resultapi.Result;
55
import org.jetbrains.annotations.NotNull;
66

7+
import java.util.function.Function;
8+
79
/**
810
* Represents command arguments
911
*/
@@ -19,10 +21,23 @@ public interface Arguments {
1921
* @param i The index of the argument
2022
* @param type The argument type
2123
* @param <T> The type of the argument type
24+
* @return The parsed argument
25+
* @throws IllegalArgumentException If the argument could not be parsed into {@code type}
26+
* @throws IndexOutOfBoundsException If {@code i} is an invalid index ({@code i} {@literal <} 0, or {@code i} {@literal >=} {@link #size()})
27+
*/
28+
default @NotNull <T> T get(int i, ArgumentType<T> type) {
29+
return getChecked(i, type).unwrapOrThrow((Function<String, IllegalArgumentException>) err -> new IllegalArgumentException("argument could not be parsed: " + err));
30+
}
31+
32+
/**
33+
* Same as {@link #get(int, ArgumentType)}, except checks if there is an error and returns a {@code Result}
34+
* @param i The index of the argument
35+
* @param type The argument type
36+
* @param <T> The type of the argument type
2237
* @return The result of the parsing
2338
* @throws IndexOutOfBoundsException If {@code i} is an invalid index ({@code i} {@literal <} 0, or {@code i} {@literal >=} {@link #size()})
2439
*/
25-
@NotNull <T> Result<T, String> get(int i, ArgumentType<T> type);
40+
@NotNull <T> Result<T, String> getChecked(int i, ArgumentType<T> type);
2641

2742
/**
2843
* Gets a simple string argument

0 commit comments

Comments
 (0)