diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index 8c70090..ff219a7 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -117,6 +117,10 @@ export interface BlockComponent
{
// 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
diff --git a/common/physics.ts b/common/physics.ts
index da224b6..732b520 100644
--- a/common/physics.ts
+++ b/common/physics.ts
@@ -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,
diff --git a/docs/modding/blocks/components.md b/docs/modding/blocks/components.md
index 0fd1ca6..cf5aa85 100644
--- a/docs/modding/blocks/components.md
+++ b/docs/modding/blocks/components.md
@@ -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
diff --git a/docs/modding/entities/index.md b/docs/modding/entities/index.md
index a29e8d7..73f3de1 100644
--- a/docs/modding/entities/index.md
+++ b/docs/modding/entities/index.md
@@ -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).
diff --git a/mods/dangerous_business/blocks/death.json b/mods/dangerous_business/blocks/death.json
new file mode 100644
index 0000000..f28faba
--- /dev/null
+++ b/mods/dangerous_business/blocks/death.json
@@ -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": {}
+ }
+ }
+}
diff --git a/mods/dangerous_business/models/empty.json b/mods/dangerous_business/models/empty.json
new file mode 100644
index 0000000..8befa4f
--- /dev/null
+++ b/mods/dangerous_business/models/empty.json
@@ -0,0 +1,7 @@
+{
+ "format_version": 1,
+ "model": {
+ "id": "dangerous_business:empty",
+ "elements": []
+ }
+}
diff --git a/mods/dangerous_business/scripts/main.ts b/mods/dangerous_business/scripts/main.ts
index 7aca94c..33fc0e1 100644
--- a/mods/dangerous_business/scripts/main.ts
+++ b/mods/dangerous_business/scripts/main.ts
@@ -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(
diff --git a/server/game/entity/entity.ts b/server/game/entity/entity.ts
index a290193..622bc8f 100644
--- a/server/game/entity/entity.ts
+++ b/server/game/entity/entity.ts
@@ -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;
diff --git a/server/game/entity/entity_manager.ts b/server/game/entity/entity_manager.ts
index 246277e..fd9c95f 100644
--- a/server/game/entity/entity_manager.ts
+++ b/server/game/entity/entity_manager.ts
@@ -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
diff --git a/server/game/entity/living_entity.ts b/server/game/entity/living_entity.ts
index c471e71..7d20569 100644
--- a/server/game/entity/living_entity.ts
+++ b/server/game/entity/living_entity.ts
@@ -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
diff --git a/server/game/game_server.ts b/server/game/game_server.ts
index ac3be73..5dd54b2 100644
--- a/server/game/game_server.ts
+++ b/server/game/game_server.ts
@@ -274,6 +274,7 @@ export class GameServer {
);
}
},
+ inside_blocks: (entity) => this.#inside_blocks(entity),
entity_loot: (entity, source) => {
const table = EverythingRegistry.get