From 63e6248f9fa86b57eea2a74e88895eb135e7bf28 Mon Sep 17 00:00:00 2001 From: Paula Date: Sat, 3 Oct 2026 17:52:58 -0300 Subject: [PATCH] Trapdoors --- client/game_mode.ts | 2 +- client/level/client_level.ts | 7 +- common/mod_api/server.ts | 13 +++ common/protocol.ts | 7 +- docs/modding/blocks/components.md | 45 +++++--- docs/modding/concepts/events.md | 2 +- mods/bworld/blocks/acacia_trapdoor.json | 57 ++++++++++ mods/bworld/blocks/birch_trapdoor.json | 57 ++++++++++ mods/bworld/blocks/cherry_trapdoor.json | 57 ++++++++++ mods/bworld/blocks/dark_oak_trapdoor.json | 57 ++++++++++ mods/bworld/blocks/jungle_trapdoor.json | 57 ++++++++++ mods/bworld/blocks/oak_trapdoor.json | 57 ++++++++++ mods/bworld/blocks/spruce_trapdoor.json | 57 ++++++++++ mods/bworld/models/trapdoor_bottom.json | 43 ++++++++ mods/bworld/models/trapdoor_open.json | 43 ++++++++ mods/bworld/models/trapdoor_open_top.json | 43 ++++++++ mods/bworld/models/trapdoor_top.json | 43 ++++++++ .../recipes/crafting_acacia_trapdoor.json | 17 +++ .../recipes/crafting_birch_trapdoor.json | 17 +++ .../recipes/crafting_cherry_trapdoor.json | 17 +++ .../recipes/crafting_jungle_trapdoor.json | 17 +++ .../bworld/recipes/crafting_oak_trapdoor.json | 17 +++ .../recipes/crafting_spruce_trapdoor.json | 17 +++ mods/bworld/scripts/server.ts | 24 +++++ .../tags/blocks/bworld/wooden_trapdoors.json | 13 +++ .../tags/items/bworld/wooden_trapdoors.json | 13 +++ mods/bworld/textures/acacia_trapdoor.png | Bin 0 -> 6247 bytes mods/bworld/textures/birch_trapdoor.png | Bin 0 -> 342 bytes mods/bworld/textures/cherry_trapdoor.png | Bin 0 -> 6598 bytes mods/bworld/textures/dark_oak_trapdoor.png | Bin 0 -> 6363 bytes mods/bworld/textures/jungle_trapdoor.png | Bin 0 -> 357 bytes mods/bworld/textures/oak_trapdoor.png | Bin 0 -> 354 bytes mods/bworld/textures/spruce_trapdoor.png | Bin 0 -> 6495 bytes server/game/game_server.ts | 50 ++++++++- tests/trapdoor_test.ts | 101 ++++++++++++++++++ 35 files changed, 927 insertions(+), 23 deletions(-) create mode 100644 mods/bworld/blocks/acacia_trapdoor.json create mode 100644 mods/bworld/blocks/birch_trapdoor.json create mode 100644 mods/bworld/blocks/cherry_trapdoor.json create mode 100644 mods/bworld/blocks/dark_oak_trapdoor.json create mode 100644 mods/bworld/blocks/jungle_trapdoor.json create mode 100644 mods/bworld/blocks/oak_trapdoor.json create mode 100644 mods/bworld/blocks/spruce_trapdoor.json create mode 100644 mods/bworld/models/trapdoor_bottom.json create mode 100644 mods/bworld/models/trapdoor_open.json create mode 100644 mods/bworld/models/trapdoor_open_top.json create mode 100644 mods/bworld/models/trapdoor_top.json create mode 100644 mods/bworld/recipes/crafting_acacia_trapdoor.json create mode 100644 mods/bworld/recipes/crafting_birch_trapdoor.json create mode 100644 mods/bworld/recipes/crafting_cherry_trapdoor.json create mode 100644 mods/bworld/recipes/crafting_jungle_trapdoor.json create mode 100644 mods/bworld/recipes/crafting_oak_trapdoor.json create mode 100644 mods/bworld/recipes/crafting_spruce_trapdoor.json create mode 100644 mods/bworld/tags/blocks/bworld/wooden_trapdoors.json create mode 100644 mods/bworld/tags/items/bworld/wooden_trapdoors.json create mode 100644 mods/bworld/textures/acacia_trapdoor.png create mode 100644 mods/bworld/textures/birch_trapdoor.png create mode 100644 mods/bworld/textures/cherry_trapdoor.png create mode 100644 mods/bworld/textures/dark_oak_trapdoor.png create mode 100644 mods/bworld/textures/jungle_trapdoor.png create mode 100644 mods/bworld/textures/oak_trapdoor.png create mode 100644 mods/bworld/textures/spruce_trapdoor.png create mode 100644 tests/trapdoor_test.ts diff --git a/client/game_mode.ts b/client/game_mode.ts index b8ea9d0..d8bac1a 100644 --- a/client/game_mode.ts +++ b/client/game_mode.ts @@ -59,7 +59,7 @@ export class MultiPlayerGameMode { 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 }); + this.client.connection.send({ type: "use_block", x: hit.x, y: hit.y, z: hit.z, face: hit.face, hit: hit.hit }); // 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 diff --git a/client/level/client_level.ts b/client/level/client_level.ts index bdb6857..393e6da 100644 --- a/client/level/client_level.ts +++ b/client/level/client_level.ts @@ -81,6 +81,8 @@ export interface BlockHitResult { z: number; block: number; face: Faces; + // where the view went into the block, 0 to 1 on each axis + hit: [number, number, number]; } const NEIGHBOR_OFFSETS = [[-1, 0], [1, 0], [0, -1], [0, 1]] as const; @@ -707,7 +709,10 @@ export class ClientLevel { : hit.axis === 1 ? (hit.sign > 0 ? "bottom" : "top") : (hit.sign > 0 ? "north" : "south"); - return { x: bx, y: by, z: bz, face, block }; + const at = [0, 1, 2].map((axis) => + Math.min(Math.max(origin[axis] + direction[axis] * hit.distance - [bx, by, bz][axis], 0), 1) + ); + return { x: bx, y: by, z: bz, face, block, hit: [at[0], at[1], at[2]] }; } return undefined; diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index c69e971..b561195 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -52,6 +52,9 @@ export interface BlockComponent

{ // a block next to it (on any of the 6 sides) was placed, broken or changed its states, like minecraft's // updateShape. neighbor is where, and what's there now on_neighbor_change?(block: BlockRef, params: P, neighbor: Position & { readonly id: Id }): void; + // the states it's placed with when a player places it, like minecraft's getStateForPlacement. states it doesn't + // return keep their defaults, and a 2 bit "facing" faces the player + state_for_placement?(placement: BlockPlacement, params: P): Record | void; } // deno-lint-ignore no-explicit-any @@ -141,9 +144,19 @@ export interface BlockPlaceEvent { readonly player: Player; readonly block: { readonly id: Id } & Position; readonly face: Face; + // where on the clicked block the player clicked, 0 to 1 on each axis + readonly hit: readonly [number, number, number]; readonly item: ItemStack | undefined; } +// a player placing a block at x y z by clicking face of the block next to it, at hit (0 to 1 on each axis of that +// block), like minecraft's BlockPlaceContext +export interface BlockPlacement extends Position { + readonly player: Player; + readonly face: Face; + readonly hit: readonly [number, number, number]; +} + export interface BlockInteractEvent { readonly player: Player; readonly block: BlockRef; diff --git a/common/protocol.ts b/common/protocol.ts index 1fbe0cc..da98938 100644 --- a/common/protocol.ts +++ b/common/protocol.ts @@ -7,7 +7,7 @@ export const AIR_ID = "bworld:air"; // bump when a client and server of different versions can't play together. // the server rejects a different version before the client downloads anything -export const PROTOCOL_VERSION = 18; +export const PROTOCOL_VERSION = 19; export interface PlayerInfo { id: string; @@ -110,8 +110,9 @@ export type ClientMessage = | { type: "swing" } // right click without looking at a block, for items' on_use | { type: "use_item" } - // right click on a block: interact with it, or place the held block against `face` - | { type: "use_block"; x: number; y: number; z: number; face: Faces } + // right click on a block: interact with it, or place the held block against `face`. hit is where on the block it + // clicked, 0 to 1 on each axis, like minecraft's click location + | { type: "use_block"; x: number; y: number; z: number; face: Faces; hit?: [number, number, number] } | { type: "select_slot"; slot: number } // a slot of the open menu, or of menu 0 when none is. shift moves the stack to the other part of the menu instead, // like minecraft's quick move. clicks for a menu that isn't open any more are ignored diff --git a/docs/modding/blocks/components.md b/docs/modding/blocks/components.md index 366b228..87da90e 100644 --- a/docs/modding/blocks/components.md +++ b/docs/modding/blocks/components.md @@ -69,6 +69,7 @@ component. | `on_second(block, params, dt)` | Every second, near a player. | | | `on_new_day(block, params, day)` | Every morning at 06:00, anywhere in the world. | | | `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. | | +| `state_for_placement(placement, params)` | A player is about to place it. | States to place it with | `block` is `{ id, x, y, z, data }`, where `data` is the block's [tile data](tiledata.md). `params` is what the block's JSON lists for this component, and `dt` is in seconds. @@ -83,6 +84,25 @@ Setting a state to what it already is changes nothing and tells no one, so block `isFaceSturdy`: its [collision shape](../resources/models.md#collision-shapes) covers the whole side. Stone's sides are, a slab's sides and plants aren't. +### Placing + +`state_for_placement` picks the states a block is placed with, like Minecraft's `getStateForPlacement`. `placement` is +`{ x, y, z, player, face, hit }`: where it's going, the face of the block that was clicked, and where on that block the +click was, from 0 to 1 on each axis. It returns states by name, and the rest keep their defaults, except that a 2 bit +`facing` already faces the player. It runs before the server checks that no one is in the way, so that's checked against +the shape the block will really have. A trapdoor clicked onto the upper half of a block's side hangs in the top half: + +```ts +state_for_placement(placement): Record { + if (placement.face !== "top" && placement.face !== "bottom") { + return { facing: ["north", "east", "south", "west"].indexOf(placement.face), half: placement.hit[1] > 0.5 ? 1 : 0 }; + } + return { half: placement.face === "top" ? 0 : 1 }; +}, +``` + +Clients guess blocks are placed with their defaults, and the server corrects them right after. + ### Interacting When a player right clicks a block, every component's `on_interact` runs. If any of them returns `true`, the click was @@ -107,17 +127,18 @@ catch up. If it's further behind than that, it skips the rest and logs a warning `mods/bworld/scripts/server.ts` registers these, and any mod that depends on `bworld` can use them: -| Component | Params | What it does | -| ----------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | -| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. | -| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. | -| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. | -| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. | -| `bworld:farmland` | none | Watered with the watering can, dries out every morning. | -| `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. | -| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. | -| `bworld:water_source` | none | Refills the watering can. | -| `bworld:fishable` | none | Can be fished in with the fishing rod. | -| `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. | +| Component | Params | What it does | +| ----------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. | +| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. | +| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. | +| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. | +| `bworld:farmland` | none | Watered with the watering can, dries out every morning. | +| `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. | +| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. | +| `bworld:water_source` | none | Refills the watering can. | +| `bworld:fishable` | none | Can be fished in with the fishing rod. | +| `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's. Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. | +| `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. | Item components work the same way, see [Items](../items/index.md#item-components). diff --git a/docs/modding/concepts/events.md b/docs/modding/concepts/events.md index 65222b4..04554cd 100644 --- a/docs/modding/concepts/events.md +++ b/docs/modding/concepts/events.md @@ -45,7 +45,7 @@ showed. These events fire for **every** player's actions, since they all go thro | Event | Before | After | Fields | | ---------------- | ------ | ----- | ------------------------------------------------------------------------------------------------------------------------------------- | | `block_break` | ✓ | ✓ | `player`, `block` (with its tile `data`), `item` held | -| `block_place` | ✓ | ✓ | `player`, `block` (`{ id, x, y, z }`), `face` clicked, `item` held | +| `block_place` | ✓ | ✓ | `player`, `block` (`{ id, x, y, z }`), `face` clicked, `hit` where on it (0-1 each axis), `item` held | | `block_interact` | ✓ | ✓ | `player`, `block`, `item` held. Before fires for every right click on a block, after only when a component's `on_interact` handled it | | `chat_send` | ✓ | ✓ | `player`, `message`. Before handlers can change `message` | | `player_damage` | ✓ | ✓ | `player`, `amount`, `cause`. Before handlers can change `amount`, after fires only when they were hurt, with what they took | diff --git a/mods/bworld/blocks/acacia_trapdoor.json b/mods/bworld/blocks/acacia_trapdoor.json new file mode 100644 index 0000000..66f62bb --- /dev/null +++ b/mods/bworld/blocks/acacia_trapdoor.json @@ -0,0 +1,57 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:acacia_trapdoor", + "name": "Acacia Trapdoor", + "textures": "bworld:acacia_trapdoor", + "model": "bworld:trapdoor_bottom", + "item_model": "bworld:trapdoor_bottom", + "render_layer": "cutout", + "states": [ + { + "name": "facing", + "bits": 2, + "default": 0 + }, + { + "name": "half", + "bits": 1, + "default": 0 + }, + { + "name": "open", + "bits": 1, + "default": 0 + } + ], + "variants": { + "open=0,half=1": { + "model": "bworld:trapdoor_top" + }, + "open=1,half=0": { + "model": "bworld:trapdoor_open" + }, + "open=1,half=1": { + "model": "bworld:trapdoor_open_top" + }, + "facing=1": { + "y": 90 + }, + "facing=2": { + "y": 180 + }, + "facing=3": { + "y": 270 + } + }, + "mining": { + "toughness": 3, + "tool": "axe" + }, + "drops": "bworld:acacia_trapdoor", + "interactive": true, + "components": { + "bworld:trapdoor": {} + } + } +} diff --git a/mods/bworld/blocks/birch_trapdoor.json b/mods/bworld/blocks/birch_trapdoor.json new file mode 100644 index 0000000..ee865ed --- /dev/null +++ b/mods/bworld/blocks/birch_trapdoor.json @@ -0,0 +1,57 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:birch_trapdoor", + "name": "Birch Trapdoor", + "textures": "bworld:birch_trapdoor", + "model": "bworld:trapdoor_bottom", + "item_model": "bworld:trapdoor_bottom", + "render_layer": "cutout", + "states": [ + { + "name": "facing", + "bits": 2, + "default": 0 + }, + { + "name": "half", + "bits": 1, + "default": 0 + }, + { + "name": "open", + "bits": 1, + "default": 0 + } + ], + "variants": { + "open=0,half=1": { + "model": "bworld:trapdoor_top" + }, + "open=1,half=0": { + "model": "bworld:trapdoor_open" + }, + "open=1,half=1": { + "model": "bworld:trapdoor_open_top" + }, + "facing=1": { + "y": 90 + }, + "facing=2": { + "y": 180 + }, + "facing=3": { + "y": 270 + } + }, + "mining": { + "toughness": 3, + "tool": "axe" + }, + "drops": "bworld:birch_trapdoor", + "interactive": true, + "components": { + "bworld:trapdoor": {} + } + } +} diff --git a/mods/bworld/blocks/cherry_trapdoor.json b/mods/bworld/blocks/cherry_trapdoor.json new file mode 100644 index 0000000..bafafad --- /dev/null +++ b/mods/bworld/blocks/cherry_trapdoor.json @@ -0,0 +1,57 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:cherry_trapdoor", + "name": "Cherry Trapdoor", + "textures": "bworld:cherry_trapdoor", + "model": "bworld:trapdoor_bottom", + "item_model": "bworld:trapdoor_bottom", + "render_layer": "cutout", + "states": [ + { + "name": "facing", + "bits": 2, + "default": 0 + }, + { + "name": "half", + "bits": 1, + "default": 0 + }, + { + "name": "open", + "bits": 1, + "default": 0 + } + ], + "variants": { + "open=0,half=1": { + "model": "bworld:trapdoor_top" + }, + "open=1,half=0": { + "model": "bworld:trapdoor_open" + }, + "open=1,half=1": { + "model": "bworld:trapdoor_open_top" + }, + "facing=1": { + "y": 90 + }, + "facing=2": { + "y": 180 + }, + "facing=3": { + "y": 270 + } + }, + "mining": { + "toughness": 3, + "tool": "axe" + }, + "drops": "bworld:cherry_trapdoor", + "interactive": true, + "components": { + "bworld:trapdoor": {} + } + } +} diff --git a/mods/bworld/blocks/dark_oak_trapdoor.json b/mods/bworld/blocks/dark_oak_trapdoor.json new file mode 100644 index 0000000..2e3d2c6 --- /dev/null +++ b/mods/bworld/blocks/dark_oak_trapdoor.json @@ -0,0 +1,57 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:dark_oak_trapdoor", + "name": "Dark Oak Trapdoor", + "textures": "bworld:dark_oak_trapdoor", + "model": "bworld:trapdoor_bottom", + "item_model": "bworld:trapdoor_bottom", + "render_layer": "cutout", + "states": [ + { + "name": "facing", + "bits": 2, + "default": 0 + }, + { + "name": "half", + "bits": 1, + "default": 0 + }, + { + "name": "open", + "bits": 1, + "default": 0 + } + ], + "variants": { + "open=0,half=1": { + "model": "bworld:trapdoor_top" + }, + "open=1,half=0": { + "model": "bworld:trapdoor_open" + }, + "open=1,half=1": { + "model": "bworld:trapdoor_open_top" + }, + "facing=1": { + "y": 90 + }, + "facing=2": { + "y": 180 + }, + "facing=3": { + "y": 270 + } + }, + "mining": { + "toughness": 3, + "tool": "axe" + }, + "drops": "bworld:oak_trapdoor", + "interactive": true, + "components": { + "bworld:trapdoor": {} + } + } +} diff --git a/mods/bworld/blocks/jungle_trapdoor.json b/mods/bworld/blocks/jungle_trapdoor.json new file mode 100644 index 0000000..7ede00d --- /dev/null +++ b/mods/bworld/blocks/jungle_trapdoor.json @@ -0,0 +1,57 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:jungle_trapdoor", + "name": "Jungle Trapdoor", + "textures": "bworld:jungle_trapdoor", + "model": "bworld:trapdoor_bottom", + "item_model": "bworld:trapdoor_bottom", + "render_layer": "cutout", + "states": [ + { + "name": "facing", + "bits": 2, + "default": 0 + }, + { + "name": "half", + "bits": 1, + "default": 0 + }, + { + "name": "open", + "bits": 1, + "default": 0 + } + ], + "variants": { + "open=0,half=1": { + "model": "bworld:trapdoor_top" + }, + "open=1,half=0": { + "model": "bworld:trapdoor_open" + }, + "open=1,half=1": { + "model": "bworld:trapdoor_open_top" + }, + "facing=1": { + "y": 90 + }, + "facing=2": { + "y": 180 + }, + "facing=3": { + "y": 270 + } + }, + "mining": { + "toughness": 3, + "tool": "axe" + }, + "drops": "bworld:jungle_trapdoor", + "interactive": true, + "components": { + "bworld:trapdoor": {} + } + } +} diff --git a/mods/bworld/blocks/oak_trapdoor.json b/mods/bworld/blocks/oak_trapdoor.json new file mode 100644 index 0000000..c242cb9 --- /dev/null +++ b/mods/bworld/blocks/oak_trapdoor.json @@ -0,0 +1,57 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:oak_trapdoor", + "name": "Oak Trapdoor", + "textures": "bworld:oak_trapdoor", + "model": "bworld:trapdoor_bottom", + "item_model": "bworld:trapdoor_bottom", + "render_layer": "cutout", + "states": [ + { + "name": "facing", + "bits": 2, + "default": 0 + }, + { + "name": "half", + "bits": 1, + "default": 0 + }, + { + "name": "open", + "bits": 1, + "default": 0 + } + ], + "variants": { + "open=0,half=1": { + "model": "bworld:trapdoor_top" + }, + "open=1,half=0": { + "model": "bworld:trapdoor_open" + }, + "open=1,half=1": { + "model": "bworld:trapdoor_open_top" + }, + "facing=1": { + "y": 90 + }, + "facing=2": { + "y": 180 + }, + "facing=3": { + "y": 270 + } + }, + "mining": { + "toughness": 3, + "tool": "axe" + }, + "drops": "bworld:oak_trapdoor", + "interactive": true, + "components": { + "bworld:trapdoor": {} + } + } +} diff --git a/mods/bworld/blocks/spruce_trapdoor.json b/mods/bworld/blocks/spruce_trapdoor.json new file mode 100644 index 0000000..ec0df23 --- /dev/null +++ b/mods/bworld/blocks/spruce_trapdoor.json @@ -0,0 +1,57 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:spruce_trapdoor", + "name": "Spruce Trapdoor", + "textures": "bworld:spruce_trapdoor", + "model": "bworld:trapdoor_bottom", + "item_model": "bworld:trapdoor_bottom", + "render_layer": "cutout", + "states": [ + { + "name": "facing", + "bits": 2, + "default": 0 + }, + { + "name": "half", + "bits": 1, + "default": 0 + }, + { + "name": "open", + "bits": 1, + "default": 0 + } + ], + "variants": { + "open=0,half=1": { + "model": "bworld:trapdoor_top" + }, + "open=1,half=0": { + "model": "bworld:trapdoor_open" + }, + "open=1,half=1": { + "model": "bworld:trapdoor_open_top" + }, + "facing=1": { + "y": 90 + }, + "facing=2": { + "y": 180 + }, + "facing=3": { + "y": 270 + } + }, + "mining": { + "toughness": 3, + "tool": "axe" + }, + "drops": "bworld:spruce_trapdoor", + "interactive": true, + "components": { + "bworld:trapdoor": {} + } + } +} diff --git a/mods/bworld/models/trapdoor_bottom.json b/mods/bworld/models/trapdoor_bottom.json new file mode 100644 index 0000000..669b885 --- /dev/null +++ b/mods/bworld/models/trapdoor_bottom.json @@ -0,0 +1,43 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:trapdoor_bottom", + "elements": [ + { + "from": [0, 0, 0], + "to": [16, 3, 16], + "faces": { + "top": { + "texture": "#texture", + "uv": [0, 0, 16, 16] + }, + "bottom": { + "texture": "#texture", + "uv": [0, 0, 16, 16], + "cullface": "bottom" + }, + "north": { + "texture": "#texture", + "uv": [0, 16, 16, 13], + "cullface": "north" + }, + "south": { + "texture": "#texture", + "uv": [0, 16, 16, 13], + "cullface": "south" + }, + "west": { + "texture": "#texture", + "uv": [0, 16, 16, 13], + "cullface": "west" + }, + "east": { + "texture": "#texture", + "uv": [0, 16, 16, 13], + "cullface": "east" + } + } + } + ] + } +} diff --git a/mods/bworld/models/trapdoor_open.json b/mods/bworld/models/trapdoor_open.json new file mode 100644 index 0000000..916fe17 --- /dev/null +++ b/mods/bworld/models/trapdoor_open.json @@ -0,0 +1,43 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:trapdoor_open", + "elements": [ + { + "from": [0, 0, 13], + "to": [16, 16, 16], + "faces": { + "north": { + "texture": "#texture", + "uv": [0, 0, 16, 16] + }, + "south": { + "texture": "#texture", + "uv": [16, 0, 0, 16], + "cullface": "south" + }, + "west": { + "texture": "#texture", + "uv": [16, 0, 13, 16], + "cullface": "west" + }, + "east": { + "texture": "#texture", + "uv": [13, 0, 16, 16], + "cullface": "east" + }, + "top": { + "texture": "#texture", + "uv": [0, 16, 16, 13], + "cullface": "top" + }, + "bottom": { + "texture": "#texture", + "uv": [0, 13, 16, 16], + "cullface": "bottom" + } + } + } + ] + } +} diff --git a/mods/bworld/models/trapdoor_open_top.json b/mods/bworld/models/trapdoor_open_top.json new file mode 100644 index 0000000..d00fd1f --- /dev/null +++ b/mods/bworld/models/trapdoor_open_top.json @@ -0,0 +1,43 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:trapdoor_open_top", + "elements": [ + { + "from": [0, 0, 13], + "to": [16, 16, 16], + "faces": { + "north": { + "texture": "#texture", + "uv": [16, 16, 0, 0] + }, + "south": { + "texture": "#texture", + "uv": [0, 16, 16, 0], + "cullface": "south" + }, + "west": { + "texture": "#texture", + "uv": [16, 16, 13, 0], + "cullface": "west" + }, + "east": { + "texture": "#texture", + "uv": [13, 16, 16, 0], + "cullface": "east" + }, + "top": { + "texture": "#texture", + "uv": [16, 16, 0, 13], + "cullface": "top" + }, + "bottom": { + "texture": "#texture", + "uv": [16, 13, 0, 16], + "cullface": "bottom" + } + } + } + ] + } +} diff --git a/mods/bworld/models/trapdoor_top.json b/mods/bworld/models/trapdoor_top.json new file mode 100644 index 0000000..1f87c48 --- /dev/null +++ b/mods/bworld/models/trapdoor_top.json @@ -0,0 +1,43 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:trapdoor_top", + "elements": [ + { + "from": [0, 13, 0], + "to": [16, 16, 16], + "faces": { + "top": { + "texture": "#texture", + "uv": [0, 0, 16, 16], + "cullface": "top" + }, + "bottom": { + "texture": "#texture", + "uv": [0, 0, 16, 16] + }, + "north": { + "texture": "#texture", + "uv": [0, 16, 16, 13], + "cullface": "north" + }, + "south": { + "texture": "#texture", + "uv": [0, 16, 16, 13], + "cullface": "south" + }, + "west": { + "texture": "#texture", + "uv": [0, 16, 16, 13], + "cullface": "west" + }, + "east": { + "texture": "#texture", + "uv": [0, 16, 16, 13], + "cullface": "east" + } + } + } + ] + } +} diff --git a/mods/bworld/recipes/crafting_acacia_trapdoor.json b/mods/bworld/recipes/crafting_acacia_trapdoor.json new file mode 100644 index 0000000..581662d --- /dev/null +++ b/mods/bworld/recipes/crafting_acacia_trapdoor.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + "PPP", + "PPP" + ], + "key": { + "P": "bworld:acacia_planks" + }, + "result": { + "id": "bworld:acacia_trapdoor", + "count": 2 + } + } +} diff --git a/mods/bworld/recipes/crafting_birch_trapdoor.json b/mods/bworld/recipes/crafting_birch_trapdoor.json new file mode 100644 index 0000000..712959a --- /dev/null +++ b/mods/bworld/recipes/crafting_birch_trapdoor.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + "PPP", + "PPP" + ], + "key": { + "P": "bworld:birch_planks" + }, + "result": { + "id": "bworld:birch_trapdoor", + "count": 2 + } + } +} diff --git a/mods/bworld/recipes/crafting_cherry_trapdoor.json b/mods/bworld/recipes/crafting_cherry_trapdoor.json new file mode 100644 index 0000000..d3419c9 --- /dev/null +++ b/mods/bworld/recipes/crafting_cherry_trapdoor.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + "PPP", + "PPP" + ], + "key": { + "P": "bworld:cherry_planks" + }, + "result": { + "id": "bworld:cherry_trapdoor", + "count": 2 + } + } +} diff --git a/mods/bworld/recipes/crafting_jungle_trapdoor.json b/mods/bworld/recipes/crafting_jungle_trapdoor.json new file mode 100644 index 0000000..76c82f3 --- /dev/null +++ b/mods/bworld/recipes/crafting_jungle_trapdoor.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + "PPP", + "PPP" + ], + "key": { + "P": "bworld:jungle_planks" + }, + "result": { + "id": "bworld:jungle_trapdoor", + "count": 2 + } + } +} diff --git a/mods/bworld/recipes/crafting_oak_trapdoor.json b/mods/bworld/recipes/crafting_oak_trapdoor.json new file mode 100644 index 0000000..c77dbe2 --- /dev/null +++ b/mods/bworld/recipes/crafting_oak_trapdoor.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + "PPP", + "PPP" + ], + "key": { + "P": "bworld:planks" + }, + "result": { + "id": "bworld:oak_trapdoor", + "count": 2 + } + } +} diff --git a/mods/bworld/recipes/crafting_spruce_trapdoor.json b/mods/bworld/recipes/crafting_spruce_trapdoor.json new file mode 100644 index 0000000..681f8aa --- /dev/null +++ b/mods/bworld/recipes/crafting_spruce_trapdoor.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + "PPP", + "PPP" + ], + "key": { + "P": "bworld:spruce_planks" + }, + "result": { + "id": "bworld:spruce_trapdoor", + "count": 2 + } + } +} diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index e4dee35..ff9f466 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -9,6 +9,7 @@ export function setup(ctx: ServerContext) { register_furnace(ctx); register_smithing_table(ctx); register_fence(ctx); + register_trapdoor(ctx); register_farming(ctx); register_fishing(ctx); register_curios(ctx); @@ -58,6 +59,29 @@ function register_fence(ctx: ServerContext) { }); } +// the facing state's values, the way a block's y turns it +const FACINGS = ["north", "east", "south", "west"]; + +// a trapdoor opens and closes when right clicked, like a wooden one in minecraft. placed against the side of a block +// it hangs from that block, in the half of the side that was clicked. placed on top of a block it lies in the bottom +// half, and under one in the top half, swinging open away from the player, like minecraft's TrapDoorBlock +function register_trapdoor(ctx: ServerContext) { + ctx.components.register_block("bworld:trapdoor", { + state_for_placement(placement): Record { + if (placement.face !== "top" && placement.face !== "bottom") { + return { facing: FACINGS.indexOf(placement.face), half: placement.hit[1] > 0.5 ? 1 : 0 }; + } + // facing already faces the player + return { half: placement.face === "top" ? 0 : 1 }; + }, + on_interact(block) { + const open = ctx.world.get_state(block.x, block.y, block.z, "open") ?? 0; + ctx.world.set_state(block.x, block.y, block.z, "open", open ? 0 : 1); + return true; + }, + }); +} + // the container a block keeps its items in, made the first time it's needed. blocks placed before tile data // existed don't have one yet function block_container(ctx: ServerContext, block: BlockRef, size: number): Container { diff --git a/mods/bworld/tags/blocks/bworld/wooden_trapdoors.json b/mods/bworld/tags/blocks/bworld/wooden_trapdoors.json new file mode 100644 index 0000000..f93caff --- /dev/null +++ b/mods/bworld/tags/blocks/bworld/wooden_trapdoors.json @@ -0,0 +1,13 @@ +{ + "format_version": 1, + "tag": { + "values": [ + "bworld:oak_trapdoor", + "bworld:spruce_trapdoor", + "bworld:birch_trapdoor", + "bworld:jungle_trapdoor", + "bworld:acacia_trapdoor", + "bworld:cherry_trapdoor" + ] + } +} diff --git a/mods/bworld/tags/items/bworld/wooden_trapdoors.json b/mods/bworld/tags/items/bworld/wooden_trapdoors.json new file mode 100644 index 0000000..f93caff --- /dev/null +++ b/mods/bworld/tags/items/bworld/wooden_trapdoors.json @@ -0,0 +1,13 @@ +{ + "format_version": 1, + "tag": { + "values": [ + "bworld:oak_trapdoor", + "bworld:spruce_trapdoor", + "bworld:birch_trapdoor", + "bworld:jungle_trapdoor", + "bworld:acacia_trapdoor", + "bworld:cherry_trapdoor" + ] + } +} diff --git a/mods/bworld/textures/acacia_trapdoor.png b/mods/bworld/textures/acacia_trapdoor.png new file mode 100644 index 0000000000000000000000000000000000000000..097896faf9c909f82660226dd3ecdbf5e3cba8c5 GIT binary patch literal 6247 zcmeHLdpwkB8y?!Flv9!%${58`bDl9{%#e&2HJL>YC7HuJ40GB!n4zzeB88~76GEFP zyH1I6s8CY2qz$2DI~NKS+Lrp>87h6d-|pA%_w9f2n=#MxT=#X~&-L8*{mg4SV}*yV zj+qVw0@0;;QhmU`2CCOoP4GQ+SGWTN^0hR^FHq*gR6xZN5tqjYpt5K&00ophE(D_N zEb#9r=_TonhRrgHT`bl(e>XS(a%f%FqX?SEh7rFrksUDl=D8=f!doK}ay7fXp8K`-t+;n@x;~?sxu({XW??aBKyB09s%!r+qD(!wGjvig~ z=`u=SQnPm~*BKP~jM!F;@{X_+76*)XB7Xh&1&QSx;w^M$XEuAW(!d@;Dn zO=oL!#fwW(n{Ez`;zCSkbn2BtYm_;MVL@pXtuGRsuFr5y{I139H;gd^)?O}-pEDzS zL%YvHSOLXQJZr${uqQ6Udb8#Ue-q_#-O?$JKcox#23vr#R<A*aUSNu z1FlP^qU*x9K1%2^)I{4HA|B-IS>{aBvE$}=J9KdOub;iwZKt~l;)zkc@!FiNGZ(JR z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d^XQqiggQO6MUY+WtVf>BP zCU8<)Mx(oHKi1Tng+}FC9b5-LDBOywo!mG%#It+Q10i;5tS)AzP+-02^i zgo~u6;m_POKe;c{MPHlO#&r z0STR_2l+|SA<5S`T}kH`9(~;7U;M`Wm3!tZMCIkN^tNA?@tY20Lw4x|cMXTXIp<>W zo6h1?jF)z3q*qkrg7Vzpr1-8CQ@y-QqDG!bsS64*efkac;g`-9*k$F7jQz0JU{QSd z?6#vu=`R<5rJwM$o%jQ98h>d0O4+Ff>N#?J_0;WGE9^!lvI6;>1s;*b z3=Dh+L6~vJ#O${~L5ULAh?3y^w370~qEv>0#LT=By}Z;C1rt3(J<}BT2$0EFJY5_^ zEc{z1ZR9=VAi%Qn+)7ObMRpb!2QME74+mxw2W63l6B7gkRi+$>_-wUbf8!Rbw|C#) z*)OhC8=>$R8iABlZ3p&`>C+Jvd(8PkW*Ek803Y{}&f;CAq3 z*vEDwqM_hVlg#JNNvHImWw01@sjDv7ckl*>LxpeF&AE(6LT#=Q`PF5n h1zXn4yK;D4?!S(Iv#L$rFoQyn!PC{xWt~$(69C$petQ4_ literal 0 HcmV?d00001 diff --git a/mods/bworld/textures/cherry_trapdoor.png b/mods/bworld/textures/cherry_trapdoor.png new file mode 100644 index 0000000000000000000000000000000000000000..f2f1d4ce709dc9432611aacf47f877bdbfd8f18c GIT binary patch literal 6598 zcmeHLc{o&U8y_zbZQja~lrbSK$LwQfm@G5ajI5)QxJ5JlJeAlOyc(I-wLzpqmmZh6`)S{V8OOEG zt_R9%_ARcfYPo3YVDmU}UTN!Z)BS?v<$;!C)eAkhO*e0?Mgnt7IoG-2_6cW?Y>!=Y zB59#TRAg~A19N$)1~y&$9CbyVqwHbsx*g}VBMUXWEi9+Z>}j0W*RtlZ%XD1<0~~hl z50@jna(mGVQ%z28$DX1I4UN&WC=6U~VHvZQn@ z&fBMHi5Dg4J&}_{%!y--qf9;Isp~pTMtgu9I96#oJB6WFb>RN9Wc*&&By<15oP8_( z7O}S)g)X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M!BFns$kv+D+&hGWKp*lcP{g$ Qpv#AGbMoXAIxb%GFZg->bpQYW literal 0 HcmV?d00001 diff --git a/mods/bworld/textures/dark_oak_trapdoor.png b/mods/bworld/textures/dark_oak_trapdoor.png new file mode 100644 index 0000000000000000000000000000000000000000..090f40466e566071592dab0474688ab79c20ce6b GIT binary patch literal 6363 zcmeHLdpuNWA0H8|MCHw8S8E!I*ty@QnIt2oMM4o)&YU?jPUdE2m_cM!cDviPsnlkZ zC|x#Dl7w_o$)c!M*bJ)eVs%xyyk~|=@AmHd_Icm^ubIzq&hvbK-{1ZBoX=U~@8>na z^ixw53N?Y}%?&{QPt^U!86$t~9}fX2)L2GBP>3o3P^0AvnGhC3XjQx%LPHu@h(c+8 zJ-9gRnAX{>Uo@r2$gy|grK-s6E3>wCEigD2XXljk%IEy^Pcd^gx$w>$7#jSw)~$~) zmRjdEVW!c#O<&?-&T${~bq>zB_cC#af4kFDx^ZlpH{oJm0nqN&C0I&Xc&>KoKj0hI znb%f##tkF{7Jv4A_arbt?Fk1vvaOwX!Q_NTh2K7qmK`&#?tRu$L7)^MeUuH{qxqYnns^^ zGSzlt??S&p+vQiLoeIfRgd}e^`K;$w?T}>uvSX5|4UjbozP^%G4_k`EQKe77D%xg) zeK|HQ8Gr0`e`e($At@ZcbM6|OJF?Eb0eKsmXIr_*8+tEJa!>+yNtWT zb>J=hyKJ~@u$ZhJI{@Zc$Z=ccvC4(?%p*JSA0cNY7}t$E;~ z^1|_}>S>Q_)G4=>LzIZIW-rZ)FqPshyZF7uw-;Y>xXT?+OIW|V*}3^E19YmyLgZbU zGQn}@m>V^AYf^TnW}A6l?9dpD?c@Y&F8Dd7*mVg%6Jef~-;e*cp=i#kE$B&C9WVTL zaaZBA6*+;O{?9sod_KGHPLosiN+av#*F%jhPnBHD;mhJF|=CWl$gb9No|xIl&9u z;j;IrWjn(VygzRJ`E#hrS)Yj(UtrQUoH)15_h5c}+Llw_1@0pn8@JBCPiGyUwJq`1 zWybRgjrP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WHf+w%Gm)9_+A8Z^ZLF6*8DFDyYF-(nYRtBX`m}8RWLxW7 zsco0tt_ZZ94v%9lEaeY`SI)6_GQR)F+1gEIoH)jWw{vTRy3hs*6^iHS$2~MRGWDPR C<&!D^ literal 0 HcmV?d00001 diff --git a/mods/bworld/textures/jungle_trapdoor.png b/mods/bworld/textures/jungle_trapdoor.png new file mode 100644 index 0000000000000000000000000000000000000000..7e04e338a0ecda656fff9a5579a186e61d745b2e GIT binary patch literal 357 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1qucLCF%=h?3y^w370~qEv>0#LT=By}Z;C1rt33 zJtM=93Yk-ZimrOPIEGmG&z)?|cgR4%rB=b)BvZiR5mU*9fD2F9Vj8SU`En}Ul5-DP z)hOkf8PBWTC}ip5Jb&fKP3NyWPxrs=TRBaCty1!JZinvekJs_tIe$`i!ZdM}70*Q( z416k?!-82CY-dzRV0?2b?T)O*AI=4`7qlC0+cUhBY;f&Tv|*dh@I|Ykf%QQp!&k0= zAJPoFI4>y4MFphZW8iq7%k&{Jn&H!lvI6;>1s;*b z3=DibL6~s|oA`d9phSslL`iUdT1k0gQ7S`0VrE{6US4X6f{C7?o@t7E1jytEo-U3d z7XH1HHgY)`^0?lwx75^VvI{6IDiCB*KX_8Up!9q8>g5H_LQ!82F3g>D?@pV!jo)jg z^QUj9JTJMnS8Kyu-&2LAYd5@KD3!=0^R9jA%0s35S7_gj+aBBSqtbZ&YX`4C9 z(gKeP@H5?OWO?FX$ZuH9#JKn6XWst~y6b!ytV>vKp3%^6V%~e4->b~HBk;8Bxr26QWWUzN%T+gZ*jUt?Qzm`!I1D$QwbkXI65=KNVZqw&pz6S1t~ uCm5N{8JRdZ)@%(pw_fc7Z`ZG$r@TKb4ZA;@eBlN9mBG{1&t;ucLK6Vc+JSHY literal 0 HcmV?d00001 diff --git a/mods/bworld/textures/spruce_trapdoor.png b/mods/bworld/textures/spruce_trapdoor.png new file mode 100644 index 0000000000000000000000000000000000000000..1067d7c4566536f4026e2985f8b197585ebdd025 GIT binary patch literal 6495 zcmeHLc|4SB`=690F=TBK)96SvW-(*TG?ucCh=UUj$}=<1Ffof+%&08o)XA1D*(-|E zBAp5q2}$LIl9G~>EXP}9IW5wn_nD#6`=0l_zdoPe`(J#Fxv%?seXr~O-q&^C^Gv+Q z1}9bJCCV@uOqJ?v?+N`^m;NcqL9eN4Ar>&00%fbWuh0_^BY1o+lNAOcgpoWD0ZLd* z7)ta3VI!6I!PU)V z1z?|DdN#fM_2jU*t`cqHROuSO+L1T3l(7!Z2;4EFq5YXpJb9`-BdXnA-cw?nUb5_E zjp4St=*?PB)^UEkqF!b4+wtq;BZwb7UR8!QryuY?O1F7?KYqA2=bW>}u`^baH>%xj zPbrr6W&dm#v_@}q6J_wxVjrgB$S_Rth|2v_I-{-oVb@pY4>y=R&&>2MGI&s%+S|(^ zhDa36f4`aXOv^fst8*nKGqBnxI`ovNk69Z_`Ec)UW5B)6YEx^u+eh=r{(c$?8*N8A z%J-N)HbDF$0t0A0HLCl>U15!PjWy@KPo#)Ugl)bz9aW9C>gSI!I6chD`1Sc0NTGE( z2XzDU>wLv>Q%n9K3K{6q`ElrviU7m<%LSP9`M-r!?aVp%q3_N2Z6ddL`DzJy zd2@Di%NoZW>Q#FUcFB)jxHM&S3a+wS{%lZOW5>xN1!IqiZ_tf*<;hRVEfT|y4#hO@77w;?@^R!y?YTo!F)2aKiz$KiOGahDnyUQ`4L-^lZthey3d~DPCW;Ju{Dh%j@9Yvk6XyhhEIio!j2B zq@+aXeam$vz*o51skz8KV~!=>y|pxTBFspXVV;-vM?jG8Vk1(45k2UZS}(7yr91Hg z@%N;O5Aj6n<{WDGhx+`SJMZfrihsBMwIjHK)sfS9OQm7{agkDO;x=HF?=pC_=+7N; zDl4iT)XoA3N3BO61u?kzBqRG{NfV!uB2N58TD|jrFXZZ zqHKTQv_#x2a9~ksfq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#!c0Q$lVMVe#weodI<#8 z%`o4mRxp^Usa)gS#;mRGC9o|Gk8e%9r`+srpLlP%_XI3fbI+#0O^zC=?UDSLDE*k+ zClvh&J6L-pWpRa7z5aoX8Q(9hyWwAdBnT$gxJ^WLTehhDj*;nc6&N$2#x$a~vy&^A zeov=KH;4JarGC-glnjNZV_7Y>sc&u-9^X_-n9_6NJTH1zw^&bf!nL|(!Go~-I}|c6 zIu@_on_JDxG0#3pNWOIDvb=~FOPG=A|hws+m`Z^I2+39W|In0wHmY&Fsj1_`?7UG GJO2qc>-*OL literal 0 HcmV?d00001 diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 795509b..0477c33 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -850,7 +850,12 @@ export class GameServer { break; case "use_block": if (is_block_position(message.x, message.y, message.z) && faces.includes(message.face)) { - this.#use_block(player, message.x, message.y, message.z, message.face); + // 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); } break; case "select_slot": @@ -975,7 +980,7 @@ export class GameServer { return true; } - #use_block(player: ServerPlayer, x: number, y: number, z: number, face: Faces) { + #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); const [tx, ty, tz] = [target.x, target.y, target.z]; @@ -1033,9 +1038,9 @@ export class GameServer { return; } // solid blocks can't go where a mob or player is, the one placing it included, like minecraft - const state = placed_state(held.block_id, player); - const placing = EverythingRegistry.get("blocks", held.block_id); - const value = state === undefined ? default_block_value(0, placing) : block_value(0, state); + const placing = EverythingRegistry.get("blocks", held.block_id)!; + const value = this.#placement_value(placing, player, { x: tx, y: ty, z: tz, face, hit }); + const state = value >>> 16; if (placing?.has_collision && this.#occupied(tx, ty, tz, collision_shape_at(placing, value))) { this.#correct(player, tx, ty, tz); return; @@ -1045,6 +1050,7 @@ export class GameServer { 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 }; @@ -1059,6 +1065,40 @@ export class GameServer { this.mods.after.block_place.emit(place); } + // the block's value with the states a player placing it gets: its defaults, facing them when it has a facing, + // then what its components' state_for_placement say, like minecraft's getStateForPlacement + #placement_value( + block: BlockRegistry, + player: ServerPlayer, + where: { x: number; y: number; z: number; face: Faces; hit: number[] }, + ): number { + const facing = placed_state(block.id, player); + let value = facing === undefined ? default_block_value(0, block) : block_value(0, facing); + const components = this.mods.components_of(block.id).filter(({ component }) => component.state_for_placement); + if (components.length === 0) return value; + const placement = { + ...where, + hit: [where.hit[0], where.hit[1], where.hit[2]] as const, + player: this.mods.player(player), + }; + for (const { mod, id, component, params } of components) { + const states = run_guarded( + mod, + `${id} state_for_placement`, + () => component.state_for_placement!(placement, params), + ); + for (const [name, state] of Object.entries(states ?? {})) { + const known = block.states?.find((s) => s.name === name); + if (known && Number.isInteger(state) && state >= 0 && state < 2 ** known.bits) { + value = set_state_value(value, block, name, state)!; + } else { + console.warn(`${id} state_for_placement: ${block.id} has no state ${name} that fits ${state}`); + } + } + } + return value; + } + // 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) => diff --git a/tests/trapdoor_test.ts b/tests/trapdoor_test.ts new file mode 100644 index 0000000..3bf60d5 --- /dev/null +++ b/tests/trapdoor_test.ts @@ -0,0 +1,101 @@ +import { assert, assertEquals } from "@std/assert"; +import { Container, ItemStack } from "$/common/inventory.ts"; +import { CRAFTING_RESULT_SLOT } from "$/common/protocol.ts"; +import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts"; +import { bake_block, type ModelJson } from "$/common/block_models.ts"; +import { collision_shape_at, shape_at } from "$/common/block_shapes.ts"; +import { get_state_value } from "$/common/utils.ts"; +import { update_crafting_result } from "$/server/game/crafting.ts"; +import { test_game } from "./helpers.ts"; + +// alice on a stone floor at y 100, south of 0 100 0 and looking north, with oak trapdoors in hand +async function setup() { + const t = await test_game("mods"); + t.join(1, "alice"); + for (let x = -3; x <= 3; x++) { + for (let z = -3; z <= 3; z++) { + t.game.set_block(x, 99, z, "bworld:stone"); + for (let y = 100; y < 103; y++) t.game.set_block(x, y, z, "bworld:air"); + } + } + t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: 0, pitch: 0 }); + t.send(1, { type: "chat", text: "/give bworld:oak_trapdoor 64" }); + t.send(1, { type: "select_slot", slot: 0 }); + const states = (x: number, y: number, z: number) => { + const info = t.game.world.get_block_info(x, y, z)!; + const value = t.game.world.get_block_value(x, y, z); + return Object.fromEntries(["facing", "half", "open"].map((name) => [name, get_state_value(value, info, name)])); + }; + return { t, states }; +} + +Deno.test("on top of a block a trapdoor lies in the bottom half, swinging open away from the player", async () => { + const { t, states } = await setup(); + t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] }); + assertEquals(t.game.world.get_block_id(0, 100, 0), "bworld:oak_trapdoor"); + // alice looks north, so it faces her, south + assertEquals(states(0, 100, 0), { facing: 2, half: 0, open: 0 }); +}); + +Deno.test("against the side of a block a trapdoor hangs from it, in the half that was clicked", async () => { + const { t, states } = await setup(); + t.game.set_block(0, 100, 0, "bworld:stone"); + t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.8, 1] }); + assertEquals(states(0, 100, 1), { facing: 2, half: 1, open: 0 }); + t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "east", hit: [1, 0.2, 0.5] }); + assertEquals(states(1, 100, 0), { facing: 1, half: 0, open: 0 }); + // under a block, in the top half + t.game.set_block(-2, 102, 0, "bworld:stone"); + t.send(1, { type: "use_block", x: -2, y: 102, z: 0, face: "bottom", hit: [0.5, 0, 0.5] }); + assertEquals(states(-2, 101, 0).half, 1); +}); + +Deno.test("right clicking a trapdoor opens and closes it, and what it's shaped like goes with it", async () => { + const { t, states } = await setup(); + t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] }); + const info = t.game.world.get_block_info(0, 100, 0)!; + const shape = () => shape_at(info, t.game.world.get_block_value(0, 100, 0)); + assertEquals(shape(), [[0, 0, 0, 1, 3 / 16, 1]]); + + t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "top", hit: [0.5, 3 / 16, 0.5] }); + assertEquals(states(0, 100, 0).open, 1); + // facing south it swung up against the north side, where its hinge is + assertEquals(shape(), [[0, 0, 0, 1, 1, 3 / 16]]); + assertEquals(collision_shape_at(info, t.game.world.get_block_value(0, 100, 0)), shape()); + // still one trapdoor in the world, none placed on top of it + assertEquals(t.game.world.get_block_id(0, 101, 0), "bworld:air"); + + t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 3 / 16] }); + assertEquals(states(0, 100, 0).open, 0); +}); + +Deno.test("trapdoors are drawn with their wood's texture, open ones as a panel standing on the hinge side", async () => { + const { t } = await setup(); + const trapdoor = EverythingRegistry.get("blocks", "bworld:birch_trapdoor")!; + const models = Object.fromEntries(EverythingRegistry.entries("models")); + t.game.set_block(0, 100, 0, "bworld:birch_trapdoor"); + for (const [open, half] of [[0, 0], [0, 1], [1, 0], [1, 1]]) { + // facing is the first 2 state bits, then half, then open + const value = (t.game.world.get_block_value(0, 100, 0) & 0xffff) | ((open << 3 | half << 2) << 16); + const quads = bake_block(trapdoor, value, models); + assertEquals(quads.length, 6); + assert(quads.every((quad) => quad.texture === "bworld:birch_trapdoor")); + const ys = quads.flatMap((quad) => quad.positions.filter((_, i) => i % 3 === 1)); + const zs = quads.flatMap((quad) => quad.positions.filter((_, i) => i % 3 === 2)); + if (open) { + // facing north, so against the south side, from the bottom to the top + assertEquals([Math.min(...zs), Math.max(...ys)], [13 / 16, 1]); + } else { + assertEquals([Math.min(...ys), Math.max(...ys)], half ? [13 / 16, 1] : [0, 3 / 16]); + } + } +}); + +Deno.test("six planks make two trapdoors of that wood", async () => { + const { game } = await test_game("mods"); + const grid = new Container(10); + for (let slot = 0; slot < 6; slot++) grid.set_item(slot, new ItemStack("bworld:jungle_planks", 1)); + update_crafting_result(grid, game.recipes.shaped); + const result = grid.get_item(CRAFTING_RESULT_SLOT); + assertEquals([result?.type_id, result?.amount], ["bworld:jungle_trapdoor", 2]); +});