// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later import { BlockRegistry, EverythingRegistry, ItemRegistry } from "@/common/everything_registry.ts"; import { AIR, FACE_OFFSETS, TICK_DELTA } from "@/common/constants.ts"; 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, fluid_block, placement_target, waterlog_placement } 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"; import { Player } from "./entity/player.ts"; import type { Entity } from "./entity/entity.ts"; const CREATIVE_DESTROY_DELAY = 2; // 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 export class MultiPlayerGameMode { client: Client; // the block being broken, and how far along it is in seconds out of destroy_time destroy_pos: { x: number; y: number; z: number } | undefined; destroy_progress = 0; destroy_time = 0; // ticks left before creative can break another block while the button is held, so holding it doesn't clear a // line of blocks every tick destroy_delay = 0; constructor(client: Client) { this.client = client; } // the click that starts breaking, for mods' on_click start_destroy_block(hit: BlockHitResult) { this.client.connection.send({ type: "hit_block", x: hit.x, y: hit.y, z: hit.z }); } // called every tick the attack button is held on a block continue_destroy_block(hit: BlockHitResult) { if (this.destroy_delay > 0) { this.destroy_delay--; return; } const block_info = EverythingRegistry.get_by_id("blocks", hit.block)!; const tool = this.held_item()?.tool; // looking at another block starts it over const pos = this.destroy_pos; if (!pos || pos.x !== hit.x || pos.y !== hit.y || pos.z !== hit.z) { this.destroy_progress = 0; } this.destroy_pos = { x: hit.x, y: hit.y, z: hit.z }; // blocks without a toughness, like water, can only be broken in creative this.destroy_time = this.client.player.creative ? 0 : block_info.toughness ?? 9999; this.destroy_progress += TICK_DELTA * (tool && tool.type === block_info.tool_to_break ? tool.speed : 1); if (this.destroy_progress >= this.destroy_time) { // show it right away, the server decides drops and corrects us if it disagrees this.#predict((seq) => { 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, seq }); }); this.destroy_progress = 0; this.destroy_time = 0; if (this.client.player.creative) this.destroy_delay = CREATIVE_DESTROY_DELAY; } } // letting go also ends the delay, so clicking again breaks right away stop_destroy_block() { this.destroy_delay = 0; this.destroy_pos = undefined; this.destroy_progress = 0; this.destroy_time = 0; } // right click on a block use_item_on(hit: BlockHitResult) { // 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.#predict((seq) => { const { x, y, z, face } = hit; this.client.connection.send({ type: "use_block", x, y, z, face, hit: hit.hit, seq }); this.#guess_use(hit); }); } // what using a block will do, shown right away #guess_use(hit: BlockHitResult) { const level = this.client.level; // the shared rules' on_interact, like a trapdoor opening, shows right away. the server runs them too and corrects // this if it disagrees. when they do something nothing is placed, like on the server // an item aimed at fluids, like a bucket, uses the fluid in a waterlogged block instead, like on the server const block_info = EverythingRegistry.get_by_id("blocks", hit.block)!; const held = this.held_item(); const value = level.get_block_value(hit.x, hit.y, hit.z); const used = (held?.targets_fluids && fluid_block(value, block_info)) || block_info; // sneaking with an item skips the block, like on the server const sneaking = this.client.player.holding_sneak; const skips_block = sneaking && held !== undefined; const interaction = { face: hit.face, hit: hit.hit, sneaking }; if (!skips_block && block_rules.interact(used, hit.x, hit.y, hit.z, interaction, this.client.player.yaw)) { return; } // guess that it places the held block, unless the block does something when used. the same rules as the server: // into the clicked block when it's replaceable, and only where place_in and place_on allow const target = placement_target(hit.x, hit.y, hit.z, FACE_OFFSETS[hit.face], block_info); const current = level.get_block(target.x, target.y, target.z); const below = level.get_block(target.x, target.y - 1, target.z); const below_id = EverythingRegistry.get_by_id("blocks", below)?.id ?? AIR_ID; // 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; // with the states the server will give it, see BlockRules.placement_value, and holding the fluid it goes into. // undefined when its rules say it can't go there const ruled_value = place_block && block_rules.placement_value(place_block, this.client.player.yaw, this.client.player.pitch, { ...target, face: hit.face, hit: hit.hit, sneaking, item: this.#held_slot().type_id!, }); const place_value = ruled_value !== undefined ? waterlog_placement( ruled_value, place_block!, level.get_block_value(target.x, target.y, target.z), EverythingRegistry.get_by_id("blocks", current), ) : undefined; const placeable = place_value !== undefined && place_id !== undefined && can_place( place_block, current === AIR ? undefined : EverythingRegistry.get_by_id("blocks", current), level.get_block_value(target.x, target.y, target.z), below_id, ) && !(place_block?.has_collision && this.#occupied(target.x, target.y, target.z, place_block, place_value!)); if ((!block_info.interactive || skips_block) && place_id && placeable) { level.add_block({ ...target, id: place_id }, place_value! >>> 16); predict_neighbors(target.x, target.y, target.z); if (!this.client.player.creative) { const slot = this.#held_slot(); slot.amount = slot.amount! - 1; } } } // q on a slot: throws one, or the whole stack. the server spawns the item, this only takes it out right away // q on a menu's slot, whose item is in container at index on this client drop_item(container: Container, index: number, menu: number, slot: number, all: boolean) { const item = container.get_item(index); if (!item) { return; } this.client.connection.send({ type: "drop_item", menu, slot, all }); item.amount = all ? 0 : item.amount - 1; container.set_item(index, item.amount > 0 ? item : undefined); } // left click on an entity or another player, the server decides whether it's hurt attack(entity: Entity) { this.client.player.send_position_now(); this.client.connection.send({ type: "attack_entity", id: entity.id }); this.client.player.last_attack = performance.now(); } // left click on nothing: a swing at the air, which uses up the attack cooldown like an attack swing() { this.client.connection.send({ type: "swing" }); this.client.player.last_attack = performance.now(); } // right click on nothing use_item() { 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, 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("entities", entity.type) : undefined; const blocks = entity instanceof Player || (json !== undefined && json.behavior !== "scripted"); if (blocks && box_overlaps_block(entity, x, y, z, shape)) return true; } return false; } // runs guess with the blocks it changes kept track of, like minecraft's startPrediction. what the server says about // them waits until it answers the message guess sends with seq, see client/level/block_predictions.ts #predict(guess: (seq: number) => void) { const predictions = this.client.level.predictions; const seq = predictions.start(); try { guess(seq); } finally { predictions.end(); } } #held_slot() { const inventories = this.client.player.inventories; return inventories.inventory.get_slot(inventories.hotbar_selected); } held_item() { return EverythingRegistry.get("items", this.#held_slot().type_id ?? ""); } }