how to double items?

Discussion in 'Plugin Development' started by Mr_maderator_UY, Mar 11, 2022.

  1. Offline

    CraftCreeper6

    @Mr_maderator_UY
    For loops are an integral part of programming, I suggest you become a little more acquainted with them ;)

    switch works I guess.
     
    Mr_maderator_UY likes this.
  2. Offline

    Mr_maderator_UY

    Is this code like:
    Code:
    ItemStack item = new ItemStack(blockBroken.getType(), 2);
    
    @CraftCreeper6
     
    Last edited: Mar 29, 2022
  3. Offline

    CraftCreeper6

  4. Offline

    Mr_maderator_UY

    @CraftCreeper6, sorry, I didn't explain.

    this code:
    Code:
     
                             ItemStack item = new ItemStack(blockBroken.getType(), 2);
    Is the same like you for loop?
     
  5. Offline

    CraftCreeper6

    @Mr_maderator_UY
    No that's not a for loop.

    A for loop looks like this:
    Code:
    for (Item item : itemList)
    {
        // code to execute for each item in itemList
    }
    If you wanted to apply that to your situation your itemList will be block.getDrops(yourPickaxeItemStack)
    Item will be replaced with ItemStack
    And for each item in the list, you'll create a new ItemStack, just like you usually do:
    Code:
    ItemStack item = new ItemStack(blockBroken.getType(), 2);
    But instead of having 2 as the amount you would use item.amount * 2
     
  6. Offline

    Mr_maderator_UY

    @CraftCreeper6, how i remember, for loop seems like that:
    Code:
    for (initialization; condition; increment/decrement) {
       statement(s)
    }
    So, i didn't understand, why you in

    put " :"
    Can you show an example?
    Edit:can i just give a sick touch for pickaxe and do not anything?
     
    Last edited: Mar 30, 2022
  7. Offline

    CraftCreeper6

    That is just one type of loop. You don't always have to follow that format.

    Using a foreach loop, which is what I have been describing above is easier to understand for sure.

    The code I posted above is a pretty good example of how to use a foreach loop in Java.

    If you wanted to keep the code as is then yeah, just add a silk touch enchantment to the pickaxe.
     
  8. Offline

    Mr_maderator_UY

    @CraftCreeper6, silk touch didn't work. I find a lot of information of for each loops
    It like that, but i don't know how can i do this
    Code:
    for (type var : array)
    {
        statements using var;
    }
    but i don't know how can i do this. Can you show in one, item what i need to do?
     
  9. Offline

    CraftCreeper6

    @Mr_maderator_UY
    That for loop is just a description of what kinds of information you need to put where. Lets go through it
    Code:
    for (type var : array)
    {
        statements using var;
    }
    The first vital piece of information is 'for', this just dictates that this will be a for loop. You do not have to change this.

    The next piece is 'type'. This is the data type of each element in your 'array' value. For you it will be ItemStack.

    Then we have 'var', this is just a name for the ItemStack in the current iteration, its not important what you name it but stick to some kind of naming convention.

    And lastly we have 'array'. This is a collection of ItemStacks that you can get with block.getDrops() like we discussed previously.

    Put it all together and you get a for loop that iterates over block.getDrops() where each element in the list is an ItemStack.
     
    Mr_maderator_UY likes this.

Share This Page