Solved Console error

Discussion in 'Plugin Development' started by DesiringSolid, Sep 24, 2016.

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

    DesiringSolid

    Hi,

    I am getting console errors; I tryed a different things but did not resolve the problem

    Error:
    Code:
    [02:58:19] [Server thread/WARN]: Unexpected exception while parsing console command "fly"
    org.bukkit.command.CommandException: Unhandled exception executing command 'fly' in plugin VanillaCheat v1.1a
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:625) ~[craftbukkit-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Bukkit-0ebb9c7]
        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchServerCommand(CraftServer.java:611) [craftbukkit-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Bukkit-0ebb9c7]
        at net.minecraft.server.v1_10_R1.DedicatedServer.aL(DedicatedServer.java:397) [craftbukkit-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Bukkit-0ebb9c7]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:361) [craftbukkit-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Bukkit-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:646) [craftbukkit-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Bukkit-0ebb9c7]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:550) [craftbukkit-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Bukkit-0ebb9c7]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_10_R1.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
        at me.decaf1.vanillacheat.FlyCmd.onCommand(FlyCmd.java:12) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.10.2-R0.1-SNAPSHOT-latest.jar:git-Bukkit-0ebb9c7]
        ... 8 more
    
    FlyCmd.java
    Code:
    package me.decaf1.vanillacheat;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class FlyCmd implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
             Player player = (Player) sender;
                if (label.equalsIgnoreCase("fly")) {
                    if (!sender.hasPermission("vanillacheat.fly")) {
                        sender.sendMessage(ChatColor.RED + "You don't have the permissions to do that!");
                        return true;
                    }
                    if (!(sender instanceof Player)) {
                        sender.sendMessage(ChatColor.RED + "[VanillaCheat] " + ChatColor.GRAY + "Only players can execute the command!");
                        return true;
                    }
                            if(player.getAllowFlight() == true) {
                                player.setAllowFlight(false);
                                player.sendMessage(ChatColor.RED + "Fly disabled");
                                return true;
                            }
                            if(player.getAllowFlight() == false) {
                                player.setAllowFlight(true);
                                player.sendMessage(ChatColor.GREEN + "Fly enabled!");
                            }
                        }
                return false;
            }
    }
    
     
    Last edited: Sep 24, 2016
  2. Offline

    DesiringSolid

    ---- deleted ----
     
    Last edited: Sep 24, 2016
  3. Offline

    Zombie_Striker

    @bwfcwalshy
    Are you sure this is for the correct thread?

    @DesiringSolid
    You are getting this error because you are forgetting to check if the sender actually is a player. In this case, you are blindly casting the sender to a Player but the sender that sent the command was the console. Since consoles are not player, it throws this exception.

    To fix this issue, add an instanceof check before creating the player variable.
     
  4. Offline

    DesiringSolid

    @Zombie_Striker
    I am doing that right?
    Code:
    Player player = (Player) sender;
                    if (!(sender instanceof Player)) {
                        sender.sendMessage(ChatColor.RED + "[VanillaCheat] " + ChatColor.GRAY + "Only players can execute the command!");
                        return true;
                    }
    
    
     
  5. Offline

    Evonoucono

    @DesiringSolid
    Think about it, you are casting the sender to a player, and then checking if sender is not an instance of the Player class... If sender is a player then that code will not run. If it's not a player then you will get an error.
     
  6. Offline

    Zombie_Striker

    @DesiringSolid
    Move the player variable inside of the if statement.
     
  7. Offline

    DesiringSolid

  8. @Zombie_Striker He edited the thread. That was correct before the edit.
     
Thread Status:
Not open for further replies.

Share This Page