Solved Delay between 'for loop'

Discussion in 'Plugin Development' started by Jesian97, Jul 25, 2015.

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

    Jesian97

    I need to delay the 2nd 'for loop' from occurring until after the first one has finished, because the valid items aren't removed from the 'inv' until after all the items are returned to their inventory. This is an issue, because the items that sell should not be returned to the players inventory, they should be removed from the 'inv', then when the 2nd for loop comes around, it returns the unsellable items to the players inventory. The 'inv' is a null holder inventory, like a blank menu to put your items in to sell.
    Code:
    if (cl.getInventory().getName().equalsIgnoreCase(inv.getName())) {
                for(String arrayElement : valid){
                    if(inv.contains(Material.getMaterial(arrayElement),1)){
                        for(int i = 0; i < 35; i++){
                            ItemStack g = h[i];
                            if((!(g == null))){
                                if(g.getType().toString().equals(arrayElement)){
                                    int itemn = g.getAmount();
                                    int itemp = plugin.getConfig().getInt("items." + arrayElement);
                                    player.sendMessage(red + WHITE + "you sold " + BLUE + itemn + WHITE + " " + arrayElement.toLowerCase() + " for " + itemp);
                                    inv.remove(g);
                                    player.getInventory().remove(Material.getMaterial(arrayElement));
                                    continue;
                                }
                            }
                        }
                    }
                }
                for(int i = 0; i < 35; i++){
                    ItemStack g = h[i];
                    if((!(g == null))){
                        player.getInventory().addItem(g);
                        inv.remove(g);
                        continue;
                    }
                }
            }
    If anyone could tell me what to add to this, I would greatly appreciate it, thanks
     
  2. Offline

    Agentleader1

    For loops generally take a split second. All you really need to do is schedule a task 1 tick after the previous for loop task and you're good.

    Don't know how to make delayed tasks?
    http://wiki.bukkit.org/Scheduler_Programming
     
  3. Offline

    567legodude

    @Agentleader1 I'm pretty sure that code runs in order of lines, and I'm also pretty sure that the second loop would not run until the first one is done anyway.
     
  4. Offline

    Agentleader1

  5. Offline

    567legodude

  6. Offline

    Jesian97

    well if it's already delayed till after, why is the 'inv' not updated for the second 'for loop', in game the items that where sold get added to my inventory along with all the other items, not filtered out from the first 'for loop', do I need to update the inventory or something?
     
  7. Offline

    567legodude

    @Jesian97 Probably just the way you are doing things. And also, with "(int i = 0; i < 35; i++)" you are going through every item in their inventory except for the last one, because a player inventory has 36 slots, and you are scanning 35 of them.
     
  8. Offline

    Jesian97

    thats because i have a menu selector block at the 36th slot, h[35], so i don't want that one to be scanned

    Also i tested the plugin, and it returns the sold items back into my inventory during the 2nd 'for loop', its almost like the 'inv' is not being edited before it gets checked by the 2nd 'for loop'

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

    Agentleader1

    player.updateInventory();
     
  10. Offline

    Jesian97

    it isn't a player inventory i need to update

    What i need to do is figure out a way to get the second loop to see the inventory change, from the first loop, because it is completely disregarding the previous loop, and I already checked, and the items are removed during the first loop. If anyone has any idea on what I need to do let me know, I've been trying to resolve this all day.

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

    1Rogue

    the second loop will never be called before the first. What are you trying to accomplish? http://xyproblem.info/
     
  12. Offline

    Jesian97

    What i'm trying to accomplish is, when a player enters all his/her inventory in a custom inventory 'inv' (already set up), on event inventoryclose, it checks from a config(not yet) if each inventory slot is a valid block, then runs a method to addPlayerfunds(right now set to just send a msg, but just getting it to work first), then it removes that itemstack, the second loop, would then remove all the remaining items that weren't valid, since its filtering through all the inventory 'inv' slots it is made to remove all the inventory and return the non-valid items to the player, my issue is, that the 'inv' must not be updating, as when i test this, i just get all the invenory returned to my player inventory, even the items I've sold. So for some reason beyond me, the second loop sees the inventory 'inv' as if it hasn't been edited by the first loop, when i know the first loop is removing the valid items(tested).
     
  13. Offline

    1Rogue

    Instead of trying to manually use the inventory methods, iterate over the inventory slots and use #setSlot and #setAmount for the itemstacks.
     
  14. Offline

    Jesian97

    example?
     
  15. Offline

    _Filip

    for (int counter = 0; counter < limit; counter++) {
    Bukkit.getScheduler().runTaskLater(plugin, () -> doSomething(), counter * delay);
    }
     
  16. Offline

    1Rogue

    Code:java
    1. Inventory inv = /*...*/;
    2. for (int i = 0; i < inv.getSize() / 9; i++) {
    3. //use #getSlot and #setSlot for all itemstacks
    4. }
     
  17. Offline

    Jesian97

    I cannot do this, as I have a loop that checks if each inventory slot is a item on the list, including the suggested code would clear all items that aren't the first array value, then would end there, as no items would be left in the inventory
     
  18. Offline

    1Rogue

    You would simply do all those checks within the loop.
     
  19. Offline

    Jesian97

    I already have the code how I need it, i just need to add something to reload, or update the inventory before loops, does anyone know how to accomplish this, possibly creating a new inventory from the old one, but with new items?
     
Thread Status:
Not open for further replies.

Share This Page