Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use length check to handle with in JEI Description #5429

Merged

Conversation

ZZZank
Copy link
Contributor

@ZZZank ZZZank commented Nov 28, 2023

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 ZZZank@2117038

JavaScript's variable accessing is buggier than my expection
@NielsPilgaard
Copy link
Collaborator

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

@ZZZank
Copy link
Contributor Author

ZZZank commented Nov 29, 2023

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
Copy link
Collaborator

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

@ZZZank
Copy link
Contributor Author

ZZZank commented Nov 29, 2023

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
Copy link
Collaborator

NielsPilgaard commented Nov 29, 2023

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.

@@ -491,9 +491,9 @@ onEvent('jei.information', (event) => {

recipes.forEach((recipe) => {
for (let i = 0; i < recipe.text.length; i++) {
if (recipe.with[i]) {
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about doing something like this:

if (Object.keys(recipe).includes("with")) {
    recipe.text[i] = Text.translate(recipe.text[i], recipe.with[i]);
} else {
    recipe.text[i] = Text.translate(recipe.text[i]);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue is that the recipe.text and recipe.with arrays can be different lengths, checking whether recipe.with exists is simply if (recipe.with){...}

Copy link
Contributor

@Kanzaji Kanzaji Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah alrighty 😄 Then you can do technically recipe.with && recipe.with.lenght > i?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly 👍

Co-authored-by: Niels Pilgaard Grøndahl <niels.pilgaard@hotmail.com>
@ZZZank
Copy link
Contributor Author

ZZZank commented Nov 30, 2023

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
Copy link
Collaborator

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

@NielsPilgaard NielsPilgaard merged commit 041446f into EnigmaticaModpacks:develop Nov 30, 2023
5 of 7 checks passed
@ZZZank ZZZank deleted the simple_decription_fix branch December 1, 2023 01:15
@ZZZank ZZZank changed the title use try-catch to handle with in JEI Description use length check to handle with in JEI Description Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants