Minecraft Bukkit:命令无效

时间:2017-03-04 17:34:52

标签: java plugins minecraft bukkit

我正在为我的服务器开发一个essentials插件,但我无法获得第五个命令。

以下是Main.java的代码:

package me.knightgeek.geekessentials;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

public void onEnable() { //On plugin startup
    Bukkit.getServer().getLogger().info("Geek Essentials has been Enabled");
}

public void onDisable() { //On plugin close
    Bukkit.getServer().getLogger().info("Geek Essentials has been Disabled");
}

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {       
    if (cmd.getName().equalsIgnoreCase("heal")) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.RED + "The console cannot run this command!");

            return true;
        }

        Player player = (Player) sender;

        if (args.length == 0) {
            player.setHealth(20);
            player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "You have been healed.");

            return true;
        }

        @SuppressWarnings("deprecation")
        Player target = Bukkit.getServer().getPlayer(args[0]);

        if (target == null) {
            player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.RED + "This player has not been found.");
            return true;
        }
        target.setHealth(20);
        target.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "You have been healed by " + ChatColor.WHITE + player.getName());
        player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "You have succesfully healed " + ChatColor.WHITE + target.getName());
        return true;
    }



    if (cmd.getName().equalsIgnoreCase("feed")) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.RED + "The console cannot run this command!");

            return true;
        }

        Player player = (Player) sender;

        if (args.length == 0) {
            player.setFoodLevel(20);
            player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "You have been fed.");

            return true;
        }

        @SuppressWarnings("deprecation")
        Player target = Bukkit.getServer().getPlayer(args[0]);

        if (target == null) {
            player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.RED + "This player has not been found.");
            return true;
        }
        target.setFoodLevel(20);
        target.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "You have been fed by " + ChatColor.WHITE + player.getName());
        player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "You have succesfully fed by " + ChatColor.WHITE + target.getName());
        return true;
    }



    if (cmd.getName().equalsIgnoreCase("survival")) { 
        if (!(sender instanceof Player)) {
            sender.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.RED + "The console cannot run this command!");

            return true;
        }

        Player player = (Player) sender;

        if (args.length == 0) {
            player.setGameMode(GameMode.SURVIVAL);
            player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "Your gamemode is set to survival.");
            return true;
        }

        @SuppressWarnings("deprecation")
        Player target = Bukkit.getServer().getPlayer(args[0]);

        if (target == null) {
            player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.RED + "This player has not been found.");
            return true;
        }
        target.setGameMode(GameMode.SURVIVAL);
        target.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "Your gamemode is set to survival by " + player.getName());
        player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "You have succesfully set " + target.getName() + "'s gamemode to survival.");
        return true;
    }



    if (cmd.getName().equalsIgnoreCase("creative")) { 
        if (!(sender instanceof Player)) {
            sender.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.RED + "The console cannot run this command!");

            return true;
        }

        Player player = (Player) sender;

        if (args.length == 0) {
            player.setGameMode(GameMode.CREATIVE);
            player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "Your gamemode is set to creative.");
            return true;
        }

        @SuppressWarnings("deprecation")
        Player target = Bukkit.getServer().getPlayer(args[0]);

        if (target == null) {
            player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.RED + "This player has not been found.");
            return true;
        }
        target.setGameMode(GameMode.CREATIVE);
        target.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "Your gamemode is set to creative by " + ChatColor.WHITE + player.getName());
        player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "You have succesfully set " + ChatColor.WHITE + target.getName() + ChatColor.GREEN + "'s gamemode to creative.");
        return true;
    }



    if (cmd.getName().equalsIgnoreCase("adventure")) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.RED + "The console cannot run this command!");

            return true;
        }

        Player player = (Player) sender;

        if (args.length == 0) {
            player.setGameMode(GameMode.ADVENTURE);
            player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "Your gamemode is set to adventure.");
            return true;
        }
        @SuppressWarnings("deprecation")
        Player target = Bukkit.getServer().getPlayer(args[0]);

        if (target == null) {
            player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.RED + "This player has not been found.");
            return true;
        }
        target.setGameMode(GameMode.ADVENTURE);
        target.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "Your gamemode is set to adventure by " + ChatColor.WHITE + player.getName());
        player.sendMessage(ChatColor.GOLD + "[Geek Essentials] " + ChatColor.GREEN + "You have succesfully set " + ChatColor.WHITE + target.getName() + ChatColor.GREEN + "'s gamemode to adventure");
        return true;
    }

    return true;
}
}

这是plugin.yml:

name: GeekEssentials
main: me.knightgeek.geekessentials.Main
author: KnightGeek
description: The essentials plugin for GeekCraft.
version: 1.0

commands:
    heal:
        usage: /<command>
        description: Heals a player.
    feed:
        usage: /<command>
        description: Feeds a player.
    survival:
        usage: /<command>
        description: Sets your gamemode to survival.
    creative:
        usage: /<command>
        description: Sets your gamemode to creative.
    adventure:
        usage: /<command>
        description: Sets your gamemode to adventure.

这里的问题是它给了我未知的命令文本。它在使用/ adventure命令时给了我这个。所有其他命令都可以正常工作。我已经重写了这个命令的代码,但仍然没有办法解决它。

也许有人知道我在做错了什么? P.S。:缩进是错误的,我知道。这是因为我不得不重新缩进所有内容,而我并没有计划这样做。在Eclipse中,它会像它应该缩进。

1 个答案:

答案 0 :(得分:0)

Op评论了他的解决方案,并将其作为答案,因为它可能会对未来的用户有所帮助。

造成这种情况的原因是你的插件没有像你期望的那样导出。

您将其导出为不同的jar文件,该文件不在您的服务器文件夹中,因此您无法测试添加到插件的任何新代码。

解决方法很简单:

从eclipse导出插件时,确保将其保存到正确的jar文件,因为服务器只会读取插件目录中的插件。