我想使用3个args命令应如下所示:/ event pop on

时间:2019-02-05 18:52:33

标签: java minecraft bukkit

正如标题中所述,我想执行以下2条命令:/ event pvp on

和/ event弹出 它不起作用。请帮助我。 顺便说一句,我是Java编码新手

这是我的代码:

public boolean onCommand(CommandSender sender, Command command, String label, String[] args, String arg2) {

    Player player = (Player) sender;
    if(label.equalsIgnoreCase("event")) {
        if(args.length == 0) {
            player.sendMessage("Syntax: /event (Event) on/off");
            return true;
        } else if(args.length == 1) {
            String mode = args[0];
            if(mode.equalsIgnoreCase("pvp")) {
                if(arg2.length() == 1) {
                    String Modus = arg2;
                    if(Modus.equalsIgnoreCase("on")) {


               new PlayerRDEvent(this);


                for(Player onps : Bukkit.getOnlinePlayers()){
                    onps.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD +  "PvP" + ChatColor.YELLOW + "Event" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "Das PvP Event hat begonnen!");
                     onps.playSound(onps.getLocation(), Sound.ARROW_HIT, 2, 0);
                }}}
                return true;
            } else if(mode.equalsIgnoreCase("off")) {
                 for(Player onps2 : Bukkit.getOnlinePlayers()){
                    onps2.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD +  "PvP" + ChatColor.YELLOW + "Event" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "Das PvP Event ist zu ende!");
                     onps2.playSound(onps2.getLocation(), Sound.ARROW_HIT, 2, 0);}
                HandlerList.unregisterAll();




                return true;
            } else {
                player.sendMessage("Use: /event (event) on/off");

            }
        }
    }
    return false;
}

1 个答案:

答案 0 :(得分:1)

您正在将mode用作args[0]的变量,而没有使用它来检查它是否打开。要检查关闭/打开字符串,请将mode等于args[1](数组中的第二个元素,因为第一个元素是pvp),并在条件之前检查args[0]是否等于pvp

此外,如果您要覆盖JavaPlugin中的方法,则无法添加参数String arg2-因此,请使用方法 onCommand 的默认参数, 参见:OnCommand Bukkit method

代码:

 Player player = (Player) sender;
    if (label.equalsIgnoreCase("event")) {
        if (args[0].equals("pvp")) { //check if args[0] is pvp, you can add an else statement to send an error message if it is not pvp, or manipulate it differently
            if (args.length != 2) { //if it does not contain both [pvp] and [off/on]
                player.sendMessage("Syntax: /event (Event) on/off");
                return true;
            } else { //if args length = 2
                String mode = args[1]; //[off/on]
                if (mode.equalsIgnoreCase("on")) { //if args[1] is "on"


                    for (Player onps : Bukkit.getOnlinePlayers()) {
                        onps.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "PvP" + ChatColor.YELLOW + "Event" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "Das PvP Event hat begonnen!");
                        onps.playSound(onps.getLocation(), Sound.ARROW_HIT, 2, 0);
                    }
                    return true;


                } else if (mode.equalsIgnoreCase("off")) { //if args[1] is "off"
                    for (Player onps2 : Bukkit.getOnlinePlayers()) {
                        onps2.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "PvP" + ChatColor.YELLOW + "Event" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "Das PvP Event ist zu ende!");
                        onps2.playSound(onps2.getLocation(), Sound.ARROW_HIT, 2, 0);
                    }
                    HandlerList.unregisterAll();
                    return true;


                } else {
                    player.sendMessage("Use: /event (event) on/off");
                }
            }
        }
    }

    return false;
}
}

我真的建议您在不久的将来实现某种命令侦听器,而不要检查onCommand方法上的每个命令,因为它使您的代码更简洁,更易于阅读