Single module modding
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// recipes a shared script registers with ctx.recipes.register: there on the server and every client alike, for their
|
||||
// recipes the shared phase registers with ctx.recipes.register: there on the server and every client alike, for their
|
||||
// scripts and the crafting grid, without being sent
|
||||
import { assert, assertEquals, assertRejects } from "@std/assert";
|
||||
import { EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
@@ -16,7 +16,7 @@ import { copy_dir, load_as_player, test_game } from "./helpers.ts";
|
||||
// types, found by the item put in
|
||||
const SHARED = `
|
||||
import type { SharedContext } from "cubi/shared";
|
||||
export function setup(ctx: SharedContext) {
|
||||
export function shared(ctx: SharedContext) {
|
||||
for (const id of ctx.items.ids()) {
|
||||
const sifts = ctx.items.components(id)["testrecipes:siftable"] as { into: string } | undefined;
|
||||
if (sifts) ctx.recipes.register("testrecipes:sifting", { input: id, result: { id: sifts.into, count: 1 } });
|
||||
@@ -32,9 +32,9 @@ const SHARED = `
|
||||
}
|
||||
`;
|
||||
|
||||
// what the server and client scripts see of them while they set up. the server's registers the siftable component
|
||||
// what the server and client phases see of them while they run. the server's registers the siftable component
|
||||
const SEEN = (side: string) => `
|
||||
export function setup(ctx: any) {
|
||||
export function ${side}(ctx: any) {
|
||||
ctx.components?.register_item?.("testrecipes:siftable", {});
|
||||
(globalThis as any).testrecipes_${side} = ctx.recipes.list("testrecipes:sifting");
|
||||
(globalThis as any).testrecipes_${side}_parsed = {
|
||||
@@ -59,7 +59,7 @@ function recipe_mod(shared_extra = "", recipes: Record<string, unknown> = {}) {
|
||||
id: "testrecipes",
|
||||
name: "Test Recipes",
|
||||
version: "1.0.0",
|
||||
scripts: { shared: "scripts/shared.ts", server: "scripts/server.ts", client: "scripts/client.ts" },
|
||||
main: "scripts/main.ts",
|
||||
});
|
||||
const item = (id: string, components?: unknown) =>
|
||||
write(`items/${id}.json`, {
|
||||
@@ -72,6 +72,10 @@ function recipe_mod(shared_extra = "", recipes: Record<string, unknown> = {}) {
|
||||
write("scripts/shared.ts", SHARED.replace("SHARED_EXTRA", shared_extra));
|
||||
write("scripts/server.ts", SEEN("server"));
|
||||
write("scripts/client.ts", SEEN("client"));
|
||||
write(
|
||||
"scripts/main.ts",
|
||||
`export { shared } from "./shared.ts"; export { server } from "./server.ts"; export { client } from "./client.ts";`,
|
||||
);
|
||||
for (const [name, recipe] of Object.entries(recipes)) write(`recipes/${name}`, { format_version: 1, recipe });
|
||||
return dir;
|
||||
}
|
||||
@@ -95,7 +99,7 @@ async function load_client(dir: string) {
|
||||
await load_client_mods([listing], [packed]);
|
||||
}
|
||||
|
||||
Deno.test("recipes a shared script registers are on the server and clients, for their scripts and crafting", async () => {
|
||||
Deno.test("recipes a mod's shared phase registers are on the server and clients, for their scripts and crafting", async () => {
|
||||
const dir = recipe_mod();
|
||||
const t = await test_game(dir);
|
||||
assertEquals(seen.testrecipes_server, SIFTING);
|
||||
@@ -180,7 +184,7 @@ Deno.test("a mod can only register recipe types in its own namespace", async ()
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("a shared script lists the recipes there are so far, from files and the scripts before it", async () => {
|
||||
Deno.test("a mod's shared phase lists the recipes there are so far, from files and the scripts before it", async () => {
|
||||
// a washing recipe for every sifting one, whatever made it
|
||||
const dir = recipe_mod(
|
||||
`for (const sifting of ctx.recipes.list("testrecipes:sifting")) {
|
||||
@@ -200,7 +204,7 @@ Deno.test("a shared script lists the recipes there are so far, from files and th
|
||||
assertEquals(washing as unknown, [{ id: "testrecipes:sand", count: 1 }, { id: "testrecipes:gravel", count: 1 }]);
|
||||
});
|
||||
|
||||
Deno.test("a broken recipe from a shared script stops the mod loading, like a broken file", async () => {
|
||||
Deno.test("a broken recipe from a mod's shared phase stops the mod loading, like a broken file", async () => {
|
||||
const dir = recipe_mod(
|
||||
`ctx.recipes.register("shaped", { pattern: ["#"], key: {}, result: { id: "testrecipes:sand", count: 1 } });`,
|
||||
);
|
||||
@@ -208,7 +212,7 @@ Deno.test("a broken recipe from a shared script stops the mod loading, like a br
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("recipes can only be registered while shared scripts set up", async () => {
|
||||
Deno.test("recipes can only be registered while shared phases set up", async () => {
|
||||
const dir = recipe_mod(
|
||||
`(globalThis as any).testrecipes_late = () => ctx.recipes.register("testrecipes:sifting", {});`,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user