Adding an extra command

Discussion in 'Plugin Development' started by ArthurHoeke, Jul 20, 2014.

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

    ArthurHoeke

    Hello,
    Im making a plugin and I just made a command.
    But now I want to add an extra command and I tried to put the if (cmd.getName().equalsIgnoreCase("givebadge")) under the code put it didn't work.
    This is my code:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("badges"))
                    if (!(sender instanceof Player)){
                        sender.sendMessage(ChatColor.RED + "You need to be a player to do this command!");
                    }
                    else {
                        final Player p = (Player)sender;
                        final Inventory myInventory = Bukkit.createInventory(null, 9, ChatColor.RED + p.getName() + "'s Badges");
                            // The first parameter, is the inventory owner. I make it null to let everyone use it.
                            //The second parameter, is the slots in a inventory. Must be a multiple of 9. Can be up to 54.
                            //The third parameter, is the inventory name. This will accept chat colors
                        ItemStack dblock2 = new ItemStack(Material.STAINED_GLASS_PANE);
                            ItemMeta dblockmeta2 = dblock2.getItemMeta();
                            dblockmeta2.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Not Achieved");
                            dblock2.setItemMeta(dblockmeta2);
                            myInventory.setItem(8, dblock2);
                        ItemStack dblock = new ItemStack(Material.REDSTONE_BLOCK);
                            ItemMeta dblockmeta = dblock.getItemMeta();
                            dblockmeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Close");
                            dblock.setItemMeta(dblockmeta);
                            myInventory.setItem(8, dblock);
                                if (Rock.containsKey(p)) {
                                    myInventory.setItem(0, new ItemStack(4182, 1));
                                }else{
                                    myInventory.setItem(0, dblock2);
                                    if (Water.containsKey(p)) {
                                        myInventory.setItem(1, new ItemStack(4183, 1));
                                    }else{
                                        myInventory.setItem(1, dblock2);
                                        if (Electric.containsKey(p)) {
                                            myInventory.setItem(2, new ItemStack(4209, 1));
                                        }else{
                                            myInventory.setItem(2, dblock2);
                                            if (Grass.containsKey(p)) {
                                                myInventory.setItem(3, new ItemStack(4203, 1));
                                            }else{
                                                myInventory.setItem(3, dblock2);
                                                if (Psychic.containsKey(p)) {
                                                    myInventory.setItem(4, new ItemStack(4197, 1));
                                                }else{
                                                    myInventory.setItem(4, dblock2);
                                                    if (Poison.containsKey(p)) {
                                                        myInventory.setItem(5, new ItemStack(4206, 1));
                                                    }else{
                                                        myInventory.setItem(5,  dblock2);
                                                        if (Fire.containsKey(p)) {
                                                            myInventory.setItem(6, new ItemStack(4210, 1));
                                                        }else{
                                                            myInventory.setItem(6,  dblock2);
                                                            if (Ground.containsKey(p)) {
                                                                myInventory.setItem(7, new ItemStack(4187, 1));
                                                            }else{
                                                                myInventory.setItem(7,  dblock2);
                                                                p.openInventory(myInventory);
                                                            }
                                                        }
                                                    }
                                                }
                                               
                                            }
                                        }
                                    }
                                }
                    }
            return false;
     
  2. Offline

    Zettelkasten

    You need to register the command for that listener and in your plugin.yml.
     
  3. Offline

    ArthurHoeke

    Zettelkasten How? I know with the plugin.yml but how to put an extra command in that commadn code?
     
  4. Offline

    Neymar11

    ArthurHoeke For your plugin.yml:
    commands:
    givebadge:
    description:
    permissions:
    usage:

    Ow and use Java language when posting code
     
  5. Offline

    ArthurHoeke

    Neymar11 I know how the plugin.yml works!
    I only need a example in my code..

    EDIT: I found the solution for this, But now is my other question:
    How to make that you can have a command behind a command
    so like this:
    /givebadge playername
     
  6. Offline

    Neymar11

    ArthurHoeke
    Here it is:
    Code:java
    1. if (label.equalsIgnoreCase("givebadge")){
    2. if(args.length==0) {
    3. //here what happens IF he only types :/givebadge
    4. } else {
    5. if (args[0].equals("playername")){
    6. //and here IF he adds the second word
    7. //In your case thats playername
    8. }


    If you need anything else just Tahg me so I can find it easier!
    Ow and mark the thread with "Solved" if you do not have any more questions!
     
  7. Offline

    Zettelkasten

    ArthurHoeke Neymar11 He prolery does not want to check if the first argument is "playername", but if it is a player or get the player object from it:
    Code:java
    1. if (args.length == 0) // Handle Input error here
    2. String playerName = args[0];
    3. Player player = Bukkit.getPlayer(playerName);
    4. if (player == null) // Handle Input error here
    5. // Do stuff with the player
     
  8. Offline

    ArthurHoeke

    Neymar11 Zettelkasten I want that if a player types /givebadge playername rock the player that is difined will get an item. But how to make that I get the name of the player in playername?
     
  9. Offline

    Neymar11

    Zettelkasten Yea maybe he does not. But most plugins have like a help page if there is no second argument, like if the user types only the main command /givebadge then it could display a help page..
    That is also what I mostly do :D

    ArthurHoeke You could add this:

    Code:java
    1. String name = sender.getName();


    So let me explain this: the name from the one who issued the command (sender) will be saved in the string name

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

    FabeGabeMC

    Neymar11
    What if the sender is console? :p
    Also, he is asking for a target player.
    ArthurHoeke
    Just try getting the player, checking if he is online or if he is null, then give that player an item.
    What I just said in code:
    Code:java
    1. Player target = Bukkit.getPlayer(args[0]);
    2. if(target == null) {
    3. //Saying 'target.getName()' will give out a NullPointerException
    4. //Since we're saying that the player is null or not online.
    5. p.sendMessage("Player " + args[0] +
    6. " is not online!");
    7. } else {
    8. //What you want to do with the player.
    9. }

    Hope it helps.
    ~Gabe
     
  11. Offline

    ArthurHoeke

    FabeGabeMC Thank you, But the only problem is is that I need to get for what sort of gym the player it does
    So I can give the player the good item

    For example I have /givebadge playername gymname

    So it needs to see what sort of gym it is because the rock needs to get a specific item and water an other specific item
     
  12. Offline

    Necrodoom

    FabeGabeMC Check your code before spoonfeeding, it would fire a NPE if a player is not found, and it cant return offline players anyway.
     
  13. Offline

    FabeGabeMC

    ArthurHoeke
    Instead of using maps, use Lists/ArrayLists.
    Then, replace containsKey(); with contains();
    plus, what are you doing with these HashMaps?

    Necrodoom
    I wasn't spoon-feeding. I was just giving an explanation in code from my statement if you read it well. (Some might not understand).
    Also, I was explaining what I was doing so I wouldn't call that spoon-feeding...

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

    Necrodoom

    FabeGabeMC Have you read the whole thing i said..?
     
  15. Offline

    Neymar11

    ArthurHoeke if you want to read the user input on the "gymname" part, then you have to increase the args[0] by 1.
    Example: /givebadge playername gymname
    so givebadge is the main command,
    playername would be args[0]
    and gymname is args[1]
     
  16. Offline

    FabeGabeMC

    Necrodoom Yes, and I disagree. I wasn't returning an offline player. I was saying if the player is not online or the player is null. I don't see any way that I have used OfflinePlayer in my statement.
     
  17. Offline

    Necrodoom

    FabeGabeMC getPlayer will never return an offlineplayer, which means your isOnline check is pointless. Further more, if the target is null, then it will first do the isOnline check, which will NPE.
     
  18. Offline

    FabeGabeMC

    Necrodoom ahh I see what you mean... woopsies. :p
    fixed it :)
     
  19. Offline

    mythbusterma

    And the JRE disagrees with you, and the JRE wins. Like he said above, it's an NPE and pointless anyway, but for what it's worth, if getPlayer(String) could return an OfflinePlayer, reversing the order would utilize Java's short-circuiting behavior to avoid the NPE.
     
  20. Offline

    FabeGabeMC

  21. Offline

    ArthurHoeke

    Neymar11 FabeGabeMC Thanks, But I have my next problem ;s Cant insert new commant, Have this but if I add another command and put in into the plugin.yml and startup mc and test it out it dont works. No error but nothing happens

    Code:
    }else if(cmd.getName().equalsIgnoreCase("givebadge")) {
                        if(args.length == 0) {
                        final Player p = (Player)sender;
                        if(p.hasPermission("impactmcbadges.addbadge")) {
                        p.sendMessage(ChatColor.DARK_RED + "Wrong Arguments. Try /givebadge playername gymtype");
                        }
                        }else if(cmd.getName().equalsIgnoreCase("test")) {
                            if(args.length == 1) {
                            final Player p = (Player)sender;
                            if(p.hasPermission("impactmcbadges.addbadge")) {
                            p.sendMessage(ChatColor.DARK_RED + "Wrong Arguments. Try /givebadge playername gymtype");
     
  22. Offline

    Neymar11

    ArthurHoeke You dont have to instert the arguments in plugin.yml
    Just the main command givebadge

    Code:java
    1. if (label.equalsIgnoreCase("givebadge")){
    2. if(args.length==0) {
    3.  
    4. //handle input error here
    5. }
    6. else if (args[0].equalsIgnoreCase("2ndCommand") && args.length > 1) {
    7.  
    8.  
    9. }
    10.  
    11. if (args[0].equalsIgnoreCase("3rdCommand)) {
    12.  
    13.  
    14. }
    15.  


    The 2nd and third Command are the arguments at args[0]
    Example: /givebadge playername
     
  23. Offline

    FabeGabeMC

    ArthurHoeke
    You are telling the code:
    "Okay, so if the args length is not 0, is the command 'test'?"
    You need to tell the code:
    "Okay, if the command isn't givebadge, is the command 'test'?"
    In other words:
    add a '}' above "}else if(cmd.getName().equalsIgnoreCase("test"){"
    That should fix the current problem.
     
  24. Offline

    ArthurHoeke

    FabeGabeMC Did it, Still doesnt work
    Code:
    }else if(cmd.getName().equalsIgnoreCase("givebadge")) {
                        if(args.length == 0) {
                        final Player p = (Player)sender;
                        if(p.hasPermission("impactmcbadges.addbadge")) {
                        p.sendMessage(ChatColor.DARK_RED + "Wrong Arguments. Try /givebadge playername gymtype");
                        }
                        }
                        }else if(cmd.getName().equalsIgnoreCase("test")) {
                            if(args.length == 1) {
                            final Player p = (Player)sender;
                            if(p.hasPermission("impactmcbadges.addbadge")) {
                            p.sendMessage(ChatColor.DARK_RED + "Wrong Arguments. Try /givebadge playername gymtype");
                            }
                            }
                        }
            return false;
     
  25. Offline

    Neymar11

  26. Offline

    ArthurHoeke

    Neymar11

    Code:
    name: BadgeCase
    main: me.Arthurhoeke.BadgeCase.Main
    version: 1.0
    commands:
      badges:
          description: Opens the badge case
      givebadge:
          description: Give a player a badge
      test:
          description: Give a player a badge
     
  27. Offline

    Neymar11

    ArthurHoeke You can't have all the commands there, only the main command givebadge, or else it does not work....
    Btw is there an error message or it doesnt say anything when you try to use the command?
     
  28. Offline

    ArthurHoeke

    Neymar11 Theres no error Nothing. No message nothing...
     
  29. Offline

    Neymar11

    ArthurHoeke Then remove the other commands.. Leave only the main command , save it, and try again
     
  30. Offline

    ArthurHoeke

    Neymar11 Already did but dont work

    ArthurHoeke I think that the code is wrong or something

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page