hamza-cskn / obliviate-invs

An efficient object-oriented inventory GUI library for Minecraft servers.
https://www.spigotmc.org/resources/obliviateinvs-—-highly-efficient-modular-gui-library.103572/
MIT License
112 stars 17 forks source link

Anyway to use onCLick method to cancel player click their own inventory? #59

Closed Srar-Git closed 5 months ago

Srar-Git commented 5 months ago

I am using the onClick() method to listen when player click their own inventory, but it seems i can't cancel the InventoryClickEvent, is there anyway to cancel the player click their own inventory? this is my code for the event, the return false; and event.setCancelled(true); not work.

@Override
    public boolean onClick(InventoryClickEvent event) {
        super.onClick(event);
        if (event.getClickedInventory()==null) {
            return false;
        }
        if (event.getClickedInventory().equals(player.getInventory())) {
            event.setCancelled(true);
            player.sendMessage("You clicked to the inv! - "+event.getAction());
            ItemStack item = event.getCurrentItem();
            if (item==null) return false;
            if (!item.getType().equals(Material.PRISMARINE_SHARD)) return false;
            int amount = item.getAmount();
            if (amount>36) amount = 36;
            // one click
            if (event.getAction().equals(InventoryAction.PICKUP_ALL) || event.getAction().equals(InventoryAction.PICKUP_HALF)){
                int slotIndex =slotsWithShard.size();
                slotsWithShard.add(order[slotIndex]);
                this.addItem(order[slotIndex], new Icon(Material.PRISMARINE_SHARD));
                return false;
            }
            //shift click
            if (event.getAction().equals(InventoryAction.MOVE_TO_OTHER_INVENTORY)){
                for (int i = 0; i < amount; i++) {
                    int slotIndex = i;
                    slotIndex+=slotsWithShard.size();
                    if (slotIndex>order.length-1) return false;
                    this.addItem(order[slotIndex], new Icon(Material.PRISMARINE_SHARD));
                    slotsWithShard.add(order[slotIndex]);
                }
                return false;
            }

            return false;
        }
        return false; // false -> cancel the event, true -> do not cancel the event.
    }

Checklist

Ensure you've done everything in this list before submitting the issue. (Just put the 'X' char between brackets.)

hamza-cskn commented 5 months ago

I see your confusion. The onClick event is called only for GUI clicks. Because other ones are not its responsibility. Using this method, you cannot cancel clicks on the player's inventory. You have to use the Bukkit event handlers. But do not be sad I didn't forget these scenarios.

You can listen events with 'GuiPreClickEvent'. It gets triggered when players click on a GUI. It is a more permitted version of the onClick and it works like you think. You can cancel this event using event.setCancelled(true);

You can check if the clicked inventory is a GUI inventory or the player's inventory with: https://github.com/hamza-cskn/obliviate-invs/blob/c0f3a4423ed7e27fc0540c8ae3120a52e121db66/core/src/main/java/mc/obliviate/inventory/InvListener.java#L40

It is true when the player clicks on the Gui. You can use another way to check it if you know. Let me know if your issue is solved or not.

Srar-Git commented 5 months ago

hi, thanks for help, I solved the problem by this code, directly cancel the GuiPreClickEvent event not work, need cancel the InventoryClickEvent in GuiPreClickEvent, maybe you could add this to wiki?

@EventHandler
    public void onPreClick(GuiPreClickEvent e) {
        InventoryClickEvent event = e.getEvent();
        e.getEvent().setCancelled(true);
    }
hamza-cskn commented 5 months ago

I agree that it does not work as it looks. I'll think about it. I am glad to hear you've solved it. I am closing this issue.