Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event does not run if armor is swapped #2

Open
gIsForGravity opened this issue May 13, 2023 · 2 comments · May be fixed by #5
Open

Event does not run if armor is swapped #2

gIsForGravity opened this issue May 13, 2023 · 2 comments · May be fixed by #5

Comments

@gIsForGravity
Copy link

If you right click an armor piece in your hotbar to swap with a piece that you're wearing, (1.19.4 feature) then the event does not run.

@iron768
Copy link

iron768 commented Aug 20, 2023

I noticed this when using this library too, it seems to be caused in the ArmorListener.java class on line 143.
The code looks something like if(newArmorType.equals(ArmorType.HELMET) && isEmpty(event.getPlayer().getInventory().getHelmet()).

The reason why its not being detected, is that the plugin is only checking if the armor slot is empty. I never got around to making a full fix in this repo since I am quite busy at the moment but I can attach my "patch" which is just a listener. Hope this helps

My comment in the patch should contain more info:

Bug: Swapping armor when already wearing armor does not call the armor change event
Cause: if(newArmorType.equals(ArmorType.HELMET) && isEmpty(event.getPlayer().getInventory().getHelmet())...
The isEmpty checks if the slot is currently empty, so it ignores when a player is swapping their gear when currently wearing gear

This "patch" is necessary since the current api has no capabilities for handling armor swapping (despite it being relatively simple given the code already present)

The following code is just copied from the ArmorListener just modified to handle swapping armor from hotbar

Eventually I should make a pull request to fix this issue, for now it stays here.

Here is the class:

public class ArmorChangeListener implements Listener {

    // TODO: check this list
    private List<Material> blockedMaterials;

    public ArmorChangeListener(CustomEnchants plugin) {
        blockedMaterials = new ArrayList<>(Arrays.asList(
                Material.FURNACE, Material.CHEST, Material.TRAPPED_CHEST, Material.BEACON, Material.DISPENSER, Material.DROPPER, Material.HOPPER, Material.CRAFTING_TABLE,
                Material.ENCHANTING_TABLE, Material.ENDER_CHEST, Material.ANVIL, Material.LEGACY_BED, Material.LEGACY_FENCE_GATE, Material.SPRUCE_FENCE_GATE, Material.BIRCH_FENCE_GATE,
                Material.ACACIA_FENCE_GATE, Material.JUNGLE_FENCE_GATE, Material.DARK_OAK_FENCE_GATE, Material.LEGACY_IRON_DOOR_BLOCK, Material.LEGACY_WOODEN_DOOR, Material.SPRUCE_DOOR,
                Material.BIRCH_DOOR, Material.JUNGLE_DOOR, Material.ACACIA_DOOR, Material.DARK_OAK_DOOR, Material.LEGACY_WOOD_BUTTON, Material.STONE_BUTTON, Material.LEGACY_TRAP_DOOR,
                Material.IRON_TRAPDOOR, Material.LEGACY_DIODE_BLOCK_OFF, Material.LEGACY_DIODE_BLOCK_ON, Material.LEGACY_REDSTONE_COMPARATOR_OFF, Material.LEGACY_REDSTONE_COMPARATOR_ON,
                Material.LEGACY_FENCE, Material.SPRUCE_FENCE, Material.BIRCH_FENCE, Material.JUNGLE_FENCE, Material.DARK_OAK_FENCE, Material.ACACIA_FENCE, Material.LEGACY_NETHER_FENCE,
                Material.BREWING_STAND, Material.CAULDRON, Material.LEGACY_SIGN_POST, Material.LEGACY_WALL_SIGN, Material.LEGACY_SIGN, Material.ACACIA_SIGN, Material.ACACIA_WALL_SIGN,
                Material.BIRCH_SIGN, Material.BIRCH_WALL_SIGN, Material.DARK_OAK_SIGN, Material.DARK_OAK_WALL_SIGN, Material.JUNGLE_SIGN, Material.JUNGLE_WALL_SIGN, Material.OAK_SIGN,
                Material.OAK_WALL_SIGN, Material.SPRUCE_SIGN, Material.SPRUCE_WALL_SIGN, Material.LEVER, Material.BLACK_SHULKER_BOX, Material.BLUE_SHULKER_BOX, Material.BROWN_SHULKER_BOX,
                Material.CYAN_SHULKER_BOX, Material.GRAY_SHULKER_BOX, Material.GREEN_SHULKER_BOX, Material.LIGHT_BLUE_SHULKER_BOX, Material.LIME_SHULKER_BOX, Material.MAGENTA_SHULKER_BOX,
                Material.ORANGE_SHULKER_BOX, Material.PINK_SHULKER_BOX, Material.PURPLE_SHULKER_BOX, Material.RED_SHULKER_BOX, Material.LEGACY_SILVER_SHULKER_BOX, Material.WHITE_SHULKER_BOX,
                Material.YELLOW_SHULKER_BOX, Material.LEGACY_DAYLIGHT_DETECTOR_INVERTED, Material.DAYLIGHT_DETECTOR, Material.BARREL, Material.BLAST_FURNACE, Material.SMOKER,
                Material.CARTOGRAPHY_TABLE, Material.COMPOSTER, Material.GRINDSTONE, Material.LECTERN, Material.LOOM, Material.STONECUTTER, Material.BELL
        ));

        plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onInteract(PlayerInteractEvent event) {
        if (event.useItemInHand().equals(Event.Result.DENY)) return;

        if (event.getAction() == Action.PHYSICAL) return;

        if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {

            Player player = event.getPlayer();

            if (!event.useInteractedBlock().equals(Event.Result.DENY)) {
                if (event.getClickedBlock() != null && event.getAction() == Action.RIGHT_CLICK_BLOCK && !player.isSneaking()) {// Having both of these checks is useless, might as well do it though.
                    // Some blocks have actions when you right click them which stops the client from equipping the armor in hand.
                    Material mat = event.getClickedBlock().getType();

                    if (blockedMaterials.contains(mat))
                        return;
                }
            }

            ArmorType newArmorType = ArmorType.matchType(event.getItem());

            // Carved pumpkins cannot be equipped using right-click
            if (event.getItem() != null && event.getItem().getType() == Material.CARVED_PUMPKIN)
                return;

            if (newArmorType == null)
                return;

            for (ItemStack armor : player.getInventory().getArmorContents()) {
                ArmorType oldArmorType = ArmorType.matchType(armor);

                if (oldArmorType == newArmorType) {
                    ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent(event.getPlayer(), ArmorEquipEvent.EquipMethod.HOTBAR, newArmorType, armor, event.getItem());

                    Bukkit.getServer().getPluginManager().callEvent(armorEquipEvent);

                    if (armorEquipEvent.isCancelled()) {
                        event.setCancelled(true);

                        player.updateInventory(); // TODO: spigot is marking this as unstable!
                    }
                }
            }
        }
    }

}

@TreemanKing TreemanKing linked a pull request Nov 28, 2023 that will close this issue
@mfnalex
Copy link
Owner

mfnalex commented Jun 21, 2024

IIRC an option was added to the APi to check if a block is "interactible" - that would solve having to hardcode all those blocks. I'll take a look whether I find that method again (probably in Material #isInteractable() or sth)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants