Hi, I would like create an inventory but I have a problem When I double-click on an object already present in the inventory, I retrieve all the objects identical to the one I just clicked Before After Code Code (Text): if(inv.getName().equals("§cTest")){ e.setCancelled(true); if(iteminv.isSimilar(getBookManager().getBasicBook().getBasic())){ getBookManager().getBasicBook().give((Player) e.getWhoClicked()); } else if(iteminv.isSimilar(getBookManager().getRareBook().getBasic())){ getBookManager().getRareBook().give((Player) e.getWhoClicked()); } else if(iteminv.isSimilar(getBookManager().getLegendaryBook().getBasic())){ getBookManager().getLegendaryBook().give((Player) e.getWhoClicked()); } else if(iteminv.isSimilar(getBookManager().getDivinBook().getBasic())){ getBookManager().getDivinBook().give((Player) e.getWhoClicked()); } } Inventory Initi: Code (Text): InventoryBuilder inventoryBuilder = new InventoryBuilder(); inventoryBuilder.setName("§cTest"); inventoryBuilder.setSize(9); inventoryBuilder.getInventoryContainers().full(new ItemStackBuilder().setMaterial(Material.STAINED_GLASS_PANE).setData((byte) 8).build()); inventoryBuilder.getInventoryContainers().setItem(1, getBookManager().getBasicBook().getBasic()); inventoryBuilder.getInventoryContainers().setItem(3, getBookManager().getRareBook().getBasic()); inventoryBuilder.getInventoryContainers().setItem(5, getBookManager().getLegendaryBook().getBasic()); inventoryBuilder.getInventoryContainers().setItem(7, getBookManager().getDivinBook().getBasic()); this.generate = inventoryBuilder.build();
Code (Text): if(inv.getName().equals("§Test")){ e.setCancelled(true); if(iteminv.isSimilar(getBookManager().getBasicBook().getBasic())){ getBookManager().getBasicBook().give((Player) e.getWhoClicked()); } else if(iteminv.isSimilar(getBookManager().getRareBook().getBasic())){ getBookManager().getRareBook().give((Player) e.getWhoClicked()); } else if(iteminv.isSimilar(getBookManager().getLegendaryBook().getBasic())){ getBookManager().getLegendaryBook().give((Player) e.getWhoClicked()); } else if(iteminv.isSimilar(getBookManager().getDivinBook().getBasic())){ getBookManager().getDivinBook().give((Player) e.getWhoClicked()); } } I already cancelled all InventoryClickEvent for Test inventory Code (Text): if(inv.getName().equals("§Test")){ e.setCancelled(true); And I juste have another condition: Code (Text): if(e.getClickedInventory() == null) return;
It may be an issue in the server not updating the inventory of the player after you have cancelled the event. Run this after you cancel the event: Code (Text): final HumanEntity player = event.getWhoClicked(); Bukkit.getScheduler().runTask(pluginInstance, () -> { player.updateInventory(); }); This will force an update of the player's inventory after the event has been cancelled.
So that it runs after the event has been handled by the server. Sometimes issues can occur if it is run in the listener as the server has not reverted the changes the player made to the inventory and therefore updateInventory will send them the same broken inventory.
Exactly, that's why I asked him to show all code, because a simple 'doesn't work' gives us no way to help...
I have the same result code : Code (Text): if(inv.getName().equals("§cTest")){ e.setCancelled(true); Bukkit.getScheduler().runTask(getTest(), () -> { ((Player) e.getWhoClicked()).updateInventory(); }); if(iteminv.isSimilar(getBookManager().getBasicBook().getBasic())){ getBookManager().getBasicBook().give((Player) e.getWhoClicked()); } else if(iteminv.isSimilar(getBookManager().getRareBook().getBasic())){ getBookManager().getRareBook().give((Player) e.getWhoClicked()); } else if(iteminv.isSimilar(getBookManager().getLegendaryBook().getBasic())){ getBookManager().getLegendaryBook().give((Player) e.getWhoClicked()); } else if(iteminv.isSimilar(getBookManager().getDivinBook().getBasic())){ getBookManager().getDivinBook().give((Player) e.getWhoClicked()); } }
I havent tested it, but it should be possibly to detect with " if(event.getClick() == event.getClick().DOUBLE_CLICK){}"
Broadcast a message to ensure that section of code is being triggered. Something like Code (Text): Bukkit.broadcastMessage("It's working"); If that doesn't work, post your whole listener, not just the part of it you think is relevant. Also, make sure your events are registered, that is a common mistake.
When I double click in my inventory, the event does not trigger because when I click on my inventory I do not click on the inventory "Test"
You used InventoryClickEvent#getClickedInventory() instead of InventoryClickEvent#getInventory(). If you had shown us your whole listener in the first place this could have been solved long ago :\.