Opening a virtual chest/inventory?

Discussion in 'Plugin Development' started by davejavu, Jul 15, 2012.

  1. Offline

    davejavu

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Hey, I was wondering how to open a virtual chest/inventory, if there's a tutorial anywhere that I've missed please link me.

    Thanks.
  2. Offline

    gjossep

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Idk about the tutorial but I think that you do Bukkit.getServer().createInventory or something like that, check that on bukkits docs about it!
  3. Offline

    r0306

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @davejavu
    Create an inventory like gjossep said and store it somewhere so that you can call it up when you need.

    Code:
    public class VirtualInventory
    {
     
      Inventory inventory;
      Player owner;
      String name;
     
      public VirtualInventory(Player p, String name)
      {
     
          this.inventory = Bukkit.createInventory(p, 27, name);
          this.owner = player;
          this.name = name;
       
      }
     
    public Inventory getInventory()
    {
     
      return this.inventory();
     
    }
     
    public Player getOwner()
    {
     
      return this.name;
     
    }
     
    public String getName()
    {
     
      return this.title;
     
    }
    Then to create a new instance of this object and save it, you would do something like:
    Code:
    public Inventory createInventory(player, name)
    {
     
        VirtualInventory vi = new VirtualInventory(player, name);
     
        return vi;
     
    }
     
    HashMap<Player, VirtualInventory> inventories = new HashMap<Player, VirtualInventory>();
     
    public void saveInventory(Player player, VirtualInventory vi)
    {
     
          inventories.put(player, vi);
     
    }
    Then to call up an inventory, you would do:
    Code:
    player.openInventory(inventories.get(player).getInventory());
    I made the VirtualInventory class because you might need to add your own methods in there. If you don't, you can simply create the inventories and store them in the hashmap and call them up when you need.

    This post has been edited 1 time. It was last edited by r0306 Jul 16, 2012.
  4. Offline

    LucasEmanuel

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Hup hup, now, havent we already said it enought times that you should never store the player object? Store the name as a reference, that way you can get the player object by calling: server.getPlayer(playername);
  5. Offline

    V10lator

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Better: server.getPlayerExact(playername) ;)
    ferrybig likes this.
  6. Offline

    davejavu

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @r0306 Thanks for the help so far, it's epic.
    Now how would I say, open an inventory and put items in it that the player CANNOT remove?
  7. Offline

    r0306

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @davejavu
    If you want to disallow the player from changing the WHOLE inventory, you can simply cancel the event if the inventory matches:
    Code:
    @EventHandler
    public void inventoryClick(InventoryClickEvent event)
    {
      if (event.getInventory().getName().equalsIgnoreCase("the other inventory name"))
          event.setCancelled(true);
    }

Share This Page