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 all 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
2 changes: 1 addition & 1 deletion packages/core/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function mockProjectData(data: Partial<ProjectData> = {}): ProjectData {
return {
cacheRoot,
config: data.config ?? VanillaConfig,
ctx: data.ctx ?? {},
ctx: data.ctx ?? { loadedVersion: '1.15' },
downloader,
ensureBindingStarted: data.ensureBindingStarted!,
externals,
Expand Down
50 changes: 32 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/index.js'

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

Expand All @@ -11,24 +12,37 @@ 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')
}
return output
}

export const OperationArgumentValues = [
'=',
Expand Down
10 changes: 7 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 Down Expand Up @@ -31,8 +32,8 @@ import {
ColorArgumentValues,
EntityAnchorArgumentValues,
GamemodeArgumentValues,
getItemSlotArgumentValues,
HeightmapValues,
ItemSlotArgumentValues,
MirrorValues,
OperationArgumentValues,
RotationValues,
Expand All @@ -58,8 +59,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 +120,9 @@ 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
6 changes: 4 additions & 2 deletions packages/java-edition/src/mcfunction/parser/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
ColorArgumentValues,
EntityAnchorArgumentValues,
GamemodeArgumentValues,
getItemSlotArgumentValues,
HeightmapValues,
ItemSlotArgumentValues,
MirrorValues,
OperationArgumentValues,
RotationValues,
Expand Down Expand Up @@ -189,7 +189,9 @@ export const argument: mcf.ArgumentParserGetter = (
case 'minecraft:item_predicate':
return wrap(itemPredicate)
case 'minecraft:item_slot':
return wrap(core.literal(...ItemSlotArgumentValues))
return wrap((src, ctx) => {
return core.literal(...getItemSlotArgumentValues(ctx))(src, ctx)
})
case 'minecraft:item_stack':
return wrap(itemStack)
case 'minecraft:message':
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
Loading