From 2b74e1ff633f56db439c6084d1bb72c16f536a88 Mon Sep 17 00:00:00 2001 From: Nico314159 Date: Wed, 22 May 2024 13:04:25 -0700 Subject: [PATCH] Implement 1.20.5+ item slots (#1179) --- packages/core/test/utils.ts | 2 +- .../src/mcfunction/common/index.ts | 50 ++++++++++++------- .../src/mcfunction/completer/argument.ts | 10 ++-- .../src/mcfunction/parser/argument.ts | 6 ++- packages/mcfunction/src/completer/index.ts | 4 +- 5 files changed, 46 insertions(+), 26 deletions(-) diff --git a/packages/core/test/utils.ts b/packages/core/test/utils.ts index d3149cf62..65abc82f5 100644 --- a/packages/core/test/utils.ts +++ b/packages/core/test/utils.ts @@ -58,7 +58,7 @@ export function mockProjectData(data: Partial = {}): ProjectData { return { cacheRoot, config: data.config ?? VanillaConfig, - ctx: data.ctx ?? {}, + ctx: data.ctx ?? { loadedVersion: '1.15' }, downloader, ensureBindingStarted: data.ensureBindingStarted!, externals, diff --git a/packages/java-edition/src/mcfunction/common/index.ts b/packages/java-edition/src/mcfunction/common/index.ts index a63852daf..ab0df9f87 100644 --- a/packages/java-edition/src/mcfunction/common/index.ts +++ b/packages/java-edition/src/mcfunction/common/index.ts @@ -1,4 +1,5 @@ import * as core from '@spyglassmc/core' +import { ReleaseVersion } from '../../dependency/index.js' export const ColorArgumentValues = [...core.Color.ColorNames, 'reset'] @@ -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 = [ '=', diff --git a/packages/java-edition/src/mcfunction/completer/argument.ts b/packages/java-edition/src/mcfunction/completer/argument.ts index af92cc3b7..2e06ec5eb 100644 --- a/packages/java-edition/src/mcfunction/completer/argument.ts +++ b/packages/java-edition/src/mcfunction/completer/argument.ts @@ -1,6 +1,7 @@ import type { Arrayable, Completer, + CompleterContext, MetaRegistry, RegistryCategory, WorldgenFileCategory, @@ -31,8 +32,8 @@ import { ColorArgumentValues, EntityAnchorArgumentValues, GamemodeArgumentValues, + getItemSlotArgumentValues, HeightmapValues, - ItemSlotArgumentValues, MirrorValues, OperationArgumentValues, RotationValues, @@ -58,8 +59,9 @@ import type { ArgumentTreeNode } from '../tree/index.js' export const getMockNodes: mcf.completer.MockNodesGetter = ( rawTreeNode, - range, + ctx: CompleterContext, ): Arrayable => { + const range = ctx.offset const treeNode = rawTreeNode as ArgumentTreeNode switch (treeNode.parser) { @@ -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': diff --git a/packages/java-edition/src/mcfunction/parser/argument.ts b/packages/java-edition/src/mcfunction/parser/argument.ts index 981f9ddf8..411569405 100644 --- a/packages/java-edition/src/mcfunction/parser/argument.ts +++ b/packages/java-edition/src/mcfunction/parser/argument.ts @@ -9,8 +9,8 @@ import { ColorArgumentValues, EntityAnchorArgumentValues, GamemodeArgumentValues, + getItemSlotArgumentValues, HeightmapValues, - ItemSlotArgumentValues, MirrorValues, OperationArgumentValues, RotationValues, @@ -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': diff --git a/packages/mcfunction/src/completer/index.ts b/packages/mcfunction/src/completer/index.ts index c5dc0dd8c..96280603d 100644 --- a/packages/mcfunction/src/completer/index.ts +++ b/packages/mcfunction/src/completer/index.ts @@ -13,7 +13,7 @@ import { export type MockNodesGetter = ( treeNode: ArgumentTreeNode, - range: core.RangeLike, + range: core.CompleterContext, ) => core.Arrayable /** @@ -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), ) ),