Shared state between client and server

This commit is contained in:
2026-10-03 18:45:07 -03:00
parent 63e6248f9f
commit 7417bfd5f1
24 changed files with 656 additions and 193 deletions
+15 -5
View File
@@ -4,7 +4,8 @@ import type { Client } from "./client.ts";
import type { BlockHitResult } from "./level/client_level.ts";
import type { Container } from "$/common/inventory.ts";
import { AIR_ID } from "$/common/protocol.ts";
import { box_overlaps_block, can_place, default_block_value, placement_target } from "$/common/utils.ts";
import { box_overlaps_block, can_place, placement_target } from "$/common/utils.ts";
import { block_rules, predict_neighbors } from "./mods.ts";
import { collision_shape_at } from "$/common/block_shapes.ts";
import type { EntityJson } from "$/common/mod_data.ts";
import { RemoteEntity } from "./entity/remote_entity.ts";
@@ -42,6 +43,7 @@ export class MultiPlayerGameMode {
if (this.destroy_progress >= this.destroy_time) {
// show it right away, the server decides drops and corrects us if it disagrees
this.client.level.break_block(hit.x, hit.y, hit.z);
predict_neighbors(hit.x, hit.y, hit.z);
this.client.connection.send({ type: "break_block", x: hit.x, y: hit.y, z: hit.z });
this.destroy_progress = 0;
this.destroy_time = 0;
@@ -74,13 +76,21 @@ export class MultiPlayerGameMode {
const place_block = place_id !== undefined
? EverythingRegistry.get<BlockRegistry>("blocks", place_id)
: undefined;
// with the states the server will give it, see BlockRules.placement_value
const value = place_block &&
block_rules.placement_value(place_block, this.client.player.yaw, {
...target,
face: hit.face,
hit: hit.hit,
});
const placeable = place_id !== undefined && can_place(
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, place_block));
) && !(place_block?.has_collision && this.#occupied(target.x, target.y, target.z, place_block, value!));
if (!block_info.interactive && place_id && placeable) {
level.add_block({ ...target, id: place_id });
level.add_block({ ...target, id: place_id }, value! >>> 16);
predict_neighbors(target.x, target.y, target.z);
const slot = this.#held_slot();
slot.amount = slot.amount! - 1;
}
@@ -118,8 +128,8 @@ export class MultiPlayerGameMode {
// 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, block: BlockRegistry) {
const shape = collision_shape_at(block, default_block_value(0, block));
#occupied(x: number, y: number, z: number, block: BlockRegistry, value: number) {
const shape = collision_shape_at(block, value);
for (const entity of this.client.level.entities.values()) {
const json = entity instanceof RemoteEntity
? EverythingRegistry.get<EntityJson>("entities", entity.type)