EnigmaticaModpacks / Enigmatica6

The official repository and issue-tracker for Enigmatica 6
https://www.curseforge.com/minecraft/modpacks/enigmatica6
240 stars 528 forks source link

use length check to handle `with` in JEI Description #5429

Closed ZZZank closed 7 months ago

ZZZank commented 7 months ago

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

NielsPilgaard commented 7 months ago

Hmm, what seems to throw an exception, do you have any examples?

ZZZank commented 7 months ago

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 .

NielsPilgaard commented 7 months ago

What about text?.with[i]? that'll return undefined instead of throwing, if with[i] isn't there

ZZZank commented 7 months ago

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.

NielsPilgaard commented 7 months ago

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.

ZZZank commented 7 months ago

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.

NielsPilgaard commented 7 months ago

Text.with[i] can try to access an element at an index that the array doesnt even have, which throws an exception.