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, type ContainerKey } from "$/common/protocol.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"; import type { Entity } from "./entity/entity.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 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; 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) { const block_info = EverythingRegistry.get_by_id("blocks", hit.block)!; const tool = this.#held_item()?.tool; this.destroy_pos = { x: hit.x, y: hit.y, z: hit.z }; this.destroy_time = 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.client.level.break_block(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; } } stop_destroy_block() { this.destroy_pos = undefined; this.destroy_progress = 0; this.destroy_time = 0; } // 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: // into the clicked block when it's replaceable, and only where place_in and place_on allow const block_info = EverythingRegistry.get_by_id("blocks", hit.block)!; 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; 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("blocks", place_id) : undefined; const placeable = place_id !== undefined && can_place( place_block, current === AIR ? undefined : EverythingRegistry.get_by_id("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(); 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 drop_item(container: Container, key: ContainerKey, index: number, all: boolean) { const item = container.get_item(index); if (!item) { return; } this.client.connection.send({ type: "drop_item", container: key, index, 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 }); } // 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) { 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)) return true; } return false; } #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 ?? ""); } }