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

Implement 1.20.5+ item slots #1179

Merged
merged 15 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions packages/java-edition/src/mcfunction/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@spyglassmc/core'
import { ReleaseVersion } from '../../dependency'

export const ColorArgumentValues = [...core.Color.ColorNames, 'reset']

Expand All @@ -11,24 +12,34 @@ export const GamemodeArgumentValues = [
'spectator',
]

export const ItemSlotArgumentValues = [
...[...Array(54).keys()].map((n) => `container.${n}`),
...[...Array(27).keys()].map((n) => `enderchest.${n}`),
...[...Array(15).keys()].map((n) => `horse.${n}`),
...[...Array(9).keys()].map((n) => `hotbar.${n}`),
...[...Array(27).keys()].map((n) => `inventory.${n}`),
...[...Array(8).keys()].map((n) => `villager.${n}`),
'armor.chest',
'armor.feet',
'armor.head',
'armor.legs',
'horse.armor',
'horse.chest',
'horse.saddle',
'weapon',
'weapon.mainhand',
'weapon.offhand',
]
export function getItemSlotArgumentValues(ctx: core.ContextBase) {
const release = ctx.project['loadedVersion'] as ReleaseVersion
const output = [
...[...Array(54).keys()].map((n) => `container.${n}`),
...[...Array(27).keys()].map((n) => `enderchest.${n}`),
...[...Array(15).keys()].map((n) => `horse.${n}`),
...[...Array(9).keys()].map((n) => `hotbar.${n}`),
...[...Array(27).keys()].map((n) => `inventory.${n}`),
...[...Array(8).keys()].map((n) => `villager.${n}`),
'armor.chest',
'armor.feet',
'armor.head',
'armor.legs',
'horse.chest',
'horse.saddle',
'weapon',
'weapon.mainhand',
'weapon.offhand',
]
if (ReleaseVersion.cmp(release, '1.20.5') >= 0) {
output.push(...[...Array(4).keys()].map((n) => `player.crafting.${n}`),
'armor.body',
'contents',
'player.cursor')
}
else { output.push('horse.armor') }
misode marked this conversation as resolved.
Show resolved Hide resolved
return output
}

export const OperationArgumentValues = [
'=',
Expand Down
9 changes: 6 additions & 3 deletions packages/java-edition/src/mcfunction/completer/argument.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
Arrayable,
Completer,
CompleterContext,
MetaRegistry,
RegistryCategory,
WorldgenFileCategory,
Expand All @@ -10,6 +11,7 @@ import {
BooleanNode,
BrigadierStringOptions,
completer,
ContextBase,
CompletionItem,
CompletionKind,
FloatNode,
Expand All @@ -32,7 +34,7 @@ import {
EntityAnchorArgumentValues,
GamemodeArgumentValues,
HeightmapValues,
ItemSlotArgumentValues,
getItemSlotArgumentValues,
MirrorValues,
OperationArgumentValues,
RotationValues,
Expand All @@ -58,8 +60,9 @@ import type { ArgumentTreeNode } from '../tree/index.js'

export const getMockNodes: mcf.completer.MockNodesGetter = (
rawTreeNode,
range,
ctx: CompleterContext
): Arrayable<AstNode> => {
const range = ctx.offset
const treeNode = rawTreeNode as ArgumentTreeNode

switch (treeNode.parser) {
Expand Down Expand Up @@ -118,7 +121,7 @@ export const getMockNodes: mcf.completer.MockNodesGetter = (
case 'minecraft:item_predicate':
return ItemNode.mock(range, true)
case 'minecraft:item_slot':
return LiteralNode.mock(range, { pool: ItemSlotArgumentValues })
return LiteralNode.mock(range, { pool: getItemSlotArgumentValues(ctx) })
case 'minecraft:item_stack':
return ItemNode.mock(range, false)
case 'minecraft:mob_effect':
Expand Down
5 changes: 3 additions & 2 deletions packages/java-edition/src/mcfunction/parser/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
EntityAnchorArgumentValues,
GamemodeArgumentValues,
HeightmapValues,
ItemSlotArgumentValues,
getItemSlotArgumentValues,
MirrorValues,
OperationArgumentValues,
RotationValues,
Expand Down Expand Up @@ -87,6 +87,7 @@ function shouldValidateLength(ctx: core.ParserContext) {
*/
export const argument: mcf.ArgumentParserGetter = (
rawTreeNode,
ctx: core.ContextBase
): core.Parser | undefined => {
misode marked this conversation as resolved.
Show resolved Hide resolved
const treeNode = rawTreeNode as ArgumentTreeNode

Expand Down Expand Up @@ -189,7 +190,7 @@ export const argument: mcf.ArgumentParserGetter = (
case 'minecraft:item_predicate':
return wrap(itemPredicate)
case 'minecraft:item_slot':
return wrap(core.literal(...ItemSlotArgumentValues))
return wrap(core.literal(...getItemSlotArgumentValues(ctx)))
Nico314159 marked this conversation as resolved.
Show resolved Hide resolved
case 'minecraft:item_stack':
return wrap(itemStack)
case 'minecraft:message':
Expand Down
7 changes: 5 additions & 2 deletions packages/java-edition/test/mcfunction/parser/argument.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
snapshotWithUri,
testParser,
} from '@spyglassmc/core/test-out/utils.js'
import { ContextBase } from '@spyglassmc/core'
import { argument } from '@spyglassmc/java-edition/lib/mcfunction/parser/index.js'
import type { ArgumentTreeNode } from '@spyglassmc/java-edition/lib/mcfunction/tree/index.js'
import * as json from '@spyglassmc/json'
Expand Down Expand Up @@ -152,7 +153,7 @@ const Suites: Partial<
content: ['stick', 'minecraft:stick', '#stick', '#stick{foo:bar}'],
},
],
'minecraft:item_slot': [{ content: ['container.5', 'weapon'] }],
'minecraft:item_slot': [{ content: ['container.5', 'weapon', 'armor.body'] }],
'minecraft:item_stack': [
{
content: ['stick', 'minecraft:stick', 'stick{foo:bar}'],
Expand Down Expand Up @@ -296,6 +297,7 @@ nbt.initialize(project)
const { meta } = project

describe('mcfunction argument parser', () => {
const ctx = ContextBase.create(project)
for (const [parserName, cases] of Object.entries(Suites)) {
describe(parserName, () => {
for (const { content, properties } of cases) {
Expand All @@ -308,6 +310,7 @@ describe('mcfunction argument parser', () => {
const itTitle = `Parse "${showWhitespaceGlyph(string)}"${
properties ? ` with ${JSON.stringify(properties)}` : ''
}`

it(itTitle, () => {
snapshotWithUri({
specName: `mcfunction argument ${parserName} ${itTitle}`,
Expand All @@ -318,7 +321,7 @@ describe('mcfunction argument parser', () => {
}.spec.js`,
import.meta.url,
),
value: testParser(argument(treeNode)!, string, {
value: testParser(argument(treeNode, ctx)!, string, {
project: { meta },
removeTopLevelChildren: RemoveExtraChildren.has(
parserName,
Expand Down
4 changes: 2 additions & 2 deletions packages/mcfunction/src/completer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {

export type MockNodesGetter = (
treeNode: ArgumentTreeNode,
range: core.RangeLike,
range: core.CompleterContext,
) => core.Arrayable<core.AstNode>

/**
Expand Down Expand Up @@ -79,7 +79,7 @@ export function command(
})
),
...argumentTreeNodes.flatMap(([_name, treeNode]) =>
core.Arrayable.toArray(getMockNodes(treeNode, ctx.offset)).flatMap(
core.Arrayable.toArray(getMockNodes(treeNode, ctx)).flatMap(
(n) => core.completer.dispatch(n, ctx),
)
),
Expand Down
1 change: 1 addition & 0 deletions packages/mcfunction/src/parser/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { ArgumentTreeNode } from '../tree/index.js'
*/
export type ArgumentParserGetter = (
treeNode: ArgumentTreeNode,
ctx: core.ContextBase,
) => core.Parser | undefined

export function argumentTreeNodeToString(
Expand Down
2 changes: 1 addition & 1 deletion packages/mcfunction/src/parser/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function dispatch(
const argumentParsers: { name: string; parser: core.Parser }[] =
argumentTreeNodes.map(([name, treeNode]) => ({
name,
parser: argument(treeNode) ?? unknown(treeNode),
parser: argument(treeNode, ctx) ?? unknown(treeNode),
}))
const literalParser = literalTreeNodes.length
? literal(
Expand Down
Loading