From ad7f34a99e52c2b78e23e85af98bd51466162b08 Mon Sep 17 00:00:00 2001 From: Paula Date: Tue, 29 Sep 2026 00:10:22 -0300 Subject: [PATCH] Proper directional blocks --- common/block_models.ts | 34 +++++++++++++-------------- common/mod_api/server.ts | 2 -- mods/bworld/blocks/furnace.json | 8 +++++++ mods/farmersdelight/blocks/stove.json | 8 +++++++ mods/farmersdelight/scripts/server.ts | 12 ---------- server/game/game_server.ts | 23 ++++++++++++++++-- server/game/mod_runtime.ts | 3 --- tests/cutting_board_test.ts | 12 ++++++++++ tests/mod_loading_test.ts | 4 ++++ 9 files changed, 69 insertions(+), 37 deletions(-) diff --git a/common/block_models.ts b/common/block_models.ts index 1a9cdff..fd307ce 100644 --- a/common/block_models.ts +++ b/common/block_models.ts @@ -56,7 +56,8 @@ const cube_face = (texture: string, cullface: ModelFace): ModelFaceJson => ({ te // the engine's own models export const BUILTIN_MODELS: Record = { - // a full block. "textures" can be one texture, { top, bottom, side } or { front, side } + // a full block. "textures" can be one texture, { top, bottom, side } or { front, side }. the front is north, like + // minecraft's furnace, so a facing state turns it towards whoever placed it "engine:cube": { id: "engine:cube", textures: { top: "#side", bottom: "#side", front: "#side" }, @@ -66,8 +67,8 @@ export const BUILTIN_MODELS: Record = { faces: { top: cube_face("#top", "top"), bottom: cube_face("#bottom", "bottom"), - north: cube_face("#side", "north"), - south: cube_face("#front", "south"), + north: cube_face("#front", "north"), + south: cube_face("#side", "south"), west: cube_face("#side", "west"), east: cube_face("#side", "east"), }, @@ -141,24 +142,21 @@ export function texture_variables(reference: string, textures: BlockTextures | u return variables; } -// the block's model and textures for a value with state bits, from the first variant whose conditions match +// the block's model, textures and turn for a value with state bits. each comes from the first variant whose conditions +// match that sets it, so "lit=1" can change the textures and "facing=1" the turn of the same block export function block_variant(block: BlockRegistry, states?: Record): Required { - const variant: Required = { - model: block.model ?? DEFAULT_MODEL, - textures: block.textures, - y: 0, - }; - if (!block.variants || !states) return variant; - for (const [condition, override] of Object.entries(block.variants)) { - if (variant_matches(condition, states)) { - return { - model: override.model ?? variant.model, - textures: override.textures ?? variant.textures, - y: override.y ?? 0, - }; + let model: string | undefined; + let textures: BlockTextures | undefined; + let y: number | undefined; + if (block.variants && states) { + for (const [condition, override] of Object.entries(block.variants)) { + if (!variant_matches(condition, states)) continue; + model ??= override.model; + textures ??= override.textures; + y ??= override.y; } } - return variant; + return { model: model ?? block.model ?? DEFAULT_MODEL, textures: textures ?? block.textures, y: y ?? 0 }; } // "age=7" or "age=7,facing=2", "" matches everything diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index 1df2243..7ba4f9a 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -207,8 +207,6 @@ export interface Player { readonly id: string; readonly name: string; readonly position: Readonly; - // where they look, in radians. 0 is north (-z), going up turning west - readonly yaw: number; readonly inventory: Container; // 36 slots, hotbar is 0-8 readonly selected_slot: number; readonly held_item: ItemStack | undefined; diff --git a/mods/bworld/blocks/furnace.json b/mods/bworld/blocks/furnace.json index e001bba..bb4ec83 100644 --- a/mods/bworld/blocks/furnace.json +++ b/mods/bworld/blocks/furnace.json @@ -16,6 +16,9 @@ "drops": "bworld:furnace", "interactive": true, "variants": { + "facing=1": { "y": 90 }, + "facing=2": { "y": 180 }, + "facing=3": { "y": 270 }, "lit=1": { "textures": { "front": "bworld:furnace_front_lit", @@ -30,6 +33,11 @@ "name": "lit", "bits": 1, "default": 0 + }, + { + "name": "facing", + "bits": 2, + "default": 0 } ], "components": { diff --git a/mods/farmersdelight/blocks/stove.json b/mods/farmersdelight/blocks/stove.json index 39e6c62..fde0c64 100644 --- a/mods/farmersdelight/blocks/stove.json +++ b/mods/farmersdelight/blocks/stove.json @@ -9,6 +9,9 @@ "side": "farmersdelight:stove_side" }, "variants": { + "facing=1": { "y": 90 }, + "facing=2": { "y": 180 }, + "facing=3": { "y": 270 }, "lit=1": { "textures": { "top": "farmersdelight:stove_top_on", @@ -29,6 +32,11 @@ "name": "lit", "bits": 1, "default": 1 + }, + { + "name": "facing", + "bits": 2, + "default": 0 } ], "components": { diff --git a/mods/farmersdelight/scripts/server.ts b/mods/farmersdelight/scripts/server.ts index c6712d6..645daaa 100644 --- a/mods/farmersdelight/scripts/server.ts +++ b/mods/farmersdelight/scripts/server.ts @@ -317,9 +317,6 @@ function register_cooking_pot(ctx: ServerContext) { // cutting board -// its facing state, the way its front points: north, east, south or west, like the java mod's HORIZONTAL_FACING -const FACINGS = 4; - // a farmersdelight:cutting recipe, like the java mod's: one ingredient (an item, or a list any of which will do), the // tool type that cuts it, and everything one of it is cut into interface CuttingRecipe { @@ -364,15 +361,6 @@ function register_cutting_board(ctx: ServerContext) { const set_on_board = (block: BlockRef, item: ItemStack | undefined) => ctx.world.set_block_item(block.x, block.y, block.z, item); - // placed facing whoever placed it, like getStateForPlacement. they look along (-sin yaw, -cos yaw), so the way - // back to them is (sin yaw, cos yaw), and north (-z) is 0 going clockwise - ctx.events.after.block_place.subscribe(({ player, block }) => { - if (block.id !== "farmersdelight:cutting_board") return; - const quarter = Math.round((Math.PI - player.yaw) / (Math.PI / 2)); - const facing = ((quarter % FACINGS) + FACINGS) % FACINGS; - ctx.world.set_state(block.x, block.y, block.z, "facing", facing); - }); - ctx.components.register_block("farmersdelight:cutting_board", { on_interact(block, _params, player) { const held = player.inventory.get(player.selected_slot); diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 161284a..96e6d0c 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -26,7 +26,14 @@ import type { ModListing, RecipeBook } from "$/common/mod_loader.ts"; import type { WorldgenSetup } from "$/common/generation.ts"; import { MAX_VIEW_DISTANCE, MIN_VIEW_DISTANCE } from "$/common/chunk_streaming.ts"; import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts"; -import { can_place, can_stand_on, display_name, placement_target } from "$/common/utils.ts"; +import { + can_place, + can_stand_on, + default_block_value, + display_name, + placement_target, + set_state_value, +} from "$/common/utils.ts"; import { EXHAUSTION, MAX_HEALTH } from "./vitals.ts"; import { ModRuntime, run_guarded, type ServerCommandSource } from "./mod_runtime.ts"; import { @@ -879,7 +886,7 @@ export class GameServer { return; } - this.set_block(tx, ty, tz, held.block_id, player); + this.set_block(tx, ty, tz, held.block_id, player, placed_state(held.block_id, player)); held_slot.amount = held_slot.amount! - 1; this.mods.after.block_place.emit(place); } @@ -1602,3 +1609,15 @@ function death_message(name: string, cause: string) { return `${name} died`; } } + +// the states a block is placed with: its defaults, and a "facing" state turned towards whoever placed it, like +// minecraft's getStateForPlacement with getHorizontalDirection().getOpposite(). facing is 0 north, 1 east, 2 south, +// 3 west, and variants turn the model with it (its north side is the front, like engine:cube's). they look along +// (-sin yaw, -cos yaw), so the way back to them is (sin yaw, cos yaw) +function placed_state(block_id: string, player: ServerPlayer): number | undefined { + const info = EverythingRegistry.get("blocks", block_id); + if (!info?.states?.some((state) => state.name === "facing" && state.bits >= 2)) return undefined; + const quarter = Math.round((Math.PI - player.yaw) / (Math.PI / 2)); + const facing = ((quarter % 4) + 4) % 4; + return set_state_value(default_block_value(0, info), info, "facing", facing)! >>> 16; +} diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index f2d5c14..566b392 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -763,9 +763,6 @@ function player_api(game: GameServer, player: ServerPlayer): Player { get position() { return { x: player.x, y: player.y, z: player.z }; }, - get yaw() { - return player.yaw; - }, inventory, get selected_slot() { return player.selected_slot; diff --git a/tests/cutting_board_test.ts b/tests/cutting_board_test.ts index 6604722..df27874 100644 --- a/tests/cutting_board_test.ts +++ b/tests/cutting_board_test.ts @@ -41,6 +41,18 @@ Deno.test("the board faces whoever placed it", async () => { assertEquals((await setup(-Math.PI / 2)).state("facing"), 3); }); +Deno.test("so does any block with a facing state, like the furnace, keeping its other states", async () => { + const t = await setup(Math.PI / 2); + const at = [0, 100, 2] as const; + t.send(1, { type: "chat", text: "/give bworld:furnace" }); + t.send(1, { type: "select_slot", slot: 0 }); + t.send(1, { type: "use_block", x: at[0], y: at[1] - 1, z: at[2], face: "top" }); + const info = t.game.world.get_block_info(...at)!; + assertEquals(info.id, "bworld:furnace"); + assertEquals(get_state_value(t.game.world.get_block_value(...at), info, "facing"), 1); + assertEquals(get_state_value(t.game.world.get_block_value(...at), info, "lit"), 0); +}); + Deno.test("a stack goes on the board, and a knife cuts one at a time by its recipe", async () => { const t = await setup(); t.send(1, { type: "chat", text: "/give bworld:cod 3" }); diff --git a/tests/mod_loading_test.ts b/tests/mod_loading_test.ts index ecd289a..53c2ab8 100644 --- a/tests/mod_loading_test.ts +++ b/tests/mod_loading_test.ts @@ -247,4 +247,8 @@ Deno.test("a lit furnace shows its lit front", async () => { const front = (lit: number) => (block_variant(furnace, { lit }).textures as Record).front; assertEquals(front(0), "bworld:furnace_front"); assertEquals(front(1), "bworld:furnace_front_lit"); + // turned and lit at once: each comes from the first variant that sets it + const variant = block_variant(furnace, { lit: 1, facing: 3 }); + assertEquals((variant.textures as Record).front, "bworld:furnace_front_lit"); + assertEquals(variant.y, 270); });