brcdev-minecraft / shopgui-api

Public API for ShopGUI+ plugin
MIT License
9 stars 6 forks source link

ShopPreTransactionEvent - Cancellation ignored at /sell all #5

Closed Angeschossen closed 1 year ago

Angeschossen commented 4 years ago

Issue: Event cancellation is ignored for the item at /sell all. The event definitely get's cancelled. However /sell all seems to ignore this cancellation for the checked item.

Reproduce: 1: Cancel ShopPreTransactionEvent 2: Execute /sell all

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onShopPreTransaction(ShopPreTransactionEvent e) {
        // we're only interested in selling
        if (e.getShopAction() == ShopManager.ShopAction.BUY) {
            return;
        }

        // we're not selling a hopper
        if (e.getShopItem().getItem().getType() != Material.HOPPER) {
            return;
        }

        /*
        Iterating over storage contents as the event clears the data container or uses a new ItemStack instance of the item stack.
        So the persistent data container of the item is not existent in the new item.
        It's not possible for us to detect a uhopper from the event so we need to iterate over inventory contents.
         */
        for (ItemStack item : e.getPlayer().getInventory().getStorageContents()) {
            if (item == null) {
                continue;
            }

            // cancel event if item has uhopper tag in persistent data container
            if (UpgradeableHoppers.getPlugin().getVersion().isHopperItem(item.getItemMeta())) {
                e.setCancelled(true);
                break;
            }
        }
    }
Angeschossen commented 4 years ago

Forgot to mention. The above method does stop people from selling upgradeable hoppers. It works fine for /sell hand, only at /sell all the cancellation is ignored.

brcdev commented 4 years ago

Thanks for the heads up, I'll investigate it this week.

Angeschossen commented 4 years ago

Any update on this?