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

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

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

    Fep310

    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:
    Code:
    @Override
        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.getWorld().setSpawnLocation(ploc);
                            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:
    Code:
    [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(PluginCommand.java:47) ~[spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:711) ~[spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchServerCommand(CraftServer.java:696) [spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at net.minecraft.server.v1_15_R1.DedicatedServer.handleCommandQueue(DedicatedServer.java:443) [spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at net.minecraft.server.v1_15_R1.DedicatedServer.b(DedicatedServer.java:407) [spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:984) [spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:824) [spigot-1.15.2.jar:git-Spigot-b9baf71-544ccdc]
       at java.lang.Thread.run(Unknown 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(SpawnCmds.java:16) ~[?:?]
       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[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

    Kars

    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

    Strahan

    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

    Fep310

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

    bowlerguy66

    @Fep310 Make sure to mark your title as solved ;)
     
Thread Status:
Not open for further replies.

Share This Page