Trapdoors

This commit is contained in:
2026-10-03 17:52:58 -03:00
parent 91655c28c5
commit 63e6248f9f
35 changed files with 927 additions and 23 deletions
+1 -1
View File
@@ -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
+6 -1
View File
@@ -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;
+13
View File
@@ -52,6 +52,9 @@ export interface BlockComponent<P = any> {
// 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<string, number> | 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;
+4 -3
View File
@@ -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
+33 -12
View File
@@ -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<string, number> {
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).
+1 -1
View File
@@ -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 |
+57
View File
@@ -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": {}
}
}
}
+57
View File
@@ -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": {}
}
}
}
+57
View File
@@ -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": {}
}
}
}
+57
View File
@@ -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": {}
}
}
}
+57
View File
@@ -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": {}
}
}
}
+57
View File
@@ -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": {}
}
}
}
+57
View File
@@ -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": {}
}
}
}
+43
View File
@@ -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"
}
}
}
]
}
}
+43
View File
@@ -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"
}
}
}
]
}
}
+43
View File
@@ -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"
}
}
}
]
}
}
+43
View File
@@ -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"
}
}
}
]
}
}
@@ -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
}
}
}
@@ -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
}
}
}
@@ -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
}
}
}
@@ -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
}
}
}
@@ -0,0 +1,17 @@
{
"format_version": 1,
"recipe": {
"type": "shaped",
"pattern": [
"PPP",
"PPP"
],
"key": {
"P": "bworld:planks"
},
"result": {
"id": "bworld:oak_trapdoor",
"count": 2
}
}
}
@@ -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
}
}
}
+24
View File
@@ -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<string, number> {
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 {
@@ -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"
]
}
}
@@ -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"
]
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 342 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 357 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 354 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

+45 -5
View File
@@ -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<BlockRegistry>("blocks", held.block_id);
const value = state === undefined ? default_block_value(0, placing) : block_value(0, state);
const placing = EverythingRegistry.get<BlockRegistry>("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) =>
+101
View File
@@ -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<BlockRegistry>("blocks", "bworld:birch_trapdoor")!;
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("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]);
});