From 9372efd90f74a32ae448320ff8c3526f36bef86e Mon Sep 17 00:00:00 2001 From: Offroaders123 <65947371+Offroaders123@users.noreply.github.com> Date: Thu, 27 Apr 2023 17:15:26 -0700 Subject: [PATCH] Regions & Chunks + Iterators I like the idea of being able to loop over the data using iterators, it seems like it could be a cool use case to find a certain chunk at a given index, things like that. You could do `BedrockRegion.find(x,y)` for a given Chunk, and it would give you either a Chunk object, or `null`. Something neat like that. --- package-lock.json | 11 ++++++++++- package.json | 3 +++ src/demo2.ts | 17 +++++++++++++++++ src/index.d.ts | 41 ++++++++++++++++++++++++++++++++++++++--- tsconfig.json | 11 +++++++++++ 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 src/demo2.ts create mode 100644 tsconfig.json diff --git a/package-lock.json b/package-lock.json index 76d2dc1..680fefc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,22 @@ { - "name": "Region-Defs", + "name": "Region-Types", "lockfileVersion": 3, "requires": true, "packages": { "": { "dependencies": { "nbtify": "^1.20.1" + }, + "devDependencies": { + "@types/node": "^18.16.2" } }, + "node_modules/@types/node": { + "version": "18.16.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.16.2.tgz", + "integrity": "sha512-GQW/JL/5Fz/0I8RpeBG9lKp0+aNcXEaVL71c0D2Q0QHDTFvlYKT7an0onCUXj85anv7b4/WesqdfchLc0jtsCg==", + "dev": true + }, "node_modules/nbtify": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/nbtify/-/nbtify-1.20.1.tgz", diff --git a/package.json b/package.json index ce934ae..3154cdc 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,8 @@ { "dependencies": { "nbtify": "^1.20.1" + }, + "devDependencies": { + "@types/node": "^18.16.2" } } diff --git a/src/demo2.ts b/src/demo2.ts new file mode 100644 index 0000000..a03bef9 --- /dev/null +++ b/src/demo2.ts @@ -0,0 +1,17 @@ +import { readFile } from "node:fs/promises"; +import { BedrockWorld } from "./index.js"; + +// Reads the world save as a single file from the file system. +const buffer: Buffer = await readFile("./Survival-World.mcworld"); + +// Creates a 'world object' from that world file binary data. +const world: BedrockWorld = new BedrockWorld(buffer); + +// Could be neat if you could just use a simple 'for' loop to iterate over all the Regions in the world. +for (const region of world){ + // You could then also go over each chunk in those regions, maybe. + for (const chunk of region){ + // 'Version' would be referencing one of the chunk's NBT header values. This is already something used by the game. + console.log(chunk.Version); + } +} \ No newline at end of file diff --git a/src/index.d.ts b/src/index.d.ts index da8ae93..ae575f8 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,16 +1,39 @@ export declare type Kind = "bedrock" | "java" | "legacy-console"; -export declare abstract class Chunk { - abstract readonly version: number; +export declare abstract class Region { + abstract [Symbol.iterator](): IterableIterator; } +export declare class BedrockRegion extends Region { + [Symbol.iterator](): IterableIterator; +} + +export declare class JavaRegion extends Region { + [Symbol.iterator](): IterableIterator; +} + +export declare class LegacyConsoleRegion extends Region { + [Symbol.iterator](): IterableIterator; +} + +export declare abstract class Chunk {} + export declare class BedrockChunk extends Chunk { - readonly version: number; + /** + * The version the chunk was written in; Ex: `1.19.20` + */ + readonly Version: string; } +export declare class JavaChunk extends Chunk {} + +export declare class LegacyConsoleChunk extends Chunk {} + export declare abstract class World { abstract readonly kind: Kind; + abstract [Symbol.iterator](): IterableIterator; + abstract toBedrock(): Promise; abstract toJava(): Promise; abstract toLegacyConsole(): Promise; @@ -19,6 +42,10 @@ export declare abstract class World { export declare class BedrockWorld extends World { readonly kind: "bedrock"; + constructor(data?: Uint8Array); + + [Symbol.iterator](): IterableIterator; + toBedrock(): Promise; toJava(): Promise; toLegacyConsole(): Promise; @@ -27,6 +54,10 @@ export declare class BedrockWorld extends World { export declare class JavaWorld extends World { readonly kind: "java"; + constructor(data?: Uint8Array); + + [Symbol.iterator](): IterableIterator; + toBedrock(): Promise; toJava(): Promise; toLegacyConsole(): Promise; @@ -35,6 +66,10 @@ export declare class JavaWorld extends World { export declare class LegacyConsoleWorld extends World { readonly kind: "legacy-console"; + constructor(data?: Uint8Array); + + [Symbol.iterator](): IterableIterator; + toBedrock(): Promise; toJava(): Promise; toLegacyConsole(): Promise; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..e330574 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "rootDir": "./src", + "outDir": "./dist", + "module": "ESNext", + "moduleResolution": "NodeNext", + "target": "ESNext", + "strict": true, + "noImplicitOverride": true + } +} \ No newline at end of file