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);
|
||||
|
||||
Reference in New Issue
Block a user