Prevent players from placing blocks inside themselves
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user