Placing schematics
This commit is contained in:
+73
-32
@@ -47,10 +47,10 @@ import { collision_shape_at, type Shape } from "@/common/block_shapes.ts";
|
||||
import { physics } from "@/common/physics.ts";
|
||||
import { EXHAUSTION } from "./vitals.ts";
|
||||
import { entity_ref, ModRuntime, new_stack, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
|
||||
import type { LootContext, Position, UseTarget } from "@/common/mod_api/server.ts";
|
||||
import type { LootContext, Position, SchematicPlacement, UseTarget } from "@/common/mod_api/server.ts";
|
||||
import type { EntityJson } from "@/common/mod_data.ts";
|
||||
import { encode_schematic } from "@/common/schematic.ts";
|
||||
import { capture_schematic } from "./schematics.ts";
|
||||
import { capture_schematic, placed_schematic, type RegisteredSchematic } from "./schematics.ts";
|
||||
import {
|
||||
argument,
|
||||
choice,
|
||||
@@ -88,8 +88,8 @@ import { Mob } from "./entity/mob.ts";
|
||||
import { LivingEntity } from "./entity/living_entity.ts";
|
||||
import { EntityManager } from "./entity/entity_manager.ts";
|
||||
import { NaturalSpawner } from "./natural_spawner.ts";
|
||||
import { create_entity, entity_type_ids } from "./entity/entity_types.ts";
|
||||
import { damage_source, type DamageSource, type SavedEntity } from "./entity/entity.ts";
|
||||
import { create_entity, entity_type_ids, load_entity } from "./entity/entity_types.ts";
|
||||
import { damage_source, type DamageSource, type Entity, type SavedEntity } from "./entity/entity.ts";
|
||||
import { effect_type, effect_type_ids, EffectInstance } from "./entity/effects.ts";
|
||||
import { ATTRIBUTES } from "./entity/attributes.ts";
|
||||
|
||||
@@ -183,6 +183,8 @@ export class GameServer {
|
||||
#mods: GameMods;
|
||||
// ctx.containers, saved with the world
|
||||
containers = new Map<string, Container>();
|
||||
// by id, what ctx.schematics.register added for place
|
||||
readonly schematics = new Map<string, RegisteredSchematic>();
|
||||
// everything in the world that isn't a block or a player
|
||||
entities: EntityManager;
|
||||
mods: ModRuntime;
|
||||
@@ -693,34 +695,8 @@ export class GameServer {
|
||||
// replaces a block: the old one's on_break, then the new one's on_create, then the blocks beside it hear of it
|
||||
set_block(x: number, y: number, z: number, id: string, player?: ServerPlayer, state?: number) {
|
||||
this.mods.rules.change(() => {
|
||||
const old_id = this.world.get_block_id(x, y, z);
|
||||
if (this.#break_stages.size > 0) this.set_break_progress(x, y, z, undefined);
|
||||
if (old_id !== AIR_ID) {
|
||||
const components = this.mods.components_of(old_id).filter(({ component }) => component.on_break);
|
||||
if (components.length > 0) {
|
||||
const block = this.mods.block_ref(x, y, z, old_id);
|
||||
const player_api = player && this.mods.player(player);
|
||||
for (const { mod, id: component_id, component, params } of components) {
|
||||
run_guarded(
|
||||
mod,
|
||||
`${component_id} on_break`,
|
||||
() => component.on_break!(block, params, player_api),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.world.remove_tile(x, y, z);
|
||||
this.world.set_block(x, y, z, id, state);
|
||||
|
||||
if (id !== AIR_ID) {
|
||||
const block = this.mods.block_ref(x, y, z, id);
|
||||
for (const { mod, id: component_id, component, params } of this.mods.components_of(id)) {
|
||||
if (component.on_create) {
|
||||
run_guarded(mod, `${component_id} on_create`, () => component.on_create!(block, params));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.#replace_block(x, y, z, id, state, player);
|
||||
this.#create_block(x, y, z, id);
|
||||
this.mods.rules.notify_neighbors(x, y, z);
|
||||
|
||||
// a block standing on this one that can't stand on what it is now breaks, like a crop on farmland that was
|
||||
@@ -734,6 +710,35 @@ export class GameServer {
|
||||
});
|
||||
}
|
||||
|
||||
// the old block's on_break, then the new one without a tile. nothing else hears of it yet
|
||||
#replace_block(x: number, y: number, z: number, id: string, state?: number, player?: ServerPlayer) {
|
||||
const old_id = this.world.get_block_id(x, y, z);
|
||||
if (this.#break_stages.size > 0) this.set_break_progress(x, y, z, undefined);
|
||||
if (old_id !== AIR_ID) {
|
||||
const components = this.mods.components_of(old_id).filter(({ component }) => component.on_break);
|
||||
if (components.length > 0) {
|
||||
const block = this.mods.block_ref(x, y, z, old_id);
|
||||
const player_api = player && this.mods.player(player);
|
||||
for (const { mod, id: component_id, component, params } of components) {
|
||||
run_guarded(mod, `${component_id} on_break`, () => component.on_break!(block, params, player_api));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.world.remove_tile(x, y, z);
|
||||
this.world.set_block(x, y, z, id, state);
|
||||
}
|
||||
|
||||
// its components' on_create
|
||||
#create_block(x: number, y: number, z: number, id: string) {
|
||||
if (id === AIR_ID) return;
|
||||
const block = this.mods.block_ref(x, y, z, id);
|
||||
for (const { mod, id: component_id, component, params } of this.mods.components_of(id)) {
|
||||
if (component.on_create) {
|
||||
run_guarded(mod, `${component_id} on_create`, () => component.on_create!(block, params));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the cracks machines are making on blocks, as the stage clients draw, by position. see set_break_progress
|
||||
#break_stages = new Map<string, number>();
|
||||
|
||||
@@ -924,6 +929,42 @@ export class GameServer {
|
||||
return this.#host.write_schematic(encode_schematic(capture_schematic(this, from, to)));
|
||||
}
|
||||
|
||||
// see ctx.schematics.place. the blocks all change before any of them hears of it, so a door's halves or a torch's
|
||||
// wall are both there by then
|
||||
place_schematic(id: string, placement: SchematicPlacement) {
|
||||
const registered = this.schematics.get(id);
|
||||
if (!registered) throw new Error(`there's no schematic ${id}`);
|
||||
const { blocks, entities } = placed_schematic(registered, placement);
|
||||
const placed = blocks.filter(({ y }) => this.world.shape.contains(y));
|
||||
this.mods.rules.change(() => {
|
||||
for (const { x, y, z, id, state } of placed) this.#replace_block(x, y, z, id, state);
|
||||
for (const { x, y, z, id, block } of placed) {
|
||||
if (id === AIR_ID) continue;
|
||||
// TODO: import_data, so blocks can make what their data points to, like a chest's container
|
||||
if (block.data !== undefined) this.set_tile_data(x, y, z, structuredClone(block.data));
|
||||
for (const [field, value] of Object.entries(block.client_data ?? {})) {
|
||||
this.set_tile_client_data(x, y, z, field, structuredClone(value));
|
||||
}
|
||||
if (block.data === undefined && block.client_data === undefined) this.#create_block(x, y, z, id);
|
||||
}
|
||||
});
|
||||
// each on its own, a big schematic tells more blocks than one change can
|
||||
for (const { x, y, z } of placed) this.mods.rules.notify_neighbors(x, y, z);
|
||||
|
||||
const seat = (vehicle: Entity) => {
|
||||
for (const passenger of vehicle.passengers) {
|
||||
vehicle.position_passenger(passenger);
|
||||
seat(passenger);
|
||||
}
|
||||
};
|
||||
for (const saved of entities) {
|
||||
const entity = load_entity(this.entities, saved);
|
||||
if (!entity) continue;
|
||||
seat(entity);
|
||||
this.entities.add_entity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
// menus
|
||||
|
||||
// the id a new menu for this player gets, 1 to 100 and around, like minecraft's containerCounter
|
||||
|
||||
Reference in New Issue
Block a user