Blocks interacting on the world api
This commit is contained in:
@@ -83,6 +83,27 @@ faster. The client times breaking itself, and the server checks the result.
|
||||
When it breaks, the block's components' `on_break` runs and it drops `drops` or what its `loot_table` gives, unless it
|
||||
has `requires_tool` and the player's tool is the wrong type or a tier below `mining.tier`.
|
||||
|
||||
### Breaking From Scripts
|
||||
|
||||
`ctx.world.break_block(x, y, z)` breaks a block like a player with nothing in their hand, and returns what it dropped,
|
||||
or `undefined` when there was nothing there. A machine that breaks blocks over time, like a drill, shows its progress
|
||||
and passes options for how it breaks them:
|
||||
|
||||
```ts
|
||||
const toughness = ctx.blocks.toughness(id); // seconds by hand, Infinity for one that can't be broken
|
||||
ctx.world.set_break_progress(x, y, z, progress); // cracks, 0 to 1, that every player who sees it sees
|
||||
// once it's through: drops what the right tool would, and hands them back instead of dropping them
|
||||
const drops = ctx.world.break_block(x, y, z, { harvest: true, collect: true });
|
||||
```
|
||||
|
||||
`set_break_progress` is like Minecraft's `destroyBlockProgress`. Only a change in the stage the cracks are drawn at is
|
||||
sent, so setting it every tick costs little. `undefined` takes the cracks off, and so does the block being broken or
|
||||
replaced. Players who load the chunk later don't see cracks set before, so keep setting it while it's being broken.
|
||||
|
||||
`harvest` drops what the right tool would, though nothing holds one, like Create's drills and saws: stone gives
|
||||
cobblestone. `collect` returns the drops without dropping them, for a machine that puts them somewhere, like onto a
|
||||
belt.
|
||||
|
||||
## Placement
|
||||
|
||||
Players place a block by right clicking a face of another block with its item. It goes next to the face they clicked, or
|
||||
@@ -151,6 +172,11 @@ So it spreads one block a step, falls off edges and spreads again where it lands
|
||||
its source is gone. It only flows into air, and stops at chunks that aren't loaded. Buckets only pick up sources. Flows
|
||||
still going when the server stops don't carry on after it starts again until something next to them changes.
|
||||
|
||||
`ctx.world.liquid_flow(x, y, z)` says which way the liquid in a block flows, like Minecraft's `FluidState.getFlow`: a
|
||||
vector 1 long toward the lower liquid beside it, and down where it falls against a wall, or `{ x: 0, y: 0, z: 0 }` where
|
||||
it's still or there's none. It's the same flow that pushes players and items along, for machines it turns, like a water
|
||||
wheel.
|
||||
|
||||
### Waterlogging
|
||||
|
||||
A block that isn't a liquid can hold one, like Minecraft's waterlogged fences. `waterloggable` names the liquid:
|
||||
|
||||
@@ -150,14 +150,41 @@ interface EntityRef {
|
||||
readonly biome: string; // where its feet are, see ../worldgen/biomes.md#seasons
|
||||
readonly season: Season; // that biome's
|
||||
readonly removed: boolean;
|
||||
readonly velocity: { x: number; y: number; z: number }; // in blocks a second
|
||||
teleport(x: number, y: number, z: number): void;
|
||||
push(x: number, y: number, z: number): void; // adds to its velocity
|
||||
remove(): void;
|
||||
get_item(): ItemStack | undefined; // for an item lying around, a copy
|
||||
set_item(item: ItemStack | undefined): void; // for an item lying around, undefined takes it away
|
||||
}
|
||||
```
|
||||
|
||||
Once an entity is removed, by a script or anything else, it stays removed, and teleporting it does nothing. Check
|
||||
`removed` before relying on one you kept from earlier.
|
||||
|
||||
`push` adds to how fast it's moving, like Minecraft's `Entity.push`: a fan blowing it along does it every tick. Players
|
||||
have a `push` too, see [Players](players.md#pushing).
|
||||
|
||||
## Finding Entities
|
||||
|
||||
`ctx.world.entities_in(from, to, type?)` finds the entities whose boxes overlap the box between two corners, like
|
||||
Minecraft's `getEntitiesOfClass`, only the ones of `type` when it's given. Players aren't among them, they're in
|
||||
`ctx.players`.
|
||||
|
||||
Items lying around are entities of type `bworld:item`. A machine that works on them, like a press, finds them, changes
|
||||
them with `set_item`, and adds what it made with `ctx.world.spawn_item`, which lays an item down still where it's put,
|
||||
unlike `drop_item`, which pops it out of a block:
|
||||
|
||||
```ts
|
||||
const [item] = ctx.world.entities_in({ x, y: y - 1, z }, { x: x + 1, y, z: z + 1 }, "bworld:item");
|
||||
const stack = item?.get_item();
|
||||
if (stack?.id === "bworld:iron_ingot") {
|
||||
item.set_item(stack.count > 1 ? { ...stack, count: stack.count - 1 } : undefined);
|
||||
const { x: ix, y: iy, z: iz } = item.position;
|
||||
ctx.world.spawn_item(ix, iy, iz, { id: "create:iron_sheet", count: 1 });
|
||||
}
|
||||
```
|
||||
|
||||
## Entity Models
|
||||
|
||||
Entity models are `models/<name>.geo.json`, as Blockbench exports them for Bedrock (the "Bedrock Entity" format), and
|
||||
|
||||
@@ -200,6 +200,11 @@ starts it with `player.start_using(ticks)`, and the item's components hear how i
|
||||
[Using Over Time](../items/index.md#using-over-time). `stop_using()` ends it without either ending, and `is_using()`
|
||||
says whether they're using one. While they are, they walk at a fifth of their speed and can't sprint.
|
||||
|
||||
## Pushing
|
||||
|
||||
`player.push(x, y, z)` adds to how fast they're moving, in blocks a second, like Minecraft's `Entity.push`: a fan
|
||||
blowing them along does it every tick. Their client moves them, so it's sent there and added to theirs.
|
||||
|
||||
## Death
|
||||
|
||||
When a player dies, everything they carried falls out where they died, and their tools lose durability (see
|
||||
|
||||
@@ -199,6 +199,19 @@ ctx.components.register_item("copper_tools:sling", {
|
||||
});
|
||||
```
|
||||
|
||||
## Machines Using Items
|
||||
|
||||
`ctx.world.use_item_on(item, x, y, z, face)` uses an item on the face of a block the way a player holding it would, like
|
||||
Create's deployer: the block's `on_interact`, or the item's `on_use` with the block as its target, or placing it. A
|
||||
player that isn't anyone, named `[machine]`, does it, standing in the block in front of that face and looking at it,
|
||||
like NeoForge's `FakePlayer`. It's never online, so nothing sent to it goes anywhere.
|
||||
|
||||
```ts
|
||||
const { item, given } = ctx.world.use_item_on({ id: "bworld:cobblestone", count: 5 }, x, y, z, "top");
|
||||
// item is what it holds after, { id: "bworld:cobblestone", count: 4 } once one is placed, undefined when used up.
|
||||
// given is anything else it was given, like a stick from a wand's on_use
|
||||
```
|
||||
|
||||
## Server Script Helpers
|
||||
|
||||
```ts
|
||||
|
||||
Reference in New Issue
Block a user