This commit is contained in:
2026-10-22 19:55:59 -03:00
parent 1b30d71a47
commit 4cd29a17b3
37 changed files with 427 additions and 155 deletions
+48 -8
View File
@@ -562,8 +562,9 @@ export class GameServer {
return this.entities.all().filter((entity) => entity instanceof ItemEntity);
}
send_chat(player: ServerPlayer, text: string) {
this.#send(player, { type: "chat", text });
// in chat, or above their hotbar for a moment with overlay, see MessageOptions
send_chat(player: ServerPlayer, text: string, overlay = false) {
this.#send(player, overlay ? { type: "chat", text, overlay } : { type: "chat", text });
}
// a mod's server script to its client script, see ctx.net
@@ -1092,16 +1093,27 @@ export class GameServer {
// a client that hadn't heard it's using an item yet: what it guessed the click did, it didn't
this.#correct_use(player, message.x, message.y, message.z, message.face);
} else {
// the middle of the block when a client doesn't say, or says something that isn't on it
const hit = Array.isArray(message.hit) && message.hit.length === 3 &&
message.hit.every((v) => typeof v === "number" && v >= 0 && v <= 1)
? message.hit
: [0.5, 0.5, 0.5];
this.#use_block(player, message.x, message.y, message.z, message.face, hit);
this.#use_block(
player,
message.x,
message.y,
message.z,
message.face,
hit_on_block(message.hit),
);
}
}
this.#ack(player, message.seq);
break;
case "scroll_block":
if (
is_block_position(message.x, message.y, message.z) && faces.includes(message.face) &&
is_int(message.amount) && message.amount !== 0
) {
const { x, y, z, face, amount } = message;
this.#scroll_block(player, x, y, z, face, hit_on_block(message.hit), amount);
}
break;
case "select_slot":
if (is_int(message.slot) && message.slot >= 0 && message.slot < 9) {
if (message.slot !== player.selected_slot) player.attack_strength_ticker = 0;
@@ -1277,6 +1289,26 @@ export class GameServer {
this.#correct(player, target.x, target.y, target.z);
}
// the mouse wheel turned at a scrollable block in reach: its components' on_scroll
#scroll_block(
player: ServerPlayer,
x: number,
y: number,
z: number,
face: Faces,
hit: [number, number, number],
amount: number,
) {
const info = this.world.get_block_info(x, y, z);
if (!info?.scrollable || !this.#in_reach(player, x, y, z)) return;
const block = this.mods.block_ref(x, y, z, info.id);
const player_api = this.mods.player(player);
const scroll = { amount, face, hit };
for (const { mod, id, component, params } of this.mods.components_with(info.id, "on_scroll")) {
run_guarded(mod, `${id} on_scroll`, () => component.on_scroll!(block, params, player_api, scroll));
}
}
#use_block(player: ServerPlayer, x: number, y: number, z: number, face: Faces, hit: number[]) {
const info = this.world.get_block_info(x, y, z);
const target = placement_target(x, y, z, FACE_OFFSETS[face], info);
@@ -2391,6 +2423,14 @@ function view_distance(requested: unknown, server: number): number {
return Math.min(server, Math.max(MIN_VIEW_DISTANCE, Math.min(MAX_VIEW_DISTANCE, requested)));
}
// where on a block a client says it clicked or looks, 0 to 1 on each axis. the middle when it doesn't say, or says
// something that isn't on it
function hit_on_block(hit: unknown): [number, number, number] {
return Array.isArray(hit) && hit.length === 3 && hit.every((v) => typeof v === "number" && v >= 0 && v <= 1)
? [hit[0], hit[1], hit[2]]
: [0.5, 0.5, 0.5];
}
// messages are parsed json, so the types in ClientMessage are only what a well behaved client sends
function is_block_position(x: unknown, y: unknown, z: unknown): boolean {
return is_int(x) && is_int(y) && is_int(z) && y >= 0 && y < CHUNK_HEIGHT &&
+2 -2
View File
@@ -1383,8 +1383,8 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
if (!EverythingRegistry.get("items", id)) throw new Error(`unknown item ${id}`);
game.give(player, id, count, data);
},
send_message(text) {
game.send_chat(player, String(text));
send_message(text, options) {
game.send_chat(player, String(text), options?.overlay === true);
},
set_worn(key, items) {
if (typeof key !== "string" || key.length === 0) throw new Error('set_worn needs a key, like "mymod:hats"');