Effect command
This commit is contained in:
@@ -82,6 +82,8 @@ Like block components, an effect that lists a component nobody registered stops
|
|||||||
|
|
||||||
## Using Effects
|
## 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):
|
Any mod's effects are added to players by id, see [Status Effects](players.md#status-effects):
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ Commands can only be registered during `setup`.
|
|||||||
| Command | What it does |
|
| Command | What it does |
|
||||||
| ------------------------------------------------------- | -------------------------------------------------------- |
|
| ------------------------------------------------------- | -------------------------------------------------------- |
|
||||||
| `/give <item> [count]` | Gives you an item |
|
| `/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 |
|
| `/summon <entity>` | Spawns an entity where you are |
|
||||||
| `/time` | Shows the time |
|
| `/time` | Shows the time |
|
||||||
| `/time set <day\|noon\|sunset\|night\|midnight\|ticks>` | Jumps to the next time of day. Time never goes back |
|
| `/time set <day\|noon\|sunset\|night\|midnight\|ticks>` | Jumps to the next time of day. Time never goes back |
|
||||||
|
|||||||
@@ -32,6 +32,10 @@ export function effect_type(id: string): EffectType | undefined {
|
|||||||
return registry.get(id);
|
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
|
// what level.json keeps of an effect, the doc's active_effects entries
|
||||||
export interface SavedEffect {
|
export interface SavedEffect {
|
||||||
id: string;
|
id: string;
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ import { EntityManager } from "./entity/entity_manager.ts";
|
|||||||
import { NaturalSpawner } from "./natural_spawner.ts";
|
import { NaturalSpawner } from "./natural_spawner.ts";
|
||||||
import { create_entity, entity_type_ids } from "./entity/entity_types.ts";
|
import { create_entity, entity_type_ids } from "./entity/entity_types.ts";
|
||||||
import { damage_source, type DamageSource, type SavedEntity } from "./entity/entity.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";
|
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
|
// 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(
|
register(
|
||||||
literal<ServerCommandSource>("tps").describe("Shows how the game loop keeps up").executes((context) => {
|
literal<ServerCommandSource>("tps").describe("Shows how the game loop keeps up").executes((context) => {
|
||||||
if (!this.loop) {
|
if (!this.loop) {
|
||||||
|
|||||||
@@ -209,3 +209,11 @@ Deno.test("the effects that show an icon are sent to the player when they change
|
|||||||
t.game.tick();
|
t.game.tick();
|
||||||
assert(!t.take(1).some((m: Msg) => m.type === "effects"), "not again while they only count down");
|
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);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user