The line "sender instanceof Player" is not changing anything.

Discussion in 'Plugin Development' started by Fep310, Feb 4, 2020.

  1. Offline


    I'm making two simple commands in one class (/setspawn and /spawn), they already work like I wnat them to, but the part wich is supposed to check if the sender is a Player is not affecting anything.

    My code for both commands:
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
            Location ploc = p.getLocation();
            if (sender instanceof Player) {
                // Setspawn command
                if (command.getName().equalsIgnoreCase("setspawn")) {
                    if (sender.hasPermission("quickpvp.setspawn")) {
                        if (args.length == 0) {
                            p.sendMessage(ChatColor.GREEN + "[!] You've successfully set the world spawn.");
                        } else sender.sendMessage(ChatColor.RED + "[!] This command doesn't support arguments.");
                    } // On plugin.yml
                // Spawn command
                else if (command.getName().equalsIgnoreCase("spawn")) {
                    if (sender.hasPermission("quickpvp.spawn")) {
                        if (args.length == 0) {
                            p.teleport(p.getWorld().getSpawnLocation().add(0.5, 0, 0.5));
                            p.sendMessage(ChatColor.GREEN + "[!] You've successfully teleported to the spawn.");
                        } else sender.sendMessage(ChatColor.RED + "[!] This command doesn't support arguments.");
                    } // On plugin.yml
            } else sender.sendMessage(ChatColor.RED + "[!] This command is for players only.");
            return true;
    The console error:
    [17:03:22 WARN]: Unexpected exception while parsing console command "spawn"
    org.bukkit.command.CommandException: Unhandled exception executing command 'spawn' in plugin QuickPvpPlugin v1.0-SNAPSHOT
       at org.bukkit.command.PluginCommand.execute( ~[spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at org.bukkit.command.SimpleCommandMap.dispatch( ~[spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand( ~[spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchServerCommand( [spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at net.minecraft.server.v1_15_R1.DedicatedServer.handleCommandQueue( [spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at net.minecraft.server.v1_15_R1.DedicatedServer.b( [spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at net.minecraft.server.v1_15_R1.MinecraftServer.a( [spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at [spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at Source) [?:1.8.0_241]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_15_R1.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
       at io.github.fep310.quickpvpplugin.Commands.SpawnCmds.onCommand( ~[?:?]
       at org.bukkit.command.PluginCommand.execute( ~[spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       ... 8 more
    I used to make plugins some time ago.
    Can someone assist me please? Thanks ^^
  2. Offline


    It has nothing to do with the instanceof, you are casting commandSender to player in the first line of your function.
    Fep310 and Strahan like this.
  3. Offline


    Your logic with the casting and the instanceof check is analogous to:
    1. Take cover off outlet
    2. Touch wires
    3. Check if power is off
    You do understand what the instanceof check is for right? And what is happening when you do Player p = (Player)sender?
    Fep310 likes this.
  4. Offline


    Whoops, I've fixed it. Sorry for the stupid mistake, I'll pay more attention next time.
    Thanks for the help c:
  5. Offline


    @Fep310 Make sure to mark your title as solved ;)

Share This Page