Reputation: 25
Hi everyone probably a nooby question. I know how to open someone else Inventory and make changes to it. But what I want is to just open inventory without way to make changes to that player inventory. Just a preview of it.
Player target = Bukkit.getPlayer(args[0]);
if (target != null) {
p.openInventory(target.getInventory());
}
Does anyone know how to just let others to see the inventory and not let them take any items out of it?
Thank you!
Upvotes: 1
Views: 107
Reputation: 7143
You can do that via an InventoryClickEvent
, as pointed by Andrew Li in the comments.
InventoryClick is a cancellable event, you just need to compare the clicked inventory to your target inventory, like so:
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
HumanEntity clicker = e.getWhoClicked();
Inventory inv = e.getClickedInventory();
if (/*inv is your saved inv*/) {
e.setCancelled(true);
}
This way you can cancel the inventory clicking, thus any action that happens in an inventory, like taking or placing an item, but still allowing the person to view the Inventory.
Upvotes: 2