Files
bworld/move_to_mods.md
T
2026-11-05 22:37:41 -03:00

12 KiB

Moving base game rules out of the engine

Three places where cubi still names bworld content, and what the modding API needs so mods/bworld can do them itself. Each part is independent. The suggested order is effects, then tooltips, then dimensions, from smallest to largest.

1. Effects: resistance and water breathing

Today

  • server/game/entity/living_entity.ts hardcodes bworld:resistance and bworld:water_breathing.
  • actually_hurt cuts damage by 20% per resistance level, unless the source is absolute or bypasses invulnerability. It does this before shielding.
  • base_tick skips losing air when the entity has water breathing.
  • Both effect JSONs in mods/bworld/effects/ have no components. The docs say "the engine checks for them".

How VoxeLibre does it

ports/voxelibre/mods/ITEMS/mcl_potions/functions.lua has no hook made for either effect:

  • Water breathing is a plain on_step that sets the entity's breath back to full while the effect is on.
  • Resistance is a modifier_func run by mcl_damage.register_modifier (ports/voxelibre/mods/CORE/mcl_damage/init.lua). That's a general list of damage modifiers every hit goes through, and it isn't specific to effects. The effect's modifier only checks whether the entity has the effect, and skips damage flagged bypasses_magic.

So effects need nothing new. What's missing is general API: scripts can't see or change an entity's air, mobs' damage goes through no event, and LivingRef can't tell which effects an entity has.

Plan

The API additions are general, and each is useful without effects:

// common/mod_api/server.ts
export interface LivingRef {
	readonly id: string;
	readonly health: number;
	readonly max_health: number;
	// ticks of air left under water, max_air when full. it drowns 20 ticks after reaching 0
	air: number;
	readonly max_air: number;
	// the one it has of that type, like Player.effect
	effect(id: Id): { readonly amplifier: number; readonly duration: number } | undefined;
	damage(amount: number, cause?: string): void;
	heal(amount: number): void;
}

// replaces PlayerDamageEvent: fires for every living entity, players included
export interface EntityDamageEvent {
	readonly entity: LivingRef;
	// set when a player is hurt
	readonly player: Player | undefined;
	// before events can change it
	amount: number;
	readonly cause: string;
	// like falling out of the world. even invulnerable entities are hurt
	readonly bypasses_invulnerability: boolean;
}

Engine:

  • ctx.events.before.entity_damage and ctx.events.after.entity_damage replace player_damage. The game is pre-alpha, so there's no need to keep both.
  • The before event moves from GameServer.hurt_player into LivingEntity.hurt, so mobs go through it too. It fires before the hurt cooldown math, which is where player_damage fires for players now.
  • living_ref gets air (the setter clamps to max_air or less) and effect(id). Player already has effect.
  • LivingEntity drops the RESISTANCE and WATER_BREATHING constants and their checks. base_tick always loses air under water unless can_breathe_underwater is true.

bworld:

  • Water breathing gets a bworld:refill_air component whose on_tick sets entity.air = entity.max_air. Effects tick after base_tick, so the air never gets to 0. This is VoxeLibre's on_step.
  • Resistance subscribes to before.entity_damage in mods/bworld/scripts/effects.ts. When entity.effect("bworld:resistance") is there and the damage doesn't bypass invulnerability, it multiplies amount by 1 - 0.2 * (amplifier + 1), clamped to 0 or more. This is VoxeLibre's damage modifier. Resistance's JSON keeps no components.
  • curios.ts moves from after.player_damage to after.entity_damage and checks player.

Behavior changes to accept:

  • Resistance now applies in the before event, in load order with other mods' handlers. Today it applies after all of them. bworld loads first, so later mods see the reduced amount. VoxeLibre orders its modifiers with a priority. Leave that out until a mod needs it.
  • With water breathing on, the air bar stays full under water instead of not changing. That's what VoxeLibre does.

NeoForge reference: LivingIncomingDamageEvent, the general version of entity_damage (ports/neoforge/src/main/java/net/neoforged/neoforge/event/entity/living/). Air is getAirSupply and setAirSupply.

Docs and tests:

  • In docs/modding/concepts/events.md, replace player_damage with entity_damage in the table, and update docs/modding/entities/players.md, which mentions it.
  • In docs/modding/entities/effects.md, rewrite "The Base Game's Effects", and add an example of an effect that works through an event, like resistance.
  • The water breathing test in tests/entity_test.ts should keep passing unchanged.
  • Add a test that before.entity_damage fires for a mob and can change the amount.
  • Add a test that a script setting air is clamped and keeps a mob from drowning.

2. Tooltip API: the sell value line

Today

  • item_tooltip in client/gui/tooltip.ts always adds a sell value line, using the bworld:copper_coin icon (VALUE_ICON).
  • sell_value and base_sell_value are engine features used by the server API. They stay. Only the line and its icon move.
  • Client scripts can't add lines to item tooltips. g.item_tooltip(id, x, y, extra) only adds lines to one drawn tooltip.
  • The client's ctx.items.sell_value(id) takes no item data, so it can't price a full juice bottle the way the engine's tooltip does.

Plan

// common/mod_api/client.ts
export interface TooltipLine {
	text: string;
	color?: Color;
	// a texture drawn before the text, as tall as the line
	icon?: Id;
	// lower comes first, TOOLTIP_PRIORITY.default when not set. lines with the same priority keep the order they were
	// added in: the game's first, then mods' in load order
	priority?: number;
}

// where the game's own lines go, so a mod's can go between them
export const TOOLTIP_PRIORITY = {
	name: 0,
	fluid: 100,
	food: 200,
	default: 500,
	lore: 1000,
	// g.item_tooltip's extra lines
	extra: 2000,
} as const;

export interface ClientUi {
	// ...
	// adds to every item's tooltip, like neoforge's ItemTooltipEvent. lines has the game's lines and earlier mods'.
	// push to it, and the lines are sorted by priority once every function has run
	add_item_tooltip(fn: (item: ItemStack, lines: TooltipLine[]) => void): void;
}

Engine:

  • item_tooltip stops adding the value line. It gives its own lines their TOOLTIP_PRIORITY, runs the registered functions in load order, each with run_mod, then does a stable sort by priority. Hovered slots and g.item_tooltip both go through item_tooltip, so both get the new lines.
  • g.item_tooltip's extra lines get TOOLTIP_PRIORITY.extra unless they set their own priority, so they stay under everything like now. Lore from the server's get_lore is part of the stack's lore, so it gets lore.
  • g.tooltip and g.item_tooltip take the same TooltipLine, so mods can draw icons there too.
  • Give the shared ctx.items.sell_value an optional data, like the server's sell_value(id, data). Both should call sell_value in common/inventory.ts with the side's fluid rules.
  • Remove VALUE_ICON and VALUE_COLOR.

bworld:

  • mods/bworld/scripts/client.ts calls ctx.ui.add_item_tooltip and pushes { icon: "bworld:copper_coin", text: String(value), color, priority: TOOLTIP_PRIORITY.food + 1 } whenever ctx.items.sell_value(item.id, item.data) is defined. That puts it between the food and the lore, where it is now.

NeoForge reference: ItemTooltipEvent (ports/neoforge/src/main/java/net/neoforged/neoforge/event/entity/player/ItemTooltipEvent.java).

Docs and tests:

  • In docs/modding/gui/index.md, document add_item_tooltip, the TooltipLine icon and priority, and the TOOLTIP_PRIORITY table.
  • In docs/modding/items/index.md, give sell_value its data argument.
  • Add a test where a test mod's client script adds a line, and it shows up in item_tooltip.
  • Add a test that two lines with a priority between the name and the lore end up there, and that two lines with the same priority keep the order they were added in.

The food line ("Restores N hunger") is the same kind of thing and could move the same way later, if hunger is base game content.

Today

  • The generator is hardcoded as GENERATOR = "bworld:overworld" in server/game/load_mods.ts. It's passed to load_worldgen and to worldgen_bundle for the workers.
  • World.#find_spawn in server/game/world.ts checks rings of chunks around 0, 0. It takes the top non-air block of each chunk's middle column, unless that block is bworld:water.
  • tools/worldgen_preview.ts also names bworld:overworld, but it's a tool for bworld, so that's fine.

Plan

A dimension is data, like NeoForge's dimension/ files (a level stem). It points at the noise settings that generate it:

// mods/bworld/worldgen/dimension/overworld.json
{
	"format_version": 1,
	"dimension": {
		"id": "bworld:overworld",
		"generator": "bworld:overworld",
		"default": true
	}
}
  • The world plays the dimension with "default": true. If no loaded mod has one, or more than one does, the server doesn't start, and the error names the mods.
  • start_game reads the generator from the dimension instead of GENERATOR, and passes it to load_worldgen and worldgen_bundle.
  • Mods add worldgen data in four places: mod_data.ts, mod_check.ts (the generator has to exist), check_mods.ts, and the .cubi reader. Dimension files need the same handling.
  • There's one dimension per world for now. The file is where more dimensions would go later, but this plan doesn't add travel between them.

Spawn search, recommended: keep a generic search in the engine, since every game needs a spawn point, but drop the block id. A column counts when its top non-air block has collision. Water has "collision": false, so bworld gets the same spawn without the engine naming anything. Columns topped by a plant without collision get skipped too, which is fine.

If mods need more control later:

  • A "spawn" field on the dimension, such as a block tag the spawn has to be on.
  • A server script hook that picks the spawn, like NeoForge's LevelEvent.CreateSpawnPosition.

Neither is needed to move this out, so neither is in the plan.

Docs and tests:

  • Add a "Dimensions" section to docs/modding/worldgen/index.md or terrain.md, and update the terrain docs that say the base game's overworld is the generator.
  • Add a test where a test mod with its own noise settings and a default dimension is what the world generates.
  • Add tests that zero or two default dimensions are a load error.
  • Add a test that the spawn never lands on a block without collision.
  • The worldgen golden tests shouldn't change, since the generator and the terrain are the same.

Open questions

  1. Spawn: is the collision rule enough, or do you want the spawn search fully in bworld (a script hook) right away?
  2. Dimensions: should the save remember which dimension a world was made with, so changing mods can't swap the generator under an existing world? It isn't needed for pre-alpha.