Setting a chest inventory

Discussion in 'Plugin Development' started by libraryaddict, Jan 27, 2013.

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

    libraryaddict

    I would like to set a chest size at 9, however I don't see any obvious methods of doing this.

    How would I set the chest size.
    If you don't know the answer to this.. How would I set a new inventory and replace the old chest inventory
     
  2. Offline

    RealDope

    I don't think you can actually adjust the size of the chest (not positive though). What you can do is associate the chest with an inventory via a HashMap, and then "intercept" it when they open the chest using PlayerInteractEvent and show them the 9 slot inventory instead.
     
  3. Offline

    drtshock

    You can make a chest any multiple of 9 (not sure how high you can go). No need to use a HashMap if you don't want to.
     
  4. Offline

    libraryaddict

    But HOW
     
  5. Offline

    libraryaddict

  6. Offline

    stuntguy3000

    Inventory chest = player.getServer().createInventory(null, (9), "Chest Title");

    player.openInventory(chest);
     
  7. Offline

    libraryaddict

    I'm not asking how to open a chest.
    I'm asking if its possible to change the chest inventory size.
     
  8. Offline

    stuntguy3000

    (null, (9), "Chest Title");

    Yeh, Just change the 9 to 9 * <rows you want>
     
  9. Offline

    Cjreek

    You create a HashMap<Location, Inventory> where you store your custom Inventories for your chests.
    In a PlayerInteractEvent you check if the (right-)clicked block is a chest you want to modifiy (I don't know your criteria).

    Something like this:

    Code:java
    1.  
    2. private HashMap<Location, Inventory> chestInvs = new HashMap<Location, Inventory>();
    3.  
    4. public void onPlayerInteract(PlayerInteractEvent event)
    5. {
    6. if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.CHEST)
    7. {
    8. event.setCanceled(true);
    9.  
    10. Location chestLoc = event.getClickedBlock().getLocation();
    11. if (!chestInvs.contains(chestLoc)
    12. {
    13. Inventory inv = Bukkit.createInventory(event.getPlayer(), 9, InventoryType.CHEST);
    14. chestInvs.put(chestLoc , inv);
    15. }
    16.  
    17. player.openInventory(chestInvs.get(chestLoc));
    18. }
    19. }
     
  10. Offline

    libraryaddict

    I didn't explain myself clear enough :\

    I meant change the chest itself. Not creating a new inventory.
    Or creating a new inventory and overriding the old chest.

    I know how to catch a player interact event etc to 'fake' its inventory. But I'd prefer to see if I can do this in a few lines of code rather then stretched out.
     
  11. Offline

    Cjreek

    libraryaddict look at my code. That's what's been suggested to you to do.

    As far as I know there is no way of actually changing the size of a chest inventory without faking one.
     
Thread Status:
Not open for further replies.

Share This Page