Skip to content

Commit

Permalink
feat(world): added give command
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Jun 6, 2024
1 parent a34b863 commit 6f8b322
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
49 changes: 49 additions & 0 deletions packages/world/src/commands/admin/give.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { IntegerEnum } from "@serenityjs/command";

import { ItemEnum, TargetEnum } from "../enums";
import { ItemStack } from "../../item";

import type { ItemIdentifier } from "@serenityjs/item";
import type { World } from "../../world";

const register = (world: World) => {
// Register the setblock command
world.commands.register(
"give",
"Gives an item to a player",
(_, parameters) => {
// Get the result of the item, amount, and metadata
const itemIdentifier = parameters.item.result as ItemIdentifier;
const amount = parameters.amount?.result ?? 1;
const metadata = parameters.metadata?.result ?? 0;

// Create a new item stack
const itemStack = new ItemStack(itemIdentifier, amount, metadata);

// Loop through the targets
for (const target of parameters.target.result) {
// Check if the target is an entity
if (!target.isPlayer()) continue;

// Get the player's inventory
const { container } = target.getComponent("minecraft:inventory");

// Add the item to the player's inventory
container.addItem(itemStack);
}

// Send the success message
return {
message: `Successfully gave x${amount} ${itemIdentifier} to ${parameters.target.result.length} players.`
};
},
{
target: TargetEnum,
item: ItemEnum,
amount: [IntegerEnum, true],
metadata: [IntegerEnum, true]
}
);
};

export default register;
3 changes: 2 additions & 1 deletion packages/world/src/commands/admin/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import GAMEMODE from "./gamemode";
import SETBLOCK from "./setblock";
import GIVE from "./give";

// Define all admin commands
const ADMIN_COMMANDS = [GAMEMODE, SETBLOCK];
const ADMIN_COMMANDS = [GAMEMODE, SETBLOCK, GIVE];

export { ADMIN_COMMANDS };
1 change: 1 addition & 0 deletions packages/world/src/commands/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./target";
export * from "./gamemode";
export * from "./block";
export * from "./item";
11 changes: 11 additions & 0 deletions packages/world/src/commands/enums/item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ItemType } from "@serenityjs/item";
import { CustomEnum } from "@serenityjs/command";

const identifiers = ItemType.getAll().map((item) => item.identifier);

class ItemEnum extends CustomEnum {
public static readonly name = "block";
public static readonly options = identifiers;
}

export { ItemEnum };

0 comments on commit 6f8b322

Please sign in to comment.