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