Real ctx.world.is_loaded implementation

This commit is contained in:
2026-10-20 14:00:21 -03:00
parent 2c1eaf5735
commit 987e7a0f9a
5 changed files with 49 additions and 3 deletions
+31
View File
@@ -99,6 +99,11 @@ export function setup(ctx: ServerContext) {
},
});
ctx.commands.register("loaded", {
description: "", usage: "",
run([x, z], player) { player.send_message("loaded " + ctx.world.is_loaded(Number(x), Number(z))); },
});
// brigadier's way, like neoforge's
const { literal, argument, types } = ctx.commands;
ctx.commands.register(
@@ -294,6 +299,32 @@ Deno.test("block states start at their defaults, can be changed, and are synced
Deno.removeSync(again.dir, { recursive: true });
});
Deno.test("is_loaded says which chunks were ever generated, saved ones too, without generating any", async () => {
const { game, send, take, dir } = await setup();
const loaded = (x: number, z: number) => {
send(1, { type: "chat", text: `/loaded ${x} ${z}` });
return take(1).filter((m) => m.type === "chat").map((m) => m.text).at(-1);
};
assertEquals(loaded(0.5, 0.5), "loaded true");
assertEquals(loaded(16 * 1000, 0), "loaded false");
assertEquals(loaded(16 * 1000, 0), "loaded false", "asking didn't generate it");
assert(!game.world.has_chunk(1000, 0));
game.world.build(-500, 0);
assertEquals(loaded(-16 * 500 + 3, 15.5), "loaded true");
const saved = game.save();
Deno.removeSync(dir, { recursive: true });
const again = await setup(saved);
assert(!again.game.world.has_chunk(-500, 0));
const fallbacks = again.game.world.fallbacks;
again.send(1, { type: "chat", text: `/loaded ${-16 * 500} 0` });
assertEquals(again.take(1).filter((m) => m.type === "chat").at(-1).text, "loaded true", "it's saved");
again.game.world.get_block_id(-16 * 500, 100, 0);
assertEquals(again.game.world.fallbacks, fallbacks, "reading it decoded it from the save, generating nothing");
Deno.removeSync(again.dir, { recursive: true });
});
Deno.test("inventory on_change fires for the slots that changed", async () => {
const { game, send, log, dir } = await setup();
send(1, { type: "chat", text: "/give bworld:stone 3" });