Effect command

This commit is contained in:
2026-10-02 13:19:12 -03:00
parent f749f6be79
commit cd07d441b2
5 changed files with 38 additions and 0 deletions
+2
View File
@@ -82,6 +82,8 @@ Like block components, an effect that lists a component nobody registered stops
## Using Effects
`/effect <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
+1
View File
@@ -108,6 +108,7 @@ Commands can only be registered during `setup`.
| Command | What it does |
| ------------------------------------------------------- | -------------------------------------------------------- |
| `/give <item> [count]` | Gives you an item |
| `/effect <effect> [seconds]` | Gives you a status effect, for 30 seconds by default |
| `/summon <entity>` | Spawns an entity where you are |
| `/time` | Shows the time |
| `/time set <day\|noon\|sunset\|night\|midnight\|ticks>` | Jumps to the next time of day. Time never goes back |
+4
View File
@@ -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;
+23
View File
@@ -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<ServerCommandSource>, seconds: number) => {
const id = context.get<string>("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<ServerCommandSource>("effect").describe("Gives you a status effect").then(
argument<string, ServerCommandSource>("effect", word())
.suggests((partial) =>
effect_type_ids().filter((id) => id.startsWith(partial)).map((text) => ({ text }))
)
.executes((context) => add_effect(context, 30))
.then(
argument<number, ServerCommandSource>("seconds", integer(1, 1_000_000))
.executes((context) => add_effect(context, context.get<number>("seconds"))),
),
),
);
register(
literal<ServerCommandSource>("tps").describe("Shows how the game loop keeps up").executes((context) => {
if (!this.loop) {
+8
View File
@@ -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);
});