Skip to content

Commit

Permalink
Project Structures
Browse files Browse the repository at this point in the history
Realized a nice way that I liked, to structure the project module-wise.

I'm thinking about if `@thing/thing` paths could be nice for a project like this :)

Going to bring some of these other nice things over to NBTify, I got them working in jsmediatags today :D

Man, Marco Minnemann's solo work gets better every time I listen. Listening through from HWD&2K: Part I, and now I'm listening to Mouth of God. Music days are the best, listened to a bunch of Mike Keneally this morning too. Gonna try 'You Must Be This Tall' again, after this.

Oh yeah! Just discovered 'Thank You Scientist' the other day, WOW. They are super cool too. Going to buy their latest release, at the same time as when Marco releases his new solo album this Friday. Bandcamp Friday for both :)
  • Loading branch information
Offroaders123 committed May 2, 2023
1 parent 9372efd commit 37bb380
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 100 deletions.
25 changes: 20 additions & 5 deletions package-lock.json

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

14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{
"name": "region-types",
"private": true,
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc --project ./tsconfig.build.json",
"dev": "tsc -w"
},
"dependencies": {
"nbtify": "^1.20.1"
},
"devDependencies": {
"@types/node": "^18.16.2"
"@types/node": "^18.16.2",
"typescript": "^5.0.4"
}
}
}
28 changes: 28 additions & 0 deletions src/bedrock/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Region, Chunk, World } from "../index.js";
import type { JavaWorld } from "../java/index.js";
import type { LegacyConsoleWorld } from "../legacy-console/index.js";

export declare class BedrockChunk extends Chunk {
readonly kind = "bedrock";

/**
* The version the chunk was written in; Ex: `1.19.20`
*/
readonly Version: string;
}

export declare class BedrockRegion extends Region {
readonly kind = "bedrock";

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

export declare class BedrockWorld extends World {
readonly kind: "bedrock";

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

toBedrock(): Promise<BedrockWorld>;
toJava(): Promise<JavaWorld>;
toLegacyConsole(): Promise<LegacyConsoleWorld>;
}
88 changes: 0 additions & 88 deletions src/index.d.ts

This file was deleted.

45 changes: 45 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export * from "./bedrock/index.js";
export * from "./java/index.js";
export * from "./legacy-console/index.js";

import type { BedrockWorld } from "./bedrock/index.js";
import type { JavaWorld } from "./java/index.js";
import type { LegacyConsoleWorld } from "./legacy-console/index.js";

export type Kind = "bedrock" | "java" | "legacy-console";

export abstract class Chunk {
abstract readonly kind: Kind;
}

export abstract class Region {
abstract readonly kind: Kind;
abstract [Symbol.iterator](): IterableIterator<Chunk>;
}

export abstract class World {
abstract readonly kind: Kind;
abstract [Symbol.iterator](): IterableIterator<Region>;

abstract toBedrock(): Promise<BedrockWorld>;
abstract toJava(): Promise<JavaWorld>;
abstract toLegacyConsole(): Promise<LegacyConsoleWorld>;
}

/**
* Reads a world from a single source file, like `.zip` or `.mcworld`.
*/
export declare function readWorld<T extends World = World>(file: Uint8Array | ArrayBufferLike): Promise<T>;
export declare function readWorld(file: Uint8Array | ArrayBufferLike): Promise<World>;

/**
* Reads the world from the given directory in the file system.
* Returns a World object which can manipulate properties of the world from a high level.
*/
export declare function openWorld<T extends World = World>(path: string): Promise<T>;
export declare function openWorld(path: string): Promise<World>;

/**
* Saves a world back to a given directory in the file system.
*/
export declare function saveWorld(path: string, world: World): Promise<void>;
23 changes: 23 additions & 0 deletions src/java/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Chunk, Region, World } from "../index.js";
import type { BedrockWorld } from "../bedrock/index.js";
import type { LegacyConsoleWorld } from "../legacy-console/index.js";

export class JavaChunk extends Chunk {
readonly kind = "java";
}

export declare class JavaRegion extends Region {
readonly kind = "java";

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

export declare class JavaWorld extends World {
readonly kind = "java";

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

toBedrock(): Promise<BedrockWorld>;
toJava(): Promise<JavaWorld>;
toLegacyConsole(): Promise<LegacyConsoleWorld>;
}
23 changes: 23 additions & 0 deletions src/legacy-console/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Chunk, Region, World } from "../index.js";
import type { BedrockWorld } from "../bedrock/index.js";
import type { JavaWorld } from "../java/index.js";

export declare class LegacyConsoleChunk extends Chunk {
readonly kind = "legacy-console";
}

export declare class LegacyConsoleRegion extends Region {
readonly kind = "legacy-console";

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

export declare class LegacyConsoleWorld extends World {
readonly kind = "legacy-console";

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

toBedrock(): Promise<BedrockWorld>;
toJava(): Promise<JavaWorld>;
toLegacyConsole(): Promise<LegacyConsoleWorld>;
}
2 changes: 1 addition & 1 deletion src/demo.ts → test/demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { openWorld, saveWorld, type JavaWorld } from "./index.js";
import { openWorld, saveWorld, type JavaWorld } from "../src/index.js";

const javaWorld = await openWorld<JavaWorld>("~/.minecraft/saves/Solo-Survival/");
console.log(javaWorld.kind);
Expand Down
4 changes: 2 additions & 2 deletions src/demo2.ts → test/demo2.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { readFile } from "node:fs/promises";
import { BedrockWorld } from "./index.js";
import { readWorld, type BedrockWorld } from "../src/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);
const world: BedrockWorld = await readWorld(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){
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"noEmit": false,
"declaration": false,
"sourceMap": false
},
"include": [
"./src/**/*"
],
"exclude": [
"./test"
]
}
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"module": "ESNext",
"moduleResolution": "NodeNext",
"target": "ESNext",
"noEmit": true,
"strict": true,
"noImplicitOverride": true
}
Expand Down

0 comments on commit 37bb380

Please sign in to comment.