Move effects out of cubi
This commit is contained in:
@@ -80,7 +80,7 @@ import type { GameServer } from "./game_server.ts";
|
||||
import { MAX_PRIORITY, MIN_PRIORITY } from "./scheduled_ticks.ts";
|
||||
import type { ServerPlayer } from "./player.ts";
|
||||
import { Menu, type MenuFluidSlot, type MenuSlot } from "./menu.ts";
|
||||
import { damage_source, type Entity } from "./entity/entity.ts";
|
||||
import { damage_source, type DamageSource, 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";
|
||||
@@ -184,7 +184,7 @@ export class ModRuntime {
|
||||
block_place: new Signal(),
|
||||
block_interact: new Signal(),
|
||||
chat_send: new Signal(),
|
||||
player_damage: new Signal(),
|
||||
entity_damage: new Signal(),
|
||||
};
|
||||
after: After = {
|
||||
block_break: new Signal(),
|
||||
@@ -193,7 +193,7 @@ export class ModRuntime {
|
||||
chat_send: new Signal(),
|
||||
player_join: new Signal(),
|
||||
player_leave: new Signal(),
|
||||
player_damage: new Signal(),
|
||||
entity_damage: new Signal(),
|
||||
player_death: new Signal(),
|
||||
server_start: new Signal(),
|
||||
tick: new Signal(),
|
||||
@@ -624,6 +624,32 @@ export class ModRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
// everything that hurts a player or a mob, see LivingEntity.hurt. what's left of amount once mods had their say, 0
|
||||
// when one canceled it
|
||||
before_damage(entity: LivingEntity, player: ServerPlayer | undefined, amount: number, source: DamageSource) {
|
||||
if (this.before.entity_damage.empty) return amount;
|
||||
const event = { ...this.#damage_event(entity, player, amount, source), cancel: false };
|
||||
this.before.entity_damage.emit(event);
|
||||
const final = Number(event.amount);
|
||||
return event.cancel || !(final > 0) ? 0 : final;
|
||||
}
|
||||
|
||||
after_damage(entity: LivingEntity, player: ServerPlayer | undefined, amount: number, source: DamageSource) {
|
||||
if (!this.after.entity_damage.empty) {
|
||||
this.after.entity_damage.emit(this.#damage_event(entity, player, amount, source));
|
||||
}
|
||||
}
|
||||
|
||||
#damage_event(entity: LivingEntity, player: ServerPlayer | undefined, amount: number, source: DamageSource) {
|
||||
return {
|
||||
entity: living_ref(entity),
|
||||
player: player && this.player(player),
|
||||
amount,
|
||||
cause: source.cause,
|
||||
bypasses_invulnerability: source.bypasses_invulnerability === true,
|
||||
};
|
||||
}
|
||||
|
||||
player(player: ServerPlayer): Player {
|
||||
let api = this.#players.get(player);
|
||||
if (!api) {
|
||||
@@ -1575,10 +1601,7 @@ 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).id);
|
||||
return effect && { amplifier: effect.amplifier, duration: effect.duration };
|
||||
},
|
||||
effect: (id) => effect_of(player, id),
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id).id),
|
||||
is_swimming: () => player.swimming,
|
||||
is_sneaking: () => player.sneaking,
|
||||
@@ -1611,7 +1634,12 @@ function known_attribute(id: string): AttributeType {
|
||||
return type;
|
||||
}
|
||||
|
||||
function living_ref(entity: LivingEntity): LivingRef {
|
||||
function effect_of(entity: LivingEntity, id: string) {
|
||||
const effect = entity.get_effect(known_effect(id).id);
|
||||
return effect && { amplifier: effect.amplifier, duration: effect.duration };
|
||||
}
|
||||
|
||||
export function living_ref(entity: LivingEntity): LivingRef {
|
||||
return {
|
||||
id: entity.id,
|
||||
get health() {
|
||||
@@ -1620,6 +1648,17 @@ function living_ref(entity: LivingEntity): LivingRef {
|
||||
get max_health() {
|
||||
return entity.max_health;
|
||||
},
|
||||
get air() {
|
||||
return entity.air;
|
||||
},
|
||||
set air(value) {
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) throw new Error(`air must be a number`);
|
||||
entity.air = Math.min(value, entity.max_air);
|
||||
},
|
||||
get max_air() {
|
||||
return entity.max_air;
|
||||
},
|
||||
effect: (id) => effect_of(entity, id),
|
||||
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)));
|
||||
|
||||
Reference in New Issue
Block a user