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

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

There is a newer version: 1.20.40-r1
Show newest version
package cn.nukkit.command.defaults;

import cn.nukkit.Player;
import cn.nukkit.api.PowerNukkitXOnly;
import cn.nukkit.api.Since;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandParamType;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.command.exceptions.CommandSyntaxException;
import cn.nukkit.command.utils.CommandParser;
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.utils.TextFormat;

import java.util.List;
import java.util.stream.Collectors;

@PowerNukkitXOnly
@Since("1.6.0.0-PNX")
public class ClearSpawnPointCommand extends VanillaCommand {

    public ClearSpawnPointCommand(String name) {
        super(name, "commands.clearspawnpoint.description");
        this.setPermission("nukkit.command.clearspawnpoint");
        this.getCommandParameters().clear();
        this.addCommandParameters("default", new CommandParameter[]{
                CommandParameter.newType("player", true, CommandParamType.TARGET),
        });
    }

    @Override
    public boolean execute(CommandSender sender, String commandLabel, String[] args) {
        if (!this.testPermission(sender)) {
            return false;
        }

        CommandParser parser = new CommandParser(this, sender, args);
        if (parser.matchCommandForm() == null) {
            sender.sendMessage(new TranslationContainer("commands.generic.usage", "\n" + this.getCommandFormatTips()));
            return false;
        }

        try {
            List players;
            if (parser.hasNext()) {
                players = parser.parseTargetPlayers();
            } else if (sender.isPlayer()) {
                players = List.of(sender.asPlayer());
            } else {
                sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.noTargetMatch"));
                return false;
            }
            for (Player player : players) {
                player.setSpawn(null);
            }
            String players_str = players.stream().map(p -> p.getName()).collect(Collectors.joining(" "));
            if (players.size() > 1) {
                sender.sendMessage(new TranslationContainer("commands.clearspawnpoint.success.multiple", players_str));
            } else {
                sender.sendMessage(new TranslationContainer("commands.clearspawnpoint.success.single", players_str));
            }
            return true;
        } catch (CommandSyntaxException e) {
            e.printStackTrace();
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy