Closed ZZZank closed 7 months ago
Hmm, what seems to throw an exception, do you have any examples?
The error happens within text.with[i]
.
When the with
is not defined, accessing it will gets an undefined
, so text.with[i]
becomes accessing the i-th element of undefined, which will causes an Exception to be thrown.
Originally I tried text.with && text.with[i]
to fix this. But on my side the problem is not fixed by doing this. So finally I switched to try-catch
.
What about text?.with[i]
? that'll return undefined instead of throwing, if with[i] isn't there
What about
text?.with[i]
?
KubeJS seems cannot recognize this syntax. The log says:
Error loading KubeJS script: syntax error
I did some researches, and found that ?.
is added in ES2020
standard. But KubeJS is using ES2015
standard. Maybe that's why they are not recognizing it.
Aw damn. You could do a check on the length of text.with to see if the entry i is there.
I'd prefer not to use try catch for a preventable issue.
recipe.with && recipe.with.length > i
does work, switched!
But I'm still confused. In theory, text.with && text.with[i]
should also be effective, because text.with[i]
can return undefined when i-th element is not found, but in fact it will cause errors on my side. Strange.
Text.with[i] can try to access an element at an index that the array doesnt even have, which throws an exception.
JavaScript's variable accessing is trickier than my expection. Errors will be reported by KubeJS at seemly completely random time. So, instead of painstakingly debugging(I've done so, really painstaking), let's just use
try-catch
.Ported from https://github.com/ZZZank/Enlightened6/commit/21170382c9f805a5359eb4f3d4718ac0a5aa98a3