I was first using only getTitle()
for this but then found you need to use getView()
as well. So i did but I stil can’t get it to work.
FYI This is the error :
Cannot resolve method 'getView' in 'Inventory'
I tried changing getInventory()
for InventoryView()
but still the same error but also the Cannot resolve method 'InventoryView' in 'Chest'
error so this means I cant get the inventory view from a block?
JavaScript
@EventHandler
public void onInteract(PlayerInteractEvent e) {
Action a = e.getAction();
Player p = e.getPlayer();
ItemStack hand = e.getItem();
Block clicked = e.getClickedBlock();
if (a != Action.PHYSICAL) {
assert clicked != null;
if(clicked.getType() == Material.CHEST){
hand.getType();
Chest chest = (Chest) clicked.getState();
Inventory inv = chest.getInventory();
if(inv.getView().getTitle().equalsIgnoreCase("Test")) {
p.sendMessage("Event triggered");
e.setCancelled(true);
}
}
}
}
Advertisement
Answer
I have figured it out after searching on around 100 sites I found a solution that works, I don’t know if it’s the best solution but good for me.
I changed the whole event to use InventoryOpenEvent
insted of PlayerInteractEvent
.
Here is the full code :
JavaScript
@EventHandler
public void onInventoryOpen(InventoryOpenEvent e) {
Player player = (Player) e.getPlayer();
if (e.getView().getTitle().equals("Test")) {
e.setCancelled(true);
player.sendMessage("Succesfull");
}
}
Here is the code stripped down :
JavaScript
@EventHandler
public void onInventoryOpen(InventoryOpenEvent e) {
if (e.getView().getTitle().equals("Test")) {
e.setCancelled(true);
}
}