diff --git a/src/nodes/ObjectNode.ts b/src/nodes/ObjectNode.ts index f738980c..c7a19422 100644 --- a/src/nodes/ObjectNode.ts +++ b/src/nodes/ObjectNode.ts @@ -40,7 +40,6 @@ export class ObjectNode extends AbstractNode { const activeFields = {...this.fields, ...activeCase} let res: any = {} Object.keys(activeFields).forEach(f => { - console.log(f) return res[f] = activeFields[f].transform(path.push(f), value[f]) }) return this.transformMod(res); diff --git a/src/nodes/ReferenceNode.ts b/src/nodes/ReferenceNode.ts index d07f735d..677f9bbc 100644 --- a/src/nodes/ReferenceNode.ts +++ b/src/nodes/ReferenceNode.ts @@ -1,37 +1,29 @@ -import { AbstractNode, NodeChildren, NodeMods, RenderOptions, INode } from './AbstractNode' +import { AbstractNode, NodeMods, RenderOptions, INode } from './AbstractNode' import { TreeView } from '../view/TreeView' import { Path } from '../model/Path' -import { SchemaRegistry } from '../schemas/SchemaRegistry' +import { SCHEMAS } from '../schemas/Registries' export class ReferenceNode extends AbstractNode { - protected reference: string + protected reference: () => INode - constructor(reference: string, mods?: NodeMods) { + constructor(id: string, mods?: NodeMods) { super(mods) - this.reference = reference - } - - get(): INode { - const node = SchemaRegistry.get(this.reference) - if (node === undefined) { - console.error(`Invalid schema reference "${this.reference}"`) - } - return node + this.reference = () => SCHEMAS.get(id) } default(value?: any) { - return this.get().default(value) + return this.reference().default(value) } transform(path: Path, value: any) { - return this.get()?.transform(path, value) + return this.reference()?.transform(path, value) } render(path: Path, value: any, view: TreeView, options?: RenderOptions) { - return this.get()?.render(path, value, view, options) + return this.reference()?.render(path, value, view, options) } renderRaw(path: Path, value: any, view: TreeView, options?: RenderOptions) { - return this.get()?.renderRaw(path, value, view, options) + return this.reference()?.renderRaw(path, value, view, options) } } diff --git a/src/nodes/custom/ResourceNode.ts b/src/nodes/custom/ResourceNode.ts index 1fab1ab1..1e2bb812 100644 --- a/src/nodes/custom/ResourceNode.ts +++ b/src/nodes/custom/ResourceNode.ts @@ -1,20 +1,16 @@ import { NodeMods, RenderOptions } from '../AbstractNode' import { EnumNode } from '../EnumNode' import { Path } from '../../model/Path' -import { DataModel } from '../../model/DataModel' import { TreeView, getId } from '../../view/TreeView' export interface ResourceNodeMods extends NodeMods { - options?: string[] - registry?: string additional?: boolean } export class ResourceNode extends EnumNode { additional: boolean - constructor(mods?: ResourceNodeMods) { - const options = mods?.options ?? [] // TODO: Support registry using `github.com/Arcensoth/mcdata` + constructor(options: string[], mods?: ResourceNodeMods) { super(options, { transform: (v) => { if (v === undefined || v.length === 0) return undefined diff --git a/src/schemas/Collections.ts b/src/schemas/Collections.ts index 44f1ad42..f7b0ad01 100644 --- a/src/schemas/Collections.ts +++ b/src/schemas/Collections.ts @@ -1,4 +1,6 @@ -export const conditions = [ +import { COLLECTIONS } from './Registries' + +COLLECTIONS.register('conditions', [ 'alternative', 'requirements', 'inverted', @@ -16,9 +18,9 @@ export const conditions = [ 'table_bonus', 'killed_by_player', 'survives_explosion' -] +]) -export const enchantments = [ +COLLECTIONS.register('enchantments', [ 'aqua_affinity', 'bane_of_arthropods', 'blast_protection', @@ -56,9 +58,9 @@ export const enchantments = [ 'sweeping', 'thorns', 'unbreaking' -] +]) -export const biomes = [ +COLLECTIONS.register('biomes', [ 'badlands', 'badlands_plateau', 'bamboo_jungle', @@ -134,9 +136,9 @@ export const biomes = [ 'wooded_badlands_plateau', 'wooded_hills', 'wooded_mountains' -] +]) -export const structures = [ +COLLECTIONS.register('structures', [ 'pillager_outpost', 'mineshaft', 'mansion', @@ -152,24 +154,24 @@ export const structures = [ 'endcity', 'buried_treasure', 'village' -] +]) -export const dimensions = [ +COLLECTIONS.register('dimensions', [ 'overworld', 'the_nether', 'the_end' -] +]) -export const slots = [ +COLLECTIONS.register('slots', [ 'mainhand', 'offhand', 'head', 'chest', 'legs', 'feet' -] +]) -export const statusEffects = [ +COLLECTIONS.register('status-effects', [ 'speed', 'slowness', 'haste', @@ -202,16 +204,16 @@ export const statusEffects = [ 'dolphins_grace', 'bad_omen', 'hero_of_the_village' -] +]) -export const gameModes = [ +COLLECTIONS.register('gamemodes', [ 'survival', 'creative', 'adventure', 'spectator' -] +]) -export const statisticTypes = [ +COLLECTIONS.register('statistic-types', [ 'minecraft:broken', 'minecraft:crafted', 'minecraft:custom', @@ -223,4 +225,10 @@ export const statisticTypes = [ 'minecraft:used', 'killedByTeam', 'teamkill' -] +]) + +COLLECTIONS.register('entity-sources', [ + 'this', + 'killer', + 'killer_player' +]) diff --git a/src/schemas/Condition.ts b/src/schemas/Condition.ts index a9276ee6..f2132fee 100644 --- a/src/schemas/Condition.ts +++ b/src/schemas/Condition.ts @@ -8,15 +8,12 @@ import { RangeNode } from '../nodes/custom/RangeNode'; import { MapNode } from '../nodes/MapNode'; import { StringNode } from '../nodes/StringNode'; import { ReferenceNode } from '../nodes/ReferenceNode'; -import { SchemaRegistry } from './SchemaRegistry'; -import { conditions, enchantments } from './Collections' +import { SCHEMAS, COLLECTIONS } from './Registries'; import './Predicates' -const entitySources = ['this', 'killer', 'killer_player'] - -export const ConditionSchema = SchemaRegistry.register('condition', new ObjectNode({ - condition: new ResourceNode({options: conditions, default: () => 'random_chance'}), +SCHEMAS.register('condition', new ObjectNode({ + condition: new ResourceNode(COLLECTIONS.get('conditions'), {default: () => 'random_chance'}), [Switch]: 'condition', [Case]: { 'alternative': { @@ -25,7 +22,7 @@ export const ConditionSchema = SchemaRegistry.register('condition', new ObjectNo ) }, 'block_state_property': { - block: new ResourceNode({registry: 'minecraft:block'}), + block: new ResourceNode(COLLECTIONS.get('blocks')), properties: new MapNode( new StringNode(), new StringNode() @@ -35,11 +32,11 @@ export const ConditionSchema = SchemaRegistry.register('condition', new ObjectNo predicate: new ReferenceNode('damage-source-predicate') }, 'entity_properties': { - entity: new EnumNode(entitySources, 'this'), + entity: new EnumNode(COLLECTIONS.get('entity-sources'), 'this'), predicate: new ReferenceNode('entity-predicate') }, 'entity_scores': { - entity: new EnumNode(entitySources, 'this'), + entity: new EnumNode(COLLECTIONS.get('entity-sources'), 'this'), scores: new MapNode( new StringNode(), new RangeNode() @@ -76,7 +73,7 @@ export const ConditionSchema = SchemaRegistry.register('condition', new ObjectNo name: new StringNode() }, 'table_bonus': { - enchantment: new ResourceNode({options: enchantments}), + enchantment: new ResourceNode(COLLECTIONS.get('enchantments')), chances: new ListNode( new NumberNode({min: 0, max: 1}) ) @@ -96,3 +93,5 @@ export const ConditionSchema = SchemaRegistry.register('condition', new ObjectNo chance: 0.5 }) })) + +export const ConditionSchema = SCHEMAS.get('condition') diff --git a/src/schemas/Predicates.ts b/src/schemas/Predicates.ts index aa8d517b..ad3b1879 100644 --- a/src/schemas/Predicates.ts +++ b/src/schemas/Predicates.ts @@ -5,22 +5,14 @@ import { ListNode } from '../nodes/ListNode'; import { RangeNode } from '../nodes/custom/RangeNode'; import { StringNode } from '../nodes/StringNode'; import { ReferenceNode } from '../nodes/ReferenceNode'; -import { SchemaRegistry } from './SchemaRegistry'; import { BooleanNode } from '../nodes/BooleanNode'; import { MapNode } from '../nodes/MapNode'; -import { - enchantments, - biomes, - structures, - dimensions, - slots, - statusEffects, - gameModes, - statisticTypes -} from './Collections' +import { SCHEMAS, COLLECTIONS } from './Registries'; -SchemaRegistry.register('item-predicate', new ObjectNode({ - item: new ResourceNode({registry: 'minecraft:item'}), +import './Collections' + +SCHEMAS.register('item-predicate', new ObjectNode({ + item: new ResourceNode(COLLECTIONS.get('items')), tag: new StringNode(), count: new RangeNode(), durability: new RangeNode(), @@ -31,13 +23,13 @@ SchemaRegistry.register('item-predicate', new ObjectNode({ ) })) -SchemaRegistry.register('enchantment-predicate', new ObjectNode({ - enchantment: new ResourceNode({options: enchantments}), +SCHEMAS.register('enchantment-predicate', new ObjectNode({ + enchantment: new ResourceNode(COLLECTIONS.get('enchantments')), levels: new RangeNode() })) -SchemaRegistry.register('block-predicate', new ObjectNode({ - block: new ResourceNode({registry: 'minecraft:block'}), +SCHEMAS.register('block-predicate', new ObjectNode({ + block: new ResourceNode(COLLECTIONS.get('blocks')), tag: new StringNode(), nbt: new StringNode(), state: new MapNode( @@ -46,8 +38,8 @@ SchemaRegistry.register('block-predicate', new ObjectNode({ ) })) -SchemaRegistry.register('fluid-predicate', new ObjectNode({ - fluid: new ResourceNode({registry: 'minecraft:fluid'}), +SCHEMAS.register('fluid-predicate', new ObjectNode({ + fluid: new ResourceNode(COLLECTIONS.get('fluids')), tag: new StringNode(), nbt: new StringNode(), state: new MapNode( @@ -56,15 +48,15 @@ SchemaRegistry.register('fluid-predicate', new ObjectNode({ ) })) -SchemaRegistry.register('location-predicate', new ObjectNode({ +SCHEMAS.register('location-predicate', new ObjectNode({ position: new ObjectNode({ x: new RangeNode(), y: new RangeNode(), z: new RangeNode() }), - biome: new ResourceNode({options: biomes}), - feature: new EnumNode(structures), - dimension: new ResourceNode({options: dimensions, additional: true}), + biome: new ResourceNode(COLLECTIONS.get('biomes')), + feature: new EnumNode(COLLECTIONS.get('structures')), + dimension: new ResourceNode(COLLECTIONS.get('dimensions'), {additional: true}), light: new ObjectNode({ light: new RangeNode() }), @@ -73,14 +65,14 @@ SchemaRegistry.register('location-predicate', new ObjectNode({ fluid: new ReferenceNode('fluid-predicate') })) -SchemaRegistry.register('statistic-predicat', new ObjectNode({ - type: new EnumNode(statisticTypes), +SCHEMAS.register('statistic-predicate', new ObjectNode({ + type: new EnumNode(COLLECTIONS.get('statistic-types')), stat: new StringNode(), value: new RangeNode() })) -SchemaRegistry.register('player-predicate', new ObjectNode({ - gamemode: new EnumNode(gameModes), +SCHEMAS.register('player-predicate', new ObjectNode({ + gamemode: new EnumNode(COLLECTIONS.get('gamemodes')), level: new RangeNode(), advancements: new MapNode( new StringNode(), @@ -95,14 +87,14 @@ SchemaRegistry.register('player-predicate', new ObjectNode({ ) })) -SchemaRegistry.register('status-effect', new ObjectNode({ +SCHEMAS.register('status-effect', new ObjectNode({ amplifier: new RangeNode(), duration: new RangeNode(), ambient: new BooleanNode(), visible: new BooleanNode() })) -SchemaRegistry.register('distance-predicate', new ObjectNode({ +SCHEMAS.register('distance-predicate', new ObjectNode({ x: new RangeNode(), y: new RangeNode(), z: new RangeNode(), @@ -110,7 +102,7 @@ SchemaRegistry.register('distance-predicate', new ObjectNode({ horizontal: new RangeNode() })) -SchemaRegistry.register('entity-predicate', new ObjectNode({ +SCHEMAS.register('entity-predicate', new ObjectNode({ type: new StringNode(), nbt: new StringNode(), team: new StringNode(), @@ -124,7 +116,7 @@ SchemaRegistry.register('entity-predicate', new ObjectNode({ is_baby: new BooleanNode() }), equipment: new MapNode( - new EnumNode(slots), + new EnumNode(COLLECTIONS.get('slots')), new ReferenceNode('item-predicate') ), // vehicle: new ReferenceNode('entity-predicate'), @@ -134,7 +126,7 @@ SchemaRegistry.register('entity-predicate', new ObjectNode({ in_open_water: new BooleanNode() }), effects: new MapNode( - new ResourceNode({options: statusEffects}), + new ResourceNode(COLLECTIONS.get('status-effects')), new ReferenceNode('status-effect-predicate') ) })) diff --git a/src/schemas/Registries.ts b/src/schemas/Registries.ts new file mode 100644 index 00000000..0769280f --- /dev/null +++ b/src/schemas/Registries.ts @@ -0,0 +1,41 @@ +import { INode } from "../nodes/AbstractNode" + +export interface Registry { + register(id: string, value: T): void + get(id: string): T +} + +class SchemaRegistry implements Registry> { + private registery: { [id: string]: INode } = {} + + register(id: string, node: INode) { + this.registery[id] = node + } + + get(id: string) { + const node = this.registery[id] + if (node === undefined) { + console.error(`Tried to access schema "${id}, but that doesn't exit.`) + } + return node + } +} + +class CollectionRegistry implements Registry { + private registery: { [id: string]: string[] } = {} + + register(id: string, list: string[]) { + this.registery[id] = list + } + + get(id: string) { + const list = this.registery[id] + if (list === undefined) { + console.warn(`Tried to access collection "${id}", but that doesn't exist`) + } + return list ?? [] + } +} + +export const SCHEMAS = new SchemaRegistry() +export const COLLECTIONS = new CollectionRegistry() diff --git a/src/schemas/SchemaRegistry.ts b/src/schemas/SchemaRegistry.ts deleted file mode 100644 index fa988f10..00000000 --- a/src/schemas/SchemaRegistry.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { INode } from '../nodes/AbstractNode'; - -type Registry = { - [id: string]: INode -} - -export class SchemaRegistry { - private static registery: Registry = {} - - static register(id: string, node: INode) { - SchemaRegistry.registery[id] = node - return node - } - - static get(id: string): INode { - return SchemaRegistry.registery[id] - } -}