#Dynamic Recipe Generation

27 messages · Page 1 of 1 (latest)

worldly prairie
#

Hello, I am trying to generate dynamically custom function. For better understanding of my achievement, please look below on this code with further explanation.

/**
     * @param {'modid:item'} input
     * @param {'modid:item'} optional_input
     * 
     * @param {'modid:item'} output
     * @param {'modid:item'} optional_output
     * 
     * @param {number} xp
     * @param {number} cooktime
     */
onEvent('recipes', event => {
    function UNSUPPORTED_MOD_MACHINE(input, optional_input1, output, optional_output, xp, cooktime) {
        event.custom({
            "type": "unsupported_mod_machine:crunch",
            "ingredient": {
                "item": input,
                "item": optional_input
            },
            "result": {
                "item": output,
                "item": optional_output
            },
            "experience": xp,
            "cookingtime": cooktime
               }
            ]
        })
    }
    UNSUPPORTED_MOD_MACHINE("minecraft:paprika", optional_input, "minecraft:piggybank", optional_output, 100, 200)
})

Hope this helps to clarify things a bit more.
Resulting behavior should be that if I do not put there optional_x, recipe just loads without it but if I write there some additional ingredients, it gets registered properly.

elder moonBOT
#

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

golden peak
#

errrr

#

theres a LOT of things wrong with that snippet

#

to start with the brackets dont match

#

you have duplicate keys in the same object

#

and you have to implement checks if some of the parameters youre passing are null or not

jovial willow
#

??editor

upper brookBOT
# jovial willow ??editor

If you're working with KubeJS scripts, configuration files, or similar things, we generally recommend using an actual IDE rather than just Notepad and its various siblings. In a lot of cases, it can help you with finding errors in your code faster and also gives you neat features like syntax highlighting, automatic formatting, et cetera! We recommend Visual Studio Code, since it's lightweight(-ish) and works very well with JavaScript files out-of-the-box.

jovial willow
#

also minecraft:paprika isn't an existing item

#

nor is minecraft:piggybank

golden peak
#

unless you create them yourself thonk

#

also is unsupported_mod_machine a mod..?

worldly prairie
#

Okay, further explanation:

- Brackets dont need to match, its example, sorry I was deleting rest of script so I keep there only improtant things for my question.
- Duplicate keys? Where? Whoops
- Non-Existant items and method and mod name and everything, is just *placeholder*
- I am using VS Code with plenty of extensions for manipulating .json, .js, .nbt, .mcmeta,...
  • How do I implement checks for null or passing thing? Thats probably what I am looking for...?
#

I will provide also example on something used. Yes I know there is create addon, this is for example 🙂

function CR_COMPACTING(input, input2, input3, output) {
        event.custom({
            "type": "create:compacting",
            "ingredients": [
                {
                    "item": input
                },
                {
                    "item": input2 // MAKE THIS OPTIONAL
                },
                {
                    "item": input3 // MAKE THIS OPTIONAL
                }
            ],
            "results": [
                {
                    "item": output
                }
            ]
        })
    }

Yes, one can say I do not need that, you know, because I just dont use it, but for sake of "function" it is needed as placeholder so if its needed I can just expand ingredient list without breaking stuff...
Also when I am at that, is there way to make basically global_script.js where I would have stored all my functions adn can access them from other files just for "add recipe" purposes?

jovial willow
#
let CR_COMPACTING = (input, input2, input3, output) => {
    ing = [Item.of(input).toJson()]
    if(input2 != null) ing.push(Item.of(input2).toJson())
    if(input3 != null) ing.push(Item.of(input3).toJson())
    event.custom({
        type: 'create:compacting',
        ingredients: ing,
        results: [Item.of(output).toJson()]
    })
}
golden peak
#

at that point you can just use an array as well thinking_lex

#

and replace the { "item": "" } with Item.of(<something>).toJson()

jovial willow
golden peak
#

yiz

#

can also be made to support item vs array input/output

#
let input = Array.isArray(inputs) ? inputs : [inputs]
jovial willow
#
let CR_COMPACTING = (input, output) => {
    event.custom({
        type: 'create:compacting',
        ingredients: Array.isArray(input) ? input.map(i => Ingredient.of(i)) : Ingredient.of(input),
        results: [Item.of(output).toJson()]
    })
}
worldly prairie
#

Now my brain is exploding. A lot.
So,...

let function_name = (inputs, outputs, xp, time) => {
event.custom({
type: 'create:compacting',
ingredients: Array.isArray(inputs) ? input.map(i => Ingredient.of(i) : Ingredient.of(input),
results: [Item.of(outputs).toJson()]})}

So, can I use it for even normal recipes and last question (sorry) is how am I suppose to then use / access function from different file?
Just use on top of file:
import net.minecraft.kubejs.functions.filename.js or how? 🙂
Then I can just do, lower in script after declaration of recipe adding method do:
function_name("minecraft:apple", "minecraft:apple", "minecraft:stick x2") = "minecraft:golden_apple x2) ? 🙂

golden peak
#

all JS files are loaded into one scope, so they are all "imported" into the same file in memory

worldly prairie
golden peak
#

yes