Skip to content

Commit

Permalink
Regions & Chunks + Iterators
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Offroaders123 committed Apr 28, 2023
1 parent 9b39bd3 commit 9372efd
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"dependencies": {
"nbtify": "^1.20.1"
},
"devDependencies": {
"@types/node": "^18.16.2"
}
}
17 changes: 17 additions & 0 deletions src/demo2.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
41 changes: 38 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<Chunk>;
}

export declare class BedrockRegion extends Region {
[Symbol.iterator](): IterableIterator<BedrockChunk>;
}

export declare class JavaRegion extends Region {
[Symbol.iterator](): IterableIterator<JavaChunk>;
}

export declare class LegacyConsoleRegion extends Region {
[Symbol.iterator](): IterableIterator<LegacyConsoleChunk>;
}

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<Region>;

abstract toBedrock(): Promise<BedrockWorld>;
abstract toJava(): Promise<JavaWorld>;
abstract toLegacyConsole(): Promise<LegacyConsoleWorld>;
Expand All @@ -19,6 +42,10 @@ export declare abstract class World {
export declare class BedrockWorld extends World {
readonly kind: "bedrock";

constructor(data?: Uint8Array);

[Symbol.iterator](): IterableIterator<BedrockRegion>;

toBedrock(): Promise<BedrockWorld>;
toJava(): Promise<JavaWorld>;
toLegacyConsole(): Promise<LegacyConsoleWorld>;
Expand All @@ -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<JavaRegion>;

toBedrock(): Promise<BedrockWorld>;
toJava(): Promise<JavaWorld>;
toLegacyConsole(): Promise<LegacyConsoleWorld>;
Expand All @@ -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<LegacyConsoleRegion>;

toBedrock(): Promise<BedrockWorld>;
toJava(): Promise<JavaWorld>;
toLegacyConsole(): Promise<LegacyConsoleWorld>;
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"module": "ESNext",
"moduleResolution": "NodeNext",
"target": "ESNext",
"strict": true,
"noImplicitOverride": true
}
}

0 comments on commit 9372efd

Please sign in to comment.