Solved Original AttributeModifiers disappearing with Custom AttributeModifiers

Discussion in 'Plugin Development' started by BaddCamden, Mar 20, 2025.

  1. Offline

    BaddCamden

    So I've run into an issue I just can't squash. I am trying to ADD attack damage and armor defense however the attributes I apply replace the existing Minecraft's attributes.
    If I ADD 1 damage to a golden pickaxe, it goes from 2 damage to 1. Why.
    I took the NBT route however that has been unsuccessful thanks to the troublesome obfuscation. FYI MiniMappingViewer is SUPREMELY helpful with that.
    I just want to be able to do either of these 2 things:
    1. Add attack or defense without replacing the old values
    2. Get the old attack or defense values.

    There is 1 stipulation, I am not going to hardcode defense or attack damage values for individual materials since there are many cases where they can change and it would be pretty annoying.

    Here is an excerpt from a method I am working this in:
    Code:
                if (isArmor) {
                    EquipmentSlot slot;
                    if (equipmentType.equalsIgnoreCase("Helmet")) {
                        slot = EquipmentSlot.HEAD;
                    } else if (equipmentType.equalsIgnoreCase("Chestplate")) {
                        slot = EquipmentSlot.CHEST;
                    } else if (equipmentType.equalsIgnoreCase("Leggings")) {
                        slot = EquipmentSlot.LEGS;
                    } else {
                        slot = EquipmentSlot.FEET;
                    }
                    double baseArmor = 0.0;
                    if (meta.hasAttributeModifiers()) {
                        for (AttributeModifier mod : meta.getAttributeModifiers(Attribute.GENERIC_ARMOR)) {
                            baseArmor += mod.getAmount();
                        }
                    }
                    double finalArmorBonus = baseArmor + bonus;
                    meta.addAttributeModifier(Attribute.GENERIC_ARMOR,
                            new AttributeModifier(UUID.randomUUID(), "MonsterHordesArmorBonus", finalArmorBonus,
                                                  AttributeModifier.Operation.ADD_NUMBER, slot));
                } else {
                    double baseDamage = 0.0;
                    if (meta.hasAttributeModifiers()) {
                        for (AttributeModifier mod : meta.getAttributeModifiers(Attribute.GENERIC_ATTACK_DAMAGE)) {
                            baseDamage += mod.getAmount();
                        }
                    }
                    double finalDamageBonus = baseDamage + bonus;
                    meta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE,
                            new AttributeModifier(UUID.randomUUID(), "MonsterHordesAttackBonus", finalDamageBonus,
                                                  AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND));
                }
    
    
    
     
    fairkredit.financing likes this.
  2. Offline

    BaddCamden

    Well I found the answer. It's not a satisfying one but it works.
    Code:
                Item i = nmsItem.g();
                DataComponentMap m = i.p();
                ItemAttributeModifiers modifiers = m.a(DataComponents.n); Item Attributes according to DataComponents
                modifiers = modifiers.a(true);
                for(b mod : modifiers.b()) { //Damage and Attack Speed
                   
                    Attribute a = CraftAttribute.minecraftHolderToBukkit(mod.a());
                    AttributeModifier b = CraftAttributeInstance.convert(mod.b());
                    meta.addAttributeModifier(a, b);
                }
                for(b mod : i.j().b()) { //LITERALLY EVERYTHING ELSE or Item Attributes according ot AttributeModifiers.
                   
                    Attribute a = CraftAttribute.minecraftHolderToBukkit(mod.a());
                    AttributeModifier b = CraftAttributeInstance.convert(mod.b());
                    meta.addAttributeModifier(a, b);
                }
    
    
    Why. WHY.

    The difference is that the components are seperate. I don't know how this makes a difference behind the scenes but practically, the difference is that Attack Damage and Attack Speed have green tooltips and everything else had blue tooltips. There should be some kind of boolean that configures whether you want the custom AttributeModifiers to erase the existing components instead of doing this.
     

Share This Page