Move effects into the modding api

This commit is contained in:
2026-10-02 11:53:44 -03:00
parent 4309c700d2
commit e5dcb97643
33 changed files with 458 additions and 109 deletions
+6 -2
View File
@@ -5,6 +5,7 @@ import type { ModelJson } from "./block_models.ts";
import { MAX_TEXTURE_SIZE } from "./constants.ts";
import {
type BlockJson,
type EffectJson,
type EntityJson,
type EntityModelJson,
ID_PATTERN,
@@ -15,6 +16,7 @@ import {
type SpawnJson,
type TagJson,
validate_block,
validate_effect,
validate_entity,
validate_entity_model,
validate_item,
@@ -57,7 +59,7 @@ export class BmodError extends Error {
// the zip's layout:
// manifest.json the mod's manifest, with "scripts" and "credits" pointing into the zip
// data.json every block, model, item, recipe, ore, entity, entity model and tag
// data.json every block, model, item, recipe, ore, entity, entity model, effect and tag
// scripts/<side>.js
// textures/<name>.png the texture <mod id>:<name>
// credits.md
@@ -149,10 +151,12 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
validate_entity_model,
problems,
) as EntityModelJson[],
effects: checked(raw_data.effects, "effects", validate_effect, problems) as EffectJson[],
tags: checked(raw_data.tags, "tags", validate_tag, problems) as TagJson[],
};
// a mod only registers ids in its own namespace
for (const { id } of [...data.blocks, ...data.models, ...data.items, ...data.entities, ...data.entity_models]) {
const registered = [...data.blocks, ...data.models, ...data.items, ...data.entities, ...data.entity_models];
for (const { id } of [...registered, ...data.effects]) {
if (typeof id === "string" && id.split(":")[0] !== mod) problems.push(`${id} isn't in this mod's namespace`);
}
+22 -1
View File
@@ -58,9 +58,30 @@ export interface ItemComponent<P = any> {
on_use?(item: ItemStack, params: P, player: Player): void;
}
// P is the component's params from the effect json. see docs/modding/entities/effects.md
// deno-lint-ignore no-explicit-any
export interface EffectComponent<P = any> {
// whether on_tick runs this tick, like neoforge's shouldApplyEffectTickThisTick. ticks is what's left of the
// effect, or how long it's been on for ones that last forever. every tick when not set
should_tick?(ticks: number, amplifier: number, params: P): boolean;
// like neoforge's applyEffectTick. returning false takes the effect off. instant effects run it once, when added
on_tick?(entity: LivingRef, amplifier: number, params: P): boolean | void;
}
// a player or a mob, what effects work on
export interface LivingRef {
readonly id: string;
readonly health: number;
readonly max_health: number;
// cause is for events and the death message, like "magic"
damage(amount: number, cause?: string): void;
heal(amount: number): void;
}
export interface ComponentRegistry {
register_block<P>(id: Id, component: BlockComponent<P>): void;
register_item<P>(id: Id, component: ItemComponent<P>): void;
register_effect<P>(id: Id, component: EffectComponent<P>): void;
}
// commands, brigadier's way like neoforge's: see common/commands.ts and docs/modding/misc/commands.md
@@ -241,7 +262,7 @@ export interface Player {
add_modifier(attribute: Id, modifier: AttributeModifier): void;
// whether it had it
remove_modifier(attribute: Id, id: string): boolean;
// status effects, like minecraft's: "bworld:regeneration", "bworld:speed"... duration in ticks, -1 for forever.
// status effects, any mod's, like the base game's "bworld:regeneration". duration in ticks, -1 for forever.
// adding one they have merges them like minecraft: the stronger wins, and a weaker longer one waits under it
add_effect(id: Id, duration: number, options?: EffectOptions): void;
// the one they have of that type, undefined when they don't
+9
View File
@@ -3,6 +3,7 @@
import {
type BlockJson,
choice_list,
type EffectJson,
type EntityJson,
type EntityModelJson,
type ItemJson,
@@ -37,6 +38,7 @@ export interface LoadedMod {
spawns: { file: string; json: SpawnJson }[];
entities: { file: string; json: EntityJson }[];
entity_models: { file: string; json: EntityModelJson }[];
effects: { file: string; json: EffectJson }[];
tags: { file: string; json: TagJson }[];
textures: string[];
// absolute paths by texture id
@@ -79,6 +81,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
const entity_jsons = new Map<string, EntityJson>();
const entity_model_owners = new Map<string, string[]>();
const entity_model_jsons = new Map<string, EntityModelJson>();
const effect_owners = new Map<string, string[]>();
const tag_owners = new Map<TagRegistry, Set<string>>();
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
@@ -99,6 +102,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
add(entity_model_owners, json.id, mod.id);
entity_model_jsons.set(json.id, json);
}
for (const { json } of mod.effects) add(effect_owners, json.id, mod.id);
for (const { json } of mod.tags) {
if (!tag_owners.has(json.registry)) tag_owners.set(json.registry, new Set());
tag_owners.get(json.registry)!.add(json.id);
@@ -233,6 +237,11 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
if (json.model !== undefined) uses(file, json.model, "entity model");
uses(file, json.texture, "texture");
}
for (const { file, json } of mod.effects) {
if ((effect_owners.get(json.id)?.length ?? 0) > 1) {
errors.push(`${file}: effect ${json.id} is also defined by ${effect_owners.get(json.id)!.join(", ")}`);
}
}
for (const { file, json } of mod.spawns) {
check_namespace(file, json.entity);
const entity = entity_jsons.get(json.entity);
+51
View File
@@ -132,6 +132,22 @@ export interface EntityJson {
arcane_shielding?: number;
}
// a status effect, like neoforge's MobEffect. what it does over time comes from components scripts register, see
// docs/modding/entities/effects.md
export interface EffectJson {
id: string;
// like minecraft's MobEffectCategory, "neutral" when not set
category?: EffectCategory;
// happens once when added instead of lasting, like instant health. its components' on_tick runs once
instant?: boolean;
// attribute modifiers while it's on, for level 1. each level adds that much again, like minecraft's speed
modifiers?: { attribute: string; amount: number; operation: EffectModifierOperation }[];
components?: Record<string, unknown>;
}
export type EffectCategory = "beneficial" | "harmful" | "neutral";
export type EffectModifierOperation = "add_value" | "add_multiplied_base" | "add_multiplied_total";
type Vec3 = [number, number, number];
// a bedrock entity model (models/<name>.geo.json) with only what the engine uses. positions and sizes are in
@@ -786,6 +802,41 @@ export function validate_entity(json: unknown): Problems {
return problems;
}
export function validate_effect(json: unknown): Problems {
if (!is_object(json)) return ["effect must be an object"];
const problems = validate_id(json.id, "id");
if (json.category !== undefined && !["beneficial", "harmful", "neutral"].includes(json.category as string)) {
problems.push('category must be "beneficial", "harmful" or "neutral"');
}
if (json.instant !== undefined && typeof json.instant !== "boolean") problems.push("instant must be true or false");
if (json.modifiers !== undefined) {
if (!Array.isArray(json.modifiers)) {
problems.push("modifiers must be a list");
} else {
json.modifiers.forEach((modifier, i) => {
const field = `modifiers[${i}]`;
if (!is_object(modifier)) {
problems.push(`${field} must be { attribute, amount, operation }`);
return;
}
problems.push(...validate_id(modifier.attribute, `${field}.attribute`));
if (typeof modifier.amount !== "number" || !Number.isFinite(modifier.amount)) {
problems.push(`${field}.amount must be a number`);
}
if (
!["add_value", "add_multiplied_base", "add_multiplied_total"].includes(modifier.operation as string)
) {
problems.push(
`${field}.operation must be "add_value", "add_multiplied_base" or "add_multiplied_total"`,
);
}
});
}
}
if (json.components !== undefined && !is_object(json.components)) problems.push("components must be an object");
return problems;
}
export function validate_entity_model(json: unknown): Problems {
if (!is_object(json)) return ["entity model must be an object"];
const problems = validate_id(json.id, "id");
+7 -1
View File
@@ -4,6 +4,7 @@ import {
block_from_json,
BlockJson,
choice_ids,
EffectJson,
EntityJson,
EntityModelJson,
grid_recipe_from_json,
@@ -34,6 +35,8 @@ export interface ModData {
spawns: SpawnJson[];
entities: EntityJson[];
entity_models: EntityModelJson[];
// status effects, see EffectJson
effects: EffectJson[];
// tag files, see common/tags.ts
tags: TagJson[];
}
@@ -75,7 +78,7 @@ export function is_registered(registry: TagRegistry, id: string): boolean {
return (registry === "blocks" && id === "bworld:air") || EverythingRegistry.get(registry, id) !== undefined;
}
// registers every mod's blocks, items, entities, tags and recipes, in the order given (dependencies first). tags load
// registers every mod's blocks, items, entities, effects, tags and recipes, in the order given (dependencies first). tags load
// once every mod's content has, since a tag can hold anything, and recipes after them, since they can use tags
export function register_mod_data(mods: { id: string; data: ModData }[]): RecipeBook {
const recipes = new RecipeBook();
@@ -101,6 +104,9 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
for (const json of data.entities) {
EverythingRegistry.register<EntityJson>("entities", json.id, json);
}
for (const json of data.effects) {
EverythingRegistry.register<EffectJson>("effects", json.id, json);
}
} catch (e) {
throw new ModLoadError(id, (e as Error).message);
}