Bukkit命令重复了吗?

时间:2015-04-24 02:06:54

标签: java minecraft bukkit

我编写了一个没有错误的Bukkit插件,但在游戏中它存在问题。

每当使用该命令时,它都不会执行它应该执行的操作。相反,它向我发送了一条大胆的消息,说明我输入的内容/leaving

这是我的代码:

public class SeeYouSoon extends JavaPlugin{

// Start
@Override
public void onEnable(){
}

@Override 
public void onDisable() {
}

//Commands



public boolean onCommand(CommandSender sender, Command cmd, String[] args){
    if(cmd.getName().equalsIgnoreCase("leaving")){
        Player player = (Player) sender;
        Bukkit.broadcastMessage(ChatColor.RED + player.getName() + ChatColor.LIGHT_PURPLE + ChatColor.ITALIC + " is about to leave the server. Please say your goodbyes!");
        Bukkit.getScheduler().runTaskLater(this, new Runnable() {
            public void run() {
                //Run your function or change stuff here.
               player.kickPlayer(ChatColor.RED + "Kicked:" + ChatColor.WHITE + " You requested to leave.");
            }
        }, 20 * 30);// There are 20 ticks in one second so we can just multiply seconds by 20.
    }
    return false;

}
}

plugin.yml是我所期望的问题所在。这是plugin.yml:

name: SeeYouSoon
main: me.mark.SeeYouSoon
version: 1.0
commands:
   leaving:
      description: Announce your leave

2 个答案:

答案 0 :(得分:5)

首先,onCommand()中的JavaPlugin方法格式为:

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

关于为什么会发生这种情况的下一个原因是因为在你按照命令行事后你不会return true。例如,您应该使用:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("leaving")){
        //your code
        return true;
    }
}

因此,您的最终代码应如下所示:

public boolean onCommand(CommandSender sender, Command cmd, String[] args){
    if(cmd.getName().equalsIgnoreCase("leaving")){
        Player player = (Player) sender;
        Bukkit.broadcastMessage(ChatColor.RED + player.getName() + ChatColor.LIGHT_PURPLE + ChatColor.ITALIC + " is about to leave the server. Please say your goodbyes!");
        Bukkit.getScheduler().runTaskLater(this, new Runnable() {
            public void run() {
               player.kickPlayer(ChatColor.RED + "Kicked:" + ChatColor.WHITE + " You requested to leave.");
            }
        }, 20 * 30);
        return true;
    }
    return false;   
}

答案 1 :(得分:1)

你没有安全地投射到一名球员:

Player player = (Player)sender;

虽然这是正确的java,但这可能会导致错误。在投射之前确保发件人实际上是一名玩家。

if (!sender instanceof Player)
{
    sender.sendMessage("Some error message");
    return true;
}
// Now you can cast to a player and continue with your code.