This commit is contained in:
2026-11-22 13:21:27 -03:00
parent e751d22c6e
commit ac791b886e
23 changed files with 494 additions and 91 deletions
+56 -3
View File
@@ -2,11 +2,14 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// the kinds of entity mods define in entities/<name>.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, and scripted
// ones that stay where scripts put them
// 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 } from "./entity.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,
@@ -30,6 +33,9 @@ export function mod_entity_factory(
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;
@@ -110,3 +116,50 @@ class ScriptedEntity extends Entity {
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;
}