ctx.blocks.solid(id) api
This commit is contained in:
@@ -9,11 +9,12 @@ import {
|
||||
collision_shape_at,
|
||||
EMPTY_SHAPE,
|
||||
FULL_SHAPE,
|
||||
is_solid,
|
||||
model_shape,
|
||||
raycast_blocks,
|
||||
type Shape,
|
||||
} from "$/common/block_shapes.ts";
|
||||
import { block_from_json, validate_block } from "$/common/mod_data.ts";
|
||||
import { block_from_json, type BlockJson, validate_block } from "$/common/mod_data.ts";
|
||||
import { move_body } from "$/common/physics.ts";
|
||||
import { box_overlaps_block } from "$/common/utils.ts";
|
||||
|
||||
@@ -173,3 +174,24 @@ Deno.test("an x turn turns a model's shape with it", () => {
|
||||
// then y turns the south half to the west
|
||||
assertEquals(model_shape(slab, 90, 90), [[0, 0, 0, 0.5, 1, 1]]);
|
||||
});
|
||||
|
||||
Deno.test("a solid block is drawn in the solid layer and is a whole block to bump into, whatever its states", () => {
|
||||
const block = (json: Partial<BlockJson>) =>
|
||||
block_from_json({ id: "test:block", textures: "test:stone", ...json }).block;
|
||||
const states = [{ name: "age", bits: 1, default: 0 }];
|
||||
assert(is_solid(block({})));
|
||||
assert(
|
||||
is_solid(block({ states, variants: { "age=1": { y: 90, textures: "test:mossy" } } })),
|
||||
"turned, other textures",
|
||||
);
|
||||
assert(!is_solid(block({ render_layer: "cutout" })), "like leaves");
|
||||
assert(!is_solid(block({ render_layer: "translucent" })), "like glass");
|
||||
assert(!is_solid(block({ collision: false })));
|
||||
assert(!is_solid(block({ shape: [[0, 0, 0, 16, 8, 16]] })), "like a slab");
|
||||
assert(!is_solid(block({ states, variants: { "age=1": { model: "engine:cross" } } })), "not in one of its states");
|
||||
assert(!is_solid(block({ states, variants: { "age=1": { collision_shape: [[0, 0, 0, 16, 8, 16]] } } })));
|
||||
assert(
|
||||
is_solid(block({ shape: [[0, 0, 0, 16, 8, 16]], collision_shape: [[0, 0, 0, 16, 16, 16]] })),
|
||||
"what it bumps into counts",
|
||||
);
|
||||
});
|
||||
|
||||
@@ -99,6 +99,16 @@ export function setup(ctx: ServerContext) {
|
||||
},
|
||||
});
|
||||
|
||||
ctx.commands.register("solid", {
|
||||
description: "", usage: "",
|
||||
run([id], player) {
|
||||
try {
|
||||
player.send_message("solid " + ctx.blocks.solid(id));
|
||||
} catch (e) {
|
||||
player.send_message("error " + (e as Error).message);
|
||||
}
|
||||
},
|
||||
});
|
||||
ctx.commands.register("loaded", {
|
||||
description: "", usage: "",
|
||||
run([x, z], player) { player.send_message("loaded " + ctx.world.is_loaded(Number(x), Number(z))); },
|
||||
@@ -325,6 +335,19 @@ Deno.test("is_loaded says which chunks were ever generated, saved ones too, with
|
||||
Deno.removeSync(again.dir, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("ctx.blocks.solid says which blocks are solid, air isn't, and unknown blocks throw", async () => {
|
||||
const { send, take, dir } = await setup();
|
||||
const solid = (id: string) => {
|
||||
send(1, { type: "chat", text: `/solid ${id}` });
|
||||
return take(1).filter((m) => m.type === "chat").map((m) => m.text).at(-1);
|
||||
};
|
||||
assertEquals(solid("bworld:stone"), "solid true");
|
||||
assertEquals(solid("testmod:lamp"), "solid true");
|
||||
assertEquals(solid("bworld:air"), "solid false");
|
||||
assertEquals(solid("nobody:block"), "error unknown block nobody:block");
|
||||
Deno.removeSync(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" });
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
// a block is what the server ends up with
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { BlockRules } from "$/common/block_rules.ts";
|
||||
import { collision_shape_at, covers_face } from "$/common/block_shapes.ts";
|
||||
import { collision_shape_at, covers_face, is_solid } from "$/common/block_shapes.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { tag_api } from "$/common/tags.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
@@ -80,6 +80,7 @@ function client_of(t: Game) {
|
||||
blocks: {
|
||||
exists: (id) => info(id) !== undefined,
|
||||
components: (id) => structuredClone(info(id)!.components ?? {}),
|
||||
solid: (id) => id !== AIR_ID && is_solid(info(id)!),
|
||||
},
|
||||
world,
|
||||
components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) },
|
||||
|
||||
Reference in New Issue
Block a user