Solved Error when used in Console but not In Game

Discussion in 'Plugin Development' started by Begreen98, Feb 16, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Begreen98

    While developing my plugin I tested everything in game and it worked fine, I then tested in Console and found an Error. The Error message it gives me is:
    Code:
    13:54:23 [WARNING] Unexpected exception while parsing console command "sick"
    org.bukkit.command.CommandException: Unhandled exception executing command 'sick
    ' in plugin GetSick v3.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    6)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer
    .java:514)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchServerCommand(Craft
    Server.java:506)
            at net.minecraft.server.v1_4_6.DedicatedServer.al(DedicatedServer.java:2
    60)
            at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:22
    5)
            at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:49
    4)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:
    427)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:84
    9)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_4_6.command.C
    olouredConsoleSender cannot be cast to org.bukkit.entity.Player
            at me.begreen98.GetSick.GetSick.onCommand(GetSick.java:38)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 8 more
    >
    
    I looked at line 38 and 44 and these are the codes it is
    Line 38: Player player = (Player) sender;
    Line 44: player.addPotionEffect(new PotionEffect(
    Note: The code on line 44 is continued in line 45.
    Is tehre a reason its giving an error only in Console but not in game?
     
  2. Offline

    Tirelessly

    Well if the sender is console, then you can't cast it to a player...
     
  3. Offline

    Begreen98

    I use if (sender instanceof Player) {
    to set it to only allow Players to use that, but even if I try to make someone else sick by using :/sick NAME which also works in game, it still causes an error on the same lines
     
  4. Offline

    Tirelessly

    Then you're doing it wrong, probably. Post the whole section of code.
     
  5. Offline

    Begreen98

    Code:
    public boolean onCommand(CommandSender sender, Command cmd,
                String commandlabel, String[] args) {
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("sick")) {
                if (args.length == 0) {
                    if (sender instanceof Player) {
                        sender.sendMessage(ChatColor.GRAY
                                + "You have made yourself sick!");
                        player.addPotionEffect(new PotionEffect(
                                PotionEffectType.CONFUSION, 500, 3));
                        player.addPotionEffect(new PotionEffect(
                                PotionEffectType.HUNGER, 500, 1));
                        if (getConfig().getString("Effects.HurtFromSick").equals(
                                "true")) {
                            player.addPotionEffect(new PotionEffect(
                                    PotionEffectType.POISON, 500, 1));
                        }
                    }
                } else if (args.length == 1) {
                    if (player.getServer().getPlayer(args[0]) != null) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        player.sendMessage(ChatColor.GRAY + "You have made "
                                + targetPlayer.getName() + " sick!");
                        targetPlayer.addPotionEffect(new PotionEffect(
                                PotionEffectType.CONFUSION, 500, 3));
                        targetPlayer.addPotionEffect(new PotionEffect(
                                PotionEffectType.HUNGER, 500, 1));
                        targetPlayer.sendMessage(ChatColor.GRAY
                                + "You have been made sick by " + player.getName()
                                + "!");
                    }
                    if (getConfig().getString("Effects.HurtFromSick")
                            .equals("true")) {
                        player.addPotionEffect(new PotionEffect(
                                PotionEffectType.POISON, 500, 1));
                    } else {
                        player.sendMessage(ChatColor.DARK_RED
                                + "Player not online!");
                    }
                }
    Thats the whole command that is used.
     
  6. Code:java
    1.  
    2. if(args.lenght == 0)
    3. {
    4. if(sender instanceof Player) // Rith thing
    5. }
    6. else if(args.lenght == 1)
    7. {
    8. //You forgot the if(sender instanceof Player)
    9. }
    10.  



    Add this ontop of you Oncommand:
    Code:java
    1.  
    2. if(!(sender instanceof Player)) return;
    3.  
     
  7. Offline

    Begreen98

    Will that make it so Console can't use the Sick command at all? I want them to be able to make OTHER people sick.
     
  8. Then don't cast sender to player ! And get rid of your player variable if you want the command to be shared on console and players too.

    And store your Bukkit.getPlayer() result if you want to do stuff on the target...

    For messages just use sender.sendMessage().
     
  9. Offline

    Tirelessly

    You cast sender to player on the first line of onCommand, before you check if sender is a player...
     
  10. Offline

    Begreen98

    I don't really understand what you're suggesting, can you explain a little?
     
  11. Code:
    Player player = (Player) sender;
    You don't need that at the start of the command, move it after you check instanceof Player, where you want to apply /sick to command sender.

    Then...
    Code:
    if (player.getServer().getPlayer(args[0]) != null) {
         Player targetPlayer = player.getServer().getPlayer(args[0]);
    Don't waste resources on searching for a player twice, store it the first time then check its value.
     
  12. Offline

    Begreen98

    Thank you, that works.
     
Thread Status:
Not open for further replies.

Share This Page