#Script doesn't work correctly on world load, but does on /reload

6 messages · Page 1 of 1 (latest)

novel jungle
#

Startup script:

onEvent('item.registry', event => {
    event.create("dust_zinc").displayName("Zinc Grit").texture("kubejs:item/metal_dust_zinc")
})

Server script:

// Shamelessly copied from stackoverflow
function getMatches(string, regex, index) {
    index || (index = 1)
    let matches = []
    let match
    while (match = regex.exec(string)) {
        matches.push(match[index])
    }
    return matches
}

onEvent("recipes", event => {
    Ingredient.of("#create:crushed_ores").stacks.forEach(itemStack => {
        console.info("ITEMSTACK: " + itemStack)
        itemStack.getTags().forEach(tag => {
            console.info("TAG: " + tag)
            let matches = getMatches(tag, /^create:crushed_ores\/(.+)$/g)
            if (matches.length) {
                console.info("MATCHES: " + matches[0])
                if (Item.of(`#forge:dusts/${matches[0]}`).isEmpty()) {
                    console.info(`ERROR: #forge:dusts/${matches[0]} does not exist or is empty`)
                    return
                }
                event.recipes.immersiveengineeringCrusher(Item.of(`#forge:dusts/${matches[0]}`), Ingredient.of(`#create:crushed_ores/${matches[0]}`))
                event.recipes.immersiveengineeringArcFurnace([Item.of(`#forge:ingots/${matches[0]}`)], Ingredient.of(`#create:crushed_ores/${matches[0]}`))
            }
        })
    })
})

onEvent("item.tags", event => {
    event.get("forge:dusts").add("kubejs:dust_zinc")
    event.get("forge:dusts/zinc").add("kubejs:dust_zinc")

    event.get("create:crushed_ores").getObjectIds().forEach(objectId => {
        console.info("OBJECTID: " + objectId)
        let matches = getMatches(objectId, /^create:crushed_(.+)_ore$/g)
        if (matches.length) {
            console.info("MATCHES: " + matches[0])
            event.get(`create:crushed_ores/${matches[0]}`).add(`create:crushed_${matches[0]}_ore`)
        }
    })
})

The getTags() function seems to return nothing on world load. Why come?

fluid dawn
#

because tags are loaded later than recipes for some mojank reason

lofty pagoda
#

this script scares me, cuz it can be shortened to only a couple lines lol

novel jungle
lofty pagoda
#

i rewrote it with what i think you want to do?
the whole script is incredibly convoluted and overcomplicated

onEvent('recipes', e => {
  Ingredient.of('#create:crushed_ores').itemIds.forEach(id => {
    let ore = /^create:crushed_(.+)_ore$/.exec(id)
    if (Item.of(`#forge:dusts/${ore}`).isEmpty()) return
    e.recipes.immersiveengineeringCrusher(`#forge:dusts/${ore}`, `#create:crushed_ores/${ore}`)
    e.recipes.immersiveengineeringArcFurnace(`#forge:ingots/${ore}`, `#create:crushed_ores/${ore}`)
  })
})

onEvent('item.tags', e => {
  e.add('forge:dusts', 'kubejs:dust_zinc')
  e.add('forge:dusts/zinc', 'kubejs:dust_zinc')

  Ingredient.of('#create:crushed_ores').itemIds.forEach(id => {
    let ore = /^create:crushed_(.+)_ore$/.exec(id)
    e.add(`create:crushed_ores/${ore}`, `create:crushed_${ore}_ore`)
  })
})
novel jungle
#

Thank for the reply, that is indeed what I wanted to do. While doing a little soul searching I came up with something a bit more compact, but yours with the exec(id) and itemIds parts (neither of which I knew of) really chopped it down.