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
+3
View File
@@ -287,6 +287,9 @@ export interface ItemRegistry<T = unknown | undefined> {
// a model it's drawn with instead of its texture, with the model's own textures, like a 3d wrench. or an entity
// model (see EntityModelJson), drawn in its texture
model?: string;
// the entity it lies in the world as instead of a cubi:item, holding the stack, like neoforge's hasCustomEntity.
// an "object" (see EntityJson.behavior)
entity?: string;
// what players see, like "Iron Ingot". made from the id when not set, see display_name in common/utils.ts
name?: string;
block_id?: string;
+12 -3
View File
@@ -381,6 +381,13 @@ export interface BlockInteractEvent {
readonly item: ItemStack | undefined;
}
// a right click on an entity, like neoforge's PlayerInteractEvent.EntityInteract
export interface EntityInteractEvent {
readonly player: Player;
readonly entity: EntityRef;
readonly item: ItemStack | undefined;
}
// a player or a mob being hurt, like neoforge's LivingIncomingDamageEvent
export interface EntityDamageEvent {
readonly entity: LivingRef;
@@ -408,6 +415,8 @@ export interface ServerBeforeEvents {
block_break: EventSignal<BlockBreakEvent & Cancelable>;
block_place: EventSignal<BlockPlaceEvent & Cancelable>;
block_interact: EventSignal<BlockInteractEvent & Cancelable>;
// handlers do what the click does, and cancel it so the held item isn't used too
entity_interact: EventSignal<EntityInteractEvent & Cancelable>;
chat_send: EventSignal<ChatSendEvent & Cancelable>;
entity_damage: EventSignal<EntityDamageEvent & Cancelable>;
}
@@ -584,10 +593,10 @@ export interface EntityRef {
// like neoforge's Entity.kill: a player or a mob dies, even when invulnerable, and anything else is removed. cause
// is for events and the death message, like LivingRef.damage's. mods' entity_damage can still cancel it
kill(cause?: string): void;
// what an item lying around is, a copy. undefined for every other kind of entity
// what an item lying around is, or what an object holds, a copy. undefined for every other kind of entity
get_item(): ItemStack | undefined;
// changes what an item lying around is, like ItemEntity.setItem. undefined or a count of 0 removes it. throws for
// every other kind of entity
// changes what an item lying around is, like ItemEntity.setItem, undefined or a count of 0 removing it. or what an
// object holds, undefined or a count of 0 leaving it holding nothing. throws for every other kind of entity
set_item(item: ItemStack | undefined): void;
// a sound following it, once, for every player who sees it and is within its range
play_sound(sound: Id, options?: PlayOptions): void;
+9 -1
View File
@@ -363,6 +363,14 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
for (const texture of Object.values(json.data_textures ?? {})) uses(file, texture, "texture");
// an entity model is drawn in the item's texture
if (json.model && !entity_model_owners.has(json.model)) uses(file, json.model, "model");
if (json.entity) {
check_namespace(file, json.entity);
const behavior = entity_jsons.get(json.entity)?.behavior;
if (!entity_owners.has(json.entity)) errors.push(`${file}: entity ${json.entity} doesn't exist`);
else if (behavior !== "object") {
errors.push(`${file}: entity ${json.entity} has to be an "object" to lie around holding the item`);
}
}
if (json.places) uses(file, json.places, "block");
if (json.food?.returns) uses(file, json.food.returns, "item");
for (const layer of json.layers ?? []) uses(file, layer.texture, "texture");
@@ -405,7 +413,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
errors.push(`${file}: entity ${json.id} is also defined by ${entity_owners.get(json.id)!.join(", ")}`);
}
if (json.model !== undefined) uses(file, json.model, "entity model");
uses(file, json.texture, "texture");
if (json.texture !== undefined) uses(file, json.texture, "texture");
if (json.loot_table) uses(file, json.loot_table, "loot table");
if (json.sounds) uses_sounds(file, json.sounds, ENTITY_SOUNDS);
// scripts register behaviors, the server checks them when it starts
+21 -6
View File
@@ -368,6 +368,8 @@ export interface ItemJson {
data_textures?: Record<string, string>;
// see ItemRegistry.model
model?: string;
// see ItemRegistry.entity
entity?: string;
// tier and speed default to 0 and 2
tool?: Omit<ToolProperties, "tier" | "speed"> & Partial<ToolProperties>;
places?: string;
@@ -390,6 +392,7 @@ const item_schema: z.ZodType<ItemJson> = z.object({
texture: id.optional(),
data_textures: z.record(z.string(), id).optional(),
model: id.optional(),
entity: id.optional(),
tool: z.object({
type: z.string(),
durability: z.int().min(1),
@@ -429,19 +432,24 @@ const item_schema: z.ZodType<ItemJson> = z.object({
components: json_object.optional(),
});
// a kind of entity, drawn with an entity model and one texture, or without a model as its texture facing the camera
const ENTITY_BEHAVIORS = ["animal", "monster", "scripted", "object"];
// a kind of entity, drawn with an entity model and one texture, or without a model as its texture facing the camera.
// an object without a model is drawn as the item it holds
const entity_schema = z.object({
id,
// an entity model, see EntityModelJson
model: id.optional(),
texture: id,
// only an object can go without one
texture: id.optional(),
// "animal" (the default) wanders around, panics when hurt and looks at players. "monster" goes after players it
// sees and whoever hurts it, and despawns far from players, like minecraft's zombies. "scripted" stays where
// scripts put it: no gravity, ai or health, and it isn't saved with the world, like the fishing hook. any other id
// is a behavior a mod's script registers
// scripts put it: no gravity, ai or health, and it isn't saved with the world, like the fishing hook. "object" lies
// around like a dropped item, holding one, without ai or health (see ItemRegistry.entity). any other id is a
// behavior a mod's script registers
behavior: z.custom<string>(
(value) => ["animal", "monster", "scripted"].includes(value as string) || id.safeParse(value).success,
'must be "animal", "monster", "scripted" or a behavior id like "my_mod:thing"',
(value) => ENTITY_BEHAVIORS.includes(value as string) || id.safeParse(value).success,
'must be "animal", "monster", "scripted", "object" or a behavior id like "my_mod:thing"',
).optional(),
// what natural spawning counts it as, see entity_category
category: z.enum(["monster", "creature"]).optional(),
@@ -465,6 +473,11 @@ const entity_schema = z.object({
loot_table: loot_table_id.optional(),
// what it sounds like: <sounds>/hurt, /death, /step and /ambient, see ENTITY_SOUNDS
sounds: sound_prefix.optional(),
// how much bigger it's drawn than its model (or its texture, or its item), 1 when not set. its box stays the same
scale: z.number().positive().optional(),
}).refine((entity) => entity.texture !== undefined || entity.behavior === "object", {
path: ["texture"],
message: "is needed unless it's an object",
});
export type EntityJson = z.infer<typeof entity_schema>;
@@ -899,6 +912,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
const json: ItemJson = { id, texture: item.texture_id };
if (item.data_textures !== undefined) json.data_textures = item.data_textures;
if (item.model !== undefined) json.model = item.model;
if (item.entity !== undefined) json.entity = item.entity;
if (item.tool !== undefined) json.tool = item.tool;
if (item.block_id !== undefined) json.places = item.block_id;
if (item.max_stack !== undefined) json.max_stack = item.max_stack;
@@ -929,6 +943,7 @@ function item_fields(json: ItemJson): Partial<ItemRegistry> {
const item: Partial<ItemRegistry> = {};
if (json.data_textures !== undefined) item.data_textures = json.data_textures;
if (json.model !== undefined) item.model = json.model;
if (json.entity !== undefined) item.entity = json.entity;
if (json.tool !== undefined) item.tool = { tier: 0, speed: 2, ...json.tool };
if (json.places !== undefined) item.block_id = json.places;
if (json.max_stack !== undefined) item.max_stack = json.max_stack;
+3 -1
View File
@@ -12,7 +12,7 @@ export const AIR_ID = "cubi:air";
// bump when a client and server of different versions can't play together.
// the server rejects a different version before the client downloads anything
export const PROTOCOL_VERSION = 30;
export const PROTOCOL_VERSION = 31;
export interface PlayerInfo {
id: string;
@@ -132,6 +132,8 @@ export type ClientMessage =
| { type: "hit_block"; x: number; y: number; z: number }
// left click on an entity, by id
| { type: "attack_entity"; id: string }
// right click on an entity, by id, for mods' entity_interact. it uses the held item when they don't stop it
| { type: "interact_entity"; id: string }
// left click at nothing, a swing at the air. it starts the attack cooldown over, like an attack
| { type: "swing" }
// right click without looking at a block, for items' on_use