aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/chrisoft/trashyaddon/commands/argument
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2024-05-18 01:24:05 -0400
committerGravatar Chris Xiong <chirs241097@gmail.com> 2024-05-18 01:24:05 -0400
commit04b4941e65693f8d6b55f924781d7dd7cd26b1d5 (patch)
treec78ff16924c246c3c0073c0f880a5582689c4a37 /src/main/java/org/chrisoft/trashyaddon/commands/argument
parent2b0c0f25b9e9ca690bb334479f798794fec6dd38 (diff)
downloadmeteor-trashy-addon-04b4941e65693f8d6b55f924781d7dd7cd26b1d5.tar.xz
commands: EntityHighlight, improved MapLocate.HEADdev
Diffstat (limited to 'src/main/java/org/chrisoft/trashyaddon/commands/argument')
-rw-r--r--src/main/java/org/chrisoft/trashyaddon/commands/argument/CEntityArgumentType.java161
-rw-r--r--src/main/java/org/chrisoft/trashyaddon/commands/argument/ColorCodeArgumentType.java79
2 files changed, 240 insertions, 0 deletions
diff --git a/src/main/java/org/chrisoft/trashyaddon/commands/argument/CEntityArgumentType.java b/src/main/java/org/chrisoft/trashyaddon/commands/argument/CEntityArgumentType.java
new file mode 100644
index 0000000..c13c08c
--- /dev/null
+++ b/src/main/java/org/chrisoft/trashyaddon/commands/argument/CEntityArgumentType.java
@@ -0,0 +1,161 @@
+package org.chrisoft.trashyaddon.commands.argument;
+
+import com.google.common.collect.Iterables;
+import com.google.gson.JsonObject;
+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.SimpleCommandExceptionType;
+import com.mojang.brigadier.suggestion.Suggestions;
+import com.mojang.brigadier.suggestion.SuggestionsBuilder;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.command.CommandRegistryAccess;
+import net.minecraft.command.CommandSource;
+import org.chrisoft.trashyaddon.commands.CEntitySelector;
+import org.chrisoft.trashyaddon.commands.CEntitySelectorReader;
+import net.minecraft.command.argument.serialize.ArgumentSerializer;
+import net.minecraft.network.PacketByteBuf;
+import net.minecraft.text.Text;
+
+public class CEntityArgumentType implements ArgumentType<CEntitySelector> {
+ private static final Collection<String> EXAMPLES = Arrays.asList("Player", "0123", "@e", "@e[type=foo]", "dd12be42-52a9-4a91-a8a1-11c01849e498");
+ public static final SimpleCommandExceptionType TOO_MANY_ENTITIES_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("argument.entity.toomany"));
+ public static final SimpleCommandExceptionType TOO_MANY_PLAYERS_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("argument.player.toomany"));
+ public static final SimpleCommandExceptionType PLAYER_SELECTOR_HAS_ENTITIES_EXCEPTION = new SimpleCommandExceptionType(
+ Text.translatable("argument.player.entities")
+ );
+ public static final SimpleCommandExceptionType ENTITY_NOT_FOUND_EXCEPTION = new SimpleCommandExceptionType(
+ Text.translatable("argument.entity.notfound.entity")
+ );
+ public static final SimpleCommandExceptionType PLAYER_NOT_FOUND_EXCEPTION = new SimpleCommandExceptionType(
+ Text.translatable("argument.entity.notfound.player")
+ );
+ public static final SimpleCommandExceptionType NOT_ALLOWED_EXCEPTION = new SimpleCommandExceptionType(
+ Text.translatable("argument.entity.selector.not_allowed")
+ );
+ final boolean singleTarget;
+ final boolean playersOnly;
+
+ protected CEntityArgumentType(boolean singleTarget, boolean playersOnly) {
+ this.singleTarget = singleTarget;
+ this.playersOnly = playersOnly;
+ }
+
+ public static CEntityArgumentType entity() {
+ return new CEntityArgumentType(true, false);
+ }
+
+ public static CEntityArgumentType entities() {
+ return new CEntityArgumentType(false, false);
+ }
+
+ public static CEntityArgumentType player() {
+ return new CEntityArgumentType(true, true);
+ }
+
+ public static CEntityArgumentType players() {
+ return new CEntityArgumentType(false, true);
+ }
+
+ public CEntitySelector parse(StringReader stringReader) throws CommandSyntaxException {
+ int i = 0;
+ CEntitySelectorReader lv = new CEntitySelectorReader(stringReader);
+ CEntitySelector lv2 = lv.read();
+ if (lv2.getLimit() > 1 && this.singleTarget) {
+ if (this.playersOnly) {
+ stringReader.setCursor(0);
+ throw TOO_MANY_PLAYERS_EXCEPTION.createWithContext(stringReader);
+ } else {
+ stringReader.setCursor(0);
+ throw TOO_MANY_ENTITIES_EXCEPTION.createWithContext(stringReader);
+ }
+ } else if (lv2.includesNonPlayers() && this.playersOnly && !lv2.isSenderOnly()) {
+ stringReader.setCursor(0);
+ throw PLAYER_SELECTOR_HAS_ENTITIES_EXCEPTION.createWithContext(stringReader);
+ } else {
+ return lv2;
+ }
+ }
+
+ public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
+ if (context.getSource() instanceof CommandSource lv) {
+ StringReader stringReader = new StringReader(builder.getInput());
+ stringReader.setCursor(builder.getStart());
+ CEntitySelectorReader lv2 = new CEntitySelectorReader(stringReader, lv.hasPermissionLevel(2));
+
+ try {
+ lv2.read();
+ } catch (CommandSyntaxException var7) {
+ }
+
+ return lv2.listSuggestions(builder, builderx -> {
+ Collection<String> collection = MinecraftClient.getInstance().getNetworkHandler().getPlayerList().stream().map(e -> e.getProfile().getName()).collect(Collectors.toSet());
+ Iterable<String> iterable = (Iterable<String>)(this.playersOnly ? collection : Iterables.concat(collection, lv.getEntitySuggestions()));
+ CommandSource.suggestMatching(iterable, builderx);
+ });
+ } else {
+ return Suggestions.empty();
+ }
+ }
+
+ public Collection<String> getExamples() {
+ return EXAMPLES;
+ }
+
+ public static class Serializer implements ArgumentSerializer<CEntityArgumentType, CEntityArgumentType.Serializer.Properties> {
+ private static final byte SINGLE_FLAG = 1;
+ private static final byte PLAYERS_ONLY_FLAG = 2;
+
+ public void writePacket(CEntityArgumentType.Serializer.Properties arg, PacketByteBuf arg2) {
+ int i = 0;
+ if (arg.single) {
+ i |= 1;
+ }
+
+ if (arg.playersOnly) {
+ i |= 2;
+ }
+
+ arg2.writeByte(i);
+ }
+
+ public CEntityArgumentType.Serializer.Properties fromPacket(PacketByteBuf arg) {
+ byte b = arg.readByte();
+ return new CEntityArgumentType.Serializer.Properties((b & 1) != 0, (b & 2) != 0);
+ }
+
+ public void writeJson(CEntityArgumentType.Serializer.Properties arg, JsonObject jsonObject) {
+ jsonObject.addProperty("amount", arg.single ? "single" : "multiple");
+ jsonObject.addProperty("type", arg.playersOnly ? "players" : "entities");
+ }
+
+ public CEntityArgumentType.Serializer.Properties getArgumentTypeProperties(CEntityArgumentType arg) {
+ return new CEntityArgumentType.Serializer.Properties(arg.singleTarget, arg.playersOnly);
+ }
+
+ public final class Properties implements ArgumentSerializer.ArgumentTypeProperties<CEntityArgumentType> {
+ final boolean single;
+ final boolean playersOnly;
+
+ Properties(boolean single, boolean playersOnly) {
+ this.single = single;
+ this.playersOnly = playersOnly;
+ }
+
+ public CEntityArgumentType createType(CommandRegistryAccess arg) {
+ return new CEntityArgumentType(this.single, this.playersOnly);
+ }
+
+ @Override
+ public ArgumentSerializer<CEntityArgumentType, ?> getSerializer() {
+ return Serializer.this;
+ }
+ }
+ }
+}
diff --git a/src/main/java/org/chrisoft/trashyaddon/commands/argument/ColorCodeArgumentType.java b/src/main/java/org/chrisoft/trashyaddon/commands/argument/ColorCodeArgumentType.java
new file mode 100644
index 0000000..e56ade3
--- /dev/null
+++ b/src/main/java/org/chrisoft/trashyaddon/commands/argument/ColorCodeArgumentType.java
@@ -0,0 +1,79 @@
+package org.chrisoft.trashyaddon.commands.argument;
+
+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.SimpleCommandExceptionType;
+import com.mojang.brigadier.suggestion.Suggestions;
+import com.mojang.brigadier.suggestion.SuggestionsBuilder;
+import meteordevelopment.meteorclient.utils.render.color.Color;
+import net.minecraft.command.CommandSource;
+import net.minecraft.text.Text;
+import net.minecraft.util.Formatting;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.concurrent.CompletableFuture;
+
+public class ColorCodeArgumentType implements ArgumentType<Color> {
+ private static final Collection<String> EXAMPLES = Arrays.asList("#000", "#FFFFFF", "#AA667788", "red");
+ private static final SimpleCommandExceptionType BAD_COLOR_CODE = new SimpleCommandExceptionType(Text.literal("Invalid color code."));
+ private ColorCodeArgumentType() {}
+ public static ColorCodeArgumentType colorCode() { return new ColorCodeArgumentType(); }
+
+ @Override
+ public Color parse(StringReader reader) throws CommandSyntaxException {
+ if (reader.peek() == '#') {
+ reader.read();
+ String v = reader.readString();
+ switch (v.length()) {
+ case 3:
+ try {
+ int iv = Integer.parseInt(v, 16);
+ int r = iv & 0xF00 >> 8;
+ int g = iv & 0xF0 >> 4;
+ int b = iv & 0xF;
+ return new Color((r << 4) | r, (g << 4) | g, (b << 4) | b);
+ } catch (NumberFormatException __) {
+ throw BAD_COLOR_CODE.create();
+ }
+ case 6:
+ try {
+ int iv = Integer.parseInt(v, 16);
+ int p = 0xFF000000 | iv;
+ return new Color(p);
+ } catch (NumberFormatException __) {
+ throw BAD_COLOR_CODE.create();
+ }
+ case 8:
+ try {
+ int iv = Integer.parseUnsignedInt(v, 16);
+ return new Color(iv);
+ } catch (NumberFormatException __) {
+ throw BAD_COLOR_CODE.create();
+ }
+ default: throw BAD_COLOR_CODE.create();
+ }
+ }
+ String str = reader.readUnquotedString();
+ Formatting lv = Formatting.byName(str);
+ if (lv != null && lv.isColor()) {
+ return new Color(lv.getColorValue()).a(255);
+ }
+ throw BAD_COLOR_CODE.create();
+ }
+
+ @Override
+ public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
+ StringReader stringReader = new StringReader(builder.getRemaining());
+ if (stringReader.canRead(1) && stringReader.peek() == '#')
+ return builder.buildFuture();
+ return CommandSource.suggestMatching(Formatting.getNames(true, false).stream().filter(x -> !x.equals("reset")), builder);
+ }
+
+ @Override
+ public Collection<String> getExamples() {
+ return EXAMPLES;
+ }
+}