[INFO] Filling Chests on Map

Discussion in 'Resources' started by AlexHH251997, Jul 3, 2014.

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

    AlexHH251997

    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
    1. public void fillAllChests(String s){
    2. for(Chunk chunk : plugin.getServer().getWorld(s).getLoadedChunks()){
    3. for(BlockState entities : chunk.getTileEntities()){
    4. if(entities instanceof Chest){
    5. Inventory inv = ((Chest) entities).getInventory();
    6. fillChests(inv);
    7. }
    8. }
    9. }
    10. }

    Then, you need to decide what goes in the chest:
    Code:java
    1. public void fillChests(Inventory inv){
    2. inv.clear();
    3. Random itemnum = new Random();
    4. int items = 3+itemnum.nextInt(getItems(inv));
    5. for(int i = 1; i < items+1; i++){
    6. Random slotnum = new Random();
    7. Random itemrand = new Random();
    8. int item = 1+itemrand.nextInt(55);
    9. int slot = slotnum.nextInt(inv.getSize());
    10. if(item == 1 || item == 19 || item == 52){
    11. inv.setItem(slot, new ItemStack(Material.ENDER_PEARL, 1));
    12. }else if(item == 2 || item == 19){
    13. inv.setItem(slot, new ItemStack(Material.FEATHER, 2));
    14. }
    15. }
    16. }

    Finally, you need to know when and how to use it. So, here's an example:
    Code:java
    1. @Override
    2. public void run() {
    3. String worldName = ...;
    4. World world = ...;
    5. getLogger().info("FILLING CHESTS ON: " + worldName);
    6. fillAllChests(worldName);
    7. getLogger().info("FILLED CHESTS ON: " + worldName);
    8. }

    I hope this could help you in any way possible.

    Regards,
    Alex Harris
     
  2. Offline

    bloodless2010

    Wouldn't it be more efficient to fill the chests when they get opened?
     
    Zupsub, iiHeroo and Skyost like this.
  3. Offline

    AlexHH251997

    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.
     
  4. Offline

    viper_monster

    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
     
    Zupsub likes this.
  5. Offline

    Theodossis

    This code is very useful!
     
  6. Offline

    bloodless2010

    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 ;)
     
  7. Offline

    iiHeroo

    LordVakar likes this.
  8. Offline

    Beeperdp


    You could use some code to remove an empty chest on inventory close though. I used it in my Maze Runner plugin.
     
  9. Offline

    gal0511Dev

    Im getting errors on getItems:
    Code:java
    1. int items = 3 + itemnum.nextInt(getItems(inv));
     
  10. gal0511
    Impossible to know what's wrong if
    1. You don't show the error
    2. You don't show the method
     
  11. Offline

    97WaterPolo

    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.
     
Thread Status:
Not open for further replies.

Share This Page