Files
bworld/docs/modding/misc/engine.md
T
2026-10-11 01:15:14 -03:00

5.2 KiB

Engine Access

For what the Game API doesn't cover yet, like a new kind of rendering, different player physics or a new entity type, a mod can reach into the engine itself and hook its methods, like NeoForge's mixins. It isn't a stable API: anything in the engine can change in any version, so a mod using it is tied to one minor version of the game.

Prefer the Game API. Many mods hooking the same thing is a sign it should become part of it.

Asking for It

{ "engine_access": true, "game_version": "~0.1.0" }

game_version has to be one minor version: ~0.1.0 (0.1.0 and later 0.1 patches) or an exact 0.1.2. Loading the mod on another version is a load error, so the server doesn't start with it.

Without engine_access, importing bworld/engine is an error in check-mods, when a .bmod loads, and when the script runs. When players join from another site, the confirm screen marks mods that change the engine.

Hooking

// server.ts: broken blocks drop twice
import { engine, hook } from "bworld/engine";

export function setup() {
	hook(engine.server.GameServer.prototype, "pop_item", {
		after(game, [x, y, z, stack], entity) {
			game.spawn_item(entity.x, entity.y, entity.z, stack.clone());
			return entity;
		},
	});
}

hook(target, method, handlers) changes target[method], usually on a class's prototype, so every instance already made changes too. It returns a function that unhooks it.

Handler Runs
before(self, args) First. Change args in place, or return { result } to skip the method and every other hook after it.
replace(self, args, original) Instead of the method. Call original(...args) to run it, or don't.
after(self, args, result) Last. Returns the result the caller gets. Returning undefined keeps the result it was given.
priority Lower runs earlier, the default is 1000. Ties go in load order.
  • Several mods can hook one method. before hooks run in priority order, replace hooks nest with the earliest outermost, and after hooks chain results.
  • A hook that throws is logged under its mod ([my_mod] an after hook on GameServer.pop_item threw: ...) and skipped. A replace hook that throws after calling original gives what original returned, so the method doesn't run twice.
  • hook only works while the mod's script loads or runs setup. Unhooking works any time.
  • Unhooking a method's last hook puts the original back, so a method nobody hooks costs nothing.

What's in engine

Side Where Like
common every script EverythingRegistry, ItemStack, Container, BlockRules, OverworldTerrain, physics
server server scripts GameServer, ServerWorld, ServerPlayer, Entity, Mob, ModRuntime, the mob goals
client client scripts Client, LocalPlayer, ClientLevel, GameRenderer, Camera, GuiScreen

The full lists are in common/engine_modules.ts, server/game/engine_modules.ts and client/engine_modules.ts. The types are the engine's own source, so check-mods typechecks against exactly the engine the mod runs on. bworld/engine also exports game_version, the version of the game that's running.

  • Module exports can't be reassigned, so functions mods may want to change are on plain objects the engine calls through: engine.common.physics.move_body is how every entity moves, on the server and on clients.
  • #private members can't be hooked. They're kept for things with rules that must hold, like a container's slots.
  • Hooks reach one JavaScript realm. Chunk workers run their own copy of the engine, so a client hook doesn't change meshing. The server generates chunks in worldgen workers, and each loads the worldgen scripts into its own copy, so hooks on the terrain (engine.common.OverworldTerrain) go in the worldgen script. A server script's hooks there only reach the few chunks the game generates itself, so the world would come out different in places.

Performance

Hooking is cheap to set up: hook in setup, before the game is running. Each call to a hooked method then goes through a wrapper that runs its hooks, which is fine for something that happens now and then, like a block breaking. On something called many times a tick, like physics.move_body or a method of every entity, the wrapper's cost adds up, and the engine can no longer inline the method into its callers.

Security

A server mod with engine access controls the game worker completely, but the worker still has no Deno permissions. A client mod with it can change anything in the game page, which every client script can already reach. See Security.