Move effects into the modding api
This commit is contained in:
@@ -11,10 +11,12 @@ import type {
|
||||
CommandRegistry,
|
||||
CommandSource,
|
||||
Container,
|
||||
EffectComponent,
|
||||
EntityRef,
|
||||
EventSignal,
|
||||
ItemComponent,
|
||||
ItemStack,
|
||||
LivingRef,
|
||||
MenuBuilder,
|
||||
MenuHandle,
|
||||
MenuType,
|
||||
@@ -45,7 +47,9 @@ import { AIR_ID } from "$/common/protocol.ts";
|
||||
import type { GameServer } from "./game_server.ts";
|
||||
import type { ServerPlayer } from "./player.ts";
|
||||
import { Menu, type MenuSlot } from "./menu.ts";
|
||||
import type { Entity } from "./entity/entity.ts";
|
||||
import { damage_source, type Entity } from "./entity/entity.ts";
|
||||
import type { LivingEntity } from "./entity/living_entity.ts";
|
||||
import type { EffectJson } from "$/common/mod_data.ts";
|
||||
import { create_entity } from "./entity/entity_types.ts";
|
||||
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
|
||||
import { effect_type, EffectInstance, type EffectType } from "./entity/effects.ts";
|
||||
@@ -108,6 +112,7 @@ interface Timer {
|
||||
export class ModRuntime {
|
||||
block_components = new Map<string, { mod: string; component: BlockComponent }>();
|
||||
item_components = new Map<string, { mod: string; component: ItemComponent }>();
|
||||
effect_components = new Map<string, { mod: string; component: EffectComponent }>();
|
||||
// every command, the engine's and mods', see common/commands.ts
|
||||
commands = new CommandDispatcher<ServerCommandSource>();
|
||||
// who registered each command name: "engine" or a mod's id
|
||||
@@ -218,10 +223,66 @@ export class ModRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
// after every setup ran: registration closes and every component blocks and items use must exist
|
||||
// every effect mods define, from their json. they're made before scripts run, since the world loads first and
|
||||
// its mobs can have effects, so their components are looked up as they tick
|
||||
effect_types(): EffectType[] {
|
||||
return EverythingRegistry.entries<EffectJson>("effects").map(([id, json]) => {
|
||||
const modifiers = (json.modifiers ?? []).map(({ attribute, amount, operation }) => {
|
||||
const type = attribute_type(attribute);
|
||||
if (!type) {
|
||||
throw new ModLoadError(
|
||||
id.split(":")[0],
|
||||
`effect ${id} changes ${attribute}, which isn't an attribute`,
|
||||
);
|
||||
}
|
||||
return { attribute: type, amount, operation };
|
||||
});
|
||||
const components = Object.entries(json.components ?? {});
|
||||
const on_tick = (entity: LivingEntity, ticks: number | undefined, amplifier: number) => {
|
||||
let keep = true;
|
||||
for (const [component_id, params] of components) {
|
||||
const { mod, component } = this.effect_components.get(component_id)!;
|
||||
const due = ticks === undefined || !component.should_tick ||
|
||||
run_guarded(
|
||||
mod,
|
||||
`${component_id} should_tick`,
|
||||
() => component.should_tick!(ticks, amplifier, params),
|
||||
);
|
||||
if (!due || !component.on_tick) continue;
|
||||
const result = run_guarded(
|
||||
mod,
|
||||
`${component_id} on_tick`,
|
||||
() => component.on_tick!(living_ref(entity), amplifier, params),
|
||||
);
|
||||
if (result === false) keep = false;
|
||||
}
|
||||
return keep;
|
||||
};
|
||||
return {
|
||||
id,
|
||||
category: json.category ?? "neutral",
|
||||
instant: json.instant === true,
|
||||
modifiers,
|
||||
on_tick: components.length > 0 ? on_tick : undefined,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// after every setup ran: registration closes and every component blocks, items and effects use must exist
|
||||
finish_setup() {
|
||||
this.#setting_up = false;
|
||||
|
||||
for (const [id, effect] of EverythingRegistry.entries<EffectJson>("effects")) {
|
||||
for (const component of Object.keys(effect.components ?? {})) {
|
||||
if (!this.effect_components.has(component)) {
|
||||
throw new ModLoadError(
|
||||
id.split(":")[0],
|
||||
`effect ${id} uses component ${component}, which nothing registered`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const [id, block] of EverythingRegistry.entries<{ components?: Record<string, unknown> }>("blocks")) {
|
||||
for (const component of Object.keys(block.components ?? {})) {
|
||||
if (!this.block_components.has(component)) {
|
||||
@@ -416,6 +477,14 @@ export class ModRuntime {
|
||||
}
|
||||
this.item_components.set(id, { mod, component: component as ItemComponent });
|
||||
},
|
||||
register_effect: (id, component) => {
|
||||
setup_only("components");
|
||||
own(id, "component");
|
||||
if (this.effect_components.has(id)) {
|
||||
throw new ModLoadError(mod, `component ${id} is registered twice`);
|
||||
}
|
||||
this.effect_components.set(id, { mod, component: component as EffectComponent });
|
||||
},
|
||||
},
|
||||
commands: {
|
||||
register: (name_or_node: string | CommandNode<CommandSource>, command?: Command) => {
|
||||
@@ -998,10 +1067,10 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
player.add_effect(new EffectInstance(type, duration, { ...options, amplifier }));
|
||||
},
|
||||
effect(id) {
|
||||
const effect = player.get_effect(known_effect(id));
|
||||
const effect = player.get_effect(known_effect(id).id);
|
||||
return effect && { amplifier: effect.amplifier, duration: effect.duration };
|
||||
},
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id)),
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id).id),
|
||||
is_swimming: () => player.swimming,
|
||||
};
|
||||
}
|
||||
@@ -1018,6 +1087,26 @@ function known_attribute(id: string): AttributeType {
|
||||
return type;
|
||||
}
|
||||
|
||||
function living_ref(entity: LivingEntity): LivingRef {
|
||||
return {
|
||||
id: entity.id,
|
||||
get health() {
|
||||
return entity.health;
|
||||
},
|
||||
get max_health() {
|
||||
return entity.max_health;
|
||||
},
|
||||
damage(amount, cause = "script") {
|
||||
if (typeof amount !== "number" || !Number.isFinite(amount)) throw new Error(`damage must be a number`);
|
||||
entity.hurt(amount, damage_source(String(cause)));
|
||||
},
|
||||
heal(amount) {
|
||||
if (typeof amount !== "number" || !Number.isFinite(amount)) throw new Error(`heal must be a number`);
|
||||
entity.heal(amount);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function entity_ref(entity: Entity): EntityRef {
|
||||
return {
|
||||
id: entity.id,
|
||||
|
||||
Reference in New Issue
Block a user