Help With Chest Filling

Discussion in 'Plugin Development' started by dlange, Feb 13, 2015.

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

    dlange

    I am making an SG based plugin and need some help with filling the chests.
    This is the code i have tried:
    Code:
            int radius = 100;
            Location loc = new Location(Bukkit.getServer().getWorld("purge"), 12, 120, 12);
            World world = loc.getWorld();
            for (int x = -radius; x < radius; x++) {
                for (int y = -radius; y < radius; y++) {
                    for (int z = -radius; z < radius; z++) {
                        Block block = world.getBlockAt(loc.getBlockX()+x, loc.getBlockY()+y, loc.getBlockZ()+z);
                        if (block.getType() == Material.CHEST) {
                            Chest chest = (Chest) block;
                            Material[] items = new Material[] { Material.GOLDEN_APPLE, Material.IRON_CHESTPLATE, Material.STONE_SWORD };
                            Random r = new Random();
                            int numItems = r.nextInt(5) + 1;        for (int i = 0; i < numItems; i++) {
                                Material material = items[r.nextInt(items.length)];
                                ItemStack item = new ItemStack(material, 1);
                                int index;
    
                                do {
                                    index = r.nextInt(chest.getInventory().getSize());
                                } while (chest.getInventory().getItem(index) != null);
    
                                chest.getInventory().setItem(index, item);
                            }
    
                        }
                    }
                }
            }
    
     
  2. @dlange Don't just cast the chest to the block, cast it to the blockState (block.getBlockState()) - BlockStates carry extra information about blocks, such as the contents of a chest or the information stored on a sign.
     
    dlange likes this.
  3. Offline

    dlange

    Example please?

    omg ty soo much <3 it worked

    @DJSkepter Do you know a way I can make a percentage system/rarities system for items in the chests?
     
    Last edited by a moderator: Feb 15, 2015
    DJSkepter likes this.
Thread Status:
Not open for further replies.

Share This Page