Skip to content

Commit

Permalink
Fix bugs with ESM, Data/ResolveNBT, data string command
Browse files Browse the repository at this point in the history
  • Loading branch information
MulverineX committed Oct 24, 2023
1 parent bcc633d commit cd29c81
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/commands/implementations/world/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class DataModifyValuesCommand<MACRO extends boolean> extends CommandArgum
* @param end Optional. Index of the first character to exclude at the end of the string
*/
block: (sourcePosition: Macroable<Coordinates<MACRO>, MACRO>, sourcePath: Macroable<string, MACRO>, start?: Macroable<number, MACRO>, end?: Macroable<number, MACRO>) => {
const command: (string | VectorClass<[string, string, string]> | number | MacroArgument)[] = ['from', 'block', coordinatesParser(sourcePosition), sourcePath]
const command: (string | VectorClass<[string, string, string]> | number | MacroArgument)[] = ['string', 'block', coordinatesParser(sourcePosition), sourcePath]
if (start) {
command.push(start)
if (end) command.push(end)
Expand All @@ -144,7 +144,7 @@ export class DataModifyValuesCommand<MACRO extends boolean> extends CommandArgum
* @param end Optional. Index of the first character to exclude at the end of the string
*/
entity: (source: Macroable<SingleEntityArgument<MACRO>, MACRO>, sourcePath: Macroable<string, MACRO>, start?: Macroable<number, MACRO>, end?: Macroable<number, MACRO>) => {
const command: (Macroable<string | SingleEntityArgument<MACRO> | number, MACRO>)[] = ['from', 'entity', targetParser(source), sourcePath]
const command: (Macroable<string | SingleEntityArgument<MACRO> | number, MACRO>)[] = ['string', 'entity', targetParser(source), sourcePath]
if (start) {
command.push(start)
if (end) command.push(end)
Expand All @@ -161,7 +161,7 @@ export class DataModifyValuesCommand<MACRO extends boolean> extends CommandArgum
* @param end Optional. Index of the first character to exclude at the end of the string
*/
storage: (source: Macroable<string, MACRO>, sourcePath: Macroable<string, MACRO>, start?: Macroable<number, MACRO>, end?: Macroable<number, MACRO>) => {
const command: (string | number | MacroArgument)[] = ['from', 'storage', source, sourcePath]
const command: (string | number | MacroArgument)[] = ['string', 'storage', source, sourcePath]
if (start) {
command.push(start)
if (end) command.push(end)
Expand Down
10 changes: 9 additions & 1 deletion src/core/sandstoneCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ export class SandstoneCore {
this.mcfunctionStack = []
this.awaitNodes = new Set()

this.depend = this.depend.bind(this)
// ESM is funny

for (const method of Object.getOwnPropertyNames(Object.getPrototypeOf(this))) {
/* @ts-ignore */
if (method !== 'constructor' && typeof this[method] === 'function' && typeof this[method].bind === 'function') {
/* @ts-ignore */
this[method] = this[method].bind(this)
}
}
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/pack/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,15 @@ export class SandstonePack {
this.setupLantern()
this.dependencies = new Map()

this.MCFunction = this.MCFunction.bind(this)
// ESM is funny

for (const method of Object.getOwnPropertyNames(Object.getPrototypeOf(this))) {
/* @ts-ignore */
if (method !== 'constructor' && typeof this[method] === 'function' && typeof this[method].bind === 'function') {
/* @ts-ignore */
this[method] = this[method].bind(this)
}
}
}

setupLantern = () => {
Expand Down
1 change: 1 addition & 0 deletions src/variables/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export class DataPointClass<TYPE extends DATA_TYPES = any> extends MacroArgument
if (value instanceof StringDataPointClass) {
if (value.sliceBounds[1]) this.string((data) => data.set, value, value.sliceBounds[0], value.sliceBounds[1])
else this.string((data) => data.set, value, value.sliceBounds[0])
return this
}
if (value instanceof Score) {
this.executeStore(storeType || 'int', scale).run.scoreboard.players.get(value.target, value.objective)
Expand Down
25 changes: 20 additions & 5 deletions src/variables/ResolveNBT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import * as util from 'util'

import { capitalize } from '../utils.js'
import { DataPointPickClass } from './abstractClasses.js'
import { StringDataPointClass } from './Data.js'
import {
NBTAnyValue,
NBTClass, NBTInt, NBTIntArray, NBTPrimitive,
} from './nbt/index.js'
import { Score } from './Score.js'

import type { NBTObject } from '../arguments/nbt.js'
import type { SandstonePack } from '../pack/index.js'
import type { DataPointClass, StringDataPointClass } from './Data.js'
import type { DataPointClass } from './Data.js'
import type {
NBTAllArrays, NBTAllNumbers,
NBTAllValues, NBTString,
Expand Down Expand Up @@ -46,6 +48,8 @@ export class ResolveNBTClass extends DataPointPickClass {
if (Array.isArray(nbt)) {
resolvedNBT = []

this.dataPoint.set([])

if (resolvedNBT.length !== 0) {
for (const [i, value] of nbt.entries()) {
const resolved = this._resolveNBT(value, `${path === undefined ? '' : path}`, i)
Expand All @@ -65,6 +69,8 @@ export class ResolveNBTClass extends DataPointPickClass {
if (nbt instanceof NBTPrimitive) {
return resolvedNBT
}
this.dataPoint.set({})

for (const [key, value] of Object.entries(nbt)) {
const resolved = this._resolveNBT(value, `${path === undefined ? '' : `${path}.`}${key}`)
if (resolved !== undefined) {
Expand All @@ -77,14 +83,23 @@ export class ResolveNBTClass extends DataPointPickClass {
/**
* @internal
*/
_resolveData(value: ResolveNBTPartClass<'data', NBTAllValues>, path: string, index?: number) {
_resolveData(part: ResolveNBTPartClass<'data', NBTAllValues>, path: string, index?: number) {
const { value } = part
const dataPoint = this.dataPoint.select(path)

if (index) {
dataPoint.insert(value, index)
let _value = value
if (value instanceof StringDataPointClass) {
_value = this.pack.DataVariable(value)
}
dataPoint.insert(_value as DataPointClass, index)
return undefined
}
if (value instanceof StringDataPointClass) {
dataPoint.set(value)
return undefined
}
dataPoint.set(value)
dataPoint.set(value as DataPointClass)
return undefined
}

Expand Down Expand Up @@ -192,5 +207,5 @@ export function ResolveNBTPart<ValueType extends 'data' | 'score' | 'scores', Pr
return new ResolveNBTPartClass<ValueType, Primitive>(value, 'score' as ValueType, (option2 || NBTInt) as Primitive, (option1 || 1) as number)
}

return new ResolveNBTPartClass<ValueType, Primitive>(value, 'data' as ValueType, option1 as Primitive)
return new ResolveNBTPartClass<ValueType, Primitive>(value, 'data' as ValueType, (option1 || NBTAnyValue) as Primitive)
}
5 changes: 4 additions & 1 deletion src/variables/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ export function targetParser(target: any): string {
return target
}
if (target._toSelector) {
return target._toSelector().toString()
return target._toSelector()
}
if (target.toMacro) {
return target
}
return target.toString()
}

0 comments on commit cd29c81

Please sign in to comment.