aboutsummaryrefslogblamecommitdiff
path: root/src/main/java/org/chrisoft/trashyaddon/commands/argument/CEntityArgumentType.java
blob: c13c08caa10a966b630baf42d072eea79e0741d3 (plain) (tree)
































































































































































                                                                                                                                                                                      
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;
         }
      }
   }
}