diff --git a/src/app/app.ts b/src/app/app.ts index 45fcf789..6f0b10b9 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -10,18 +10,22 @@ import { BooleanNode } from '../nodes/BooleanNode' import { MapNode } from '../nodes/MapNode' import { NumberNode } from '../nodes/NumberNode' import { RangeNode } from '../nodes/custom/RangeNode' +import { ResourceNode } from '../nodes/custom/ResourceNode' const EntityCollection = ['sheep', 'pig'] const predicateTree = new RootNode('predicate', { condition: new EnumNode(['foo', 'bar'], { + default: () => 'bar', transform: (s: string) => (s === 'foo') ? {test: 'baz'} : s }), number: new NumberNode({integer: false, min: 0}), range: new RangeNode(), predicate: new ObjectNode({ type: new EnumNode(EntityCollection), - nbt: new StringNode(), + nbt: new ResourceNode({ + default: (v) => "hahaha" + }), test: new BooleanNode(), recipes: new MapNode( new StringNode(), diff --git a/src/nodes/AbstractNode.ts b/src/nodes/AbstractNode.ts index 1b582eeb..8623a11c 100644 --- a/src/nodes/AbstractNode.ts +++ b/src/nodes/AbstractNode.ts @@ -2,9 +2,6 @@ import { DataModel } from "../model/DataModel" import { Path } from "../model/Path" import { TreeView } from "../view/TreeView" -export type IDefault = (value?: T) => T | undefined -export type ITransform = (value: T) => any - export interface INode { setParent: (parent: INode) => void default: IDefault @@ -26,9 +23,12 @@ export type NodeChildren = { [name: string]: INode } +export type IDefault = (value?: T) => T | undefined +export type ITransform = (value: T) => any + export interface NodeMods { default?: IDefault - transform?: (value: T) => any + transform?: ITransform } export abstract class AbstractNode implements INode { @@ -36,11 +36,9 @@ export abstract class AbstractNode implements INode { defaultMod: IDefault transformMod: ITransform - constructor(mods?: NodeMods, defaultMods?: NodeMods, ) { - this.defaultMod = mods?.default ? mods.default : - defaultMods?.default ? defaultMods.default : (v) => v - this.transformMod = mods?.transform ? mods.transform : - defaultMods?.transform ? defaultMods.transform : (v) => v + constructor(mods?: NodeMods) { + this.defaultMod = mods?.default ? mods.default : (v) => v + this.transformMod = mods?.transform ? mods.transform : (v) => v } setParent(parent: INode) { diff --git a/src/nodes/BooleanNode.ts b/src/nodes/BooleanNode.ts index d8d4a767..3a770f35 100644 --- a/src/nodes/BooleanNode.ts +++ b/src/nodes/BooleanNode.ts @@ -3,16 +3,16 @@ import { Path } from "../model/Path"; import { TreeView } from "../view/TreeView"; export interface BooleanNodeMods extends NodeMods { - force: boolean + force?: boolean } export class BooleanNode extends AbstractNode { force: boolean constructor(mods?: BooleanNodeMods) { - super(mods, { - default: () => false - }) + super({ + default: () => false, + ...mods}) this.force = (mods?.force === true) } diff --git a/src/nodes/EnumNode.ts b/src/nodes/EnumNode.ts index bc16e65e..310fb85a 100644 --- a/src/nodes/EnumNode.ts +++ b/src/nodes/EnumNode.ts @@ -6,8 +6,8 @@ import { Path } from '../model/Path' export class EnumNode extends AbstractNode implements StateNode { protected options: string[] - constructor(options: string[], mods?: NodeMods) { - super(mods) + constructor(options: string[], mods?: NodeMods | string) { + super(typeof mods === 'string' ? {default: () => mods} : mods) this.options = options } diff --git a/src/nodes/ListNode.ts b/src/nodes/ListNode.ts index 99bf6b34..d38b0067 100644 --- a/src/nodes/ListNode.ts +++ b/src/nodes/ListNode.ts @@ -8,9 +8,9 @@ export class ListNode extends AbstractNode { protected children: INode constructor(values: INode, mods?: NodeMods) { - super(mods, { - default: () => [] - }) + super({ + default: () => [], + ...mods}) this.children = values } diff --git a/src/nodes/MapNode.ts b/src/nodes/MapNode.ts index 8c6b5394..8b0b20d4 100644 --- a/src/nodes/MapNode.ts +++ b/src/nodes/MapNode.ts @@ -12,9 +12,9 @@ export class MapNode extends AbstractNode { protected values: INode constructor(keys: StateNode, values: INode, mods?: NodeMods) { - super(mods, { - default: () => ({}) - }) + super({ + default: () => ({}), + ...mods}) this.keys = keys this.values = values } diff --git a/src/nodes/NumberNode.ts b/src/nodes/NumberNode.ts index fdede5a5..fd2d2dd0 100644 --- a/src/nodes/NumberNode.ts +++ b/src/nodes/NumberNode.ts @@ -15,9 +15,9 @@ export class NumberNode extends AbstractNode implements StateNode 0 - }) + super({ + default: () => 0, + ...mods}) this.integer = mods?.integer ? mods.integer : false this.min = mods?.min !== undefined ? mods.min : -Infinity this.max = mods?.max !== undefined ? mods.max : Infinity diff --git a/src/nodes/ObjectNode.ts b/src/nodes/ObjectNode.ts index 651280f6..cc423d97 100644 --- a/src/nodes/ObjectNode.ts +++ b/src/nodes/ObjectNode.ts @@ -10,9 +10,9 @@ export class ObjectNode extends AbstractNode { protected fields: NodeChildren constructor(fields: NodeChildren, mods?: NodeMods) { - super(mods, { - default: () => ({}) - }) + super({ + default: () => ({}), + ...mods}) this.fields = fields Object.values(fields).forEach(child => { child.setParent(this) diff --git a/src/nodes/StringNode.ts b/src/nodes/StringNode.ts index 7406080e..614605a1 100644 --- a/src/nodes/StringNode.ts +++ b/src/nodes/StringNode.ts @@ -3,9 +3,16 @@ import { Path } from '../model/Path' import { DataModel } from '../model/DataModel' import { TreeView } from '../view/TreeView' +export interface StringNodeMods extends NodeMods { + allowEmpty?: boolean +} + export class StringNode extends AbstractNode implements StateNode { - constructor(mods?: NodeMods) { + allowEmpty: boolean + + constructor(mods?: StringNodeMods) { super(mods) + this.allowEmpty = (mods?.allowEmpty === true) } getState(el: Element) { @@ -13,7 +20,8 @@ export class StringNode extends AbstractNode implements StateNode) { + super({ + transform: (v) => { + if (v === undefined) return undefined + return v.startsWith('minecraft:') ? v : 'minecraft:' + v + }, ...mods}) + } +}