Solved Command bug

Discussion in 'Plugin Development' started by trulis, Jan 6, 2013.

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

    trulis

    This is the code for my plugin and i want to ask if can someone help me resolve an error.
    Code:
    package com.manor3x;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class SimpleCommands extends JavaPlugin{
       
        public final Logger logger = Logger.getLogger("Minecraft");
        public static SimpleCommands plugin;
       
       
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Disabled!");
        }
       
        @Override
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
        }
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("sc")){
                if(args.length == 0){
                    player.sendMessage("Simple Commands Plugin v1.0 by trulis");
                    player.sendMessage(ChatColor.AQUA + "1./sc help - shows you the commands of Simple Commands");
                }if(args[0].equalsIgnoreCase("help")){
                    if(player.isOp()){
                        player.sendMessage("Simple Commands Plugin v1.0 by trulis");
                        player.sendMessage(ChatColor.AQUA + "1./sc help - shows you the commands of Simple Commands");
                        player.sendMessage(ChatColor.AQUA + "2./sc heal - fills your hp bar");
                        player.sendMessage(ChatColor.AQUA + "3./sc feed - fills your food bar");
                        player.sendMessage(ChatColor.AQUA + "4./sc spawn - teleports you to your bed");
                    }if(!player.isOp()){
                        player.sendMessage("Simple Commands Plugin v1.0 by trulis");
                        player.sendMessage(ChatColor.AQUA + "1./sc help - shows you the commands of Simple Commands");
                        player.sendMessage(ChatColor.AQUA + "2./sc spawn - teleports you to your bed");
                    }
                }else if(args[0].equalsIgnoreCase("heal")){
                    if(player.isOp()){
                        player.setHealth(20);
                    }if(!player.isOp()){
                        player.sendMessage("You don't have the permission to use that command!");
                    }
                }else if(args[0].equalsIgnoreCase("feed")){
                    if(player.isOp()){
                        player.setFoodLevel(20);
                    }if(!player.isOp()){
                        player.sendMessage("You don't have the permission to use that command!");
                    }
                }
            }
            return false;
        }
    }

    I get this error when i am using the /sc command
    Code:
    2013-01-07 01:21:20 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'sc' in plugin Simple Commands v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
        at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer.java:514)
        at net.minecraft.server.v1_4_6.PlayerConnection.handleCommand(PlayerConnection.java:979)
        at net.minecraft.server.v1_4_6.PlayerConnection.chat(PlayerConnection.java:897)
        at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:852)
        at net.minecraft.server.v1_4_6.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
        at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
        at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
        at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
        at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
        at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at com.manor3x.SimpleCommands.onCommand(SimpleCommands.java:37)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
     
  2. Offline

    CubixCoders

    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    3. Player player = (Player) sender;
    4. if(commandLabel.equalsIgnoreCase("sc")){
    5. if(args.length == 0){ // Im guessing this if args.length == 0
    6. player.sendMessage("Simple Commands Plugin v1.0 by trulis");
    7. player.sendMessage(ChatColor.AQUA + "1./sc help - shows you the commands of Simple Commands");
    8. }if(args[0].equalsIgnoreCase("help")){ //Then this?
    9. if(player.isOp()){
    10. player.sendMessage("Simple Commands Plugin v1.0 by trulis");
    11. player.sendMessage(ChatColor.AQUA + "1./sc help - shows you the commands of Simple Commands");
    12. player.sendMessage(ChatColor.AQUA + "2./sc heal - fills your hp bar");
    13. player.sendMessage(ChatColor.AQUA + "3./sc feed - fills your food bar");
    14. player.sendMessage(ChatColor.AQUA + "4./sc spawn - teleports you to your bed");
    15. }if(!player.isOp()){
    16. player.sendMessage("Simple Commands Plugin v1.0 by trulis");
    17. player.sendMessage(ChatColor.AQUA + "1./sc help - shows you the commands of Simple Commands");
    18. player.sendMessage(ChatColor.AQUA + "2./sc spawn - teleports you to your bed");
    19. }
    20. }else if(args[0].equalsIgnoreCase("heal")){
    21. if(player.isOp()){
    22. player.setHealth(20);
    23. }if(!player.isOp()){
    24. player.sendMessage("You don't have the permission to use that command!");
    25. }
    26. }else if(args[0].equalsIgnoreCase("feed")){
    27. if(player.isOp()){
    28. player.setFoodLevel(20);
    29. }if(!player.isOp()){
    30. player.sendMessage("You don't have the permission to use that command!");
    31. }
    32. }
    33. }
    34. return false;
    35. }
    36.  

    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    3. Player player = (Player) sender;
    4. if(commandLabel.equalsIgnoreCase("sc")){
    5. if(args.length == 0){
    6. player.sendMessage("Simple Commands Plugin v1.0 by trulis");
    7. player.sendMessage(ChatColor.AQUA + "1./sc help - shows you the commands of Simple Commands");
    8. }else if(args.length == 1){
    9. if(args[0].equalsIgnoreCase("help")){
    10. if(player.isOp()){
    11. player.sendMessage("Simple Commands Plugin v1.0 by trulis");
    12. player.sendMessage(ChatColor.AQUA + "1./sc help - shows you the commands of Simple Commands");
    13. player.sendMessage(ChatColor.AQUA + "2./sc heal - fills your hp bar");
    14. player.sendMessage(ChatColor.AQUA + "3./sc feed - fills your food bar");
    15. player.sendMessage(ChatColor.AQUA + "4./sc spawn - teleports you to your bed");
    16. }if(!player.isOp()){
    17. player.sendMessage("Simple Commands Plugin v1.0 by trulis");
    18. player.sendMessage(ChatColor.AQUA + "1./sc help - shows you the commands of Simple Commands");
    19. player.sendMessage(ChatColor.AQUA + "2./sc spawn - teleports you to your bed");
    20. }
    21. }else if(args[0].equalsIgnoreCase("heal")){
    22. if(player.isOp()){
    23. player.setHealth(20);
    24. }if(!player.isOp()){
    25. player.sendMessage("You don't have the permission to use that command!");
    26. }
    27. }else if(args[0].equalsIgnoreCase("feed")){
    28. if(player.isOp()){
    29. player.setFoodLevel(20);
    30. }if(!player.isOp()){
    31. player.sendMessage("You don't have the permission to use that command!");
    32. }
    33. }
    34. }
    35. }
    36. return false;
    37. }
    38.  
     
    trulis likes this.
  3. Offline

    jojohnson1

    I'm not sure if it works or not, but you have only got one return statement. You should try and specify clearly when to return true or false, otherwise the server would send you the usage of the /sc command EVERYTIME this command is executed...

    Oh, and I just realise something. In line 37, you have got
    Code:
    }if(args[0].equalsIgnoreCase("help")){
    . Change into
    Code:
    }else if(args[0].equalsIgnoreCase("help")){
    , otherwise your plugin would work like this:
    Check, if args.length == 0, if yes, send "1./sc help - shows you the commands of Simple Commands". Then, check whether args[0] = heal, ... , ...
    If you insert the "else", the rest will only be checked if arg.length is NOT 0, which it will be if a player types /sc heal.
    I hope i could help you.

    Oh, and have a look at this: http://forums.bukkit.org/threads/ho...ubleshoot-your-own-plugins-by-yourself.32457/
    It explains how to read an errorlog, if you were able to read errors, you wouldn't have to wait for our answers ;-)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 8, 2016
  4. Offline

    trulis

    I am new to developing plugins for bukkit so thank you for your help CubixCoders.
     
  5. Offline

    CubixCoders

    Np :p
     
Thread Status:
Not open for further replies.

Share This Page