From 2d29de7903dc70a4fe7b11a7fe583823fa60372c Mon Sep 17 00:00:00 2001 From: Misode Date: Mon, 15 Jun 2020 15:16:02 +0200 Subject: [PATCH] Fetch registries from mcdata --- src/app/RegistryFetcher.ts | 11 +++++ src/app/Sandbox.ts | 87 +++++++++++++++++--------------------- src/app/app.ts | 23 +++++++++- 3 files changed, 70 insertions(+), 51 deletions(-) create mode 100644 src/app/RegistryFetcher.ts diff --git a/src/app/RegistryFetcher.ts b/src/app/RegistryFetcher.ts new file mode 100644 index 00000000..8357fc20 --- /dev/null +++ b/src/app/RegistryFetcher.ts @@ -0,0 +1,11 @@ +import { CollectionRegistry } from "minecraft-schemas"; + +export const mcdata = (version: string, registry: string) => `https://raw.githubusercontent.com/Arcensoth/mcdata/${version}/processed/reports/registries/${registry}/${registry}.min.json` + +export const RegistryFetcher = async (target: CollectionRegistry, registries: string[], version = 'master') => { + await Promise.all(registries.map(async r => { + const res = await fetch(mcdata(version, r)) + const data = await res.json() + target.register(r, data.values) + })) +} diff --git a/src/app/Sandbox.ts b/src/app/Sandbox.ts index 8034acc5..a8c40905 100644 --- a/src/app/Sandbox.ts +++ b/src/app/Sandbox.ts @@ -1,57 +1,46 @@ import { - ObjectNode, - EnumNode, StringNode, - NumberNode, BooleanNode, - RangeNode, + EnumNode, + NumberNode, + ObjectNode, + ListNode, MapNode, - ListNode + Switch, + Case, + Reference, + JsonNode, + RangeNode, + Resource, + SCHEMAS } from 'minecraft-schemas' -const EntityCollection = ['sheep', 'pig'] - -export const SandboxSchema = new ObjectNode({ - condition: new EnumNode(['foo', 'bar'], { - default: () => 'bar' - }), - number: new NumberNode({integer: false, min: 0}), - range: new RangeNode({ - enable: (path) => path.push('condition').get() === 'foo' - }), - predicate: new ObjectNode({ - type: new EnumNode(EntityCollection), - nbt: new StringNode({ - default: (v) => 'hahaha' - }), - test: new BooleanNode({force: () => true}), - recipes: new MapNode( - new StringNode(), - new RangeNode({ - default: (v) => RangeNode.isExact(v) ? 2 : v, - transform: (v: any) => RangeNode.isRange(v) ? ({ - min: v?.min ?? -2147483648, - max: v?.max ?? 2147483647 - }) : v - }) - ) - }), - effects: new ListNode( - new ObjectNode({ - type: new EnumNode(EntityCollection), - nbt: new StringNode() - }, { - default: () => ({ - type: 'sheep' - }) +SCHEMAS.register('foo', ObjectNode({ + foo: StringNode(), + bar: BooleanNode({ radio: true }), + nested: ObjectNode({ + baz: NumberNode({ min: 1 }), + range: RangeNode() + }, { collapse: true }), + arr: ListNode( + ObjectNode({ + aaa: StringNode(), + bbb: JsonNode() }) - ) -}, { - default: () => ({ - condition: 'foo', - predicate: { - nbt: 'hi' + ), + map: MapNode( + EnumNode(['pig', 'sheep']), + Resource(StringNode()) + ), + recursive: ListNode( + Reference('foo') + ), + [Switch]: path => path.push('foo').get(), + [Case]: { + 'blah': { + haha: StringNode() } - }), - transform: (v: any) => v?.condition === 'foo' ? ({...v, test: 'hello'}) : v -}); + } +})) + +export const SandboxSchema = SCHEMAS.get('foo') diff --git a/src/app/app.ts b/src/app/app.ts index a9d84ed0..2b981d74 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -1,3 +1,4 @@ +import { RegistryFetcher } from './RegistryFetcher' import { DataModel, IView, @@ -9,7 +10,8 @@ import { DimensionSchema, DimensionTypeSchema, LOCALES, - locale + locale, + COLLECTIONS } from 'minecraft-schemas' import Split from 'split.js' @@ -33,10 +35,27 @@ const languages: { [key: string]: string } = { 'zh-CN': '简体中文' } +const registries = [ + 'attribute', + 'biome', + 'biome_source', + 'block', + 'enchantment', + 'entity_type', + 'fluid', + 'item', + 'loot_condition_type', + 'loot_function_type', + 'loot_pool_entry_type', + 'stat_type', + 'structure_feature' +] + const publicPath = process.env.NODE_ENV === 'production' ? '/dev/' : '/'; Promise.all([ fetch(publicPath + 'locales/schema/en.json').then(r => r.json()), - fetch(publicPath + 'locales/app/en.json').then(r => r.json()) + fetch(publicPath + 'locales/app/en.json').then(r => r.json()), + RegistryFetcher(COLLECTIONS, registries) ]).then(responses => { LOCALES.register('en', {...responses[0], ...responses[1]})