Repeating code

Discussion in 'Plugin Development' started by HazePvPHD, May 1, 2015.

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

    HazePvPHD

    So I am currently working on a plugin and I'm currently stuck. I need to find a way to repeat some code specified for the amount of times in args[1]. What I mean by this is say I have this:
    Code:
    if(cmd.getName().equalsIgnoreCase("block")) {
                if (p.hasPermission("ac.block")) {
                       if (p.getInventory().contains(Material.DIAMOND, 9)) {
                                         p.getInventory().removeItem(new ItemStack[] { new ItemStack(Material.DIAMOND, 9) });
                                         p.getInventory().addItem(new ItemStack[] { new ItemStack(Material.DIAMOND_BLOCK) });
                                         p.updateInventory();
                                        
                                         plugin.msg(p, "&aSuccessfully recrafted your ores!");
                                    } else {
                                        plugin.msg(p, "&cYou don't have enough materials!!");
                                    }
                             }
                      }
                }
            }
    This is not the exact use of the code I'm looking at but it is 99.99% similar and I didn't want to post the real code because I've heard of times when some people make other peoples ideas before they can, but anyway so say that was the code (I know I can just replace if with while and it will do it until there is no more left in the inventory) and someone did /block 9. How would I make it get that 9 and repeat the task 9 times? Please help, with examples. Thanks :)
     
  2. Offline

    BlazingBroGamer

    @HazePvPHD
    To get an integer from a string, you would probably want to use Integer.parseInt(String), and just use a for loop for repeating. A simple for loop:

    Code:
    for(int i = 0;i < 9;i++){
        //Code here
    }
    The i = 0 is what to start of with, i < 9 is it will repeat the code if it is less than 9, and i++ will keep adding the code until i is not < 9
     
  3. Offline

    HazePvPHD

    @BlazingBroGamer Ok but how would I make it repeat for as many times that are in args[0] ?

    @BlazingBroGamer and also not to be a pain in the ass or something but for example you could need more then 1 item to turn it into blocks (In what I am using this for you could even need up to 5 items for one)

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: May 1, 2015
  4. Offline

    1Rogue

  5. Code:
    int i = Integer.parseInt(args[0]);
     
  6. Offline

    HazePvPHD

    It does give a lot on the for statement, but I'm not sure how I would use it with multiple items.. Sorry if I am being very dumb tho, I learnt java about 1 year ago and recently started working full time so I forgot a lot :/ If you could post an example that would be amazing, thanks. :)

    (Using the args[0] thing and many items at once please :()

    I already had that from a page that I was looking at before. But how would that work because whenever I try use it it will give me an error telling me to rename it.

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: May 1, 2015
  7. Offline

    1Rogue

    That page has examples and demonstrations on exactly what you just asked
     
  8. Offline

    meguy26

    @HazePvPHD
    Like this:
    Code:
    int a = Integer.parseInt(args[0]); //Convert the string args[0] to the int a.
    for(int i = 0 ; i  < a; i++){
    //declare variable i, and while i is less than a, add one to i and do the following:
        p.getInventory().removeItem(new ItemStack[] { new ItemStack(Material.DIAMOND) }); //remove an itemstack from the player.
        p.updateInventory(); //update their inventory
    
    //do anything you want, replace the above code or do more stuff afterward or in between.
    }
    @1Rogue, sorry for spoonfeeding, I desperately hope that @HazePvPHD tries to learn the code I provided, and re-learn java as well.
    :)
     
  9. Offline

    HazePvPHD

    Ok so I managed to remember how to do it last night but then pretty much fell asleep, but now I'm stuck with another problem :/

    Is there a way to make it detect all names for an item, eg. Say you wanted to do a /give command, instead of doing:
    Code:
    if(args[0].equalsIgnoreCase("Bookshelf") || args[0].equalsIgnoreCase("Bookcase")
                                || args[0].equalsIgnoreCase("47") || args[0].equalsIgnoreCase("minecraft:bookshelf")) {
    is there an easier way to do it? I was thinking to do maybe:
    Code:
    if(args[0].equals(material.BOOKSHELF) {
    but that wouldn't work because you are trying to get a material from a command, so any suggestions are appreciated.

    PS. Yes, I have been told to learn java. Yes, I have learnt java before. No, I do not have time to re-learn it. I'm just coding off what I can remember and asking everyone else for help on what I cannot remember.
     
  10. Offline

    teej107

    You can never finish learning Java. There are so many things to learn, so many libraries to use and plenty of techniques to try. If you don't have time to learn Java, then you don't have time to learn how to write Bukkit plugins because that is what it takes in order to make them.

    There are methods in the Material enum to help you achieve what you want. I suggest you read the JavaDocs for Material.
     
    Konato_K likes this.
  11. Offline

    mythbusterma

    @HazePvPHD

    No, there is no way to do that other than to think of all the possible valid names for something. Why would there be another way?
     
Thread Status:
Not open for further replies.

Share This Page