Multiple commands isn't working how they suposed to.

Discussion in 'Plugin Development' started by FightManiac, Jan 26, 2014.

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

    FightManiac

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if(sender instanceof Player) {
    3. Player player = (Player) sender;
    4.  
    5. if(cmd.getName().equalsIgnoreCase("ungold")); {
    6.  
    7. Inventory playerInventory = player.getInventory();
    8.  
    9.  
    10. if(playerInventory.contains(Material.GOLD_BLOCK)){
    11.  
    12. playerInventory.removeItem(new ItemStack[] { new ItemStack(Material.GOLD_BLOCK) });
    13. playerInventory.addItem(new ItemStack[] { new ItemStack(Material.GOLD_INGOT, 9) });
    14. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "UncraftItems" + ChatColor.GRAY + "]" + " Your gold block has been uncrafted !");
    15.  
    16. return true;
    17.  
    18.  
    19.  
    20. } else if(cmd.getName().equalsIgnoreCase("uniron"));
    21.  
    22. Player player1 = (Player) sender;
    23. Inventory playerInventory1 = player.getInventory();
    24.  
    25.  
    26. if(playerInventory1.contains(Material.IRON_BLOCK)){
    27.  
    28. playerInventory1.removeItem(new ItemStack[] { new ItemStack(Material.IRON_BLOCK) });
    29. playerInventory1.addItem(new ItemStack[] { new ItemStack(Material.IRON_INGOT, 9) });
    30. player1.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "UncraftItems" + ChatColor.GRAY + "]" + " Your iron block has been uncrafted !");
    31. return true;
    32.  
    33.  
    34. } else if(cmd.getName().equalsIgnoreCase("unemerald"));
    35. Player player2 = (Player) sender;
    36.  
    37.  
    38. player2.sendMessage("f");
    39. return true;
    40. }
    41. }
    42.  
    43. return false;
    44. }
    45. }


    The multiple commands works thank to great helpers off this forum ,but there is a problem..
    If i write /ungold and if i would have have gold and iron it would replace both of them but it should replace only gold. The problem exists in every command what did i did wrong?



    And whats the diference from:
    " if(cmd.getName().equalsIgnoreCase("MYCOMMAND")); "
    and this ?
    " if (commandLabel.equalsIgnoreCase("MYCOMMAND")) { "
     
  2. Offline

    Th3Br1x

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
    2.  
    3. if(cmd.getName().equalsIgnoreCase("cmd1"){
    4. //Do something
    5. return true;
    6. }
    7.  
    8. if(cmd.getName().equalsIgnoreCase("cmd2"){
    9. //Do something
    10. return true;
    11. }
    12.  
    13. }
    14.  
    15. return false;
    16. }


    This should be what you're looking for.
    I would recommend using seperate classes for each command (using CommandExecutor, explained here: http://wiki.bukkit.org/Plugin_Tutorial#Using_a_separate_CommandExecutor_class)
     
  3. Offline

    FightManiac

    Thanks but can you explain me what is the diference
    " if(cmd.getName().equalsIgnoreCase("MYCOMMAND")); "
    from this?
    " if (commandLabel.equalsIgnoreCase("MYCOMMAND")) {
     
  4. Offline

    DeGambler

    FightManiac
    Command Label doesn't check aliases in comparison to Command Name, so you'd have to check for each alias individually if you used Label.
     
  5. Offline

    felixfritz

    FightManiac
    I believe that cmd.getName() is the name of the command stated in the plugin.yml file. So if it's
    Code:
    commands:
      myCommand:
    , this will return "myCommand", even if the player typed in "MYCOMMAND".

    The string commandLabel however will give you the command 1:1 to what the player typed in. So, if he typed "MYCOMMAND", you'd get "MYCOMMAND" back.

    (this has not been tested, it's just my assumption which seems logical to me)
     
  6. Offline

    Th3Br1x

    No, thats why you put the string into a "equalsIgnoreCase()". This function compares to strings case-insensitive.
    Sorry to 'break' your logical assumption :D
     
  7. Offline

    FightManiac

    Okay now i can add multiple commands (only sendmessage commands) .something is not working i want 2 commands do almoust exactly what it does I want another command "test2 " do almoust exactly what "test" does
    How can i add this command can anyone give me the full code?

    Code:java
    1.  
    2.  
    3. if(cmd.getName().equalsIgnoreCase("test2")); {
    4. Inventory playerInventory = player.getInventory();
    5.  
    6.  
    7. if(playerInventory.contains(Material.IRON_BLOCK)){
    8.  
    9. playerInventory.removeItem(new ItemStack[] { new ItemStack(Material.IRON_BLOCK) });
    10. playerInventory.addItem(new ItemStack[] { new ItemStack(Material.IRON_INGOT, 9) });
    11. player.sendMessage( "TEST2 MESSAGE");
     
  8. Offline

    Gater12

    FightManiac Why. Have you put a semi colon. In front of your if statement? It's not. Gonna go far...
     
    Wizehh and Jake6177 like this.
  9. Offline

    Th3Br1x

    FightManiac
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("test2")); {

    That doesn't work. Maybe you should learn a bit more about Java and then try do develop Bukkit plugins.
    I can garantuee, it helps. Just search on YouTube or Google "basic java tutorial" and you should be fine.
     
  10. Offline

    DeGambler

    Gater12 I see whatcha did there...
     
    Jake6177 and Gater12 like this.
  11. Offline

    FightManiac

    I know everyone told me i need to start learning Java first i will do that but atleast i want to know how to do multiple commands ..
    Th3Br1x Can you please say me why it will not work and what shall i place?
     
  12. Offline

    Th3Br1x

    FightManiac I already posted it above, but here you go (again):

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
    2.  
    3. if(cmd.getName().equalsIgnoreCase("cmd1"){
    4. //Do something
    5. return true;
    6. }
    7.  
    8. if(cmd.getName().equalsIgnoreCase("cmd2"){
    9. //Do something
    10. return true;
    11. }
    12.  
    13. }
    14.  
    15. return false;
    16. }
     
  13. Offline

    FightManiac

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if(sender instanceof Player) {
    3. Player player = (Player) sender;
    4.  
    5. if(cmd.getName().equalsIgnoreCase("ungold")); {
    6.  
    7. Inventory playerInventory = player.getInventory();
    8.  
    9.  
    10. if(playerInventory.contains(Material.GOLD_BLOCK)){
    11.  
    12. playerInventory.removeItem(new ItemStack[] { new ItemStack(Material.GOLD_BLOCK) });
    13. playerInventory.addItem(new ItemStack[] { new ItemStack(Material.GOLD_INGOT, 9) });
    14. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "UncraftItems" + ChatColor.GRAY + "]" + " Your gold block has been uncrafted !");
    15.  
    16. return true;
    17.  
    18.  
    19.  
    20. } else if(cmd.getName().equalsIgnoreCase("uniron"));
    21.  
    22. Player player1 = (Player) sender;
    23. Inventory playerInventory1 = player.getInventory();
    24.  
    25.  
    26. if(playerInventory1.contains(Material.IRON_BLOCK)){
    27.  
    28. playerInventory1.removeItem(new ItemStack[] { new ItemStack(Material.IRON_BLOCK) });
    29. playerInventory1.addItem(new ItemStack[] { new ItemStack(Material.IRON_INGOT, 9) });
    30. player1.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "UncraftItems" + ChatColor.GRAY + "]" + " Your iron block has been uncrafted !");
    31. return true;
    32.  
    33.  
    34. } else if(cmd.getName().equalsIgnoreCase("unemerald"));
    35. Player player2 = (Player) sender;
    36.  
    37.  
    38. player2.sendMessage("f");
    39. return true;
    40. }
    41. }
    42.  
    43. return false;
    44. }
    45. }


    The multiple commands works thank to great helpers off this forum ,but there is a problem..
    If i write /ungold and if i would have have gold and iron it would replace both of them but it should replace only gold. The problem exists in every command what did i did wrong?

    BUMP

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

    Jake6177


    Dude..learn to Java.

    Try iterating the inventory instead of deleting an anonymous array of gold blocks and giving an anonymous array of gold ingots..
     
  15. Offline

    Sagacious_Zed Bukkit Docs

    If you have a command "hello" and you want it such that if a player types the command "good day" to have the exact same behavior as "hello" you should declare "goodday" as an alias of "hello" in your plugin.yml. Bukkit will handle the rest for you, and all you have to do is check if the command name is "hello"

    As to the current problem you are having, it probably has something to do with the way if and else statements are nested. In fact, right now you check if it is ungold. Inside ungold you check if it is unemerald , which can never happen.
     
  16. Offline

    FightManiac

    You could tell me how to do this after i make this plugin i surely will be learning Java programing not Bukkit

    Sagacious_Zed My biggest mistake is was learning Bukkit programing first than Java im a noob now after this il start Java but can you tell me where is my mistake because i don't see any..

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

    Jake6177


    Your if else statement. You check for "ungold" then you check if they have a gold block then you check for the next command. Add a "}" to tell Java you're finished with the "ungold" command.
     
  18. Offline

    FightManiac

    Jake6177 Ughh can you please correct my code im strugling with it these 3 days ..
     
  19. Offline

    Sagacious_Zed Bukkit Docs

    All IDEs that I know of can highlight the matching curly brace. Some will highlight all the code that are between two braces to help you identify what are between brances.
     
  20. Offline

    FightManiac

  21. Offline

    Sagacious_Zed Bukkit Docs

    The fix is moving one } to the right place.
     
  22. Offline

    FightManiac

    Sagacious_Zed It's kinda akward to say but can you add me on skype: eriukas71 I need help..

    Still need help..

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

    MOMOTHEREAL

    Wizehh, Jake6177 and DeGambler like this.
  24. Offline

    Monkey_Swag

    if(cmd.getName().equals("cmd1")) { //<-- opening bracket. Notice how There's no semicolon
    //Do the ungold thing
    } <-- closing bracket

    That's basically how you're supposed to do it. In Java, when you put an opening bracket "{" you will always need a closing bracket "}"
    FightManiac

    EDIT: One more thing, don't ask for direct code that'll fix your plugin. We don't do that around here. Try to read our help messages and figure out how to fix your plugin. Now go learn some Java, trust me, it'll really help you, and please do it before you finish this plugin. Thank you!
     
  25. Offline

    DeGambler

    What Im wondering is what he's using to write his code...I mean what IDE doesn't correct a bracket mistake? oO
     
  26. Offline

    Sagacious_Zed Bukkit Docs

    All of them. The mistake here is not in syntax. The mistake here is in logic, which no IDE could ever determine with the limited context of the given source.
     
  27. Offline

    Wizehh

    I see what you did there :p
     
    Gater12 likes this.
  28. Offline

    FightManiac

    Sorry i watched on my code i had brackets there but it still not working how it should be if i write /ungold or /uniron and i will not have gold or iron i will get the message "f" and if i have both iron and gold it will replace with any command i type..
    Here is the code:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
        Player player = (Player) sender;
        if(cmd.getName().equalsIgnoreCase("ungold")); { 
            Inventory playerInventory = player.getInventory();
       
        if(playerInventory.contains(Material.GOLD_BLOCK)){
       
       
            playerInventory.removeItem(new ItemStack[] { new ItemStack(Material.GOLD_BLOCK) });
            playerInventory.addItem(new ItemStack[] { new ItemStack(Material.GOLD_INGOT, 9) });
            player.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "UncraftItems" + ChatColor.GRAY + "]" + " Your gold block has been uncrafted !");//
           
            {
           
            }
       
       
    return true;
     
     
       
     
       
       
        } else if(cmd.getName().equalsIgnoreCase("uniron"));
       
        Player player1 = (Player) sender;
    Inventory playerInventory1 = player.getInventory();
       
     
        if(playerInventory1.contains(Material.IRON_BLOCK)){
       
            playerInventory1.removeItem(new ItemStack[] { new ItemStack(Material.IRON_BLOCK) });
            playerInventory1.addItem(new ItemStack[] { new ItemStack(Material.IRON_INGOT, 9) });
            player1.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "UncraftItems" + ChatColor.GRAY + "]" + " Your iron block has been uncrafted !");//
            {
               
            }
     
        return true;
       
       
        } else if(cmd.getName().equalsIgnoreCase("unemerald"));
        Player player2 = (Player) sender;
     
         
        player2.sendMessage("f");//
        {
           
        }
        return true;
        }
            }
       
        return false;
        }
    }
    I think i wont get help..

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

    Monkey_Swag

    READ WHAT WE ARE TELLING YOU!!!!!!
    REMOVE YOUR SEMICOLONS THAT YOU DONT NEED!!!!
     
  30. Offline

    FightManiac

    I don't really know what "SEMICOLONS" Are..
    Im not the best at harder english words

    Monkey_Swag (Sorry for the double post )

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

Share This Page