How to prevent item dispensing when equipping armor with a dispenser

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

  1. Offline


    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:

        public void onDispenserArmorEvent(BlockDispenseArmorEvent event) {
            if (!(event.getTargetEntity() instanceof Player player)) return;
            if (!(event.getItem().getType() == Material.LEATHER_HELMET)) return;
            player.getInventory().setItem(EquipmentSlot.HEAD, new ItemStack(Material.LEATHER_CHESTPLATE));
    Thanks :)
  2. Offline


    @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