aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils/command
diff options
context:
space:
mode:
authorRime <81419447+Emirlol@users.noreply.github.com>2024-07-11 23:38:25 +0300
committerRime <81419447+Emirlol@users.noreply.github.com>2024-07-11 23:38:25 +0300
commit3a7dce4e3d60232805e6fb07ec5cbd5c26b7673e (patch)
tree74b0123a1d182e7323f86dce646ea256c522a14a /src/main/java/de/hysky/skyblocker/utils/command
parentbd9a837d955f4d28e19180cd842624b143d8eae6 (diff)
downloadSkyblocker-3a7dce4e3d60232805e6fb07ec5cbd5c26b7673e.tar.gz
Skyblocker-3a7dce4e3d60232805e6fb07ec5cbd5c26b7673e.tar.bz2
Skyblocker-3a7dce4e3d60232805e6fb07ec5cbd5c26b7673e.zip
Add color argument types and update usages
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils/command')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java2
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/ColorArgumentType.java28
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/HexColorArgumentType.java38
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/RgbColorArgumentType.java39
4 files changed, 106 insertions, 1 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java
index 5448ad82..a6532ff8 100644
--- a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java
+++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/EggTypeArgumentType.java
@@ -12,7 +12,7 @@ import net.minecraft.command.CommandSource;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
-public class EggTypeArgumentType implements ArgumentType<EggFinder.EggType> {
+public final class EggTypeArgumentType implements ArgumentType<EggFinder.EggType> {
@Override
public EggFinder.EggType parse(StringReader reader) throws CommandSyntaxException {
String name = reader.readUnquotedString();
diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/ColorArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/ColorArgumentType.java
new file mode 100644
index 00000000..a6dc8510
--- /dev/null
+++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/ColorArgumentType.java
@@ -0,0 +1,28 @@
+package de.hysky.skyblocker.utils.command.argumenttypes.color;
+
+import com.mojang.brigadier.context.CommandContext;
+import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
+
+/**
+ * Utility class that provides static methods for abstracting away the actual argument type classes.
+ */
+public final class ColorArgumentType {
+ private ColorArgumentType() {} // Prevent instantiation
+
+ public static RgbColorArgumentType rgb() {
+ return new RgbColorArgumentType();
+ }
+
+ public static HexColorArgumentType hex() {
+ return new HexColorArgumentType();
+ }
+
+ public static int getIntFromHex(CommandContext<FabricClientCommandSource> context, String name) {
+ return HexColorArgumentType.getInt(context, name);
+ }
+
+ public static int getIntFromRgb(CommandContext<FabricClientCommandSource> context, String name) {
+ return RgbColorArgumentType.getInt(context, name);
+ }
+}
+
diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/HexColorArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/HexColorArgumentType.java
new file mode 100644
index 00000000..516eb7b9
--- /dev/null
+++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/HexColorArgumentType.java
@@ -0,0 +1,38 @@
+package de.hysky.skyblocker.utils.command.argumenttypes.color;
+
+import com.mojang.brigadier.StringReader;
+import com.mojang.brigadier.arguments.ArgumentType;
+import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
+import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
+import net.minecraft.text.Text;
+import org.apache.commons.lang3.StringUtils;
+
+@SuppressWarnings("RedundantCast")
+public final class HexColorArgumentType implements ArgumentType<Integer> {
+ public static final DynamicCommandExceptionType WRONG_INPUT_WIDTH = new DynamicCommandExceptionType(found -> Text.translatable("argument.color.hex.invalidString", ((String) found).length()));
+ public static final DynamicCommandExceptionType INVALID_CHARACTER = new DynamicCommandExceptionType(character -> Text.translatable("argument.color.hex.invalidChar", (String) character));
+
+ @Override
+ public Integer parse(StringReader reader) throws CommandSyntaxException {
+ String input = reader.readString();
+ if (StringUtils.startsWithIgnoreCase(input, "0x")) input = input.substring(2);
+// else if (input.startsWith("#")) input = input.substring(1); // This doesn't work because minecraft has the # prefix reserved for tags, so inputs with that prefix never reach this reader
+
+ if (input.length() != 6) throw WRONG_INPUT_WIDTH.create(input);
+
+ for (int i = 0; i < input.length(); i++) {
+ char character = input.charAt(i);
+ if ((character < '0' || character > '9') && (character < 'a' || character > 'f') && (character < 'A' || character > 'F')) {
+ throw INVALID_CHARACTER.create(String.valueOf(character)); //Have to wrap character in a string, because mcdev doesn't appreciate chars and I cba to suppress the warnings
+ }
+ }
+
+ return Integer.decode("#" + input);
+ }
+
+ public static int getInt(CommandContext<FabricClientCommandSource> context, String name) {
+ return context.getArgument(name, Integer.class);
+ }
+}
diff --git a/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/RgbColorArgumentType.java b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/RgbColorArgumentType.java
new file mode 100644
index 00000000..d9a99fae
--- /dev/null
+++ b/src/main/java/de/hysky/skyblocker/utils/command/argumenttypes/color/RgbColorArgumentType.java
@@ -0,0 +1,39 @@
+package de.hysky.skyblocker.utils.command.argumenttypes.color;
+
+import com.mojang.brigadier.StringReader;
+import com.mojang.brigadier.arguments.ArgumentType;
+import com.mojang.brigadier.arguments.IntegerArgumentType;
+import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
+import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
+import net.minecraft.text.Text;
+
+public final class RgbColorArgumentType implements ArgumentType<Integer> {
+ public static final SimpleCommandExceptionType INCOMPLETE_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("argument.color.rgb.incomplete"));
+
+ @Override
+ public Integer parse(StringReader reader) throws CommandSyntaxException {
+ int i = reader.getCursor();
+ int redArgument = IntegerArgumentType.integer(0x00, 0xFF).parse(reader);
+ if (reader.canRead() && reader.peek() == ' ') {
+ reader.skip();
+ int greenArgument = IntegerArgumentType.integer(0x00, 0xFF).parse(reader);
+ if (reader.canRead() && reader.peek() == ' ') {
+ reader.skip();
+ int blueArgument = IntegerArgumentType.integer(0x00, 0xFF).parse(reader);
+ return redArgument << 16 | greenArgument << 8 | blueArgument;
+ } else {
+ reader.setCursor(i);
+ throw INCOMPLETE_EXCEPTION.createWithContext(reader);
+ }
+ } else {
+ reader.setCursor(i);
+ throw INCOMPLETE_EXCEPTION.createWithContext(reader);
+ }
+ }
+
+ public static int getInt(CommandContext<FabricClientCommandSource> context, String name) {
+ return context.getArgument(name, Integer.class);
+ }
+}