| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.brigadier.CommandDispatcher;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import java.util.Collection;
|
| 7 | import net.minecraft.commands.CommandSourceStack;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.commands.arguments.EntityArgument;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 |
|
| 13 | public class KillCommand {
|
| 14 | private static final CommandResponseTracker.Messages<Entity> RESPONSE_KILL = CommandResponseTracker.messages(
|
| 15 | (entity, var1) -> Component.translatable("commands.kill.success.single", entity.getDisplayName()),
|
| 16 | (entityCount, var1) -> Component.translatable("commands.kill.success.multiple", entityCount)
|
| 17 | );
|
| 18 |
|
| 19 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 20 | dispatcher.register(
|
| 21 | Commands.literal("kill")
|
| 22 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 23 | .executes(c -> kill(c.getSource(), ImmutableList.of(c.getSource().getEntityOrException())))
|
| 24 | .then(Commands.argument("targets", EntityArgument.entities()).executes(c -> kill(c.getSource(), EntityArgument.getEntities(c, "targets"))))
|
| 25 | );
|
| 26 | }
|
| 27 |
|
| 28 | private static int kill(final CommandSourceStack source, final Collection<? extends Entity> victims) throws CommandSyntaxException {
|
| 29 | CommandResponseTracker<Entity> tracker = CommandResponseTracker.create();
|
| 30 |
|
| 31 | for (Entity entity : victims) {
|
| 32 | entity.kill(source.getLevel());
|
| 33 | tracker.track(entity);
|
| 34 | }
|
| 35 |
|
| 36 | return tracker.sendFeedback(source, true, RESPONSE_KILL);
|
| 37 | }
|
| 38 | }
|
| 39 |
|