Bukkit 无法使用控制台治愈玩家

Bukkit cant heal player using console

我想用控制台治疗玩家。我正在使用 bukkit 1.8.3。当我尝试使用我的代码修复它时,出现错误。我尝试在互联网上搜索它说的因为控制台不是播放器而且我知道我删除了所有播放器。并替换为消息或发件人。还是一样的错误。

package me.herobrine112211.ulticmdpack;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class System extends JavaPlugin implements Listener{

    public void onEnable() {
        getConfig().options().copyDefaults(true);
        saveConfig();
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
        Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been actived!");
    }

    public void onDisable() {
        Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been disabled! This will give big problems to server.");
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent joinevent){
        Player getplayer = joinevent.getPlayer();
        getplayer.sendMessage(ChatColor.AQUA + "Hey " + getplayer.getName() + "! Welcome to the Ultimate Prison server!");
        if(getplayer.getName().equals("Herobrine112211")){
            Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.WHITE + "] " + ChatColor.GOLD + "Server Creator Herobrine112211 has joined the game!!!!!!!!!!");
        }
    }

    @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){


        Player player = (Player) sender;

            if (cmd.getName().equalsIgnoreCase("contactowner")) {
                if (!sender.hasPermission("contactowner")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "Console will never need owner support.");
                    return true;
                }
                player.sendMessage(ChatColor.GOLD + "Owner: Herobrine112211. Live chat skype: gangsteris33.");
            }

            if (cmd.getName().equalsIgnoreCase("reloadconfig")){
                if (!sender.hasPermission("config.reload")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                reloadConfig();
                sender.sendMessage(ChatColor.RED + "Server config reloaded successfully.");
                return true;
            }

            if (cmd.getName().equalsIgnoreCase("saveconfig")){
                if (!sender.hasPermission("config.save")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                reloadConfig();
                sender.sendMessage(ChatColor.RED + "Server config saved successfully.");
                return true;
            }

            if (cmd.getName().equalsIgnoreCase("fakeop")) {
                if (!sender.hasPermission("fake.op")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify a player!");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage(ChatColor.RED + "Player must be online!");
                    return true;
                }
                target.sendMessage(ChatColor.YELLOW + "You are now op!");
            }

            if (cmd.getName().equalsIgnoreCase("fakejoin")) {
                if (!sender.hasPermission("fake.join")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify a name!");
                    return true;
                }
                Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + args[0] + " joined the game.");
            }

            if (cmd.getName().equalsIgnoreCase("broadcast")) {
                if (!sender.hasPermission("broadcast")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please give a message to broadcast!");
                    return true;
                }
                StringBuilder strbl = new StringBuilder();
                for(int i = 0; i < args.length; i++){
                    strbl.append(args[i] + " ");
                }
                String broadcastas = strbl.toString();
                Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "BROADCAST" + ChatColor.WHITE + "] " + ChatColor.RED + broadcastas);
            }

            if (cmd.getName().equalsIgnoreCase("heal")) {
                if (!sender.hasPermission("heal")){
                    sender.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (!(sender instanceof Player)) {
                    if (args.length == 0) {
                        sender.sendMessage(ChatColor.RED + "Please specify a player!");
                        return true;
                }
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null) {
                        sender.sendMessage(ChatColor.RED + "Player must be online to be healed!");
                            return true;
                    }
                    target.setHealth(20);
                    target.setFireTicks(0);
                    target.sendMessage(ChatColor.GREEN + "CONSOLE" + " healed you!");
                    sender.sendMessage(ChatColor.GREEN + "Player is healed!");
                    return true;
                }
                    if (args.length == 0) {
                            player.setHealth(20);
                            player.setFireTicks(0);
                            player.sendMessage(ChatColor.GREEN + "Your health is now full!");
                            return true;
                    }
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null) {
                        sender.sendMessage(ChatColor.RED + "Player must be online to be healed!");
                            return true;
                    }
                    target.setHealth(20);
                    target.setFireTicks(0);
                    target.sendMessage(ChatColor.GREEN + player.getName() + " healed you!");
                    player.sendMessage(ChatColor.GREEN + target.getName() + " is healed!");
            }

            if (cmd.getName().equalsIgnoreCase("feed")) {
                if (!sender.hasPermission("feed")){
                    player.sendMessage(ChatColor.RED + "Access denied.");
                    return true;
                }
                if (!(sender instanceof Player)) {
                    if (args.length == 0) {
                        sender.sendMessage(ChatColor.RED + "Please specify a player!");
                        return true;
                }
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null) {
                        sender.sendMessage(ChatColor.RED + "Player must be online to be fed!");
                            return true;
                    }
                    target.setHealth(20);
                    target.setFireTicks(0);
                    target.sendMessage(ChatColor.GREEN + "CONSOLE" + " fed you!");
                    sender.sendMessage(ChatColor.GREEN + "Player hunger is now full!");
                    return true;
                }
                if (args.length == 0) {
                        player.setFoodLevel(20);
                        player.sendMessage(ChatColor.GREEN + "Your hunger is now full!");
                        return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    player.sendMessage(ChatColor.RED + "Player must be online to be fed!");
                        return true;
                }
                target.setFoodLevel(20);
                target.sendMessage(ChatColor.GREEN + player.getName() + " fed you!");
                player.sendMessage(ChatColor.GREEN + target.getName() + " hunger is now full!");
        }
        return true;
   }
}

有人可以修复我的代码并粘贴新的代码进行复制吗?我需要这个修好。我尝试了很多次修复自己都没有用。

请求的错误日志:

10:39:14 AM [WARN] Unexpected exception while parsing console command "heal Herobrine112211"
10:39:14 AM org.bukkit.command.CommandException: Unhandled exception executing command 'heal' in plugin UltimateCommandPack v1.0.5
10:39:14 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-61ef214]
10:39:14 AM at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[bukkit.jar:git-Bukkit-61ef214]
10:39:14 AM at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:625) ~[bukkit.jar:git-Bukkit-61ef214]
10:39:14 AM at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchServerCommand(CraftServer.java:611) [bukkit.jar:git-Bukkit-61ef214]
10:39:14 AM at net.minecraft.server.v1_8_R2.DedicatedServer.aN(DedicatedServer.java:372) [bukkit.jar:git-Bukkit-61ef214]
10:39:14 AM at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:337) [bukkit.jar:git-Bukkit-61ef214]
10:39:14 AM at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:626) [bukkit.jar:git-Bukkit-61ef214]
10:39:14 AM at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:534) [bukkit.jar:git-Bukkit-61ef214]
10:39:14 AM at java.lang.Thread.run(Unknown Source) [?:1.7.0_75]
10:39:14 AM Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R2.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
10:39:14 AM at me.herobrine112211.ulticmdpack.System.onCommand(System.java:39) ~[?:?]
10:39:14 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-61ef214]
10:39:14 AM ... 8 more

您收到错误消息是因为您试图将 ColouredConsoleSender 转换为 Player

要解决此问题,您应该仅在

时将 sender 转换为 Player
if(sender instanceof Player)

因此,您应该删除此行:

Player player = (Player) sender

并且只有在检查 sender instanceof Player 后才使用它。所以,你可以这样做:

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    if(!(sender instanceof Player)){
        return false;
    }
    else{
        Player player = (Player) sender;

        //code...
    }
}

如果您还想允许控制台 运行 命令,您可以只在检查 sender instanceof Player.

后使用 Player player = (Player) sender

因此,例如:

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

  if(cmd.getName().equalsIgnoreCase("heal"){
    if(sender instanceof Player){
      Player player = (Player) sender;
      if(args.length > 0){
        Player target = Bukkit.getServer().getPlayer(args[0]);
        target.setHealth(20.0);
        player.sendMessage("You have healed " + args[0]);
      }
      else{
        player.setHealth(20.0);
        player.sendMessage("You have been healed");
      }
    }
    else{
      if(args.length > 0){
        Player target = Bukkit.getServer().getPlayer(args[0]);
        target.setHealth(20.0);
        sender.sendMessage("You have healed " + args[0]);
      }
      else{
        sender.sendMessage("Correct usage: /heal <player>");
      }
    }
  }
  return true;
}

从表面上看,我相信您从未成功过。如果你没有,你可以这样设置:

// configure variables.

ConsoleSender console = getServer().getConsoleSender();
Player target = getServer().getPlayer(args[0]);
String[] commands = {"heal"};

if(command.getLabel().equalsIgnoreCase(commands[0])){
    // this checks to see if the sender, is the console.
    if(sender == console){                 
        target.setHealth(20.0);
        target.sendMessage("healed by " + sender.getDisplayName());
    }
    return true;                                               
}