Add collection registry

This commit is contained in:
Misode
2020-05-28 02:25:01 +02:00
parent 9189bbe229
commit 3aa134c544
8 changed files with 110 additions and 101 deletions

View File

@@ -40,7 +40,6 @@ export class ObjectNode extends AbstractNode<IObject> {
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);

View File

@@ -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<any> {
protected reference: string
protected reference: () => INode<any>
constructor(reference: string, mods?: NodeMods<any>) {
constructor(id: string, mods?: NodeMods<any>) {
super(mods)
this.reference = reference
}
get(): INode<any> {
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)
}
}

View File

@@ -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<string> {
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

View File

@@ -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'
])

View File

@@ -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')

View File

@@ -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')
)
}))

41
src/schemas/Registries.ts Normal file
View File

@@ -0,0 +1,41 @@
import { INode } from "../nodes/AbstractNode"
export interface Registry<T> {
register(id: string, value: T): void
get(id: string): T
}
class SchemaRegistry implements Registry<INode<any>> {
private registery: { [id: string]: INode<any> } = {}
register(id: string, node: INode<any>) {
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<string[]> {
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()

View File

@@ -1,18 +0,0 @@
import { INode } from '../nodes/AbstractNode';
type Registry = {
[id: string]: INode<any>
}
export class SchemaRegistry {
private static registery: Registry = {}
static register(id: string, node: INode<any>) {
SchemaRegistry.registery[id] = node
return node
}
static get(id: string): INode<any> {
return SchemaRegistry.registery[id]
}
}