How to prevent item dispensing when equipping armor with a dispenser

Discussion in 'Plugin Development' started by OshagHenesy, Jan 22, 2024.

  1. Offline

    OshagHenesy

    Hello, I am creating a plugin that is supposed to react to a player equipping a leather helmet via a dispenser, and replace the equipped helmet with a leather chestplate. I've got the general functionality working, but for some reason I am still receiving a leather helmet in my inventory from the dispenser, in addition to the helmet being placed in my helmet slot. Do I need to cancel the Item Dispense Event as well as the Armor Dispense event? How does this work? I am relatively new to Bukkit/Spigot development and ChatGPT doesn't know the BlockDispenseArmorEvent exists, so I can't get help there haha

    Here's what I'm working with so far:

    Code:
        @EventHandler
        public void onDispenserArmorEvent(BlockDispenseArmorEvent event) {
            if (!(event.getTargetEntity() instanceof Player player)) return;
    
            if (!(event.getItem().getType() == Material.LEATHER_HELMET)) return;
    
            event.setCancelled(true);
            event.setItem(null);
            player.getInventory().setItem(EquipmentSlot.HEAD, new ItemStack(Material.LEATHER_CHESTPLATE));
    
            player.sendMessage(plugin.getConfig().getString("listeners.leather-helmet.equip"));
        }
    Thanks :)
     
  2. Offline

    KarimAKL

    @OshagHenesy According to the Javadoc for the BlockDispenseArmorEvent, cancelling it makes it so that the item is not equipped on the target entity, which most likely means it is already in the entity's inventory.
    You could instead try removing it from their inventory.
     

Share This Page