All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.command.defaults.BanCommand Maven / Gradle / Ivy

package cn.nukkit.command.defaults;

import cn.nukkit.Player;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandParamType;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.command.tree.ParamList;
import cn.nukkit.command.utils.CommandLogger;
import cn.nukkit.event.player.PlayerKickEvent;

import java.util.Map;

/**
 * @author MagicDroidX (Nukkit Project)
 */
public class BanCommand extends VanillaCommand {

    public BanCommand(String name) {
        super(name, "commands.ban.description", "%commands.ban.usage");
        this.setPermission("nukkit.command.ban.player");
        this.commandParameters.clear();
        this.commandParameters.put("default",
                new CommandParameter[]{
                        CommandParameter.newType("player", CommandParamType.STRING),
                        CommandParameter.newType("reason", true, CommandParamType.STRING)
                });
        this.enableParamTree();
    }

    @Override
    public int execute(CommandSender sender, String commandLabel, Map.Entry result, CommandLogger log) {
        var list = result.getValue();
        String name = list.getResult(0);
        String reason = list.getResult(1);
        sender.getServer().getNameBans().addBan(name, reason, null, sender.getName());

        Player player = sender.getServer().getPlayerExact(name);
        if (player != null) {
            player.kick(PlayerKickEvent.Reason.NAME_BANNED, (reason.length() > 0) ? "Banned by admin. Reason: " + reason : "Banned by admin");
        }
        log.addSuccess("commands.ban.success", player != null ? player.getName() : name).output(true);
        return 1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy