167 lines
5.2 KiB
Diff
167 lines
5.2 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: MrHua269 <novau233@163.com>
|
|
Date: Mon, 29 Jan 2024 13:28:54 +0000
|
|
Subject: [PATCH] Leaves Command Utilities
|
|
|
|
|
|
diff --git a/src/main/java/top/leavesmc/leaves/command/CommandArgument.java b/src/main/java/top/leavesmc/leaves/command/CommandArgument.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..eadc6d168fb13299348b0c275ae352ee2f1e1ea2
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/command/CommandArgument.java
|
|
@@ -0,0 +1,43 @@
|
|
+package top.leavesmc.leaves.command;
|
|
+
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+import java.util.ArrayList;
|
|
+import java.util.Arrays;
|
|
+import java.util.List;
|
|
+
|
|
+public class CommandArgument {
|
|
+
|
|
+ private final List<CommandArgumentType<?>> argumentTypes;
|
|
+ private final List<List<String>> tabComplete;
|
|
+
|
|
+ public CommandArgument(CommandArgumentType<?>... argumentTypes) {
|
|
+ this.argumentTypes = List.of(argumentTypes);
|
|
+ this.tabComplete = new ArrayList<>();
|
|
+ for (int i = 0; i < argumentTypes.length; i++) {
|
|
+ tabComplete.add(new ArrayList<>());
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public List<String> tabComplete(int n) {
|
|
+ if (tabComplete.size() > n) {
|
|
+ return tabComplete.get(n);
|
|
+ } else {
|
|
+ return List.of();
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public CommandArgument setTabComplete(int index, List<String> list) {
|
|
+ tabComplete.set(index, list);
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ public CommandArgumentResult parse(int index, String @NotNull [] args) {
|
|
+ Object[] result = new Object[argumentTypes.size()];
|
|
+ Arrays.fill(result, null);
|
|
+ for (int i = index, j = 0; i < args.length && j < result.length; i++, j++) {
|
|
+ result[j] = argumentTypes.get(j).pasre(args[i]);
|
|
+ }
|
|
+ return new CommandArgumentResult(new ArrayList<>(Arrays.asList(result)));
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/command/CommandArgumentResult.java b/src/main/java/top/leavesmc/leaves/command/CommandArgumentResult.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..340eaca64c96180b895a075ce9e44402cd104eed
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/command/CommandArgumentResult.java
|
|
@@ -0,0 +1,62 @@
|
|
+package top.leavesmc.leaves.command;
|
|
+
|
|
+import net.minecraft.core.BlockPos;
|
|
+import org.bukkit.util.Vector;
|
|
+
|
|
+import java.util.List;
|
|
+import java.util.Objects;
|
|
+
|
|
+public class CommandArgumentResult {
|
|
+
|
|
+ private final List<Object> result;
|
|
+
|
|
+ public CommandArgumentResult(List<Object> result) {
|
|
+ this.result = result;
|
|
+ }
|
|
+
|
|
+ public Integer readInt(int def) {
|
|
+ return Objects.requireNonNullElse(read(Integer.class), def);
|
|
+ }
|
|
+
|
|
+ public Double readDouble(double def) {
|
|
+ return Objects.requireNonNullElse(read(Double.class), def);
|
|
+ }
|
|
+
|
|
+ public String readString(String def) {
|
|
+ return Objects.requireNonNullElse(read(String.class), def);
|
|
+ }
|
|
+
|
|
+ public BlockPos readPos() {
|
|
+ Integer[] pos = {read(Integer.class), read(Integer.class), read(Integer.class)};
|
|
+ for (Integer po : pos) {
|
|
+ if (po == null) {
|
|
+ return null;
|
|
+ }
|
|
+ }
|
|
+ return new BlockPos(pos[0], pos[1], pos[2]);
|
|
+ }
|
|
+
|
|
+ public Vector readVector() {
|
|
+ Double[] pos = {read(Double.class), read(Double.class), read(Double.class)};
|
|
+ for (Double po : pos) {
|
|
+ if (po == null) {
|
|
+ return null;
|
|
+ }
|
|
+ }
|
|
+ return new Vector(pos[0], pos[1], pos[2]);
|
|
+ }
|
|
+
|
|
+ public <T> T read(Class<T> tClass) {
|
|
+ if (result.isEmpty()) {
|
|
+ return null;
|
|
+ }
|
|
+
|
|
+ Object obj = result.remove(0);
|
|
+ if (tClass.isInstance(obj)) {
|
|
+ return tClass.cast(obj);
|
|
+ } else {
|
|
+ return null;
|
|
+ }
|
|
+ }
|
|
+
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/command/CommandArgumentType.java b/src/main/java/top/leavesmc/leaves/command/CommandArgumentType.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..edf12195c7224ca2fb5d3c2ac3fcf485d3049d07
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/command/CommandArgumentType.java
|
|
@@ -0,0 +1,37 @@
|
|
+package top.leavesmc.leaves.command;
|
|
+
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public abstract class CommandArgumentType<E> {
|
|
+
|
|
+ public static final CommandArgumentType<Integer> INTEGER = new CommandArgumentType<>() {
|
|
+ @Override
|
|
+ public Integer pasre(@NotNull String arg) {
|
|
+ try {
|
|
+ return Integer.parseInt(arg);
|
|
+ } catch (NumberFormatException e) {
|
|
+ return null;
|
|
+ }
|
|
+ }
|
|
+ };
|
|
+
|
|
+ public static final CommandArgumentType<Double> DOUBLE = new CommandArgumentType<>() {
|
|
+ @Override
|
|
+ public Double pasre(@NotNull String arg) {
|
|
+ try {
|
|
+ return Double.parseDouble(arg);
|
|
+ } catch (NumberFormatException e) {
|
|
+ return null;
|
|
+ }
|
|
+ }
|
|
+ };
|
|
+
|
|
+ public static final CommandArgumentType<String> STRING = new CommandArgumentType<>() {
|
|
+ @Override
|
|
+ public String pasre(@NotNull String arg) {
|
|
+ return arg;
|
|
+ }
|
|
+ };
|
|
+
|
|
+ public abstract E pasre(@NotNull String arg);
|
|
+}
|