// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the kinds of entity mods define in entities/.json (see EntityJson): animals that wander around, panic when hurt // and look at players now and then, like minecraft's simpler animals, monsters that go after players, scripted ones // that stay where scripts put them, and objects that lie around holding an item import { type ItemData, ItemStack } from "@/common/inventory.ts"; import { entity_category, type EntityJson } from "@/common/mod_data.ts"; import type { EntityData } from "@/common/protocol.ts"; import { ATTRIBUTES, type AttributeType } from "./attributes.ts"; import { Entity, type SavedEntity } from "./entity.ts"; import { move_like_item } from "./item_entity.ts"; import type { EntityFactory } from "./entity_types.ts"; import { FloatGoal, HurtByTargetGoal, LookAtPlayerGoal, MeleeAttackGoal, NearestAttackablePlayerGoal, PanicGoal, RandomLookAroundGoal, RandomStrollGoal, } from "./goals.ts"; import { Mob, type MobCategory } from "./mob.ts"; // the entity's class. the attributes are read while LivingEntity is being built, before any field of a subclass is // set, so each kind gets its own class that has its json already. behaviors are mods' (see ctx.entities), looked up // when a mob first thinks, after every script has registered its own export function mod_entity_factory( json: EntityJson, behaviors: ReadonlyMap void>, ): EntityFactory { if (json.behavior === "scripted") { return (level) => new ScriptedEntity(level, json); } if (json.behavior === "object") { return (level) => new ObjectEntity(level, json); } const monster = json.behavior === "monster"; const behavior = json.behavior?.includes(":") ? json.behavior : undefined; const category = entity_category(json); class ModEntity extends Mob { readonly type = json.id; override width = json.width; override height = json.height; override eye_height = json.height * 0.85; override jumps = json.jumps ?? true; override max_fall = json.max_fall ?? 3; override get category(): MobCategory { return category; } override default_attributes(): [AttributeType, number?][] { return [ ...super.default_attributes(), [ATTRIBUTES.max_health, json.max_health ?? 10], [ATTRIBUTES.movement_speed, json.movement_speed ?? 0.25], [ATTRIBUTES.physical_shielding, json.physical_shielding ?? 0], [ATTRIBUTES.arcane_shielding, json.arcane_shielding ?? 0], [ATTRIBUTES.attack_damage, json.attack_damage ?? 2], ]; } override register_goals() { if (behavior) { behaviors.get(behavior)!(this); return; } if (monster) { // like undergarden's rotspawn and minecraft's zombies this.goals.add_goal(0, new FloatGoal(this)); this.goals.add_goal(1, new MeleeAttackGoal(this, 1.25)); this.goals.add_goal(2, new RandomStrollGoal(this, 0.6)); this.goals.add_goal(3, new RandomLookAroundGoal(this)); this.target_goals.add_goal(0, new HurtByTargetGoal(this)); this.target_goals.add_goal(1, new NearestAttackablePlayerGoal(this)); return; } this.goals.add_goal(0, new FloatGoal(this)); this.goals.add_goal(1, new PanicGoal(this, 1.4)); this.goals.add_goal(5, new RandomStrollGoal(this, 1)); this.goals.add_goal(6, new LookAtPlayerGoal(this, 6)); this.goals.add_goal(7, new RandomLookAroundGoal(this)); } // creatures stay, like minecraft's farm animals. monsters go away when no player is near override remove_when_far_away() { return category === "monster"; } } return (level) => new ModEntity(level); } // it doesn't fall, think or get hurt, the script that spawned it moves it and takes it away, like the fishing hook. // it isn't saved with the world: after a restart that script wouldn't know about it anymore class ScriptedEntity extends Entity { readonly type: string; override no_gravity = true; constructor(level: Entity["level"], json: EntityJson) { super(level); this.type = json.id; this.width = json.width; this.height = json.height; this.eye_height = json.height * 0.85; } override tick() { this.base_tick(); } override get persistent() { return false; } } // a thing lying around, holding an item, like neoforge's custom item entities (see ItemRegistry.entity): it falls, // slides and floats like a dropped item, but nobody picks it up by walking into it, it never despawns or merges, and it // can't be hurt. what happens when it's clicked is up to mods export class ObjectEntity extends Entity { readonly type: string; // it falls in move_like_item instead override gravity = 0; item: ItemStack | undefined; constructor(level: Entity["level"], json: EntityJson) { super(level); this.type = json.id; this.width = json.width; this.height = json.height; this.eye_height = json.height * 0.85; } override tick() { this.base_tick(); if (this.removed || this.vehicle) return; move_like_item(this); } // it isn't hurt by falling either override update_fall_distance() {} override synced_data(): EntityData { const data = super.synced_data(); if (this.item) data.item = this.item.to_synced_data(); return data; } override save_additional(saved: SavedObjectEntity) { super.save_additional(saved); if (this.item) saved.item = this.item.to_data(); } override load_additional(saved: SavedObjectEntity) { super.load_additional(saved); this.item = saved.item ? ItemStack.from_data(saved.item) : undefined; } } interface SavedObjectEntity extends SavedEntity { item?: ItemData; }