Command Issue

Discussion in 'Plugin Development' started by bhuss05, Feb 19, 2016.

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

    bhuss05

    Hello, it's me, and I have been developing my very first (funny) Minecraft Bukkit plugin. I make a command, and use it n-game and it gives me the error "An internal error occurred while attempting to perform this command." This is the error that I get on the console:

    Code:
    [21:35:32 WARN]: Unexpected exception while parsing console command "hi"
    org.bukkit.command.CommandException: Unhandled exception executing command 'hi' in plugin Emotions v1.0
       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
       at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]
       at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
       at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
       at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
       at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
       at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot_server.jar:git-Spigot-db6de12-18fbb24]
       at java.lang.Thread.run(Thread.java:745) [?:1.8.0_72]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
       at me.bhuss05.emotions.Emotions.onCommand(Emotions.java:33) ~[?:?]
       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_server.jar:git-Spigot-db6de12-18fbb24]


    I don't understand what it means by that, but this is my main file:

    Code:
    package me.bhuss05.emotions;
    
    import java.util.logging.Logger;
    import net.md_5.bungee.api.ChatColor;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Emotions extends JavaPlugin {
    
       public final Logger logger = Logger.getLogger("Minecraft");
       public static Emotions plugin;
      
       public void onEnable()
       {
         PluginManager mpm = this.getServer().getPluginManager();
         PluginDescriptionFile pdfFile = this.getDescription();
         this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
    
       }
       public void onDisable()
       {
         PluginDescriptionFile pdfFile = this.getDescription();
         this.logger.info(pdfFile.getName() + " Has Been Disabled!");
       }
       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
       {
         Player player = (Player) sender;
         Player targetPlayer = player.getServer().getPlayer(args[0]);
         if (commandLabel.equalsIgnoreCase("hi"))
         {
           Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + ChatColor.GREEN + " says hi!");
         }
         else if (commandLabel.equalsIgnoreCase("lol"))
         {
           Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + ChatColor.GREEN + " laughs!");
         }
         else if (commandLabel.equalsIgnoreCase("sticktongueout"))
         {
           Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + ChatColor.GREEN + " sticks his/her tongue out!");
         }
         else if (commandLabel.equalsIgnoreCase("grin"))
         {
           Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + ChatColor.GREEN + " grins!");
         }
         else if (commandLabel.equalsIgnoreCase("happy"))
         {
           Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + ChatColor.GREEN + " is happy!");
         }
         else if (commandLabel.equalsIgnoreCase("surprised"))
         {
           Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + ChatColor.GREEN + " is surprised!");
         }
         else if (commandLabel.equalsIgnoreCase("sad"))
         {
           Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + ChatColor.GREEN + " is sad...");
         }
         else if (commandLabel.equalsIgnoreCase("laughinghard"))
         {
           Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + ChatColor.GREEN + " laughs so hard that he/she cries!");
         }
         else if (commandLabel.equalsIgnoreCase("crying"))
         {
           Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + ChatColor.GREEN + " is crying sadly...");
         }
         else if (commandLabel.equalsIgnoreCase("bye"))
         {
           Bukkit.broadcastMessage(ChatColor.AQUA + player.getName() + ChatColor.GREEN + " says bye!");
         }
         else
         {
           player.sendMessage(ChatColor.DARK_RED + "Unable to use command!");
         }
         return false;
       }
    }
    This is my plugin.yml file:

    Code:
    name: Emotions
    main: me.bhuss05.emotions.Emotions
    version: 1.0
    description: >
                 A plugin to express emotions with.
    commands:
      hi:
        description: Say hello to everyone!
      lol:
        description: Say you are laughing to everyone!
      sticktongueout:
        description: Stick your tongue out!
      grin:
        description: Say you are grinning to everyone!
      happy:
        description: Say you are happy to everyone!
      surprised:
        description: Say you are surprised to everyone!
      sad:
        description: Say you are sad to everyone!
      laughinghard:
        description: Say you are laughing so hard you are crying to everyone!
      crying:
        description: Say you are crying to everyone!
      bye:
        description: Say good-bye to everyone!
    I have tried YouTube tutorials and looking over Google and my code, but nothing seems to appear wrong. Can somebody please help?
     
  2. Offline

    teej107

  3. Offline

    Zombie_Striker

    Very bad idea.

    @bhuss05
    Just remember: never blindly cast objects. If you have to cast an object, check if that object is an InstanceOf what you're casting the object to.
     
  4. Offline

    SkyleTyler1337

    is that all of the code?

    Code:
    /* try these */
    
      // checking if the sender is a player
      if(sender instanceof Player){
        // casting
            Player player = (Player) sender;
             
    }
    
    
    // checking if the sender is not a player 
    if(!(sender instanceof Player)){
        //DO something
    }
    //casting
    Player player = (Player) sender;
    
     
    Last edited: Feb 19, 2016
  5. Offline

    Lightspeed

    Code:
    if (sender instanceof Player)
    {
      //WHOOO CAST
    }
    else
    {
      //Nota player keep it nice and short
    }
    A bit cleaner and memory saving is always good ;)
     
    Xerox262 likes this.
  6. Offline

    Xerox262

    @Lightspeed That's what he posted, he just didn't explain it well, what he meant was.

    If you want to check if they're a player
    Code:
     if(sender instanceof Player){ // Will only trigger if it's a player, no matter what else it could e
       // Cast to player since there is no chance they're not a player
       Player player = (Player) sender;
    } else {
       // Not a player
       return true; // replace with whatever return method is needed
    }
    If you want to check that they're not a player
    Code:
    if(!(sender instanceof Player)){ // Everything, so long as it's not a player, will trigger this if
       //DO something
       return true;   // Return statement will stop the method here, it wont go to the casting
    }
    // Cast to player since everything else would've got caught by the if and the method would end before this
    Player player = (Player) sender;
    It did look as if he was just posting it as one method however, took me a few minutes to recognize what he meant.
     
  7. Offline

    BobTheHamster9

    You have to make sure you are importing the correct ChatColor.

    You imported net.MD_5.bungee.API.ChatColor

    You should be using org.bukkit.ChatColor
     
  8. Offline

    TheManiacGamers

    I agree with this

    Import org.bukkit.ChatColor instead of net.MD_5.bungee.API.ChatColor

    Just delete the import and re-import it.
     
  9. Offline

    Lightspeed

    Ohhh dang even more compact. XD
     
Thread Status:
Not open for further replies.

Share This Page