Filling Chest Inventory

Discussion in 'Plugin Development' started by ShadowWizardMC, Jul 4, 2015.

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

    ShadowWizardMC

    So first of all, Hi I hope you're in an alright mood to have to deal with me right now haha just a small problem that I can't manage to fix myself.

    So I'm trying to fill all the chest in the world with items that are loaded when the server begins:

    Code:
            for (Chunk c : w.getLoadedChunks()) {
                for (BlockState b : c.getTileEntities()) {
                    if (b instanceof Chest) {
                        Chest chest = (Chest) b;
                        Inventory inv = chest.getInventory();
                        inv.clear();
                        int items = r.nextInt(4) + 1;
                        int index = 0;
                        do {
                            inv.setItem(r.nextInt(36), presets.get(r.nextInt(presets.size())));
                        }while (index <= items);
                    }
                }
            }
    But Whenever I do setItem I get an error when the method is call:

    Code:
    [16:23:22] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to RefillMev1.0
    Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    If I do inv.addItem() Somehow it works fine but I want the random layout of the Chest Inventory. By Chance is there anything you can see that I'm doing wrong here? It would be greatly appreciated!
     
  2. Offline

    mine-care

    Code:
                        do {
                            inv.setItem(r.nextInt(36), presets.get(r.nextInt(presets.size())));
                        }while (index <= items);
    Τhis is the cause most likely.

    presets may be empty, check this before or change the loop from do while to while (with some tweaks)
     
  3. Offline

    ShadowWizardMC

    @mine-care

    That's not the problem I'm definitely sure. Presets is never empty beacause the ArrayList is always attempted to be filled before.

    Code:
    //Right before the above method i do
    loadItems();
    
    and this method looks like this:
    
    public void loadItems(){
            if(presets.isEmpty()){
                presets.add(new ItemStack(Material.STONE_SWORD));
                presets.add(new ItemStack(Material.WOOD_SWORD));
                presets.add(new ItemStack(Material.IRON_AXE));
                presets.add(new ItemStack(Material.FEATHER));
                presets.add(new ItemStack(Material.FLINT));
            }
        }
     
  4. Offline

    FisheyLP

    Normal chests have 27 slots. Double chests have 54 slots
     
  5. Offline

    ShadowWizardMC

    @FisheyLP :O I have no idea why I thought it was 36 haha I'll get back to you with an update thanks for the point!
     
  6. Offline

    MCMatters

    @ShadowWizardMC by the way instead of 36 you should be able to get the size of the Inventory
     
Thread Status:
Not open for further replies.

Share This Page