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

✨ Add support for item components #1131

Merged
merged 28 commits into from
Jun 15, 2024
Merged

Conversation

Trivaxy
Copy link
Contributor

@Trivaxy Trivaxy commented May 13, 2024

This PR lays the foundation for supporting the new Item Component format Mojang introduced in 1.20.5.

There is still work to be done - for now, this PR is a draft but almost ready for merging.

Done

  • Implement parsing for item components, e.g. give @s cooked_beef[food={nutrition:5,saturation:2}]
  • Validate and throw errors when duplicate components are defined, e.g. give @s stick[food=...,food=...]
  • Implement support for the new item predicate format, used in execute if|unless and clear

What's left (outside this PR's scope)

  • Validate the values given to components. This is waiting on mcdoc (will be done in a separate PR)
  • Validate JSON files which use item components. This is waiting on mcdoc (will be done in a separate PR)

@Trivaxy Trivaxy requested a review from SPGoding as a code owner May 13, 2024 12:51
@Trivaxy Trivaxy removed the request for review from SPGoding May 13, 2024 12:53
@Trivaxy Trivaxy self-assigned this May 13, 2024
@Trivaxy Trivaxy requested review from SPGoding, misode and MulverineX May 13, 2024 12:56
@TheAfroOfDoom
Copy link
Contributor

[ ] Validate the values given to components. This is waiting on mcdoc (will be done in a separate PR)
[ ] Validate JSON files which use item components. This is waiting on mcdoc (will be done in a separate PR)

these could be spun into separate tickets maybe? or at least once this PR is merged we should update the original ticket #1118 with the progress of item components and what needs to still be done, etc.

@MulverineX MulverineX changed the title ✨Add support for item components ✨ Add support for item components May 13, 2024
@Trivaxy Trivaxy force-pushed the item-components branch from 2abf973 to 0b3af75 Compare May 17, 2024 14:28
Copy link
Member

@SPGoding SPGoding left a comment

Choose a reason for hiding this comment

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

Can you merge the main branch in and add some tests in packages/java-edition/test/mcfunction/parser/argument.spec.ts to document the parse tree? You might need to add some logic in the test file so that both the old item parser and the new item parser can be tested.

packages/java-edition/src/mcfunction/node/argument.ts Outdated Show resolved Hide resolved
packages/locales/src/locales/en.json Outdated Show resolved Hide resolved
Comment on lines 198 to 206
if (components.length > 1) {
components.forEach(component => {
ctx.err.report(
localize('duplicate-components', componentName),
component.key!.range,
core.ErrorSeverity.Error,
)
})
}
Copy link
Member

Choose a reason for hiding this comment

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

You can use core.LanguageErrorInfo.related to report only one error for this with the duplicated keys' locations added in the info.related field (core.Location.create() would be helpful to create the entries for the field)

Copy link
Member

Choose a reason for hiding this comment

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

Vscode also creates an error for each duplicate key in a json file for example, imo this is the most clean

packages/java-edition/src/mcfunction/node/argument.ts Outdated Show resolved Hide resolved
packages/java-edition/src/mcfunction/node/argument.ts Outdated Show resolved Hide resolved
packages/java-edition/src/mcfunction/node/argument.ts Outdated Show resolved Hide resolved
packages/java-edition/src/mcfunction/parser/argument.ts Outdated Show resolved Hide resolved
packages/java-edition/src/mcfunction/parser/argument.ts Outdated Show resolved Hide resolved
Comment on lines 1889 to 1891
sep: [',', '|'],
end: ']',
trailingSep: true,
Copy link
Member

Choose a reason for hiding this comment

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

This would allow trailing |'s, although upon my testing trailing commas aren't allowed by the game in item predicates (but they are allowed in non predicate), so just removing trailingSep: true probably works (would be nice if a comment could be added there to explain this). The parse tree also won't reflect the real hierarchy of the tests but I guess that doesn't matter much for us.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The parse tree you see barely functions as is, unfortunately. There's (supposed to) be a push to fix it when I have the time

packages/java-edition/src/mcfunction/parser/argument.ts Outdated Show resolved Hide resolved
@Trivaxy
Copy link
Contributor Author

Trivaxy commented May 23, 2024

Can you merge the main branch in and add some tests in packages/java-edition/test/mcfunction/parser/argument.spec.ts to document the parse tree? You might need to add some logic in the test file so that both the old item parser and the new item parser can be tested.

Sure, once the parser is fixed and the PR is ready to incorporate all the reviews

@MulverineX MulverineX self-requested a review May 23, 2024 21:58
@NeunEinser NeunEinser self-requested a review May 26, 2024 20:38
@misode
Copy link
Member

misode commented May 30, 2024

I did some refactoring. It is mostly done now apart from support for | and the completer for item predicate components. Also still needs tests, but the AST is likely to change when supporting |.

@misode misode linked an issue May 31, 2024 that may be closed by this pull request
@Trivaxy
Copy link
Contributor Author

Trivaxy commented Jun 15, 2024

The PR should be ready for final reviews now

range: core.Range.get(range),
children: [id],
id,
}
}
}

export interface ComponentTestsNode extends core.AstNode {
type: 'mcfunction:component_tests'
children: ComponentTestsAnyOfNode[]
Copy link
Member

Choose a reason for hiding this comment

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

I think there is a layer of children too much here. This children field will always have exactly zero or one child. Maybe the best way to solve this is by getting rid of this node, and using ComponentTestsAnyOfNode in ItemPredicateNode

Copy link
Contributor Author

@Trivaxy Trivaxy Jun 15, 2024

Choose a reason for hiding this comment

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

Originally ComponentTestsAnyOfNode was being used directly, but IMO encapsulating it in a ComponentTestsNode is more readable, and potentially less prone to breakage if the representation for the AST changes

Copy link
Member

Choose a reason for hiding this comment

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

I disagree with that. We don't need unnecessary wrapper nodes. It also makes things like checkers and completers more complex for no reason.

Copy link
Contributor Author

@Trivaxy Trivaxy Jun 15, 2024

Choose a reason for hiding this comment

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

Hmm I'm not so sure. My main concern is future-proofing, since having things refer to ComponentTestsNode seems better (and more understandable at a glance to new devs) than having to change all references from ComponentTestsAnyOfNode to MaybeSomethingMoreComplicatedNode[] if that ever happens. I doubt it's a big deal in either case so I'm OK with the change.

packages/java-edition/src/mcfunction/parser/argument.ts Outdated Show resolved Hide resolved
packages/java-edition/src/mcfunction/parser/argument.ts Outdated Show resolved Hide resolved
@misode misode merged commit 005f26d into SpyglassMC:main Jun 15, 2024
3 checks passed
@Trivaxy Trivaxy deleted the item-components branch June 17, 2024 06:37
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.

Item Component Support
6 participants