From 15204e428a67d26ad5b1066b67b30a92770d6213 Mon Sep 17 00:00:00 2001 From: naomi Date: Tue, 17 Dec 2024 04:05:32 +0000 Subject: [PATCH] Item detection changes (#60) * [fix] Remove shields from weapons tag * [fix] Implement extra check for armour - As `#c:armor` tag didn't officially exist in this version, almost 0 mods use it. * [fix] Ranged weapon fixes * [fix] Use cached `ItemStack#item` instead of iteration Lazily evaluated methods are an insidious killer at times * [fix] Remove unneeded item conversion * [fix] target `SwordItem`, `CrossbowItem` und `BowItem` in `isWeapon` --------- Co-authored-by: Bibi Reden --- .../com/bibireden/playerex/mixin/ItemStackMixin.java | 5 +---- .../com/bibireden/playerex/api/PlayerEXTags.kt | 3 +++ .../com/bibireden/playerex/util/PlayerEXUtil.kt | 8 ++++++-- .../resources/data/playerex/tags/items/weapons.json | 12 ++++++++++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/bibireden/playerex/mixin/ItemStackMixin.java b/src/main/java/com/bibireden/playerex/mixin/ItemStackMixin.java index 71da4f4c..eb7cc61b 100644 --- a/src/main/java/com/bibireden/playerex/mixin/ItemStackMixin.java +++ b/src/main/java/com/bibireden/playerex/mixin/ItemStackMixin.java @@ -10,13 +10,10 @@ import com.llamalad7.mixinextras.sugar.Local; import net.fabric_extras.ranged_weapon.api.EntityAttributes_RangedWeapon; import net.minecraft.ChatFormatting; -import net.minecraft.core.registries.Registries; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; -import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; -import net.minecraft.tags.TagKey; import net.minecraft.util.RandomSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; @@ -115,7 +112,7 @@ public void modifyAttributeModifiers(EquipmentSlot slot, CallbackInfoReturnable< HashMultimap hashmap = HashMultimap.create(cir.getReturnValue()); if (PlayerEX.CONFIG.getWeaponLevelingSettings().getEnabled() && PlayerEXUtil.isWeapon(stack)) { PlayerEXUtil.addToModifier(hashmap, Attributes.ATTACK_DAMAGE, getLevel() * PlayerEX.CONFIG.getWeaponLevelingSettings().getDamagePerLevel()); - if (stack.is(TagKey.create(Registries.ITEM, ResourceLocation.tryBuild("c", "bows")))) { + if (hashmap.containsKey(EntityAttributes_RangedWeapon.DAMAGE.attribute)) { PlayerEXUtil.addToModifier(hashmap, EntityAttributes_RangedWeapon.DAMAGE.attribute, getLevel() * PlayerEX.CONFIG.getWeaponLevelingSettings().getDamagePerLevel()); } } diff --git a/src/main/kotlin/com/bibireden/playerex/api/PlayerEXTags.kt b/src/main/kotlin/com/bibireden/playerex/api/PlayerEXTags.kt index 7e109687..ce0fed97 100644 --- a/src/main/kotlin/com/bibireden/playerex/api/PlayerEXTags.kt +++ b/src/main/kotlin/com/bibireden/playerex/api/PlayerEXTags.kt @@ -14,4 +14,7 @@ object PlayerEXTags { @JvmField val ARMOR: TagKey = TagKey.create(Registries.ITEM, PlayerEX.id("armor")) + + @JvmField + val ARMOR_BLACKLIST: TagKey = TagKey.create(Registries.ITEM, PlayerEX.id("armor_blacklist")) } diff --git a/src/main/kotlin/com/bibireden/playerex/util/PlayerEXUtil.kt b/src/main/kotlin/com/bibireden/playerex/util/PlayerEXUtil.kt index 69afed4f..2f16e138 100644 --- a/src/main/kotlin/com/bibireden/playerex/util/PlayerEXUtil.kt +++ b/src/main/kotlin/com/bibireden/playerex/util/PlayerEXUtil.kt @@ -9,7 +9,11 @@ import com.google.common.collect.Multimap import net.minecraft.world.entity.ai.attributes.Attribute import net.minecraft.world.entity.ai.attributes.AttributeModifier import net.minecraft.world.entity.player.Player +import net.minecraft.world.item.ArmorItem +import net.minecraft.world.item.BowItem +import net.minecraft.world.item.CrossbowItem import net.minecraft.world.item.ItemStack +import net.minecraft.world.item.SwordItem import net.objecthunter.exp4j.Expression import net.objecthunter.exp4j.ExpressionBuilder import net.objecthunter.exp4j.function.Function @@ -104,12 +108,12 @@ object PlayerEXUtil { @JvmStatic fun isWeapon(stack: ItemStack): Boolean { - return stack.`is`(PlayerEXTags.WEAPONS) + return stack.item is BowItem || stack.item is CrossbowItem || stack.item is SwordItem || stack.`is`(PlayerEXTags.WEAPONS) } @JvmStatic fun isArmor(stack: ItemStack): Boolean { - return stack.`is`(PlayerEXTags.ARMOR) + return (stack.item is ArmorItem || stack.`is`(PlayerEXTags.ARMOR)) && !stack.`is`(PlayerEXTags.ARMOR_BLACKLIST) } @JvmStatic diff --git a/src/main/resources/data/playerex/tags/items/weapons.json b/src/main/resources/data/playerex/tags/items/weapons.json index 587cfc5d..e0df7d72 100644 --- a/src/main/resources/data/playerex/tags/items/weapons.json +++ b/src/main/resources/data/playerex/tags/items/weapons.json @@ -5,6 +5,14 @@ "id": "#c:swords", "required": false }, + { + "id": "#fabric:swords", + "required": false + }, + { + "id": "#c:weapons", + "required": false + }, { "id": "#c:axes", "required": false @@ -14,11 +22,11 @@ "required": false }, { - "id": "#c:shields", + "id": "#c:bows", "required": false }, { - "id": "#c:bows", + "id": "#fabric:bows", "required": false }, "#minecraft:swords",