#AddJson() replacement

75 messages · Page 1 of 1 (latest)

neon aspen
#

AddJson() doesn't exist for ServerEvents.loaded, is there a replacement?

lucid spireBOT
#

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

sage cloud
#

ummm how did you use it

neon aspen
#
/**
 * Removes advancement at given file-path and adds it to a hidden parent advancement
 * @param {String | Array} advancementFilePath - Where the advancement file is located. Can easily be found through /advancement command or GitHub
 */
function removeAdvancement(advancementFilePath) {
    let arr = []
    if (typeof (advancementFilePath) == 'string') arr = [advancementFilePath]
    else if (typeof (advancementFilePath) == 'object') arr = advancementFilePath
    else console.log(`Invalid type for removeAdvancement(${advancementFilePath})`)

    ServerEvents.loaded(event => {
        arr.forEach(advancement => {
            event.addJson(`${advancement}.json`, {
                parent: 'minecraft:advancements/root',
                display: { hidden: true },
                criteria: {
                    impossible: {
                        trigger: 'minecraft:impossible'
                    }
                },
                requirements: [['impossible']]
            })
        })
    })
}

removeAdvancement([
    'create:advancements/root',
    'create:root',
    'create:advancements/andesite_alloy'
])

My current full code, i assumed AddJson just doesn't exist for that event or something

#

ingame error

raw scaffold
#

you want the datapack event, not server loaded

#

??kjswiki

cinder flowerBOT
sage cloud
#

i was about to say that cri_lex

#

im also surprised the event is picked up inside the top level function hmmm

#

makes me think about redoing scripts thinking_lex

raw scaffold
#

events are just functions themseleves
kinda

neon aspen
#

in hindsight i should have seen that, looking at the code i thought it was more so that the event fired when the server started but its not that weird that i can't edit datapack stuff without using datapack events

sage cloud
#

yeah but i thought the top function is being executed when the script is loaded, which in most cases is way before the event fires

#

but i guess it just caches the event somewhere and runs it later

raw scaffold
#

the function is run immediately

#

at the bottom

sage cloud
#

ye thats what im saying

raw scaffold
#

kubejs executes the scrilt once when its loadef

sage cloud
#

ye

neon aspen
#

well, guess thats another issue solved within seconds of posting it. next time i should probably triple check everything, should i close the thread or wait until you're done talking lol

sage cloud
#

but the event isnt a thing until way after the top function is executed

raw scaffold
#

we can always close it again

raw scaffold
sage cloud
#

which is instant

#

not when the event fires

#

i would write a debug script but im at work and i cant

raw scaffold
#

its like how

function foo() {
  console.log('hi')
}

foo()

still logs hi immediately
the event handler is still 'caught' cause it was added when the script was loaded and run for the first time

sage cloud
#

ye but i dont think you understand what im saying thinking_lex

#

what if you have the recipes event

#

or item rightclick event

#

neither of those are run anytime near when the top function is

#

hmm i just had a thought

raw scaffold
#

you pass a function (the event => {} bit ) to an event (ie ServerEvents.loaded), that function is what gets stored to run later

sage cloud
#

whats this gonna do? can you test it?

let test = () => {  
  console.log(1)
  ServerEvents.loaded(_ => console.log(2))
  console.log(3)
}

test()
raw scaffold
#

that wont do anything

sage cloud
#

w0t

raw scaffold
#

cause you never call the test function

sage cloud
#

well i left that part out...

#

thought its self explanatory xD

raw scaffold
#

it would log
1
3
2

sage cloud
#

so it does cache it for later

raw scaffold
#

yes, it stores the function you pass to the event to run for later

sage cloud
#

hmmmm

#

sec

raw scaffold
#

the _ => console.log(2) bit

sage cloud
#
let test = () => {
  let list = [1]
  console.log(list)
  ServerEvents.loaded(_ => {
    list.push(2)
    console.log(list)
  })
  list.push(3)
  console.log(list)
}

would that print

[1]
[1, 3]
[1, 2]
raw scaffold
#

not quite
the last one would be```js
[1, 3, 2]

sage cloud
#

so its not a copy of the event, its just a reference

#

*event derpy_cat_lex

raw scaffold
#

yes. the function stores its context which includes all of the variables currently in scope

sage cloud
#

cool cool PinkYAYA

raw scaffold
#

theoretically you may even be able to call test() from the event

sage cloud
#

Джава always confuses me sometimes with copies and references lol

sage cloud
raw scaffold
#

no

sage cloud
#

wha-

raw scaffold
#

because you cant add event handlers after the initial load

sage cloud
#

oh

raw scaffold
#

and the loaded event would only be called once anyway

sage cloud
#

so it would just run everything except the event part inside it

raw scaffold
#

yeah

sage cloud
#

right

raw scaffold
#

actually it might errror there

#

and not run anything after it

#

im not sure

sage cloud
#

mmm doubt it

#

i wish i could test NotLikeThis

#

get me out of here

raw scaffold
#

as you wish