Help with console sent commands

Discussion in 'Plugin Development' started by Drat333, Apr 4, 2012.

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

    Drat333

    Hi, I'm fairly new to Java and this is my first 'real' plugin. I'm having issues with the plugin spitting out errors when the console attempts to send the command. Here is the whole code for the command:

    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
         
            Player player = null;
                if (sender instanceof Player){
                    player = (Player) sender;
                }
             
            GameMode creative = GameMode.CREATIVE;
            GameMode survival = GameMode.SURVIVAL;
         
         
            if (args.length == 0){
                if (player == null){
                    player.sendMessage("You cannot change the console's gamemode, silly.");
                    return true;
                }else{
                    if (player.getGameMode() == survival){
                        player.setGameMode(creative);
                        player.sendMessage(ChatColor.GREEN + "Creative mode enabled.");
                    }else if (player.getGameMode() == creative){
                        player.setGameMode(survival);
                        player.sendMessage(ChatColor.GREEN + "Creative mode disabled.");
                    }
                }
            }else if (args.length == 1){
                Server server = Bukkit.getServer();
                Player target = server.getPlayer(args[0]);
                if (target == null){
                    player.sendMessage(ChatColor.RED+""+target + " does not exist, or is not online.");
                }else{
                    if (target.getGameMode() == survival){
                        target.setGameMode(creative);
                        target.sendMessage(ChatColor.GREEN + "Creative mode enabled by " + player + ".");
                        player.sendMessage(ChatColor.GREEN + "Creative mode enabled for " + target);
                    }else if (player.getGameMode() == creative){
                        target.setGameMode(survival);
                        target.sendMessage(ChatColor.GREEN + "Creative mode disabled " + player + ".");
                        player.sendMessage(ChatColor.GREEN + "Creative mode disabled for " + target);
                    }
                }
             
            }else if (args.length > 1){
                player.sendMessage(ChatColor.RED + "Too many arguments!");
                return false;
            }
         
            return true;
         
         
        }
    What is supposed to happen is:

    If the command is sent without arguments and is the player, toggle the player's gamemode.
    If the command is sent without arguments and is the console, give the console an error.
    If the command is sent with an argument, toggle the gamemode of the target, regardless of who sent the command.

    This bug has been bothering me all day, causing me to rewrite everything twice. I'm lost.

    Any help is appreciated.

    EDIT: The errors currently being spit out are unexpected exceptions, both with and without arguments sent by console.
     
  2. Offline

    Technius

    Give me your error report.
     
  3. Offline

    Drat333

    Code:
    >creative
    00:23:11 [WARNING] Unexpected exception while parsing console command
    org.bukkit.command.CommandException: Unhandled exception executing command 'creative' in plugin SimpleCreative v0.2
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473)
        at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:469)
        at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:599)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:568)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:452)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: java.lang.NullPointerException
        at org.dyndns.dratcraft.SimpleCreative.SimpleCreativeExecutor.onCommand(SimpleCreativeExecutor.java:34)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 7 more
    
     
  4. Offline

    Njol

    Code:
    Caused by: java.lang.NullPointerException
        at org.dyndns.dratcraft.SimpleCreative.SimpleCreativeExecutor.onCommand(SimpleCreativeExecutor.java:34)
    You have a NPE. Fix it.
     
  5. Offline

    Drat333

    Again, new to this.

    So, I'm going to assume this has to do with the player variable equaling null when the sender is the console. Kinda what I suspected, and I've been playing around with it all day. Any quick suggestions you can give?
     
  6. Offline

    nighteyes604

    The SimpleCreativeExecutor.java:34 tells you which file and which line had the error. It seems like it's this line:

    Code:
     player.sendMessage(ChatColor.GREEN + "Creative mode enabled for " + target);
    It's because if args.length=1 and the console is sending the command, you don't actually set a player. You call them target and server.

    A fix that might work would be to change your Player target = server.getPlayer(args[0]); to just be player = server.getPlayer(args[0]);, and if that's null tell the console that player wasn't found or something.
     
  7. Offline

    Drat333

    Wow, that's just really dumb of me not to see that. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page