Respawning chests with random loot

Discussion in 'Plugin Development' started by kjSmitten, Dec 11, 2013.

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

    kjSmitten

    I've searched for quite a while and have found nothing. I would like to know how I would go about making chests at certain locations respawn with random loot.
     
  2. Offline

    xize

    kjSmitten
    some psuodo;)

    Code:
    Location loc = yourlocation;
    Block block = yourlocation.getBlock();
    if(block.getType() == Material.AIR) {
          block.setType(Material.CHEST);
          Chest chest = (Chest) block.getState();
           chest.getInventory().addItem(do something); //could also be setContents(); you also have getChestInventory() aswell
    }
    
    however make sure you don't put null ItemStacks or something in it, also make sure that a chest inventory can be smaller than a players inventory.

    a DoubleChest is harder atleast for me to get it to work however this is my old code which I wrote for a friend when I whas kinda unserious don't expect professional code:p

    Code:
     @EventHandler
     public void onDeath(PlayerDeathEvent e) {
      if(e.getEntity() instanceof Player) {
       if(e.getEntity().hasPermission("DeathManChest.use")) {
        Location loc = e.getEntity().getLocation();
        if(e.getDrops().size() > 27) {
         //double chest
         loc.getBlock().setType(Material.CHEST);
         loc.getBlock().getRelative(BlockFace.NORTH).setType(Material.CHEST);
         Chest chest = (Chest) loc.getBlock().getState();
         players.put(e.getEntity().getName(), chest);
         InventoryHolder hold = chest.getInventory().getHolder();
         DoubleChest doubleChest = (DoubleChest) hold;
         for(int i = 0; i < e.getDrops().size(); i++) {
          if(i >= 27) {
           doubleChest.getRightSide().getInventory().addItem(e.getDrops().get(i));
          } else {
           doubleChest.getLeftSide().getInventory().addItem(e.getDrops().get(i));
          }
         }
        } else {
         //normal chest
         loc.getBlock().setType(Material.CHEST);
         Chest chest = (Chest) loc.getBlock().getState();
         players.put(e.getEntity().getName(), chest);
         for(ItemStack item : e.getDrops()) {
          chest.getInventory().addItem(item);
         }
        }
        e.getDrops().clear();
       }
      }
     }
    
    its better to use exceptions with try and catch instead of what I did testing on slot sizes:p
     
  3. Offline

    kjSmitten

    xize I don't want death chest code. I want to know how I could make chests respawn after they are emptied, and every time they respawn they have different loot.
     
  4. Offline

    xize

    kjSmitten
    what you could do then is add items in a ArrayList make a scheduler
    and check if the location block is a chest if its not create a chest again and use Random within the ArrayList and when the event InventoryCloseEvent triggers on that chest and it is empty remove the block again or hold the chest but then add items again.

    just a simple psuodo code for the random:
    Code:
    public ArrayList<ItemStack> array = new ArrayList<ItemStack>();
     
    public void setItems() {
         //add items in the array if nothing is in it
    }
     
    public ItemStack randItem() {
           if(array.isEmpty()){
                  setItems();
            } 
           ItemStack[] items = array.toArray(new ItemStack[array.size()]);
            Random rand = new Random();
            int numberID = rand.next(items.length)+1; //from 1 to length
            return items[numberID];
    }
    
     
  5. Offline

    kjSmitten

    xize still not entirely what I was looking for. I wanted different "types" of chests that had specific loot that could be randomly generated in them. And the number of those specific items would be random.
     
  6. Offline

    The_Doctor_123

    kjSmitten
    xize has given you some mighty fine examples for you to get an idea on what to do.
     
  7. Offline

    xize

    kjSmitten

    you could do something along the lines of:
    Code:
    public HashMap<String, ArrayList<ItemStack>> chests = new HashMap<String, ArrayList<ItemStack>>();
    
    means that your key is the chest id and if you use get(key); you get the full ArrayList of items from that kind of key.
    though I'm not sure if this could cause problems within the memory and such what I recomend to do is always first clearing all ArrayList values and then start removing the keys from the HashMap.
     
Thread Status:
Not open for further replies.

Share This Page