Solved Filling a chest at know location

Discussion in 'Plugin Development' started by blablubbabc, May 25, 2013.

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

    blablubbabc

    Hey, I can't seem to figure out this one..

    I have a list of locations, and want to fill the chests inventories which are located at this locations.

    So here is what I am doing (and what seems to fail silently):

    Code:
    void fillChests() {
            List<Location> forRemoval = new ArrayList<Location>();
            // other chests is a list of locations where I will find the chests
            for (Location loc : otherChests) {
                //getting the block at this location -> works fine
                Block block = loc.getWorld().getBlockAt(loc);
                // checking if it is a chest -> works fine
                if (block.getState() instanceof Chest) {
                    // casting the block state to Chest
                    Chest chest = (Chest) block.getState();
                    // filling the chest with method below
                    fillChest(chest.getInventory());
                    // to be save: update block state
                    chest.update();
                    // continue with the next location
                    continue;
                }
                // if a block is no chest (because someone broke it), I remove it later from the list of locations
                forRemoval.add(loc);
            }
            // removing and saving all locations which were no chests anymore
            if (!forRemoval.isEmpty()) {
                otherChests.removeAll(forRemoval);
                Config.saveCurrentConfig();
            }
           
        }
       
        // filling the chests inventory with random items from a given list of ItemStacks
        private void fillChest(Inventory inv) {
            if (inv != null && Config.items != null && !Config.items.isEmpty()) {
                // for each slot in the chest
                for (int i = 0; i < inv.getSize(); i++) {
                    // put item in it, or not
                    if (Utils.random.nextInt(2) == 1) {
                        // getting a random ItemStack from the list of ItemStacks
                        ItemStack is = Config.items.get(Utils.random.nextInt(Config.items.size())).clone();
                        // randomly change the amount a bit
                        is.setAmount(Utils.random.nextInt(is.getAmount()) + 1);
                        // placing the item at the slot in the inventory
                        inv.setItem(i, is);
                    }
                }
            }
        }
    It seems that everythign works fine, because:
    * with some debug messages I found out, that it successfully fills the given inventory with some items.
    * The blocks are really chests and he is able to locate them
    * The code above works fine if I call it inside an IteractEvent, itneracting with the chest I want to fill. But if I want to get the chests "remotly" and filling them it fails silently = seems to still fill the chests BUT: When I check the chests, they are all empty..

    Something bukkit specific I am missing here ?

    Okay, I found the problem I think (finally):

    I load the locations from config and store them in a list.
    But my plugin unloads and loads the world these locations are referencing every once and a while resulting (I think) in a new world object being generated and the world my stored locations are referencing not being completly removed internal by java -> I can still modify blocks in this world and check block types there, but the world bukkit is referencing is a new one, with same name etc..

    So everything I do with the stored locations is affecting the "old" world (before unloading/loading) while I see the new world where nothing is changing :/ So I will have to recreate the location objects everytime I reload the world..

    This was a very tricky bug :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page