Solved No more need for help.

Discussion in 'Plugin Development' started by chrisman0091, Nov 9, 2013.

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

    chrisman0091

    Basically I have my command which works fine, then I have the same command but with an argument which does nothing. No return, error, or anything. Here is my current command code:
    Code:java
    1. Player player = (Player) sender;
    2. if(commandLabel.equalsIgnoreCase("colors") & player.hasPermission("bcolors.use")){
    3. sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Colors]");
    4. sender.sendMessage(ChatColor.BLACK + "&0 " + ChatColor.DARK_BLUE + " &1 " + ChatColor.DARK_GREEN + " &2 " + ChatColor.DARK_AQUA + " &3");
    5. sender.sendMessage(ChatColor.DARK_RED + "&4 " + ChatColor.DARK_PURPLE + " &5 " + ChatColor.GOLD + " &6 " + ChatColor.GRAY + " &7");
    6. sender.sendMessage(ChatColor.DARK_GRAY + "&8 " + ChatColor.BLUE + " &9 " + ChatColor.GREEN + " &a " + ChatColor.AQUA + " &b");
    7. sender.sendMessage(ChatColor.RED + "&c " + ChatColor.LIGHT_PURPLE + " &d " + ChatColor.YELLOW + " &e " + ChatColor.WHITE + " &f");
    8. /*
    9. *separation
    10. */
    11. sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Formatting]");
    12. sender.sendMessage(ChatColor.RESET + "&k " + ChatColor.MAGIC + "Magic");
    13. sender.sendMessage("&r Reset");
    14. sender.sendMessage(ChatColor.BOLD + "&l " + ChatColor.RESET + ChatColor.STRIKETHROUGH + " &m" + ChatColor.RESET);
    15. sender.sendMessage(ChatColor.UNDERLINE + "&n " + ChatColor.RESET + ChatColor.ITALIC + " &o " + ChatColor.RESET);
    16. return true;
    17. }
    18.  
    19. else if(args.length > 0){
    20. if(args[0].equalsIgnoreCase("red") & player.hasPermission("bcolors.use")){
    21. sender.sendMessage("test");
    22. }
    23. return true;
    24. }


    Everything works besides /color red, which is what is doing nothing, not even in console besides it telling me that I issued the command. What exactly am I doing wrong? This is my first time working with arguments.
     
  2. Offline

    1Rogue

    Don't use an else on the if check, just have the if statement within the block that checks for the command:

    Code:java
    1. if ("colors".equalsIgnoreCase(cmd.getName()) {
    2. if (args.length > 0) {
    3. //work with args[0]
    4. } else {
    5. //no args
    6. }
    7. }
     
  3. Offline

    AKMiner98

     
  4. Offline

    chrisman0091

    Code:java
    1. if(commandLabel.equalsIgnoreCase("colors") & player.hasPermission("bcolors.use")){
    2. if(args.length > 0){
    3. if(args[0].equalsIgnoreCase("red") & player.hasPermission("bcolors.use")){
    4. sender.sendMessage("test");
    5. }
    6. return true;
    7. }
    8. else{
    9. sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Colors]");
    10. sender.sendMessage(ChatColor.BLACK + "&0 " + ChatColor.DARK_BLUE + " &1 " + ChatColor.DARK_GREEN + " &2 " + ChatColor.DARK_AQUA + " &3");
    11. sender.sendMessage(ChatColor.DARK_RED + "&4 " + ChatColor.DARK_PURPLE + " &5 " + ChatColor.GOLD + " &6 " + ChatColor.GRAY + " &7");
    12. sender.sendMessage(ChatColor.DARK_GRAY + "&8 " + ChatColor.BLUE + " &9 " + ChatColor.GREEN + " &a " + ChatColor.AQUA + " &b");
    13. sender.sendMessage(ChatColor.RED + "&c " + ChatColor.LIGHT_PURPLE + " &d " + ChatColor.YELLOW + " &e " + ChatColor.WHITE + " &f");
    14. /*
    15.   *separation
    16.   */
    17. sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Formatting]");
    18. sender.sendMessage(ChatColor.RESET + "&k " + ChatColor.MAGIC + "Magic");
    19. sender.sendMessage("&r Reset");
    20. sender.sendMessage(ChatColor.BOLD + "&l " + ChatColor.RESET + ChatColor.STRIKETHROUGH + " &m" + ChatColor.RESET);
    21. sender.sendMessage(ChatColor.UNDERLINE + "&n " + ChatColor.RESET + ChatColor.ITALIC + " &o " + ChatColor.RESET);
    22. }
    23. }
    24.  
    25. return true;


    So I'm using this code right now, and I still get nothing.

    Fixed that, didn't notice it till now, but it didn't seem to effect anything when changed, so that wasn't the issue.

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

    AKMiner98

    I added in the Override (if you don't already have that in the code) and fixed some &s you were still missing

    Code:
    @Override
    if(commandLabel.equalsIgnoreCase("colors") && player.hasPermission("bcolors.use")){
                if(args.length > 0){
                    if(args[0].equalsIgnoreCase("red") && player.hasPermission("bcolors.use")){
                        sender.sendMessage("test");
                    }
                    return true;
                }
                else{
                    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Colors]");
                    sender.sendMessage(ChatColor.BLACK + "&0 " + ChatColor.DARK_BLUE + " &1 " + ChatColor.DARK_GREEN + " &2 " + ChatColor.DARK_AQUA + " &3");
                    sender.sendMessage(ChatColor.DARK_RED + "&4 " + ChatColor.DARK_PURPLE + " &5 " + ChatColor.GOLD + " &6 " + ChatColor.GRAY + " &7");
                    sender.sendMessage(ChatColor.DARK_GRAY + "&8 " + ChatColor.BLUE + " &9 " + ChatColor.GREEN + " &a " + ChatColor.AQUA + " &b");
                    sender.sendMessage(ChatColor.RED + "&c " + ChatColor.LIGHT_PURPLE + " &d " + ChatColor.YELLOW + " &e " + ChatColor.WHITE + " &f");
                    /*
                    *separation
                    */
                    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Formatting]");
                    sender.sendMessage(ChatColor.RESET + "&k " + ChatColor.MAGIC + "Magic");
                    sender.sendMessage("&r Reset");
                    sender.sendMessage(ChatColor.BOLD + "&l " + ChatColor.RESET + ChatColor.STRIKETHROUGH + " &m" + ChatColor.RESET);
                    sender.sendMessage(ChatColor.UNDERLINE + "&n " + ChatColor.RESET + ChatColor.ITALIC + " &o " + ChatColor.RESET);
                }
            }
     
            return true;
     
  6. Offline

    chrisman0091

    Already added the &'s, and the @Override gives me an Enum error.

    Syntax error, insert "EnumBody" to complete BlockStatement
    Syntax error, insert "enum Identifier" to complete EnumHeaderNameBColors.java
     
  7. Offline

    AKMiner98


    I fixed it
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        Player player = (Player) sender;
        if(label.equalsIgnoreCase("colors") && player.hasPermission("bcolors.use")){
                    if(args.length > 0){
                        if(args[0].equalsIgnoreCase("red") && player.hasPermission("bcolors.use")){
                            sender.sendMessage("test");
                        }
                        return true;
                    }
                    else{
                        sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Colors]");
                        sender.sendMessage(ChatColor.BLACK + "&0 " + ChatColor.DARK_BLUE + " &1 " + ChatColor.DARK_GREEN + " &2 " + ChatColor.DARK_AQUA + " &3");
                        sender.sendMessage(ChatColor.DARK_RED + "&4 " + ChatColor.DARK_PURPLE + " &5 " + ChatColor.GOLD + " &6 " + ChatColor.GRAY + " &7");
                        sender.sendMessage(ChatColor.DARK_GRAY + "&8 " + ChatColor.BLUE + " &9 " + ChatColor.GREEN + " &a " + ChatColor.AQUA + " &b");
                        sender.sendMessage(ChatColor.RED + "&c " + ChatColor.LIGHT_PURPLE + " &d " + ChatColor.YELLOW + " &e " + ChatColor.WHITE + " &f");
                        /*
                        *separation
                        */
                        sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Formatting]");
                        sender.sendMessage(ChatColor.RESET + "&k " + ChatColor.MAGIC + "Magic");
                        sender.sendMessage("&r Reset");
                        sender.sendMessage(ChatColor.BOLD + "&l " + ChatColor.RESET + ChatColor.STRIKETHROUGH + " &m" + ChatColor.RESET);
                        sender.sendMessage(ChatColor.UNDERLINE + "&n " + ChatColor.RESET + ChatColor.ITALIC + " &o " + ChatColor.RESET);
                    }
                }
                return true;
        }
    }
     
  8. Offline

    chrisman0091

    Got rid of the error(thanks), but /colors red still does nothing.
     
  9. Offline

    GusGold

    chrisman0091
    Also, please check the command against the actual command, not the label. If I add an alias into bukkit.yml, your plugin won't ever know I use it. Replace label with command.getName()
     
  10. Offline

    AKMiner98

    That is looking for the command (label is). It is not looking for a player name.

    Working on it

    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        Player player = (Player) sender;
        if(label.equalsIgnoreCase("colors") && player.hasPermission("bcolors.use")){
                    if(args.length >= 0){
                        if(args[0].equalsIgnoreCase("red") && player.hasPermission("bcolors.use")){
                            sender.sendMessage("test");
                        }
                        return true;
                    }
                    else{
                        sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Colors]");
                        sender.sendMessage(ChatColor.BLACK + "&0 " + ChatColor.DARK_BLUE + " &1 " + ChatColor.DARK_GREEN + " &2 " + ChatColor.DARK_AQUA + " &3");
                        sender.sendMessage(ChatColor.DARK_RED + "&4 " + ChatColor.DARK_PURPLE + " &5 " + ChatColor.GOLD + " &6 " + ChatColor.GRAY + " &7");
                        sender.sendMessage(ChatColor.DARK_GRAY + "&8 " + ChatColor.BLUE + " &9 " + ChatColor.GREEN + " &a " + ChatColor.AQUA + " &b");
                        sender.sendMessage(ChatColor.RED + "&c " + ChatColor.LIGHT_PURPLE + " &d " + ChatColor.YELLOW + " &e " + ChatColor.WHITE + " &f");
                        /*
                        *separation
                        */
                        sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Formatting]");
                        sender.sendMessage(ChatColor.RESET + "&k " + ChatColor.MAGIC + "Magic");
                        sender.sendMessage("&r Reset");
                        sender.sendMessage(ChatColor.BOLD + "&l " + ChatColor.RESET + ChatColor.STRIKETHROUGH + " &m" + ChatColor.RESET);
                        sender.sendMessage(ChatColor.UNDERLINE + "&n " + ChatColor.RESET + ChatColor.ITALIC + " &o " + ChatColor.RESET);
                    }
                }
                return true;
        }
    }
    You were missing a = sign. Try now :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  11. Offline

    chrisman0091

    I actually knew that equal wasn't there as I wasn't checking also if equal to, but I added it anyway and nothing changed):
     
  12. Offline

    GusGold

    What? when you check label, it could be any string that is defined in alias, when you check command, it is only the commands defined in your plugin.yml i.e. a known string.
     
  13. Offline

    AKMiner98

    This is what I have in my Class:
    Code:
    package Coding;
     
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class Main extends JavaPlugin implements Listener{
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        Player player = (Player) sender;
        if(label.equalsIgnoreCase("colors") && player.hasPermission("bcolors.use")){
                    if(args.length == 1){
                        if(args[0].equalsIgnoreCase("red") && player.hasPermission("bcolors.use")){
                            sender.sendMessage("test");
                        }
                        return true;
                    }
                    else{
                        sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Colors]");
                        sender.sendMessage(ChatColor.BLACK + "&0 " + ChatColor.DARK_BLUE + " &1 " + ChatColor.DARK_GREEN + " &2 " + ChatColor.DARK_AQUA + " &3");
                        sender.sendMessage(ChatColor.DARK_RED + "&4 " + ChatColor.DARK_PURPLE + " &5 " + ChatColor.GOLD + " &6 " + ChatColor.GRAY + " &7");
                        sender.sendMessage(ChatColor.DARK_GRAY + "&8 " + ChatColor.BLUE + " &9 " + ChatColor.GREEN + " &a " + ChatColor.AQUA + " &b");
                        sender.sendMessage(ChatColor.RED + "&c " + ChatColor.LIGHT_PURPLE + " &d " + ChatColor.YELLOW + " &e " + ChatColor.WHITE + " &f");
                        /*
                        *separation
                        */
                        sender.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC + "[Formatting]");
                        sender.sendMessage(ChatColor.RESET + "&k " + ChatColor.MAGIC + "Magic");
                        sender.sendMessage("&r Reset");
                        sender.sendMessage(ChatColor.BOLD + "&l " + ChatColor.RESET + ChatColor.STRIKETHROUGH + " &m" + ChatColor.RESET);
                        sender.sendMessage(ChatColor.UNDERLINE + "&n " + ChatColor.RESET + ChatColor.ITALIC + " &o " + ChatColor.RESET);
                    }
                }
                return true;
        }
    }
    
    And this in my plugin.yml:
    Code:
    name: Test
    main: Coding.Main
    version: 1.0
    description: >
                Poop On A Stick
    commands:
      colors:
        description: Test
    It all works...

    I believe that this is completely finished. Add this to solved.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  14. Offline

    chrisman0091

    Its still not working for me, whatever, I'll figure it out.
     
  15. Offline

    BillyGalbreath

    Offtopic, but

    Code:
        Player player = (Player) sender;
        if(label.equalsIgnoreCase("colors") && player.hasPermission("bcolors.use")){
    
    needs to be

    Code:
        if(label.equalsIgnoreCase("colors") && sender.hasPermission("bcolors.use")){
    
    1) You cant cast a CommandSender to a Player without first checking if it [em]is[/em] a Player.

    2) CommandSender implements Permissible, so you dont need to have a Player at all in this use-case.
     
Thread Status:
Not open for further replies.

Share This Page