diff --git a/docs/modding/entities/effects.md b/docs/modding/entities/effects.md index f09cbd4..6944351 100644 --- a/docs/modding/entities/effects.md +++ b/docs/modding/entities/effects.md @@ -82,6 +82,8 @@ Like block components, an effect that lists a component nobody registered stops ## Using Effects +`/effect [seconds]` gives you one while testing, for 30 seconds unless you say. + Any mod's effects are added to players by id, see [Status Effects](players.md#status-effects): ```ts diff --git a/docs/modding/misc/commands.md b/docs/modding/misc/commands.md index b32e8af..8eb8077 100644 --- a/docs/modding/misc/commands.md +++ b/docs/modding/misc/commands.md @@ -108,6 +108,7 @@ Commands can only be registered during `setup`. | Command | What it does | | ------------------------------------------------------- | -------------------------------------------------------- | | `/give [count]` | Gives you an item | +| `/effect [seconds]` | Gives you a status effect, for 30 seconds by default | | `/summon ` | Spawns an entity where you are | | `/time` | Shows the time | | `/time set ` | Jumps to the next time of day. Time never goes back | diff --git a/server/game/entity/effects.ts b/server/game/entity/effects.ts index 7b297f5..a9ca367 100644 --- a/server/game/entity/effects.ts +++ b/server/game/entity/effects.ts @@ -32,6 +32,10 @@ export function effect_type(id: string): EffectType | undefined { return registry.get(id); } +export function effect_type_ids(): string[] { + return [...registry.keys()]; +} + // what level.json keeps of an effect, the doc's active_effects entries export interface SavedEffect { id: string; diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 9eba235..af98a1a 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -65,6 +65,7 @@ import { EntityManager } from "./entity/entity_manager.ts"; import { NaturalSpawner } from "./natural_spawner.ts"; import { create_entity, entity_type_ids } from "./entity/entity_types.ts"; import { damage_source, type DamageSource, type SavedEntity } from "./entity/entity.ts"; +import { effect_type, effect_type_ids, EffectInstance } from "./entity/effects.ts"; import { ATTRIBUTES } from "./entity/attributes.ts"; // how far from a player's eyes a block can be changed, a bit more than the client's reach @@ -1186,6 +1187,28 @@ export class GameServer { ), ); + // TODO: only let operators do this + const add_effect = (context: CommandContext, seconds: number) => { + const id = context.get("effect"); + const type = effect_type(id); + if (!type) throw new CommandError(`There's no effect ${id}`); + context.source.server_player.add_effect(new EffectInstance(type, seconds * TICKS_PER_SECOND)); + context.source.send_message(`Gave ${display_name(id)} for ${seconds} seconds`); + }; + register( + literal("effect").describe("Gives you a status effect").then( + argument("effect", word()) + .suggests((partial) => + effect_type_ids().filter((id) => id.startsWith(partial)).map((text) => ({ text })) + ) + .executes((context) => add_effect(context, 30)) + .then( + argument("seconds", integer(1, 1_000_000)) + .executes((context) => add_effect(context, context.get("seconds"))), + ), + ), + ); + register( literal("tps").describe("Shows how the game loop keeps up").executes((context) => { if (!this.loop) { diff --git a/tests/vitals_test.ts b/tests/vitals_test.ts index 512d966..553e958 100644 --- a/tests/vitals_test.ts +++ b/tests/vitals_test.ts @@ -209,3 +209,11 @@ Deno.test("the effects that show an icon are sent to the player when they change t.game.tick(); assert(!t.take(1).some((m: Msg) => m.type === "effects"), "not again while they only count down"); }); + +Deno.test("/effect gives you an effect, for 30 seconds or as many as you say", async () => { + const t = await setup(); + t.send(1, { type: "chat", text: "/effect bworld:speed" }); + assertEquals(t.player.get_effect("bworld:speed")?.duration, 600); + t.send(1, { type: "chat", text: "/effect bworld:regeneration 5" }); + assertEquals(t.player.get_effect("bworld:regeneration")?.duration, 100); +});