workflow for creating pack,
changeable pack version
This commit is contained in:
100
server/main.ts
100
server/main.ts
@@ -1,7 +1,7 @@
|
||||
import { SockpuppetPlus } from "@cgg/sockpuppet";
|
||||
import { serveDir, serveFile } from "@std/http/file-server";
|
||||
import { BearMetalStore } from "@bearmetal/store";
|
||||
import { ensureDir } from "@std/fs";
|
||||
import { ensureDir, ensureFile, exists } from "@std/fs";
|
||||
import { Router } from "./router.ts";
|
||||
|
||||
const installPath = Deno.env.get("BMP_INSTALL_DIR") || "./";
|
||||
@@ -92,8 +92,6 @@ router.route("/api/world")
|
||||
}
|
||||
const realWorldPath = Deno.realPathSync(worldPath);
|
||||
store.set("world", realWorldPath);
|
||||
store.set("packlocation", realWorldPath + "/datapacks/bmp_dev");
|
||||
await ensureDir(store.get("packlocation") as string);
|
||||
return new Response(null, { status: 200 });
|
||||
})
|
||||
.get((req) => {
|
||||
@@ -103,6 +101,88 @@ router.route("/api/world")
|
||||
return serveFile(req, worldPath);
|
||||
});
|
||||
|
||||
router.route("/api/pack")
|
||||
.get(() => {
|
||||
using store = new BearMetalStore();
|
||||
return new Response(
|
||||
JSON.stringify({ packName: store.get("packname") as string }),
|
||||
{ status: 200 },
|
||||
);
|
||||
})
|
||||
.post(async (req) => {
|
||||
using store = new BearMetalStore();
|
||||
const formData = await req.formData();
|
||||
const packName = formData.get("packName") as string;
|
||||
if (!packName) return new Response(null, { status: 400 });
|
||||
createPack(store, packName);
|
||||
|
||||
store.set("packname", packName);
|
||||
return new Response(JSON.stringify({ packName }), { status: 200 });
|
||||
});
|
||||
|
||||
router.route("/api/packs")
|
||||
.get(async () => {
|
||||
using store = new BearMetalStore();
|
||||
const packs: { name: string; path: string }[] = [];
|
||||
|
||||
const world = store.get("world") as string;
|
||||
for await (const pack of Deno.readDir(world + "/datapacks")) {
|
||||
if (
|
||||
pack.isDirectory &&
|
||||
await exists(world + "/datapacks/" + pack.name + "/bmp_dev")
|
||||
) {
|
||||
packs.push({
|
||||
name: pack.name,
|
||||
path: Deno.realPathSync(world + "/datapacks/" + pack.name),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return new Response("No BMP packs found", { status: 400 });
|
||||
});
|
||||
|
||||
router.route("/api/pack/version")
|
||||
.get(() => {
|
||||
using store = new BearMetalStore();
|
||||
const packMeta = Deno.readTextFileSync(
|
||||
store.get("packlocation") + "/pack.mcmeta",
|
||||
);
|
||||
const packMetaJson = JSON.parse(packMeta);
|
||||
return new Response(packMetaJson.pack.pack_format.toString(), {
|
||||
status: 200,
|
||||
});
|
||||
})
|
||||
.post(async (req) => {
|
||||
using store = new BearMetalStore();
|
||||
const version = await req.text();
|
||||
if (!version) return new Response(null, { status: 400 });
|
||||
|
||||
try {
|
||||
store.set("version", version);
|
||||
const packMeta = Deno.readTextFileSync(
|
||||
store.get("packlocation") + "/pack.mcmeta",
|
||||
);
|
||||
const packMetaJson = JSON.parse(packMeta);
|
||||
packMetaJson.pack.pack_format = parseInt(version);
|
||||
await Deno.writeTextFile(
|
||||
store.get("packlocation") + "/pack.mcmeta",
|
||||
JSON.stringify(packMetaJson),
|
||||
);
|
||||
} catch (e: any) {
|
||||
return new Response(e, { status: 500 });
|
||||
}
|
||||
return new Response(null, { status: 200 });
|
||||
});
|
||||
|
||||
router.route("/api/versions")
|
||||
.get(() => {
|
||||
const versions = Array.from(Deno.readDirSync(installPath + "pack_versions"))
|
||||
.filter((v) => v.isFile).map((version) =>
|
||||
version.name.replace(".json", "")
|
||||
);
|
||||
return new Response(JSON.stringify(versions), { status: 200 });
|
||||
});
|
||||
|
||||
sockpuppet.addHandler((req: Request) => {
|
||||
if (new URL(req.url).pathname.startsWith("/api")) return;
|
||||
|
||||
@@ -112,3 +192,17 @@ sockpuppet.addHandler((req: Request) => {
|
||||
});
|
||||
|
||||
sockpuppet.addHandler(router.handle);
|
||||
|
||||
async function createPack(store: BearMetalStore, packName: string) {
|
||||
const realWorldPath = store.get("world");
|
||||
store.set("packlocation", realWorldPath + "/datapacks/" + packName);
|
||||
await ensureDir(store.get("packlocation"));
|
||||
await ensureFile(store.get("packlocation") + "/bmp_dev");
|
||||
await ensureFile(store.get("packlocation") + "/pack.mcmeta");
|
||||
if (!Deno.readTextFileSync(store.get("packlocation") + "/pack.mcmeta")) {
|
||||
await Deno.writeTextFile(
|
||||
store.get("packlocation") + "/pack.mcmeta",
|
||||
`{"pack":{"pack_format":48,"description":"${packName}"}}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user