This release is 10 versions behind 0.6.7 — the latest version of @azuryth/azuryth-core. Jump to latest
@azuryth/azuryth-core@0.5.0
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
JSR Score
29%
Published
2 months ago (0.5.0)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167import { getOrThrow, getOrUndefined } from "../util/json.ts"; import { ComponentStore, type JsonObject, type JsonValue } from "./types.ts"; export class Permutation { condition: string; components: ComponentStore<JsonValue>; constructor(condition: string, components: ComponentStore<JsonValue>) { this.components = components; this.condition = condition; } static fromJson(json: JsonObject): Permutation { const condition = getOrThrow<string>("condition", json)!; const components: Map<string, JsonValue> = new Map(); const permComponents = getOrThrow<JsonObject>("components", json)!; for (const [key, info] of Object.entries(permComponents)) { components.set(key, info as JsonValue); } return new Permutation(condition, new ComponentStore(components)); } static fromObject(condition: string, permComponents: JsonObject): Permutation { const components: Map<string, JsonValue> = new Map(); for (const [key, info] of Object.entries(permComponents)) { components.set(key, info as JsonValue); } return new Permutation(condition, new ComponentStore(components)); } jsonBlob(): JsonObject { const json: JsonObject = {}; const componentInfo = this.components.jsonBlob(); json["components"] = componentInfo; json["condition"] = this.condition; return json; } } export class Block { components: ComponentStore<JsonValue>; traits: ComponentStore<JsonValue>; private permutations: Array<Permutation>; private parsedFormat: string; description: JsonValue; id: string; constructor(rawJson: JsonObject) { const blockInfo = rawJson["minecraft:block"] as JsonObject; const blockComponents = getOrThrow<JsonObject>( "components", blockInfo )!; const description = getOrThrow<JsonObject>("description", blockInfo)!; const id = getOrThrow<string>("identifier", description)!; const format = getOrThrow<string>("format_version", rawJson)!; const components: Map<string, JsonValue> = new Map(); for (const [key, info] of Object.entries(blockComponents)) { components.set(key, info as JsonValue); } const permutations = new Array<Permutation>(); const blockPermutations = getOrUndefined<JsonValue[]>( "permutations", blockInfo ); if (blockPermutations) { for (const info of blockPermutations) { permutations.push(Permutation.fromJson(info as JsonObject)); } } const traits = new Map(); const traitInfo = getOrUndefined<JsonObject>("traits", description); if (traitInfo) { for (const [key, info] of Object.entries(traitInfo)) { traits.set(key, info as JsonValue); } } this.id = id; this.parsedFormat = format; this.components = new ComponentStore(components); this.permutations = permutations; this.description = description; this.traits = new ComponentStore(traits); } pushPermutation(perm: Permutation) { this.permutations.push(perm); } popPermutation(): Permutation | undefined { return this.permutations.pop(); } *getCustomComponentsRoot(): IterableIterator<[string, JsonValue, ComponentStore<JsonValue>]> { for (const [key, info] of this.components.entries()) { if (key.startsWith("minecraft:")) { continue; } yield [key, info, this.components]; } } *getCustomComponentsPermutation(): IterableIterator<[string, JsonValue, ComponentStore<JsonValue>]> { for (const perm of this.permutations) { for (const [key, info] of perm.components.entries()) { if (key.startsWith("minecraft:")) { continue; } yield [key, info, perm.components] } } } hasCustomComponent(): boolean { for (const [key, _] of this.components.entries()) { if (!key.startsWith("minecraft:")) { return true; } } for (const perm of this.permutations) { for (const [key, _] of perm.components.entries()) { if (!key.startsWith("minecraft:")) { return true; } } } return false; } jsonBlob(): JsonObject { const permutationInformation = Array.from(this.permutations.map((perm) => perm.jsonBlob())); const componentInformation = this.components.jsonBlob(); let traitInfo: JsonObject | undefined; if (this.traits.count()) { traitInfo = this.traits.jsonBlob(); } const des = this.description as JsonObject; des["identifier"] = this.id; if (traitInfo) { des["traits"] = traitInfo; } const endInfo: JsonObject = {}; endInfo["format_version"] = this.parsedFormat; const blockInfo: JsonObject = {}; blockInfo["description"] = des; blockInfo["components"] = componentInformation; blockInfo["permutations"] = permutationInformation; endInfo["minecraft:block"] = blockInfo; return endInfo; } }