From f0cf4e4eebeba3fa3398a614a377d3c99840d601 Mon Sep 17 00:00:00 2001 From: Paula Date: Tue, 29 Sep 2026 19:09:20 -0300 Subject: [PATCH] Prevent players from placing blocks inside themselves --- client/entity/local_player.ts | 6 ++++++ client/game_mode.ts | 27 ++++++++++++++++++++++++--- common/utils.ts | 13 +++++++++++++ server/game/game_server.ts | 24 ++++++++++++++++++++++++ tests/mod_entity_test.ts | 26 ++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 3 deletions(-) diff --git a/client/entity/local_player.ts b/client/entity/local_player.ts index 96a191a..ffcb2e3 100644 --- a/client/entity/local_player.ts +++ b/client/entity/local_player.ts @@ -188,6 +188,12 @@ export class LocalPlayer extends Player { } } + // tells the server where it is right away, before something that depends on it like placing a block, so the + // server doesn't decide with where it was up to SEND_POSITION_TICKS ago + send_position_now() { + this.#send_position(true); + } + #send_position(now: boolean) { this.#ticks_since_sent += 1; if (!now && this.#ticks_since_sent < SEND_POSITION_TICKS) { diff --git a/client/game_mode.ts b/client/game_mode.ts index abf96dc..b7f879c 100644 --- a/client/game_mode.ts +++ b/client/game_mode.ts @@ -4,7 +4,10 @@ import type { Client } from "./client.ts"; import type { BlockHitResult } from "./level/client_level.ts"; import type { Container } from "$/common/inventory.ts"; import { AIR_ID, type ContainerKey } from "$/common/protocol.ts"; -import { can_place, placement_target } from "$/common/utils.ts"; +import { box_overlaps_block, can_place, placement_target } from "$/common/utils.ts"; +import type { EntityJson } from "$/common/mod_data.ts"; +import { RemoteEntity } from "./entity/remote_entity.ts"; +import { Player } from "./entity/player.ts"; // breaking and using blocks against a server, like minecraft's MultiPlayerGameMode. the client times breaking // and guesses the results so they feel instant, the server decides what really happens @@ -52,6 +55,8 @@ export class MultiPlayerGameMode { // right click on a block use_item_on(hit: BlockHitResult) { const level = this.client.level; + // the server checks that the block isn't going inside the player with where it is now, like this does + this.client.player.send_position_now(); this.client.connection.send({ type: "use_block", x: hit.x, y: hit.y, z: hit.z, face: hit.face }); // guess that it places the held block, unless the block does something when used. the same rules as the server: @@ -64,11 +69,14 @@ export class MultiPlayerGameMode { const held = this.#held_item(); // items with components might do something else on the server, like on_use const place_id = held?.components ? undefined : held?.block_id; + const place_block = place_id !== undefined + ? EverythingRegistry.get("blocks", place_id) + : undefined; const placeable = place_id !== undefined && can_place( - EverythingRegistry.get("blocks", place_id), + place_block, current === AIR ? undefined : EverythingRegistry.get_by_id("blocks", current), below_id, - ); + ) && !(place_block?.has_collision && this.#occupied(target.x, target.y, target.z)); if (!block_info.interactive && place_id && placeable) { level.add_block({ ...target, id: place_id }); const slot = this.#held_slot(); @@ -92,6 +100,19 @@ export class MultiPlayerGameMode { this.client.connection.send({ type: "use_item" }); } + // whether a mob's or player's collision box overlaps the block, the server doesn't let solid blocks go there. mobs + // are the mod entities that aren't scripted, players are this one and the others + #occupied(x: number, y: number, z: number) { + for (const entity of this.client.level.entities.values()) { + const json = entity instanceof RemoteEntity + ? EverythingRegistry.get("entities", entity.type) + : undefined; + const blocks = entity instanceof Player || (json !== undefined && json.behavior !== "scripted"); + if (blocks && box_overlaps_block(entity, x, y, z)) return true; + } + return false; + } + #held_slot() { const inventories = this.client.player.inventories; return inventories.inventory.get_slot(inventories.hotbar_selected); diff --git a/common/utils.ts b/common/utils.ts index 48ca210..8a1f629 100644 --- a/common/utils.ts +++ b/common/utils.ts @@ -162,6 +162,19 @@ export function can_place( return in_ok && can_stand_on(block, below); } +// whether an entity's collision box, centered on x and z with its feet at y, overlaps the block at bx, by, bz +export function box_overlaps_block( + entity: { x: number; y: number; z: number; width: number; height: number }, + bx: number, + by: number, + bz: number, +): boolean { + const half = entity.width / 2; + return entity.x + half > bx && entity.x - half < bx + 1 && + entity.y + entity.height > by && entity.y < by + 1 && + entity.z + half > bz && entity.z - half < bz + 1; +} + // numeric so looking chunks up doesnt allocate a string every time, fine for |x|, |z| < 32768 export function chunk_key(x: number, z: number) { return (x + 32768) * 65536 + (z + 32768); diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 66b5eee..c2161e5 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -27,6 +27,7 @@ import type { WorldgenSetup } from "$/common/generation.ts"; import { MAX_VIEW_DISTANCE, MIN_VIEW_DISTANCE } from "$/common/chunk_streaming.ts"; import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts"; import { + box_overlaps_block, can_place, can_stand_on, default_block_value, @@ -61,6 +62,7 @@ import { PLAYER_DROP_PICKUP_DELAY, type SavedItemEntity, } from "./entity/item_entity.ts"; +import { Mob } from "./entity/mob.ts"; import { EntityManager } from "./entity/entity_manager.ts"; import { create_entity, entity_type_ids } from "./entity/entity_types.ts"; import type { SavedEntity } from "./entity/entity.ts"; @@ -874,6 +876,14 @@ export class GameServer { this.#correct(player, tx, ty, tz); return; } + // solid blocks can't go where a mob or player is, the one placing it included, like minecraft + if ( + EverythingRegistry.get("blocks", held.block_id)?.has_collision && + this.#occupied(tx, ty, tz) + ) { + this.#correct(player, tx, ty, tz); + return; + } const place = { player: player_api, @@ -893,6 +903,20 @@ export class GameServer { this.mods.after.block_place.emit(place); } + // whether a mob's or player's collision box overlaps the block at x, y, z + #occupied(x: number, y: number, z: number) { + return this.players().some((player) => + box_overlaps_block( + { x: player.x, y: player.y, z: player.z, width: PLAYER_WIDTH, height: PLAYER_HEIGHT }, + x, + y, + z, + ) + ) || this.entities.entities_near(x + 0.5, y, z + 0.5, 3).some((entity) => + entity instanceof Mob && box_overlaps_block(entity, x, y, z) + ); + } + #drop_item(player: ServerPlayer, key: unknown, index: unknown, all: boolean) { if (!is_int(index)) { return; diff --git a/tests/mod_entity_test.ts b/tests/mod_entity_test.ts index 2c8f979..e64e24d 100644 --- a/tests/mod_entity_test.ts +++ b/tests/mod_entity_test.ts @@ -85,3 +85,29 @@ Deno.test("/summon spawns a mod's entity where the player is", async () => { send(1, { type: "chat", text: "/summon bworld:item" }); assertStringIncludes(take(1).find((m) => m.type === "chat").text, "There's no entity bworld:item"); }); + +Deno.test("solid blocks can't be placed where a mob or player is", async () => { + const { join, take, send, game } = await test_game("mods"); + join(1, "alice"); + game.set_block(1, 100, 1, "bworld:stone"); + send(1, { type: "move", x: 3.5, y: 101, z: 3.5, yaw: 0, pitch: 0 }); + send(1, { type: "chat", text: "/give bworld:stone 2" }); + const inventory = take(1).filter((m) => m.type === "container" && m.container === "inventory").at(-1).items; + send(1, { type: "select_slot", slot: inventory.findIndex((i: { id: string } | null) => i?.id === "bworld:stone") }); + + // the chicken's box pokes into the block above the stone, even from the one next to it + const chicken = create_entity(game.entities, "bworld:chicken")!; + chicken.set_position(2.1, 101, 1.5); + game.entities.add_entity(chicken); + send(1, { type: "use_block", x: 1, y: 100, z: 1, face: "top" }); + assertEquals(game.world.get_block_id(1, 101, 1), "bworld:air"); + + chicken.set_position(2.5, 101, 1.5); + send(1, { type: "use_block", x: 1, y: 100, z: 1, face: "top" }); + assertEquals(game.world.get_block_id(1, 101, 1), "bworld:stone"); + + // nor where a player is, the one placing it included + game.set_block(3, 100, 3, "bworld:stone"); + send(1, { type: "use_block", x: 3, y: 100, z: 3, face: "top" }); + assertEquals(game.world.get_block_id(3, 101, 3), "bworld:air"); +});