[UTIL] Chest Restock Method

Discussion in 'Resources' started by MineStein, Aug 15, 2014.

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

    MineStein

    Hey everyone! I am currently writing a Survival Games plugin from scratch. I have recently finished my regenerateChests method. I decided to share it with you!

    Code:java
    1. public static void regenerateChests() {
    2. for (Chunk chunks : Core.forsakenAscension.getLoadedChunks()) {
    3. for (BlockState chests : chunks.getTileEntities()) {
    4. if (chests instanceof Chest) {
    5. Chest chest = (Chest) chests;
    6.  
    7. Random random = new Random();
    8. int slot1 = random.nextInt(26);
    9. int slot2 = random.nextInt(26);
    10. int slot3 = random.nextInt(26);
    11. Material[] items = new Material[] {
    12. Material.WOOD_SWORD, Material.STONE_SWORD,
    13. Material.WOOD_AXE, Material.STONE_AXE,
    14. Material.LEATHER_HELMET, Material.LEATHER_CHESTPLATE,
    15. Material.LEATHER_LEGGINGS, Material.LEATHER_BOOTS,
    16. Material.IRON_HELMET, Material.IRON_LEGGINGS,
    17. Material.IRON_BOOTS, Material.IRON_CHESTPLATE
    18. };
    19.  
    20. chest.getInventory().clear();
    21.  
    22. chest.getInventory().setItem(slot1, new ItemStack(items[random.nextInt(items.length)]));
    23. chest.getInventory().setItem(slot2, new ItemStack(items[random.nextInt(items.length)]));
    24. chest.getInventory().setItem(slot3, new ItemStack(items[random.nextInt(items.length)]));
    25. }
    26. }
    27. }
    28. }


    NOTES
    * Change the "Core.forsakenAscension" to your world's name. You could also pass the world in the method parameters if you wanted to.
     
    HeavyMine13 likes this.
  2. Offline

    HeavyMine13

    You might want to check if the next item your are setting doesn't have the same slot id as the other one, so they don't get overridden!
     
  3. Offline

    MineStein

    HeavyMine13 That was intentional. Sometimes the item will be overridden, therefore making it so there are less than three items. Thanks though :D
     
    HeavyMine13 likes this.
  4. Offline

    Serializator

    Maybe you should create the Random instance and items array outside the of the loops because you are creating a new Random instance and items array for every chest in the world.
     
    artish1 likes this.
Thread Status:
Not open for further replies.

Share This Page