Remove duplicate code
This commit is contained in:
+36
-60
@@ -134,7 +134,7 @@ export interface SavedLevel {
|
||||
seed: string;
|
||||
// world time in ticks
|
||||
time: number;
|
||||
tiles: SavedTile[];
|
||||
tiles: Tile[];
|
||||
// every ctx.containers container, by id
|
||||
containers: Record<string, (ItemData | null)[]>;
|
||||
players: Record<string, SavedPlayer>;
|
||||
@@ -144,19 +144,6 @@ export interface SavedLevel {
|
||||
mod_storage: Record<string, Record<string, unknown>>;
|
||||
}
|
||||
|
||||
interface SavedTile {
|
||||
id: string;
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
// what BlockRef.data holds
|
||||
mod_data?: unknown;
|
||||
// what it shows, see ServerWorld.set_block_item in the mod api
|
||||
item?: ItemData;
|
||||
// false when its components turned ticking off for it
|
||||
ticking?: false;
|
||||
}
|
||||
|
||||
// the loaded mods, see load_mods.ts
|
||||
export interface GameMods {
|
||||
listings: ModListing[];
|
||||
@@ -270,17 +257,7 @@ export class GameServer {
|
||||
container.load(items);
|
||||
this.containers.set(id, container);
|
||||
}
|
||||
for (const tile of saved?.tiles ?? []) {
|
||||
this.world.add_tile({
|
||||
id: tile.id,
|
||||
x: tile.x,
|
||||
y: tile.y,
|
||||
z: tile.z,
|
||||
mod_data: tile.mod_data,
|
||||
item: tile.item,
|
||||
ticking: tile.ticking,
|
||||
});
|
||||
}
|
||||
for (const tile of saved?.tiles ?? []) this.world.add_tile({ ...tile });
|
||||
this.entities.load(saved?.version === 4 ? upgrade_v4_entities(saved.entities) : saved?.entities ?? []);
|
||||
this.#saved_players = saved?.players ?? {};
|
||||
// a new world is saved straight away
|
||||
@@ -430,15 +407,7 @@ export class GameServer {
|
||||
version: 5,
|
||||
seed: this.world.seed,
|
||||
time: this.world.time,
|
||||
tiles: [...this.world.tiles.values()].map((tile) => ({
|
||||
id: tile.id,
|
||||
x: tile.x,
|
||||
y: tile.y,
|
||||
z: tile.z,
|
||||
mod_data: tile.mod_data,
|
||||
item: tile.item,
|
||||
ticking: tile.ticking,
|
||||
})),
|
||||
tiles: [...this.world.tiles.values()].map((tile) => ({ ...tile })),
|
||||
containers: Object.fromEntries(
|
||||
[...this.containers].map(([id, container]) => [id, container.to_data()]),
|
||||
),
|
||||
@@ -1187,19 +1156,8 @@ export class GameServer {
|
||||
return;
|
||||
}
|
||||
|
||||
const place = {
|
||||
player: player_api,
|
||||
block: { id: held.block_id, x: tx, y: ty, z: tz },
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]] as const,
|
||||
item: player_api.held_item,
|
||||
};
|
||||
const before_place = { ...place, cancel: false };
|
||||
this.mods.before.block_place.emit(before_place);
|
||||
if (before_place.cancel) {
|
||||
this.#correct(player, tx, ty, tz);
|
||||
return;
|
||||
}
|
||||
const place = this.#place_event(player, { id: held.block_id, x: tx, y: ty, z: tz }, face, hit);
|
||||
if (!place) return;
|
||||
|
||||
this.set_block(tx, ty, tz, held.block_id, player, state);
|
||||
held_slot.amount = held_slot.amount! - 1;
|
||||
@@ -1210,20 +1168,13 @@ export class GameServer {
|
||||
// so a bucket empties like it does placing water anywhere else
|
||||
#waterlog(player: ServerPlayer, x: number, y: number, z: number, face: Faces, hit: number[]) {
|
||||
const held_slot = player.inventory.get_slot(player.selected_slot);
|
||||
const player_api = this.mods.player(player);
|
||||
const place = {
|
||||
player: player_api,
|
||||
block: { id: this.world.get_block_info(x, y, z)!.waterloggable!, x, y, z },
|
||||
const place = this.#place_event(
|
||||
player,
|
||||
{ id: this.world.get_block_info(x, y, z)!.waterloggable!, x, y, z },
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]] as const,
|
||||
item: player_api.held_item,
|
||||
};
|
||||
const before_place = { ...place, cancel: false };
|
||||
this.mods.before.block_place.emit(before_place);
|
||||
if (before_place.cancel) {
|
||||
this.#correct(player, x, y, z);
|
||||
return;
|
||||
}
|
||||
hit,
|
||||
);
|
||||
if (!place) return;
|
||||
const info = this.world.get_block_info(x, y, z)!;
|
||||
this.set_block_state(
|
||||
x,
|
||||
@@ -1235,6 +1186,31 @@ export class GameServer {
|
||||
this.mods.after.block_place.emit(place);
|
||||
}
|
||||
|
||||
// the event for a player placing a block, after mods had the chance to cancel it. undefined when one did, and the
|
||||
// player is told what's really there
|
||||
#place_event(
|
||||
player: ServerPlayer,
|
||||
block: { id: string; x: number; y: number; z: number },
|
||||
face: Faces,
|
||||
hit: number[],
|
||||
) {
|
||||
const player_api = this.mods.player(player);
|
||||
const place = {
|
||||
player: player_api,
|
||||
block,
|
||||
face,
|
||||
hit: [hit[0], hit[1], hit[2]] as const,
|
||||
item: player_api.held_item,
|
||||
};
|
||||
const before = { ...place, cancel: false };
|
||||
this.mods.before.block_place.emit(before);
|
||||
if (before.cancel) {
|
||||
this.#correct(player, block.x, block.y, block.z);
|
||||
return undefined;
|
||||
}
|
||||
return place;
|
||||
}
|
||||
|
||||
// whether a mob's or player's collision box overlaps a block with that shape at x, y, z
|
||||
#occupied(x: number, y: number, z: number, shape: Shape) {
|
||||
return this.players().some((player) =>
|
||||
|
||||
Reference in New Issue
Block a user