Resizing custom inventory after creation (world.createInventory)

Discussion in 'Plugin Development' started by Father Of Time, Mar 21, 2012.

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

    Father Of Time

    Good afternoon to all,

    I’ve been making a “vault” plug-in for our server that allows people to have one none physical inventory associated to them as a sort of “doomsday” vault, so even if the player is griefed they will always have a small “stash” of goods to fall back on. Everything is working great, each player has their own vault, the vaults are serializing and deserializing, etc… however, here is the problem…

    The system is designed so that the size of the inventory scales based on the players user group. Lower level user groups should only have an inventory of 2 rows (2x9=18 slots), while donors get 4 rows ( 4x9=36 slots). So here is my question…

    Is it possible to set the index size of an inventory once its been created using world.createInventory? in the create inventory function you pass the size of the inventory as the second argument to initially set the size of the inventory, but once its set can you later come back and “resize” the inventory to a small/larger size? I see the following functions:

    getSize () Returns the size of the inventory.
    setMaxStackSize (int size) This method allows you to change the maximum stack size for an inventory.
    getMaxStackSize ()

    But I don’t see a setSize() function. Does anyone know of a way to handle this without creating a second inventory with the size I want then copying the content from the old inventory and trashing the old larger/smaller one?

    I am sure I can “ghetto rig” this to work, but if I can find a legitimate method of resizing an inventory without creating a new instance I would definitely prefer that.

    Thank you in advance to anyone who can offer advice on this topic.
    p.s. Celtic Minstrel I know you are a busy man, but if you happen to find yourself with the time to review this question I would be extremely appreciative. Also well done on the inventory API, I love some of the new flexibility it offers!
     
  2. Offline

    Celtic Minstrel

    Inventories are fixed-size, like arrays (they use arrays internally); thus, having a setSize() function would require quite a bit of work. However, I'm pretty sure that creating a new inventory and copying the contents can be done in two lines:
    Code:
    newInventory = Bukkit.createInventory(inventory.getHolder(), newSize);
    newInventory.setContents(inventory.getContents());
    
    Provided that the new inventory is larger than the old... if it's smaller you'd need to do a bit more work.

    I won't completely rule out the eventual addition of a setSize() function, but it's fairly unlikely, and certainly won't be soon.
     
    Father Of Time likes this.
  3. Offline

    Father Of Time

    You are quickly becoming my personal hero Celtic, thanks for the insanely fast and knowledgeable response.

    I figured this would likely be the method I would need to resolve my issue, but I figured I would run it past the public just to make sure. As you stated the going from 2 rows to 4 is no problem, but when a players donor status wears off and it shrinks back down is the issue. However, I'm pretty sure I will just make their excess goods fall to the ground the next time they open it (when and if their account downgrades ).

    Thank you once again for sharing your experience, it's extremely appreciated. :D
     
Thread Status:
Not open for further replies.

Share This Page