Hey Guys, Im working on a survival games plugin and have a bit of a problem with the randome items and chests. Code:java Block block = world.getBlockAt(randomX, randomY, randomZ);block.setType(Material.CHEST); BlockState state = block.getState(); if (state instanceof Chest) { Chest chest = (Chest) block.getState();
Adoma_ use Code: Random r = new Random(); int rNumber = r.nextInt(100) + 1; //generates random number between 1 and 100
Make a material array with the materials you are allowed to put in the chests Generate a random number, with the max. being the size of the array Place that Material (possibly with a random amount) in the chest in a random slot Do it as many times as you want items in the chest
This is what I do, Code:java Random rand = new Random(); Chest chest = (Chest) b.getState(); chest.getInventory().clear();chest.getInventory().addItem(new ItemStack(lootlist.get(rand.nextInt(lootlist.size())))); Then I'd have a lootlist containing itemstacks Code:java List<ItemStack> lootlist = new ArrayList<ItemStack>(); After adding items to the list, Use the method above as many times as you want to add items into the chest randomly.
Code:java Material[] items = new Material[] { //Array of items, using Material }; Random r = new Random(); for (Chest chest : <Array of chests>) { int numItems = 1+r.nextInt(Random item ratio); int ammount = 1+r.nextInt(Max int of items a player can get); for (int i = 0; i < numItems; i++) { Material material = items[r.nextInt(items.length)]; ItemStack item = new ItemStack(material, ammount); int index; do { index = r.nextInt(chest.getInventory().getSize()); } while (chest.getInventory().getItem(index) != null); chest.getInventory().setItem(index, item); } Based off of PogoStick29