使用/ command / on或/ command off打开和关闭事件

时间:2019-01-25 20:08:12

标签: java minecraft bukkit

我想制作一个PVPEventPlugin插件并打开和关闭事件,但是我想制作/pvpevent on/pvpevent off,但是我需要用args来做。 这是我的代码:

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {      
    if(command.getName().equalsIgnoreCase("PVPEVENTon") {
        if(sender instanceof Player) {
            getServer().getPluginManager().registerEvents(this, this);
            for (Player player : Bukkit.getOnlinePlayers()) {
                player.sendMessage(ChatColor.GREEN + "PVP - Event Startet!");
            }    
        }
    }
    if(command.getName().equalsIgnoreCase("PVPEVENToff")) {
        HandlerList.unregisterAll();
    }
    return true;
}

@EventHandler
public void onRespawnPVPEVENT(PlayerRespawnEvent pvpevent ) {
    Player p = pvpevent.getPlayer();
    double x = 48.69925614938256;
    double y = 7.0;
    double z = 47.4376551334051;
    Location loc = new Location(Bukkit.getWorld("world"), x, y, z);
    pvpevent.setRespawnLocation(loc);
    p.sendMessage("");


}

@EventHandler
public void sed(PlayerDeathEvent totevent) {
    Player p1 = totevent.getEntity();
    p1.sendMessage(ChatColor.GOLD + "[PvP Event] " + ChatColor.AQUA + "Du Bist Gestorben und somit raus");
}

1 个答案:

答案 0 :(得分:0)

使用标签(这是命令),然后解析参数。如下所示:

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(!(sender instanceOf Player)) {
        sender.sendMessage("Only a player can use this command");
        return true;
    }
    Player player = (Player) sender;
    if(label.equalsIgnoreCase("pvpevent") {
        if(args.length == 0) {
            player.sendMessage("Use: /pvpevent on/off");
            return true;
        } else if(args.length == 1) {
            String mode = args[0];
            if(mode.equalsIgnoreCase("on") {
                Bukkit.getPluginManager().registerEvents(this, this);
                //what you want on /pvpevent on
                return true;
            } else if(mode.equalsIgnoreCase("off") {
                HandlerList.unregisterAll(this);
                //what you want to do if /pvpevent off is entered.
                return true;
            } else {
                //what is called when not off/on is entered after /pvpevent
            }
        }
    }
    return false;
}

因此,我们在这里检查的是arg的长度是否为0,如果为0,则发送实际用法。之后,我们检查/pvpevent之后的第一个参数是什么。