This commit is contained in:
2026-11-16 23:49:42 -03:00
parent 94b0e3789c
commit eedaa955f6
13 changed files with 148 additions and 2 deletions
+7
View File
@@ -117,6 +117,10 @@ export interface BlockComponent<P = any> {
// an entity is standing on it, every tick the entity moves, like minecraft's updateEntityAfterFallOn: an item
// dropped into a basin going in. items lying still move every tick too
on_entity_stand?(block: BlockRef, params: P, entity: EntityRef): void;
// an entity's box is in it, every tick the entity moves, like neoforge's entityInside: a block that kills or slows
// what walks into it. players' after each move their client sends. any block, whatever its shape, so one that
// cares where in it the entity is checks the entity's position
on_entity_inside?(block: BlockRef, params: P, entity: EntityRef): void;
// what pipes, buckets and other blocks put fluid in and take it out by, reached from face, or from no side in
// particular when it's undefined, like neoforge's Capabilities.FluidHandler.BLOCK. undefined when it has none
// there. see ServerFluidApi.block_handler
@@ -447,6 +451,9 @@ export interface EntityRef {
// adds to how fast it's moving, in blocks a second, like minecraft's Entity.push: a fan blowing it along
push(x: number, y: number, z: number): void;
remove(): void;
// like neoforge's Entity.kill: a player or a mob dies, even when invulnerable, and anything else is removed. cause
// is for events and the death message, like LivingRef.damage's. mods' entity_damage can still cancel it
kill(cause?: string): void;
// what an item lying around is, a copy. undefined for every other kind of entity
get_item(): ItemStack | undefined;
// changes what an item lying around is, like ItemEntity.setItem. undefined or a count of 0 removes it. throws for
+14
View File
@@ -231,6 +231,19 @@ function overlaps_solid(min: number[], max: number[], is_solid: BlockCollision)
return false;
}
// every block the body's box is in, whatever's there, like minecraft's checkInsideBlocks. a box resting exactly on
// a face isn't in the block on the other side
export function blocks_inside(body: Body, visit: (x: number, y: number, z: number) => void) {
const half = body.width / 2;
for (let x = Math.floor(body.x - half + EPSILON); x <= Math.floor(body.x + half - EPSILON); x++) {
for (let y = Math.floor(body.y + EPSILON); y <= Math.floor(body.y + body.height - EPSILON); y++) {
for (let z = Math.floor(body.z - half + EPSILON); z <= Math.floor(body.z + half - EPSILON); z++) {
visit(x, y, z);
}
}
}
}
// in water like minecraft's wasTouchingWater, close enough: its feet or the middle of its box are in fluid
export function in_water(body: Body, is_fluid: (x: number, y: number, z: number) => boolean): boolean {
const x = Math.floor(body.x);
@@ -390,6 +403,7 @@ export const physics = {
move_body,
back_off_from_edge,
box_is_free,
blocks_inside,
in_water,
eyes_in_water,
fluid_flow,
+16
View File
@@ -73,6 +73,7 @@ the block and the component.
| `on_state_change(block, params, before)` | Its own states change without it being replaced, by `set_state` or a rule. See [Changes](#changes). | |
| `on_scheduled_tick(block, params)` | A tick it asked for comes. See [Scheduled Ticks](#scheduled-ticks). | |
| `on_entity_stand(block, params, entity)` | An entity standing on it moves, every tick it does. See [Entities on It](#entities-on-it). | |
| `on_entity_inside(block, params, entity)` | An entity whose box is in it moves, every tick it does. See [Entities on It](#entities-on-it). | |
| `fluid_handler(block, params, face?)` | Something reaches for its tanks from `face`. See [Fluid Handlers](../resources/fluids.md#fluid-handlers). | a `FluidHandler`, or `undefined` |
| `export_data(block, params)` | It goes into a schematic. See [Schematics](../misc/schematics.md#block-data). | its data for the schematic |
@@ -98,6 +99,21 @@ ctx.components.register_block("create:basin", {
});
```
`on_entity_inside` runs every tick an entity moves while its box is in the block, like NeoForge's `entityInside`, for
each block its box is in. Players move on their own clients, so for them it runs after each move their client sends, and
they come as an `EntityRef` of type `cubi:player`: `ctx.players.get(entity.id)` gives the
[`Player`](../entities/players.md). Being against a block's side isn't being in it. It runs whatever the block's shape,
so a block that only cares about part of itself checks the entity's `position`. Dangerous Business's death block kills
whatever walks or falls into it:
```ts
ctx.components.register_block("dangerous_business:death", {
on_entity_inside(_block, _params, entity) {
entity.kill("death_block");
},
});
```
### Changes
`on_neighbor_change` is the server's side of the [rule](#neighbors) with the same name, like NeoForge's
+5
View File
@@ -155,6 +155,7 @@ interface EntityRef {
teleport(x: number, y: number, z: number): void;
push(x: number, y: number, z: number): void; // adds to its velocity
remove(): void;
kill(cause?: string): void; // players and mobs die, anything else is removed
get_item(): ItemStack | undefined; // for an item lying around, a copy
set_item(item: ItemStack | undefined): void; // for an item lying around, undefined takes it away
}
@@ -163,6 +164,10 @@ interface EntityRef {
Once an entity is removed, by a script or anything else, it stays removed, and teleporting it does nothing. Check
`removed` before relying on one you kept from earlier.
`kill` is like NeoForge's `Entity.kill`: a player or a mob dies even when it's invulnerable, the way falling out of the
world hurts it, and `cause` goes to `entity_damage` and the death message like [`damage`](players.md)'s does. Mods'
`before.entity_damage` can still cancel it. Anything that can't die, like an item lying around, is removed instead.
`push` adds to how fast it's moving, like Minecraft's `Entity.push`: a fan blowing it along does it every tick. Players
have a `push` too, see [Players](players.md#pushing).
+15
View File
@@ -0,0 +1,15 @@
{
"format_version": 1,
"block": {
"id": "dangerous_business:death",
"textures": "cubi:missing",
"model": "dangerous_business:empty",
"item_model": "cubi:cube",
"render_layer": "cutout",
"shape": [[0, 0, 0, 16, 16, 16]],
"collision": false,
"components": {
"dangerous_business:death": {}
}
}
}
@@ -0,0 +1,7 @@
{
"format_version": 1,
"model": {
"id": "dangerous_business:empty",
"elements": []
}
}
+10
View File
@@ -9,6 +9,7 @@ export { shared } from "./shared.ts";
export function server(ctx: ServerContext) {
register_facility_door(ctx);
register_door(ctx);
register_death(ctx);
register_commands(ctx);
}
@@ -103,6 +104,15 @@ function register_door(ctx: ServerContext) {
});
}
// an invisible block that kills whatever walks or falls into it, for the bottoms of pits and the like
function register_death(ctx: ServerContext) {
ctx.components.register_block("dangerous_business:death", {
on_entity_inside(_block, _params, entity) {
entity.kill("death_block");
},
});
}
function register_commands(ctx: ServerContext) {
const { literal, argument, types } = ctx.commands;
ctx.commands.register(
+9
View File
@@ -104,6 +104,8 @@ export interface EntityLevel {
entity_loot(entity: Entity, source: DamageSource): ItemStack[];
// it's standing on the block at x y z after moving, for the block's components, see on_entity_stand
stands_on(entity: Entity, x: number, y: number, z: number): void;
// it moved, for the components of the blocks its box is in, see on_entity_inside
inside_blocks(entity: Entity): void;
}
// an entity as level.json keeps it, see Entity.save. each kind adds its own fields
@@ -308,6 +310,7 @@ export abstract class Entity {
if (this.on_ground) {
this.level.stands_on(this, Math.floor(this.x), Math.floor(this.y - STANDING_DEPTH), Math.floor(this.z));
}
this.level.inside_blocks(this);
this.update_fall_distance(this.y - from_y);
return collisions;
}
@@ -362,6 +365,12 @@ export abstract class Entity {
this.fire = 0;
}
// like minecraft's Entity.kill: ones that can die do, even when invulnerable, the rest go away. cause is for events
// and the death message
kill(_cause: string) {
this.remove();
}
// takes it out of the world. the entity manager tells clients and forgets it
remove() {
if (this.removed) return;
+6
View File
@@ -30,6 +30,8 @@ export interface EntityHost {
entity_loot?(entity: Entity, source: DamageSource): ItemStack[];
// see EntityLevel.stands_on
stands_on?(entity: Entity, x: number, y: number, z: number): void;
// see EntityLevel.inside_blocks
inside_blocks?(entity: Entity): void;
}
// what a client was last told about an entity, so only changes get sent
@@ -188,6 +190,10 @@ export class EntityManager implements EntityLevel {
this.#host.stands_on?.(entity, x, y, z);
}
inside_blocks(entity: Entity) {
this.#host.inside_blocks?.(entity);
}
// ticking
// every entity active says should move, with their passengers. entities removed along the way are forgotten
+5
View File
@@ -313,6 +313,11 @@ export abstract class LivingEntity extends Entity {
return true;
}
// mods can still cancel it, like any damage
override kill(cause: string) {
this.hurt(Number.MAX_VALUE, { cause, type: "absolute", bypasses_invulnerability: true });
}
// after mods and the hurt cooldown
actually_hurt(amount: number, source: DamageSource) {
// absolute damage, take it and theres no counterplay
+24
View File
@@ -274,6 +274,7 @@ export class GameServer {
);
}
},
inside_blocks: (entity) => this.#inside_blocks(entity),
entity_loot: (entity, source) => {
const table = EverythingRegistry.get<EntityJson>("entities", entity.type)?.loot_table;
if (!table) return [];
@@ -1153,6 +1154,7 @@ export class GameServer {
player.sneaking = message.sneaking === true;
this.#broadcast(player_move(player), player);
this.#update_view(player);
this.#inside_blocks(player);
break;
}
case "view_distance":
@@ -2197,6 +2199,28 @@ export class GameServer {
}
}
// the components of every block an entity's box is in see it, after it moved. players move on their clients, so
// theirs is after each move they send
#inside_blocks(entity: Entity) {
const { x, y, z } = entity;
physics.blocks_inside(entity, (bx, by, bz) => {
// a block before this one killed it, took it away or moved it somewhere else
if (entity.removed || (entity instanceof LivingEntity && entity.dead)) return;
if (entity.x !== x || entity.y !== y || entity.z !== z) return;
const id = this.world.get_block_id(bx, by, bz);
const components = this.mods.components_with(id, "on_entity_inside");
if (components.length === 0) return;
const block = this.mods.block_ref(bx, by, bz, id);
for (const { mod, id: component_id, component, params } of components) {
run_guarded(
mod,
`${component_id} on_entity_inside`,
() => component.on_entity_inside!(block, params, entity_ref(entity)),
);
}
});
}
// feet on top of something solid, anywhere under the player's box: a thin box just under their feet touches a
// block's collision shape. shapes, not whole blocks, so standing on a fence, a slab or a path counts too
#on_ground(x: number, y: number, z: number) {
+3
View File
@@ -1685,6 +1685,9 @@ export function entity_ref(entity: Entity): EntityRef {
remove() {
entity.remove();
},
kill(cause = "script") {
entity.kill(String(cause));
},
get_item: () => entity instanceof ItemEntity ? item_copy(entity.item) : undefined,
set_item(item) {
if (!(entity instanceof ItemEntity)) throw new Error(`${entity.type} isn't an item lying around`);
+27 -2
View File
@@ -2,8 +2,9 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// dangerous business's placement components: slabs and other blocks that go in the top or bottom half, blocks that
// join the same block beside them, and blocks that pick some of their looks when placed
import { assertEquals } from "@std/assert";
// join the same block beside them, and blocks that pick some of their looks when placed. and the death block
import { assert, assertEquals, assertNotEquals } from "@std/assert";
import { ItemStack } from "@/common/inventory.ts";
import { get_state_value } from "@/common/utils.ts";
import { copy_dir, test_game } from "./helpers.ts";
@@ -11,6 +12,7 @@ const HALF = "dangerous_business:concrete_wall_slab";
const CONNECTS = "dangerous_business:floor_grate";
const RANDOM_LOOK = "dangerous_business:water_puddle";
const FLOOR = "dangerous_business:concrete_floor";
const DEATH = "dangerous_business:death";
// alice on a concrete floor at y 100, south of 0 100 0 and looking north, holding each of the blocks. it's a game of
// its own, which tests playing mods/ leave out
@@ -81,3 +83,26 @@ Deno.test("a block with a random look picks the same one wherever it's placed ag
for (const look of first) assertEquals(Number(look.split(" ")[0]) < 3, true);
assertEquals(new Set(first).size > 1, true);
});
Deno.test("the death block kills a player who walks into it, but not one just beside it", async () => {
const { t } = await setup();
t.game.set_block(0, 101, 1, DEATH);
// touching its side isn't being in it
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.3, yaw: 0, pitch: 0 });
assert(!t.take(1).some((m) => m.type === "chat" && m.text === "alice died"));
// its head goes into it
t.send(1, { type: "move", x: 0.5, y: 100, z: 1.9, yaw: 0, pitch: 0 });
assert(t.take(1).some((m) => m.type === "chat" && m.text === "alice died"));
const player = t.game.players().find((p) => p.name === "alice")!;
assertEquals(player.health, player.max_health);
assertNotEquals([player.x, player.y, player.z], [0.5, 100, 1.9]);
});
Deno.test("the death block takes away an item falling into it", async () => {
const { t } = await setup();
t.game.set_block(2, 100, 0, DEATH);
const item = t.game.spawn_item(2.5, 101.5, 0.5, new ItemStack(FLOOR));
for (let i = 0; i < 40 && !item.removed; i++) t.game.tick();
assert(item.removed);
});