Blocks interacting on the world api

This commit is contained in:
2026-10-15 12:49:44 -03:00
parent 7faa070d83
commit b43e8fe8b8
15 changed files with 493 additions and 28 deletions
+45 -2
View File
@@ -234,8 +234,12 @@ export interface ServerAfterEvents {
export interface ServerWorld {
get_block(x: number, y: number, z: number): Id | undefined; // undefined when the chunk isn't loaded
set_block(x: number, y: number, z: number, id: Id): boolean; // runs on_break / on_create, synced to everyone
// like breaking it by hand without a tool: on_break, then its drops. false when there was nothing to break
break_block(x: number, y: number, z: number): boolean;
// like breaking it by hand without a tool: on_break, then its drops, which it returns (copies). undefined when there
// was nothing to break. see BreakOptions for machines breaking it
break_block(x: number, y: number, z: number, options?: BreakOptions): ItemStack[] | undefined;
// cracks on the block, 0 to 1 of the way to breaking, shown to everyone who sees it, like minecraft's
// destroyBlockProgress: a drill getting through it. undefined takes them off, and so does the block changing
set_break_progress(x: number, y: number, z: number, progress: number | undefined): void;
get_state(x: number, y: number, z: number, name: string): number | undefined;
set_state(x: number, y: number, z: number, name: string, value: number): boolean;
// whether the block's side is a full square things can attach to, like minecraft's isFaceSturdy: its collision
@@ -254,11 +258,27 @@ export interface ServerWorld {
get_block_item(x: number, y: number, z: number): ItemStack | undefined;
// pops an item out of the block at x y z, like a broken block's drops
drop_item(x: number, y: number, z: number, item: ItemStack): void;
// an item lying still at x y z, which players can pick up after half a second, like a new minecraft ItemEntity
spawn_item(x: number, y: number, z: number, item: ItemStack): EntityRef;
// the entities whose boxes touch the box between two corners, like minecraft's getEntitiesOfClass. only ones of type
// when given, like "bworld:item" for items lying around. players aren't among them, they're in ctx.players
entities_in(from: Position, to: Position, type?: Id): EntityRef[];
// uses item on face of the block at x y z the way a player holding it would, like a deployer: the block's
// on_interact, or the item's on_use, or placing it. a player that isn't anyone does it, standing in the block in
// front of that face and looking at it, like neoforge's FakePlayer. returns what it's holding after (what's left of
// item, or what it turned into) and anything else it was given, like a bucket filled from a stack of them
use_item_on(item: ItemStack, x: number, y: number, z: number, face: Face): {
item: ItemStack | undefined;
given: ItemStack[];
};
// an entity from entities/ with its feet at x y z. throws for kinds that don't exist
spawn_entity(id: Id, x: number, y: number, z: number): EntityRef;
// the first block on the line from one point to another, like minecraft's Level.clip. it hits what entities bump
// into, so it goes through plants and other blocks without collision. undefined when nothing is in the way
raycast(from: Position, to: Position, options?: RaycastOptions): RaycastHit | undefined;
// which way the liquid at x y z flows, a vector 1 long, or 0 0 0 where it's still or there's none, like minecraft's
// FluidState.getFlow: toward lower cells of it beside it, and down where it falls. for what it pushes, like a wheel
liquid_flow(x: number, y: number, z: number): Position;
is_loaded(x: number, z: number): boolean;
// the column's biome, like "bworld:plains", and that biome's season. in chunks that aren't loaded it's what
// generation makes there, which takes a while the first time
@@ -274,6 +294,14 @@ export interface ServerWorld {
set_time(time: number): void;
}
// how a machine breaks a block, see ServerWorld.break_block
export interface BreakOptions {
// it drops what it would for the right tool, though nothing holds one, like create's drills and saws
harvest?: boolean;
// the drops are only returned, not dropped, for a machine to put somewhere, like a saw onto a belt
collect?: boolean;
}
export interface RaycastOptions {
// like minecraft's ClipContext.Fluid: "none" (the default) goes through liquids, "source_only" hits their sources
// and goes through where they flow, "any" hits all of them. a liquid is hit at its surface
@@ -301,8 +329,17 @@ export interface EntityRef {
readonly biome: Id;
readonly season: Season;
readonly removed: boolean;
// how fast it's moving, in blocks a second
readonly velocity: Readonly<Position>;
teleport(x: number, y: number, z: number): void;
// adds to how fast it's moving, in blocks a second, like minecraft's Entity.push: a fan blowing it along
push(x: number, y: number, z: number): void;
remove(): void;
// what an item lying around is, a copy. undefined for every other kind of entity
get_item(): ItemStack | undefined;
// changes what an item lying around is, like ItemEntity.setItem. undefined or a count of 0 removes it. throws for
// every other kind of entity
set_item(item: ItemStack | undefined): void;
}
export interface Player {
@@ -357,6 +394,9 @@ export interface Player {
is_swimming(): boolean;
// holding the sneak key, flying or not, like minecraft's isShiftKeyDown. their client says when
is_sneaking(): boolean;
// adds to how fast they're moving, in blocks a second, like minecraft's Entity.push: a fan blowing them along. their
// client moves them, so it's sent there and added to theirs
push(x: number, y: number, z: number): void;
// keeps using the held item while they hold use, like minecraft's startUsingItem: its components' on_use_tick runs
// every tick, then on_finish_using after ticks, or on_release_using if they let go first. -1 uses it until they let
// go, like a bow. it slows them down like sneaking. for an on_use to call. switching away from it stops it
@@ -435,6 +475,9 @@ export interface ItemApi {
export interface BlockApi {
exists(id: Id): boolean;
// how long breaking it by hand takes without a tool, in seconds, from the block's mining. Infinity for one that
// can't be broken. for machines that break blocks, like a drill
toughness(id: Id): number;
// the components its json lists and their params, a copy, like ItemApi.components
components(id: Id): Record<Id, unknown>;
}