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
+78 -5
View File
@@ -8,7 +8,8 @@ import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/co
import { block_states, get_state_value, is_waterlogged, liquid_block, set_state_value } from "$/common/utils.ts";
import { LootTables } from "./loot.ts";
import { collision_shape_at, covers_face, raycast_blocks, type Shape } from "$/common/block_shapes.ts";
import { AIR, ID_MASK } from "$/common/constants.ts";
import { AIR, faces, ID_MASK } from "$/common/constants.ts";
import { physics } from "$/common/physics.ts";
import { clock } from "$/common/time.ts";
import { BlockRules } from "$/common/block_rules.ts";
import type { SharedContext } from "$/common/mod_api/shared.ts";
@@ -62,6 +63,7 @@ import { damage_source, type Entity } from "./entity/entity.ts";
import type { LivingEntity } from "./entity/living_entity.ts";
import type { EffectJson } from "$/common/mod_data.ts";
import { create_entity } from "./entity/entity_types.ts";
import { BLOCK_DROP_PICKUP_DELAY, ItemEntity } from "./entity/item_entity.ts";
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
import { effect_type, EffectInstance, type EffectType } from "./entity/effects.ts";
import { biome_season } from "$/common/worldgen/biomes.ts";
@@ -673,10 +675,18 @@ export class ModRuntime {
game().set_block(x, y, z, id);
return true;
},
break_block: (x, y, z) => {
if (game().world.get_block_id(x, y, z) === AIR_ID) return false;
game().destroy_block(x, y, z);
return true;
break_block: (x, y, z, options = {}) => {
if (game().world.get_block_id(x, y, z) === AIR_ID) return undefined;
const how = { harvest: options.harvest === true, collect: options.collect === true };
return game().destroy_block(x, y, z, undefined, how)?.map(item_copy);
},
set_break_progress: (x, y, z, progress) => {
if (progress !== undefined && !Number.isFinite(progress)) {
throw new Error(
`break progress is 0 to 1, or undefined to take the cracks off, not ${progress}`,
);
}
game().set_break_progress(x, y, z, progress);
},
get_state: (x, y, z, name) => {
const id = game().world.get_block_id(x, y, z);
@@ -710,6 +720,28 @@ export class ModRuntime {
get_block_item: (x, y, z) =>
structuredClone(game().world.get_tile(x, y, z)?.client_data?.item as ItemStack),
drop_item: (x, y, z, item) => void game().pop_item(x, y, z, new_stack(item)),
spawn_item: (x, y, z, item) => {
if (!(item.count > 0)) throw new Error("an item lying around needs a count of 1 or more");
return entity_ref(game().spawn_item(x, y, z, new_stack(item), BLOCK_DROP_PICKUP_DELAY));
},
entities_in: (from, to, type) => {
const [x0, x1] = [Math.min(from.x, to.x), Math.max(from.x, to.x)];
const [y0, y1] = [Math.min(from.y, to.y), Math.max(from.y, to.y)];
const [z0, z1] = [Math.min(from.z, to.z), Math.max(from.z, to.z)];
return game().entities.all().filter((entity) => {
const half = entity.width / 2;
return (type === undefined || entity.type === type) &&
entity.x - half < x1 && entity.x + half > x0 && entity.y < y1 &&
entity.y + entity.height > y0 &&
entity.z - half < z1 && entity.z + half > z0;
}).map(entity_ref);
},
use_item_on: (item, x, y, z, face) => {
if (!faces.includes(face)) throw new Error(`a face is one of ${faces.join(", ")}, not ${face}`);
if (!(item.count > 0)) throw new Error("using an item needs a count of 1 or more");
const { held, given } = game().use_item_on(new_stack(item), x, y, z, face);
return { item: held && item_copy(held), given: given.map(item_copy) };
},
spawn_entity: (id, x, y, z) => {
const entity = EverythingRegistry.get("entities", id)
? create_entity(game().entities, id)
@@ -754,6 +786,17 @@ export class ModRuntime {
},
};
},
liquid_flow: (x, y, z) => {
const [fx, fy, fz] = physics.liquid_flow(
Math.floor(x),
Math.floor(y),
Math.floor(z),
(x, y, z) => game().entities.liquid_at(x, y, z),
(x, y, z) => game().world.collision_shape(x, y, z),
);
// without any -0
return { x: fx + 0, y: fy + 0, z: fz + 0 };
},
is_loaded: () => true,
biome_at: (x, z) => game().world.biome_at(Math.floor(x), Math.floor(z)),
season_at: (x, z) =>
@@ -815,6 +858,11 @@ export class ModRuntime {
},
blocks: {
exists: (id) => EverythingRegistry.get("blocks", id) !== undefined,
toughness: (id) => {
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
if (!block) throw new Error(`unknown block ${id}`);
return block.toughness ?? Infinity;
},
components: (id) => {
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
if (!block) throw new Error(`unknown block ${id}`);
@@ -1129,6 +1177,11 @@ export function item_api(stack: EngineItemStack): ItemStack {
};
}
// an engine stack as a script's item, a copy
export function item_copy(stack: EngineItemStack): ItemStack {
return structuredClone(stack.to_data());
}
export function new_stack(item: ItemStack): EngineItemStack {
if (!EverythingRegistry.get("items", item.id)) throw new Error(`unknown item ${item.id}`);
const stack = new EngineItemStack(item.id, item.count);
@@ -1347,6 +1400,10 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
remove_effect: (id) => player.remove_effect(known_effect(id).id),
is_swimming: () => player.swimming,
is_sneaking: () => player.sneaking,
push(x, y, z) {
if (![x, y, z].every(Number.isFinite)) throw new Error("a push is three numbers, in blocks a second");
game.push_player(player, x, y, z);
},
start_using(ticks) {
if (!Number.isInteger(ticks) || (ticks < 1 && ticks !== -1)) {
throw new Error("start_using takes a whole number of ticks, or -1 until they let go");
@@ -1408,11 +1465,27 @@ export function entity_ref(entity: Entity): EntityRef {
get removed() {
return entity.removed;
},
get velocity() {
return { x: entity.vx, y: entity.vy, z: entity.vz };
},
teleport(x, y, z) {
if (!entity.removed) entity.set_position(x, y, z);
},
push(x, y, z) {
if (![x, y, z].every(Number.isFinite)) throw new Error("a push is three numbers, in blocks a second");
entity.vx += x;
entity.vy += y;
entity.vz += z;
},
remove() {
entity.remove();
},
get_item: () => entity instanceof ItemEntity ? item_copy(entity.item) : undefined,
set_item(item) {
if (!(entity instanceof ItemEntity)) throw new Error(`${entity.type} isn't an item lying around`);
if (entity.removed) return;
if (!item || item.count <= 0) entity.remove();
else entity.item = new_stack(item);
},
};
}