Skip to content

Commit

Permalink
feat(world): added ability command
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Jul 14, 2024
1 parent caaef3c commit 55ab2c5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
46 changes: 46 additions & 0 deletions packages/world/src/commands/admin/ability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { type AbilitySet, CommandPermissionLevel } from "@serenityjs/protocol";

import { AbilityEnum, BoolEnum, TargetEnum } from "../enums";

import type { World } from "../../world";

const register = (world: World) => {
// Register the setblock command
world.commands.register(
"ability",
"Sets the ability of a player",
(_, parameters) => {
// Iterate over the entities
for (const entity of parameters.target.result) {
// Check if the entity is a player
if (!entity.isPlayer()) continue;

// Get the ability from the parameters
const ability = parameters.ability.result as AbilitySet;

// Get the ability component
const component = entity.getComponent(ability);

// Get the enabled value from the parameters
const enabled = parameters.enabled.result === "true";

// Set the ability value
component.setCurrentValue(enabled);
}

return {
message: "Successfully set the ability of the targeted players."
};
},
{
target: TargetEnum,
ability: AbilityEnum,
enabled: BoolEnum
},
{
permission: CommandPermissionLevel.Operator
}
);
};

export default register;
12 changes: 11 additions & 1 deletion packages/world/src/commands/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ import FILL from "./fill";
import SUMMON from "./summon";
import KICK from "./kick";
import CLEAR from "./clear";
import ABILITY from "./ability";

// Define all admin commands
const ADMIN_COMMANDS = [GAMEMODE, SETBLOCK, GIVE, FILL, SUMMON, KICK, CLEAR];
const ADMIN_COMMANDS = [
GAMEMODE,
SETBLOCK,
GIVE,
FILL,
SUMMON,
KICK,
CLEAR,
ABILITY
];

export { ADMIN_COMMANDS };
11 changes: 11 additions & 0 deletions packages/world/src/commands/enums/ability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CustomEnum } from "@serenityjs/command";
import { AbilitySet } from "@serenityjs/protocol";

const identifiers = Object.values(AbilitySet);

class AbilityEnum extends CustomEnum {
public static readonly name = "ability";
public static readonly options = identifiers;
}

export { AbilityEnum };
8 changes: 8 additions & 0 deletions packages/world/src/commands/enums/bool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { CustomEnum } from "@serenityjs/command";

class BoolEnum extends CustomEnum {
public static readonly name = "bool";
public static readonly options = ["true", "false"];
}

export { BoolEnum };
2 changes: 2 additions & 0 deletions packages/world/src/commands/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from "./gamemode";
export * from "./block";
export * from "./item";
export * from "./entity";
export * from "./bool";
export * from "./ability";

0 comments on commit 55ab2c5

Please sign in to comment.