minecraft-src/net/minecraft/server/commands/KillCommand.java
2025-07-04 02:00:41 +03:00

38 lines
1.4 KiB
Java

package net.minecraft.server.commands;
import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.CommandDispatcher;
import java.util.Collection;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
public class KillCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("kill")
.requires(commandSourceStack -> commandSourceStack.hasPermission(2))
.executes(commandContext -> kill(commandContext.getSource(), ImmutableList.of(commandContext.getSource().getEntityOrException())))
.then(
Commands.argument("targets", EntityArgument.entities())
.executes(commandContext -> kill(commandContext.getSource(), EntityArgument.getEntities(commandContext, "targets")))
)
);
}
private static int kill(CommandSourceStack source, Collection<? extends Entity> targets) {
for (Entity entity : targets) {
entity.kill(source.getLevel());
}
if (targets.size() == 1) {
source.sendSuccess(() -> Component.translatable("commands.kill.success.single", ((Entity)targets.iterator().next()).getDisplayName()), true);
} else {
source.sendSuccess(() -> Component.translatable("commands.kill.success.multiple", targets.size()), true);
}
return targets.size();
}
}