Solved Check command sender

Discussion in 'Plugin Development' started by FluffyMe420, Aug 5, 2017.

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

    FluffyMe420

    i want to check if the console sends a command and give the console the message "ERROR" if it does

    Here's the code https://pastebin.com/mNupgCy4

    I have the if(!(sender instanceof Player)) but i still get and console message with the following error:

    org.bukkit.craftbukkit.v1_12_R1.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
     
  2. Online

    timtower Administrator Administrator Moderator

    @FluffyMe420 Your check is in reverse, if it IS a player then you give the error.
     
  3. Offline

    FluffyMe420

    @timtower if i use the command as a MC Player i dont want to get the error and i dont get an error if i do. I want the Console to get an error when he tries to use the command /invsee
     
  4. Online

    timtower Administrator Administrator Moderator

    @FluffyMe420 Check line 4 of your pastebin.
    It will give the player the error.
     
  5. Offline

    FluffyMe420

    @timtower I see what you meen, I corrected it in my code to:
    if (!(sender instanceof Player))

    but i still get this error when i type it in the console: rg.bukkit.craftbukkit.v1_12_R1.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
     
  6. Online

    timtower Administrator Administrator Moderator

    @FluffyMe420 Please post your full class and full error.
     
  7. Offline

    FluffyMe420

    @timtower look at the lowest row of this paste bin: https://pastebin.com/yuC7RiWv

    ERROR:
    Unexpected exception while parsing console command "invsee"
    org.bukkit.command.CommandException: Unhandled exception executing command 'invsee' in plugin Fessentials vBETA
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-86aa17f]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-86aa17f]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:621) ~[craftbukkit.jar:git-Bukkit-86aa17f]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchServerCommand(CraftServer.java:607) [craftbukkit.jar:git-Bukkit-86aa17f]
    at net.minecraft.server.v1_12_R1.DedicatedServer.aP(DedicatedServer.java:407) [craftbukkit.jar:git-Bukkit-86aa17f]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:371) [craftbukkit.jar:git-Bukkit-86aa17f]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:650) [craftbukkit.jar:git-Bukkit-86aa17f]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:554) [craftbukkit.jar:git-Bukkit-86aa17f]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_12_R1.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at xyz.fluffyme.fessentials.main.onCommand(main.java:26) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-86aa17f]
    ... 8 more

    im creating this plugin for learning purpose so that explains the commands
     
  8. Online

    timtower Administrator Administrator Moderator

    @FluffyMe420 That is a lot of additional code.
    Line 26 is causing one of the errors, could be more after that though.
     
  9. Offline

    FluffyMe420

    @timtower that was the error thanks, can i ask you a quick question? How did you learn bukkit/java??
     
  10. Offline

    Reflxction

    You could just do
    Code:
    if(sender instanceof ConsoleCommandSender) {
        sender.sendMessage("ERROR");
    }
    Also, I suggest using if(sender instanceof Player) instead of Player p = (Player) sender, especially when you write it in the beginning of the onCommand method.
     
  11. Online

    timtower Administrator Administrator Moderator

    @xTechno_ CommandBlocks will break that check.

    @FluffyMe420 Learned the basics of Java at school, Bukkit by trial and error.
     
Thread Status:
Not open for further replies.

Share This Page