Give a player a lot of unstacked items?

Discussion in 'Plugin Development' started by the_cows, Jul 27, 2014.

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

    the_cows

    Hi,

    Basically, I want to give a player 32 soup when they type a command. But how do I make it so it doesn't stack and gives them it seperately?

    Because if I use:
    Code:java
    1. ItemStack soup = new ItemStack(Material.MUSHROOM_SOUP, 33);

    it stacks the items which I don't want, so how can I make it seperate?
     
  2. Offline

    Niknea

    the_cows You'd need to set each slot in their inventory instead of adding the item. The setItem(); method would work for you :).
     
  3. Offline

    the_cows

    Niknea But wouldn't that use 33 lines of code? Surely that would be RAM wasteful?
     
  4. Offline

    Deleted user

    Code:java
    1.  
    2. for (int itemAmount = 0; itemAmount < 10; itemAmount++) {
    3. player.getInventory().addItem(new ItemStack(Material.APPLE));
    4. }
    5.  
     
  5. Offline

    the_cows

    Eballer48
    Huh? xD
    Can you explain it.

    Like, what's the point of itemAmount = 0, itemAmount < 10 etc
     
  6. Offline

    Niknea

    the_cows Yes it would, unless you use a for loop, which is what you should do.
     
  7. Offline

    Iron_Crystal

    It would not be very ram wasteful. You are doing the exact same thing if you use a for loop..

    What a for loop does is simply do the same code over and over again.

    Also, what it is doing is not very ram intensive at all, so I wouldn't worry about it.

    the_cows take a look online for help with for loops.

    Here

    He is simply looping 10 times.
     
  8. Offline

    Niknea

    Iron_Crystal I wasn't talking about the ram , I was talking about reducing the amount of lines of code, and why do you have to try to be cool and give him a www.lmgtfy.com link? Would it kill you to give him a direct link? As lmgtfy brings up a lot of websites, some may not be for what he needs, and he'll get the wrong knowledge.
     
  9. Offline

    Iron_Crystal


    Literally any of the links on the first page of Google would have helped him

    Sorry I didn't realize you were talking about lines of code. The OP asked about RAM, and it seemed you responded to that question.
     
    Niknea likes this.
  10. Offline

    Deleted user

    Break it down for ya now boiiiii
    Code:java
    1.  
    2. for (int itemAmount = 0; itemAmount < 10; itemAmount++) {
    3. player.getInventory().addItem(new ItemStack(Material.APPLE));
    4. }
    5.  

    So this is what we call a "for loop" basically it loops (repeats) and executes specific task during the process
    so an example would be like this:
    Code:
    for (Player player : Bukkit.getServer().getOnlinePlayers()) {
    player.sendMessage("weak sauce");
    }
    
    So as you can see here, for every player online it will send them that message.
    So, my for loop is a little more advanced (compressed)
    Code:
    for (int itemAmount = 0; itemAmount < 10; itemAmount++) {
    
    Basically what this means is:
    • int itemAmount = 0 - we've defined a variable (an integer) as '0' and named it 'itemAmount'
    • itemAmount < 10 - we check to see if the defined integer (itemAmount) is less than 10 (the for loop will keep working until itemAmount is above 10
    • itemAmount++ - if the itemAmount is less than 10 then increase it's value by 1 (or add 1 basically)
    Each type the goes through a loop it acknowledge the int 'itemAmount' and gets it's value (which is 0 the very first time it loops). It then goes on to check if the int 'itemAmount' is less than 10, if it is not then it will cancel the loop however if it is less than 10 it add 1 to the int 'itemAmount' and run whatever code is actually inside of the "for loop", eg; player.sendMessage() or player.getInventory().addItem().
    So.... this loop here:
    Code:java
    1.  
    2. for (int itemAmount = 0; itemAmount < 10; itemAmount++) {
    3. player.getInventory().addItem(new ItemStack(Material.APPLE));
    4. }
    5.  

    Will add 10 apples to the player's inventory, now.. directing the OP's issue: I believe that using addItem() may auto-stack like items and seeing as how you want "a lot of unstacked items" Niknea is correct about needing to use the setItem() which will allow us to set the item of a specified slot in an inventory. The problem with this is that we do not want to override any existing items and replace, per say, a diamond with an apple. So, we need to find an empty slot to add the desired item to.
    To do this we can use this code:
    Code:
    Randomw r = new Random(); // Used when getting random values and what not
    int inventorySlot = r.nextInt(player.getInventory().getSize()); // Chooses a random number between 0 and however large the player's inventory is (I think it's like 36 or something like that)
    while (player.getInventory().getItem(inventorySlot ) != null) // We then get the slot within the player's inventory that we had chosen (so for example if inventorySlot = 8 then it'd get slot 8 of the player's inventory) if the slot is not null (meaning there is an item in that slot) then we reset inventorySlot and it will continue to check until it finds an empty slot.
    inventorySlot = r.nextInt(player.getInventory().getSize());
    
    Now, using these two examples of code together:
    Code:java
    1.  
    2. Randomw r = new Random();
    3. for (int itemAmount = 0; itemAmount < 10; itemAmount++) {
    4. int inventorySlot = r.nextInt(player.getInventory().getSize());
    5. while (player.getInventory().getItem(inventorySlot ) != null)
    6. inventorySlot = r.nextInt(player.getInventory().getSize());
    7. player.getInventory().setItem(inventorySlot, new ItemStack(Material.APPLE));
    8. }
    9. - p.s. an updateInventory() may be necessary after all of this -
    10. player.updateInventory();
    11.  

    So now we are almost done.. lol, you were talking about using RAM and worried about the efficiency of your plugin.
    So basically with our code above: each time the loop goes through it checks for an empty slot in the player's inventory (totally of checking 10 times) the problem with this is that depending on how many items the player has and the slot's retrieved by the random process - the code COULD check a player's inventory for a free slot 36 times or so each loop! So to avoid this, we can first check to see if a player has at least 10 available slots..
    Code:
    ArrayList<Integer> freeSlots = new ArrayList<Integer>();
    int currentSlot = 0;
    for (ItemStack i : player.getInventory().getContents()) {
    currentSlot++;
    // NOTE: you may need to do some adjusting with this to see which slot the loop begins at (rather it's 0, 1, or 36..)
    if (i == null)
    freeSlots.add(currentSlot);
    }
    if (freeSlots.size() < 10)
    return;
    
    (I've written so much at this point I CBA to explain every piece of code anymore - but if you're confused about something feel free to ask)
    So FINALLY to throw all this shit together..
    Code:java
    1.  
    2. ArrayList<Integer> freeSlots = new ArrayList<Integer>();
    3. Random r = new Random();
    4. int currentSlot = 0;
    5. for (ItemStack i : player.getInventory().getContents()) {
    6. currentSlot++;
    7. if (i == null)
    8. freeSlots.add(currentSlot);
    9. }
    10. if (freeSlots.size() < 10)
    11. return;
    12. for (int itemAmount = 0; itemAmount < 10; itemAmount++) {
    13. player.getInventory().setItem(freeSlots.get(0), new ItemStack(Material.APPLE));
    14. freeSlots().remove(0);
    15. }
    16. player.updateInventory();
    17.  

    ^^ THIS CODE COULD ACTUALLY BE ALTERED (AND CONDENSED INTO ONE LOOP) TO SIMPLY ADD ITEMS WHILE ITERATING THE INVENTORY AND JUST KEEP TRACK OF HOW MANY ITEM YOU'VE ADDED - ALTHOUGH FOR THE SAKE OF TEACHING AND DEMONSTRATING LOOPS, #420SWAG.
    ALSO:
    Noticed I've used two different type of loops here, a 'for loop' and a 'while loop'
    To better understand looping, what they do, how they work, and what each type is used for..
    I suggest you read THIS. This does a wonderful job of explaing each type of loop and even gives
    examples of each type and the outcome they produce!
     
  11. Offline

    Giraffeknee

Thread Status:
Not open for further replies.

Share This Page