File size: 1,662 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package net.minecraft.server.commands;

import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
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> p_137808_) {
        p_137808_.register(
            Commands.literal("kill")
                .requires(p_137812_ -> p_137812_.hasPermission(2))
                .executes(p_137817_ -> kill(p_137817_.getSource(), ImmutableList.of(p_137817_.getSource().getEntityOrException())))
                .then(
                    Commands.argument("targets", EntityArgument.entities())
                        .executes(p_137810_ -> kill(p_137810_.getSource(), EntityArgument.getEntities(p_137810_, "targets")))
                )
        );
    }

    private static int kill(CommandSourceStack p_137814_, Collection<? extends Entity> p_137815_) {
        for (Entity entity : p_137815_) {
            entity.kill(p_137814_.getLevel());
        }

        if (p_137815_.size() == 1) {
            p_137814_.sendSuccess(() -> Component.translatable("commands.kill.success.single", p_137815_.iterator().next().getDisplayName()), true);
        } else {
            p_137814_.sendSuccess(() -> Component.translatable("commands.kill.success.multiple", p_137815_.size()), true);
        }

        return p_137815_.size();
    }
}