add vanilla custom command to mirror canopy's

This commit is contained in:
ForestOfLight
2025-06-07 21:34:55 -07:00
Unverified
parent 597e0c7d79
commit 42bd7e9023
+28 -2
View File
@@ -1,6 +1,6 @@
import { Command } from '../lib/canopy/CanopyExtension';
import { extension } from '../config';
import { world, system, EntityComponentTypes, ItemStack } from '@minecraft/server';
import { world, system, EntityComponentTypes, ItemStack, CommandPermissionLevel, CustomCommandStatus, Player } from '@minecraft/server';
import { MenuForm } from '../classes/MenuForm';
import { structureCollection } from '../classes/Structure/StructureCollection'
@@ -10,10 +10,36 @@ const menuCmd = new Command({
name: 'construct',
description: { text: 'Gives you the Construct item. Use it to open the Construct menu.' },
usage: 'construct',
callback: (sender) => sender.getComponent(EntityComponentTypes.Inventory)?.container?.addItem(new ItemStack(ACTION_ITEM))
callback: (sender) => {
const origin = { sourceEntity: sender };
givePlayerConstructItem(origin);
}
});
extension.addCommand(menuCmd);
system.beforeEvents.startup.subscribe((event) => {
const command = {
name: 'construct:item',
description: 'Gives you the Construct item. Use it to open the Construct menu.',
permissionLevel: CommandPermissionLevel.Any
};
event.customCommandRegistry.registerCommand(command, givePlayerConstructItem)
});
function givePlayerConstructItem(origin) {
const player = origin.sourceEntity;
if (player instanceof Player === false)
return { status: CustomCommandStatus.Failure, message: 'This command can only be used by players.' };
system.run(() => {
const givenItemStack = player.getComponent(EntityComponentTypes.Inventory)?.container?.addItem(new ItemStack(ACTION_ITEM));
if (givenItemStack)
player.sendMessage('§cFailed to give you the Construct item.');
else
player.sendMessage('§aYou recieved the Construct item! Use it to open the Construct menu.');
});
return { status: CustomCommandStatus.Success };
}
world.beforeEvents.itemUse.subscribe((event) => {
if (!event.source || event.itemStack?.typeId !== ACTION_ITEM) return;
event.cancel = true;