| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.StringArgumentType;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.HashSet;
|
| 9 | import java.util.Set;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.Commands;
|
| 12 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 13 | import net.minecraft.commands.arguments.EntityArgument;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.network.chat.ComponentUtils;
|
| 16 | import net.minecraft.world.entity.Entity;
|
| 17 |
|
| 18 | public class TagCommand {
|
| 19 | private static final SimpleCommandExceptionType ERROR_ADD_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.tag.add.failed"));
|
| 20 | private static final SimpleCommandExceptionType ERROR_REMOVE_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.tag.remove.failed"));
|
| 21 | private static final CommandResponseTracker.Messages<Entity> RESPONSE_NO_TAGS = CommandResponseTracker.messages(
|
| 22 | (entity, var1) -> Component.translatable("commands.tag.list.single.empty", entity.getDisplayName()),
|
| 23 | (entityCount, var1) -> Component.translatable("commands.tag.list.multiple.empty", entityCount)
|
| 24 | );
|
| 25 | private static final CommandResponseTracker.MessagesWithArg<Entity, String> RESPONSE_REMOVE = CommandResponseTracker.messages(
|
| 26 | ERROR_REMOVE_FAILED,
|
| 27 | (entity, var1, name) -> Component.translatable("commands.tag.remove.success.single", name, entity.getDisplayName()),
|
| 28 | (entityCount, var1, name) -> Component.translatable("commands.tag.remove.success.multiple", name, entityCount)
|
| 29 | );
|
| 30 | private static final CommandResponseTracker.MessagesWithArg<Entity, String> RESPONSE_ADD = CommandResponseTracker.messages(
|
| 31 | ERROR_ADD_FAILED,
|
| 32 | (entity, var1, name) -> Component.translatable("commands.tag.add.success.single", name, entity.getDisplayName()),
|
| 33 | (entityCount, var1, name) -> Component.translatable("commands.tag.add.success.multiple", name, entityCount)
|
| 34 | );
|
| 35 | private static final CommandResponseTracker.MessagesWithArg<Entity, Set<String>> RESPONSE_LIST = CommandResponseTracker.messages(
|
| 36 | (entity, var1, tags) -> Component.translatable("commands.tag.list.single.success", entity.getDisplayName(), tags.size(), ComponentUtils.formatList(tags)),
|
| 37 | (entityCount, var1, tags) -> Component.translatable("commands.tag.list.multiple.success", entityCount, tags.size(), ComponentUtils.formatList(tags))
|
| 38 | );
|
| 39 |
|
| 40 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 41 | dispatcher.register(
|
| 42 | Commands.literal("tag")
|
| 43 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 44 | .then(
|
| 45 | Commands.argument("targets", EntityArgument.entities())
|
| 46 | .then(
|
| 47 | Commands.literal("add")
|
| 48 | .then(
|
| 49 | Commands.argument("name", StringArgumentType.word())
|
| 50 | .executes(c -> addTag(c.getSource(), EntityArgument.getEntities(c, "targets"), StringArgumentType.getString(c, "name")))
|
| 51 | )
|
| 52 | )
|
| 53 | .then(
|
| 54 | Commands.literal("remove")
|
| 55 | .then(
|
| 56 | Commands.argument("name", StringArgumentType.word())
|
| 57 | .suggests((c, p) -> SharedSuggestionProvider.suggest(getTags(EntityArgument.getEntities(c, "targets")), p))
|
| 58 | .executes(c -> removeTag(c.getSource(), EntityArgument.getEntities(c, "targets"), StringArgumentType.getString(c, "name")))
|
| 59 | )
|
| 60 | )
|
| 61 | .then(Commands.literal("list").executes(c -> listTags(c.getSource(), EntityArgument.getEntities(c, "targets"))))
|
| 62 | )
|
| 63 | );
|
| 64 | }
|
| 65 |
|
| 66 | private static Collection<String> getTags(final Collection<? extends Entity> entities) {
|
| 67 | Set<String> result = new HashSet<>();
|
| 68 |
|
| 69 | for (Entity entity : entities) {
|
| 70 | result.addAll(entity.entityTags());
|
| 71 | }
|
| 72 |
|
| 73 | return result;
|
| 74 | }
|
| 75 |
|
| 76 | private static int addTag(final CommandSourceStack source, final Collection<? extends Entity> targets, final String name) throws CommandSyntaxException {
|
| 77 | CommandResponseTracker<Entity> response = CommandResponseTracker.create();
|
| 78 |
|
| 79 | for (Entity entity : targets) {
|
| 80 | response.track(entity, entity.addTag(name));
|
| 81 | }
|
| 82 |
|
| 83 | return response.sendFeedback(source, true, RESPONSE_ADD, name);
|
| 84 | }
|
| 85 |
|
| 86 | private static int removeTag(final CommandSourceStack source, final Collection<? extends Entity> targets, final String name) throws CommandSyntaxException {
|
| 87 | CommandResponseTracker<Entity> response = CommandResponseTracker.create();
|
| 88 |
|
| 89 | for (Entity entity : targets) {
|
| 90 | response.track(entity, entity.removeTag(name));
|
| 91 | }
|
| 92 |
|
| 93 | return response.sendFeedback(source, true, RESPONSE_REMOVE, name);
|
| 94 | }
|
| 95 |
|
| 96 | private static int listTags(final CommandSourceStack source, final Collection<? extends Entity> targets) throws CommandSyntaxException {
|
| 97 | CommandResponseTracker<Entity> response = CommandResponseTracker.create();
|
| 98 | Set<String> tags = new HashSet<>();
|
| 99 |
|
| 100 | for (Entity entity : targets) {
|
| 101 | Set<String> entityTags = entity.entityTags();
|
| 102 | tags.addAll(entityTags);
|
| 103 | response.track(entity, entityTags.size());
|
| 104 | }
|
| 105 |
|
| 106 | if (tags.isEmpty()) {
|
| 107 | response.sendFeedback(source, false, RESPONSE_NO_TAGS);
|
| 108 | } else {
|
| 109 | response.sendFeedback(source, false, RESPONSE_LIST, tags);
|
| 110 | }
|
| 111 |
|
| 112 | return tags.size();
|
| 113 | }
|
| 114 | }
|
| 115 |
|