2015-04-23 53 views
7

我试图在我拿起物品时打开我的库存。这是在Bukkit。如何通过活动打开我自己的库存?

这是迄今为止的事件,player.openInventory的参数为空。

@EventHandler 
public void blank(PlayerDropItemEvent e){ 
    Player player = e.getPlayer(); 
    player.openInventory(); 
} 
+0

你是否注册了听众? – CoderMusgrove

+2

插件工作,player.openInventory()需要一个参数 –

回答

6

尝试使用player.getInventory()来检索他们的库存,然后使用player.openInventory(inventory)将其打开。

@EventHandler 
public void blank(PlayerDropItemEvent e) { 
    Player player = e.getPlayer(); 
    Inventory inventory = player.getInventory(); 
    player.openInventory(inventory); 
} 
+3

我只想添加,我喜欢在打开库存之前快速执行'player.closeInventory()'。这可能不是必要的,但服务器不知道客户端是否已打开库存,并且知道mojang,如果服务器试图告诉他们在已打开另一个库存时打开另一个库存,则可能会破坏客户端。 – hintss

+1

@hintss他们实际上确实没有问题,打开库存会自动关闭上一个库存,它甚至会调用InventoryCloseEvent。^_^ – 2015-06-28 10:10:08

+0

并且客户是否永远都会足够聪明,永远做到这一点?这是一个重要的问题。 – hintss

3

要获得玩家的库存,你可以使用:

player.getInventory(); 

如果你想打开播放器的库存,你可以使用:

player.openInventory(player.getInventory()); 

所以,你的代码可能看起来像这样:

@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 
} 
相关问题