命令无效

时间:2015-12-07 03:29:57

标签: java bukkit

public class Main extends JavaPlugin
{

    // Logger variable
    Logger myPluginLogger = Bukkit.getLogger();

    // Does when plugin is disabled - console logging
    @Override
    public void onDisable()
    {
        myPluginLogger.info("Disabling Political Parties...");
    }

    // Plugin chat help function
    public void sendPlayerHelp(String permissions, Player player)
    {
        // If player does not belong to a party, show them these commands
        if (permissions == "nonpartisan")
        {
            player.sendMessage(ChatColor.YELLOW + "Political Parties by TerraCraft - version 1.0");
            player.sendMessage(ChatColor.RED + "/party create <party name> " + ChatColor.YELLOW + "Start a political party");
            player.sendMessage(ChatColor.RED + "/party list " + ChatColor.YELLOW + "Shows a list of all of the current political parties");
            player.sendMessage(ChatColor.RED + "/party info <party> " + ChatColor.YELLOW + "Shows info about a specific party");
        }
        // If a player does belong to a party, show them these commands
        else if (permissions == "inparty")
        {
            player.sendMessage(ChatColor.YELLOW + "Political Parties by TerraCraft - version 1.0");
            player.sendMessage(ChatColor.RED + "/party leave " + ChatColor.YELLOW + "Leave your current political party");
            player.sendMessage(ChatColor.RED + "/party list " + ChatColor.YELLOW + "Shows a list of all of the current political parties");
            player.sendMessage(ChatColor.RED + "/party info <party> " + ChatColor.YELLOW + "Shows info about a specific party");
            player.sendMessage(ChatColor.RED + "/party deposit <amount> " + ChatColor.YELLOW + "Deposit a specific sum into the party bank account");
            player.sendMessage(ChatColor.RED + "/party chat <message> " + ChatColor.YELLOW + "Say specified message in party chat");
        }
        // If a player is the leader of a party, show them these commands
        else if (permissions == "partyleader")
        {
            player.sendMessage(ChatColor.YELLOW + "Political Parties by TerraCraft - version 1.0");
            player.sendMessage(ChatColor.RED + "/party delete " + ChatColor.YELLOW + "Deletes your current political party");
            player.sendMessage(ChatColor.RED + "/party list " + ChatColor.YELLOW + "Shows a list of all of the current political parties");
            player.sendMessage(ChatColor.RED + "/party info <party> " + ChatColor.YELLOW + "Shows info about a specific party");
            player.sendMessage(ChatColor.RED + "/party deposit <amount> " + ChatColor.YELLOW + "Deposit a specific sum into the party bank account");
            player.sendMessage(ChatColor.RED + "/party withdraw <amount> " + ChatColor.YELLOW + "Withdraw a specific sum from the party bank account");
            player.sendMessage(ChatColor.RED + "/party policy <add|remove> [index] [message] " + ChatColor.YELLOW + "Add or remove a specific policy to/from your political party");
            player.sendMessage(ChatColor.RED + "/party invite <player> " + ChatColor.YELLOW + "Invite a player to your political party");
            player.sendMessage(ChatColor.RED + "/party kick <player> " + ChatColor.YELLOW + "Kick a player from your political party");
            player.sendMessage(ChatColor.RED + "/party giveleader <player> " + ChatColor.YELLOW + "Resign yourself as party leader and give it to another player");
            player.sendMessage(ChatColor.RED + "/party chat <message> " + ChatColor.YELLOW + "Say specified message in party chat");    
        }
    }

    // Command function
    public boolean onCommand(CommandSender theSender, Command cmd, String[] args, String commandLabel)
    {
        // Party command
        if (cmd.getName().equalsIgnoreCase("party"))
        {
            Player player = (Player) theSender;

            // If no arguments are specified, or if the first argument is equal to help, or ?, show them help
            if (args.length == 0 || args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("?"))
            {
                sendPlayerHelp("nonpartisan", player);
            }
            else
            {

            }
            return true;
        }
        else
        {
        }
        return false;
    }

}

您好,我以前创建过基本的Bukkit服务器插件,并且运行正常。我正在创建一个新的,但由于某种原因,它不会运行该命令。只是为了澄清,插件YAML是正确完成的(导入也是如此,我只是觉得不需要包含它们),并且没有编译错误。我在命令YAML文件中有命令,当我加载插件时,它会显示在我的插件列表中,当我执行/帮助时。当我运行命令时,它只返回用法:&#39; / party&#39;,但不会执行其他操作(向播放器发送帮助消息)。我不太确定为什么它不起作用。我错误地忘记了什么吗?此外,这是我的第一个问题,如果不符合标准,请道歉。

2 个答案:

答案 0 :(得分:1)

我不确定我上面的人在说什么,但让我解释一下: 是的,使用cmd.getName()比commandLabel更好,这就是原因: 在plugin.yml中注册命令时,必须为其命名。然后在这个名称下你可以设置它的描述,权限等等,最重要的是在这种情况下 - 它的别名。设置其别名时,将在命令中调用任何别名时运行该命令。标签将是运行的确切别名,cmd.getName()将是您注册的命令的名称。 至于你的问题,我猜这是在这一行:

public boolean onCommand(CommandSender theSender, Command cmd, String[] args, String commandLabel)

应该是

public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args)

订单始终是CommandSender,Command,String,String []。 你应该在你的onCommand方法上面添加@Override标签,这样就会抛出这样的错误,你就会知道这就是问题所在。

答案 1 :(得分:0)

问题是在开始时你有terminator如果我没有错,就是要求命令提示符/ cmd获取用户名。

我相信你想要的是对你的命令进行排列。所以对于前者。 if(cmd.getName().equalsIgnoreCase("party"))

在代码中我添加了一些注释来帮助您应用插件。

首先,您要通过执行以下操作来设置第一个命令:

/party help, /party setup ...

现在在关闭命令之前,您现在想要添加如果 if(label.equalsIgnoreCase("PoP") || label.equalsIgnoreCase("PoliticalParty")){ //this will be your first command so you want to replace cmd.getName().equalsIgnoreCase("party") as that is really wrong //below tells the user if command is just "POP" then give user message if(args.length == 0){//if less then 0 input player.sendMessage("Wrong command type /pop help"); } 多于一个的参数。 所以在上面的代码下面把它放在:

args.length