#InventoryClickEvent problem

1 messages · Page 1 of 1 (latest)

strange ruin
#
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {

        ItemStack clickedItem = new ItemStack(Objects.requireNonNull(event.getCurrentItem()));
        ItemStack cursorItem = new ItemStack(Objects.requireNonNull(event.getCursor()));
        Bukkit.broadcastMessage(ChatColor.RED + "Before: " + event.getCursor().toString() + " | " + event.getCurrentItem().toString());
        if (event.getCursor().getType() == Material.ENCHANTED_BOOK) {
            if (event.getCurrentItem().getType() != Material.AIR) {
                EnchantmentStorageMeta enchMeta = (EnchantmentStorageMeta) cursorItem.getItemMeta();
                event.setCancelled(true);
                assert enchMeta != null;
                if (enchMeta.hasStoredEnchants()) {
                    Bukkit.broadcastMessage("Has enchs");
                    Map<Enchantment, Integer> enchants = enchMeta.getStoredEnchants();
                    clickedItem.addUnsafeEnchantments(enchants);
                    event.getWhoClicked().getInventory().addItem(cursorItem);
                    event.getCursor().setType(Material.AIR);
                    Bukkit.getScheduler().runTaskLater(mainClass, () -> {
                        event.getWhoClicked().getInventory().setItem(event.getSlot(), clickedItem);
                    }, 5L);

                    Bukkit.broadcastMessage(ChatColor.GREEN + "After: " + event.getCursor().toString() + " | " + event.getCurrentItem().toString());
                } else {
                    Bukkit.broadcastMessage("No enchs");
                }
            }
        }
    }
```here's the debug code
warm nexus
strange ruin
#

It is not seeing the cursor as sword because I haven't ran the event again, and when I try clearing clicked item that doesn't seem to work either

warm nexus
warm nexus
#

why are you

#

uh

#

doing new itemstacks

strange ruin
#

Sure, the first red message is when I pick up the book, the green message is when I click the book onto the sword, and the second red message is when I put the sword back into my inventory

warm nexus
#

for the event items

strange ruin
#

Because for some reason when I was just applying the enchants without creating a new item stack and setting the item in the inventory, it wasn't working

#

Like it didn't enchant the item

warm nexus
strange ruin
#
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        ItemStack clickedItem = new ItemStack(Objects.requireNonNull(event.getCurrentItem()));
        ItemStack cursorItem = new ItemStack(Objects.requireNonNull(event.getCursor()));
        if (event.getAction().equals(InventoryAction.SWAP_WITH_CURSOR) && event.getCursor().getType() == Material.ENCHANTED_BOOK) {
            EnchantmentStorageMeta enchMeta = (EnchantmentStorageMeta) cursorItem.getItemMeta();
            assert enchMeta != null;
            if (enchMeta.hasStoredEnchants()) {
                event.setCancelled(true);
                Map<Enchantment, Integer> enchants = enchMeta.getStoredEnchants();
                clickedItem.addUnsafeEnchantments(enchants);
                event.getWhoClicked().getInventory().addItem(cursorItem);
                event.getWhoClicked().setItemOnCursor(null);
                Bukkit.getScheduler().runTaskLater(mainClass, () -> {
                    event.getWhoClicked().getInventory().setItem(event.getSlot(), clickedItem);
                }, 1L);
            } else {
                Bukkit.broadcastMessage("No enchs");
            }
        } else {
            Bukkit.broadcastMessage(event.getAction().toString());
        }
    }
``` I changed the if statement and it works fine now, I am instead checking for inventory swap and the cursor item instead of cursor and clicked item
warm nexus
#

the book is the cursor

#

and the sword is the current item

#

not the other way around

#

but that's good to know u figured it out