Skip to content

Commit e69969b

Browse files
committed
wip structure set parsing
1 parent 719b3ba commit e69969b

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/datapack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface Datapack {
1616
modules: Set<Module>;
1717
}
1818

19-
const Modules = {
19+
export const Modules = {
2020
STRUCTURE_SET: "structure_set",
2121
BIOME: "biome",
2222
OVERWORLD: "overworld",

src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type Datapack, loadDatapack } from "./datapack";
22
import { type DatapackStoreEvents, datapackStore } from "./datapackStore";
3+
import { getStructureSets } from "./structureSet";
34

45
const fileUploadElement = document.getElementById("input")!;
56
fileUploadElement.addEventListener("change", onFileUploaded, { passive: true });
@@ -17,6 +18,8 @@ async function onFileUploaded(e: Event) {
1718
console.timeEnd("loadDatapacks");
1819
console.log(validDatapacks);
1920

21+
getStructureSets(validDatapacks);
22+
2023
datapackStore.add(validDatapacks);
2124
}
2225

src/structureSet.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Modules, type Datapack } from "./datapack";
2+
3+
type Placement = {
4+
type: string;
5+
[key: string]: unknown;
6+
};
7+
8+
interface StructureSet {
9+
id: string;
10+
modified: boolean;
11+
type: string;
12+
placement: Placement;
13+
originalPlacement: Readonly<Placement>;
14+
}
15+
16+
function constructStructureSetItem(
17+
id: string,
18+
originalPlacement: Readonly<Placement>
19+
): StructureSet {
20+
// TODO is deleting keys needed
21+
22+
return {
23+
id,
24+
placement: Object.assign({}, originalPlacement),
25+
originalPlacement,
26+
modified: false,
27+
type: originalPlacement.type,
28+
};
29+
}
30+
31+
export function setStructurePlacement(structure: StructureSet, data: Readonly<Placement>) {
32+
for (const [key, value] of Object.entries(data)) {
33+
structure.placement[key] = value;
34+
}
35+
36+
structure.modified = true;
37+
}
38+
39+
export function resetStructurePlacement(structure: StructureSet) {
40+
structure.placement = Object.assign({}, structure.originalPlacement);
41+
structure.modified = false;
42+
}
43+
44+
export function getStructureDatapacks(datapacks: ReadonlyArray<Datapack>) {
45+
return datapacks.filter((dp) => dp.modules.has(Modules.STRUCTURE_SET));
46+
}
47+
48+
export function getStructureSets(datapacks: ReadonlyArray<Datapack>) {
49+
for (const datapack of datapacks) {
50+
const divider = "/worldgen/structure_set/";
51+
const files = Object.entries(datapack.zip.files).filter(
52+
([filePath, _]) => filePath.includes(divider) && filePath.endsWith(".json")
53+
);
54+
55+
const structureSets = new Set(
56+
files.map(([filePath, _]) => filePathToSetName(filePath, divider))
57+
);
58+
59+
const jsons: Record<string, any> = {};
60+
61+
structureSets.forEach((set) =>
62+
files.filter((filePath, _) => {
63+
const splitSet = set.split(":");
64+
const file = `${splitSet[0]}${divider}${splitSet[1]}.json`;
65+
return filePath.includes(file);
66+
})
67+
);
68+
69+
console.log(structureSets);
70+
}
71+
}
72+
73+
function filePathToSetName(filePath: string, div: string) {
74+
let [prefix, fileName] = filePath.split(div);
75+
prefix = prefix.split("/").at(-1) ?? "unknown";
76+
fileName = fileName.replace(".json", "");
77+
78+
return `${prefix}:${fileName}`;
79+
}

0 commit comments

Comments
 (0)