Prevent players from placing blocks inside themselves
This commit is contained in:
@@ -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) {
|
||||
|
||||
+24
-3
@@ -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<BlockRegistry>("blocks", place_id)
|
||||
: undefined;
|
||||
const placeable = place_id !== undefined && can_place(
|
||||
EverythingRegistry.get<BlockRegistry>("blocks", place_id),
|
||||
place_block,
|
||||
current === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("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<EntityJson>("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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<BlockRegistry>("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;
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user