#add nbt to items on startup

8 messages · Page 1 of 1 (latest)

solid oyster
#

I would like to add an attribute modifier to items on startup, so thats I can do it once instead of for all output recipes. How do I add nbt tags to items through startup modification?
$aceplanteissue

severe spireBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

solid oyster
#

    let applyArmorSetNBT = (base, values) => {
        let pieces = [
            { suffix: '_helmet', slot: 'head', key: 'helmet' },
            { suffix: '_chestplate', slot: 'chest', key: 'chestplate' },
            { suffix: '_leggings', slot: 'legs', key: 'leggings' },
            { suffix: '_boots', slot: 'feet', key: 'boots' }
        ]
        pieces.forEach(p => {
            let slow = values[p.key]
            let armor = base + p.suffix
            let speed = -0.01 * slow
            let nbt = `{AttributeModifiers:[{
                AttributeName:"generic.movement_speed",Name:"speed_penalty",Operation:1,UUID:[I;-126220,10241,172043,-20482],
                Amount:${speed},
                Slot:"${p.slot}"
            }]}`
            event.replaceOutput({ output: armor },armor,Item.of(armor, nbt))
        })
    }

    applyArmorSetNBT('minecraft:leather', {
        helmet: 2,
        chestplate: 3,
        leggings: 3,
        boots: 2
    })

    applyArmorSetNBT('tconstruct:travelers', {
        helmet: 1,
        chestplate: 4,
        leggings: 3,
        boots: 3
    })
#

For anyone wondering, I was able to get this working, but specifically it only works on crafting recipes. armor output by trading for example cannot be edited in this way. Which is why i would like to edit the properties of the base item on startup for all output sources

#

nevermind, it does not in fact work with travelers armor. Anyone know why? It seems to be a crafting recipe in game, so im not sure why I cant edit it this way

solid oyster
#

so i got this, which seems like it has all the proper nbt, but when equipped nothing works, not even the armor bars. Its as if it loses all properties even though the nbt is all there

PlayerEvents.tick(event => {

    let player = event.player

    let check = (slot, slow) => {

        let stack = player.getItemBySlot(slot)

        if (stack.id.startsWith('tconstruct:')) {

            let nbt = stack.nbt ?? {}

            if (!nbt.kubejs_speed_applied) {

                let speed = -0.01 * slow

                nbt.kubejs_speed_applied = 1

                nbt.AttributeModifiers = [{
                    AttributeName: "generic.movement_speed",
                    Name: "speed_penalty",
                    Amount: speed,
                    Slot: slot,
                    Operation: 1,
                    UUID: [-126220,10241,172043,-20482]
                }]

                stack.setNbt(nbt)
                player.setItemSlot(slot, stack)
            }
        }
    }

    check('head', 2)
    check('chest', 5)
    check('legs', 4)
    check('feet', 2)

})
solid oyster
#

I found a solution. i was able to add the modifiers via a datapack, and it now applies to tinkers items with no nbt issues!

The only files i needed were:
tinkering>modifiers>weight.json (copied from existing heavy modifier)
tinkering>tool_definitions>travelers_chestplate.json (added a traits section, see example on the boots)

#

but this question still remains valid for other situations such as villager trading or other mod's custom crafting