gpt4 book ai didi

java - 命令不起作用

转载 作者:行者123 更新时间:2023-12-01 10:46:58 24 4
gpt4 key购买 nike

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 文件中有该命令,当我加载插件时,它会显示在我的插件列表中,当我执行/help 时,它会显示在我的插件列表中。当我运行该命令时,它只返回用法:“/party”,但不会执行其他操作(向玩家发送帮助消息)。我不太清楚为什么它不起作用。我是否错误地忘记了什么?另外,这是我的第一个问题,如果不符合标准,我深表歉意。

最佳答案

我不确定楼上的人在说什么,但让我解释一下:是的,使用 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 标记,这样就会抛出这样的错误,您就会知道这就是问题所在。

关于java - 命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34125978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com