From df9acfa0d155df7aa59f817b81ed4a1d11b6d934 Mon Sep 17 00:00:00 2001 From: Gitoido Date: Fri, 9 Oct 2026 11:03:06 +0300 Subject: [PATCH 1/2] fix: equipping stackable items now equips one item of stack --- CHANGELOG.md | 2 +- .../mod/common/item/WearableHatItem.kt | 9 ++-- .../cobblemon/mod/common/item/WearableItem.kt | 52 +++++++++++++++++-- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47ffb99958..a68081dbcd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -169,7 +169,7 @@ - Fixed crashes for addon species with non-Cobblemon namespaces, in evolution, fishing bait and Pokédex lookups. - Bees now grow Apricorns and the Galarica Nut Bush, which were in the bee growables tag but never handled, and correctly spend their nectar when they do - Saccharine Leaves can now be bone mealed - +- Fixed equipping a stack of items instead of one item of a stack ### Developer - Evolution requirements can now set `pokedexIcon` and `pokedexTooltip` directly in their JSON to control how they're shown on the Pokédex's evolution tree, for example: ```json diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableHatItem.kt b/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableHatItem.kt index ab06191c2c..70dc60e821 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableHatItem.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableHatItem.kt @@ -19,14 +19,13 @@ import net.minecraft.world.level.Level class WearableHatItem(override val name: String): CobblemonItem(Properties()), WearableItem { override fun use(level: Level, player: Player, usedHand: InteractionHand): InteractionResultHolder { - val itemStack = player.getItemInHand(usedHand) val hitPokemon = player.traceFirstEntityCollision( entityClass = PokemonEntity::class.java, maxDistance = player.entityInteractionRange().toFloat() ) // If the player is looking at a Pokémon within range, block the ability to swap the item onto their head - return if (player.isCrouching && hitPokemon != null && hitPokemon.isOwnedBy(player)) { - InteractionResultHolder.fail(itemStack) - } else this.swapWithEquipmentSlot(this, level, player, usedHand) + if (player.isCrouching && hitPokemon != null && hitPokemon.isOwnedBy(player)) return InteractionResultHolder.fail(player.getItemInHand(usedHand)) + + return this.swapStackWithEquipmentSlot(level, player, usedHand) } -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableItem.kt b/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableItem.kt index 34955fcfd2..b5021270a7 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableItem.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableItem.kt @@ -10,10 +10,18 @@ package com.cobblemon.mod.common.item import com.cobblemon.mod.common.util.cobblemonResource import net.minecraft.resources.ResourceLocation +import net.minecraft.stats.Stats +import net.minecraft.world.InteractionHand +import net.minecraft.world.InteractionResultHolder import net.minecraft.world.entity.EquipmentSlot +import net.minecraft.world.entity.player.Player import net.minecraft.world.item.Equipable +import net.minecraft.world.item.ItemStack +import net.minecraft.world.item.enchantment.EnchantmentEffectComponents.PREVENT_ARMOR_CHANGE +import net.minecraft.world.item.enchantment.EnchantmentHelper +import net.minecraft.world.level.Level -interface WearableItem: Equipable { +interface WearableItem : Equipable { val name: String companion object { @@ -23,5 +31,43 @@ interface WearableItem: Equipable { fun getModel3d(): ResourceLocation = cobblemonResource("${MODEL_PATH}/${this.name}") fun getModel2d(): ResourceLocation = cobblemonResource(this.name) - override fun getEquipmentSlot(): EquipmentSlot { return EquipmentSlot.HEAD } -} \ No newline at end of file + override fun getEquipmentSlot(): EquipmentSlot { + return EquipmentSlot.HEAD + } + + /** + * Swap stack with equipment slot + * Copied from vanilla, modified to account for stackable items + * @see Equipable.swapWithEquipmentSlot + * + * @param item + * @param level + * @param player + * @param hand + * @return the interaction result holder + */ + fun swapStackWithEquipmentSlot( + level: Level, + player: Player, + hand: InteractionHand + ): InteractionResultHolder { + var itemStack = player.getItemInHand(hand) + val equipmentSlot = player.getEquipmentSlotForItem(itemStack) + + if (!player.canUseSlot(equipmentSlot)) return InteractionResultHolder.pass(itemStack) + + val slotStack = player.getItemBySlot(equipmentSlot) + + if (!(!EnchantmentHelper.has(slotStack, PREVENT_ARMOR_CHANGE) || player.isCreative) || ItemStack.matches(itemStack, slotStack)) return InteractionResultHolder.fail(itemStack) + if (!level.isClientSide()) player.awardStat(Stats.ITEM_USED.get(itemStack.item)) + + val equipStack = itemStack.split(1) + when { + !itemStack.isEmpty -> player.addItem(slotStack.copyAndClear()) + else -> itemStack = slotStack.copyAndClear() + } + + player.setItemSlot(equipmentSlot, equipStack) + return InteractionResultHolder.sidedSuccess(itemStack, level.isClientSide()) + } +} From d6c7b271e4e1d48ef0ca18285cdedc5fc04cc828 Mon Sep 17 00:00:00 2001 From: Gitoido Date: Fri, 9 Oct 2026 11:11:34 +0300 Subject: [PATCH 2/2] chore: flip when condition for readability --- .../main/kotlin/com/cobblemon/mod/common/item/WearableItem.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableItem.kt b/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableItem.kt index b5021270a7..e9567eba93 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableItem.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/item/WearableItem.kt @@ -63,8 +63,8 @@ interface WearableItem : Equipable { val equipStack = itemStack.split(1) when { - !itemStack.isEmpty -> player.addItem(slotStack.copyAndClear()) - else -> itemStack = slotStack.copyAndClear() + itemStack.isEmpty -> itemStack = slotStack.copyAndClear() + else -> player.addItem(slotStack.copyAndClear()) } player.setItemSlot(equipmentSlot, equipStack)