7

I am trying to open up my inventory whenever I pick up an item. This is in Bukkit.

Here is the event so far, the arguments for player.openInventory are empty.

@EventHandler
public void blank(PlayerDropItemEvent e){
    Player player = e.getPlayer();
    player.openInventory();
}
livibetter
  • 19,832
  • 3
  • 42
  • 42
Mark Johnson
  • 127
  • 5

2 Answers2

6

Try using player.getInventory() to retrieve their inventory then using player.openInventory(inventory) to open it.

@EventHandler
public void blank(PlayerDropItemEvent e) {
    Player player = e.getPlayer();
    Inventory inventory = player.getInventory();
    player.openInventory(inventory);
}
Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
Rishaan Gupta
  • 563
  • 4
  • 18
  • 3
    I'd just like to add, I like to do a quick `player.closeInventory()` before I open the inventory. It's probably not necessary, but the server doesn't know if the client already has their inventory open, and knowing mojang, it just might break the client if the server tries to tell them to open another inventory when they already have one open. – hintss May 02 '15 at 16:46
  • 1
    @hintss they actually did it ok, opening a inventory will automatically close the last inventory, it will even call a InventoryCloseEvent. ^_^ –  Jun 28 '15 at 10:10
  • and will the client always be smart enough to do this, forever and ever? That's the important question. – hintss Jun 30 '15 at 06:40
3

To get a player's inventory, you could use:

player.getInventory();

If you wanted to open the player's inventory, you could use:

player.openInventory(player.getInventory());

So, your code could look something like this:

@EventHandler
public void dropItem(PlayerDropItemEvent e){
    Player player = e.getPlayer(); //get the player that dropped the item
    player.openInventory(player.getInventory()); //open the player's inventory
}
Jojodmo
  • 23,357
  • 13
  • 65
  • 107