Hey there guys, I've seen alot of threads lately asking how to fill chests on a map. So I thought that today I would share my way of filling them so you could see if it was beneficial to you. Now, this is mainly used in Survival Games plugins, I coded this for my Survival Games plugin so that's why I have it. First you want to create this method: Code:java public void fillAllChests(String s){for(Chunk chunk : plugin.getServer().getWorld(s).getLoadedChunks()){for(BlockState entities : chunk.getTileEntities()){if(entities instanceof Chest){Inventory inv = ((Chest) entities).getInventory();fillChests(inv);}}}} Then, you need to decide what goes in the chest: Code:java public void fillChests(Inventory inv){inv.clear();Random itemnum = new Random();int items = 3+itemnum.nextInt(getItems(inv));for(int i = 1; i < items+1; i++){Random slotnum = new Random();Random itemrand = new Random();int item = 1+itemrand.nextInt(55);int slot = slotnum.nextInt(inv.getSize());if(item == 1 || item == 19 || item == 52){inv.setItem(slot, new ItemStack(Material.ENDER_PEARL, 1));}else if(item == 2 || item == 19){inv.setItem(slot, new ItemStack(Material.FEATHER, 2));}}} Finally, you need to know when and how to use it. So, here's an example: Code:java @Overridepublic void run() {String worldName = ...;World world = ...;getLogger().info("FILLING CHESTS ON: " + worldName);fillAllChests(worldName);getLogger().info("FILLED CHESTS ON: " + worldName);} I hope this could help you in any way possible. Regards, Alex Harris
You could do that, but then if you were doing survival games. The chest would be refilled everytime its opened, whereas with this method it is only filled when its called. But yes, you could fill the chest when its opened but it wouldn't be more efficient.
AlexHH251997 well, then you can add location of that chest or the Chest object to a Set and check it every time someone opens it, if its already in the list, do nothing, otherwise fill it with your items
I'm sure my method would be a lot more efficient. It's super easy to check if they're looking in a chest and it would be easy to check if it's already been looted, and it only uses the resources when needed, and especially would be more efficient if you have a decent amount of chests. and there's still the chance of them not being opened so what's the point filling them all, they both have their pro's and con's
It's not really efficient, especially when you're using method names that aren't in the example? http://horrgs.org/img/0vFV.png
You could use some code to remove an empty chest on inventory close though. I used it in my Maze Runner plugin.
gal0511 Well i believe iiHeroo pointed that out, his getItems() method is somewhere else in his class, and he didn't add it within the example.