[help] Craftbukkit 1.7.9 won't allow casting of sender to player?

Discussion in 'Plugin Development' started by GlacialCreeper, Jun 26, 2014.

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

    GlacialCreeper

    So, I was remaking a small explosive projectiles plugin that I've already done once (private plugin).
    I was making some commands -- /bow, /snowballs, /egg -- that would give you some materials to, y'know, shoot and blow up stuff. I have it so that if the console tries to issue any of the commands, it sends back a message. *Quick edit* The message worked before, but now it wont, for some reason.
    Anyway, to the point, the console says this:
    Code:
    >egg
    [18:57:34 WARN]: Unexpected exception while parsing console command "egg"
    org.bukkit.command.CommandException: Unhandled exception executing command 'egg' in plugin ExplosiveProjectiles v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchServerCommand(CraftServer.java:688) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.aB(DedicatedServer.java:296) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:261) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
        at me.glacialcreeper.projectiles.Initial.onCommand(Initial.java:29) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        ... 8 more
    Here's my Initial class:
    Code:java
    1. package me.glacialcreeper.projectiles;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Initial extends JavaPlugin {
    13.  
    14. public void onEnable()
    15. {
    16. Bukkit.getServer().getPluginManager().registerEvents(new Projectile_Listeners(), this);
    17. Bukkit.getServer().getLogger().info(ChatColor.BLUE+"Get ready for projectile explosions. :)");
    18. }
    19.  
    20. public void onDisable()
    21. {
    22.  
    23. }
    24.  
    25. @SuppressWarnings("deprecation")
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    27. {
    28.  
    29. Player player = (Player) sender;
    30.  
    31. if (!(sender instanceof Player)){
    32. sender.sendMessage(ChatColor.RED+"That's for players, silly!");
    33. }
    34.  
    35.  
    36.  
    37.  
    38. if (label.equalsIgnoreCase("bow")){
    39. player.getInventory().setItem(0, new ItemStack(Material.BOW));
    40. player.getInventory().addItem(new ItemStack(Material.ARROW, 32));
    41. player.updateInventory();
    42. player.sendMessage(ChatColor.GREEN+"You got a bow!");
    43. }
    44.  
    45. if ((label.equalsIgnoreCase("snowballs"))){
    46. player.getInventory().addItem(new ItemStack(Material.SNOW_BALL, 32));
    47. player.updateInventory();
    48. player.sendMessage(ChatColor.GREEN+"Here are some snowballs!");
    49. }
    50.  
    51. if (label.equalsIgnoreCase("egg")){
    52. player.getInventory().addItem(new ItemStack(Material.EGG, 25));
    53. player.updateInventory();
    54. player.sendMessage(ChatColor.GREEN+"Have some eggs!");
    55. }
    56.  
    57. return true;
    58. }
    59.  
    60. }
    61.  


    I checked line 29, and its:
    Code:java
    1. Player player = (Player) sender;


    This used to work before without an exception; has bukkit changed since 1.7.2 bukkit? And how do I fix these problems (console wont receive chat message+the exception)?
    **I just noticed CommandSender is an interface, but Idk if that's how its always been.
     
  2. Offline

    xTigerRebornx

    GlacialCreeper You are casting the sender to a Player before you check if they actually are. You need to cast it after you check.
     
    dsouzamatt likes this.
  3. Offline

    Krakyn<C

    Try using:
    Code:java
    1. if(sender instanceof Player){
    2. //dowhatever
    3. }else{
    4. //dowhatever
    5. }
     
Thread Status:
Not open for further replies.

Share This Page