Files
Construct/packs/BP/scripts/commands/construct.js
T
2025-09-18 18:42:41 -07:00

62 lines
2.4 KiB
JavaScript

import { Command } from '../lib/canopy/CanopyExtension';
import { extension } from '../config';
import { world, system, EntityComponentTypes, ItemStack, CommandPermissionLevel, CustomCommandStatus, Player } from '@minecraft/server';
import { MenuForm } from '../classes/MenuForm';
import { structureCollection } from '../classes/Structure/StructureCollection'
import { Builders } from '../classes/Builder/Builders';
export const MENU_ITEM = 'construct:menu';
const menuCmd = new Command({
name: 'construct',
description: { text: 'Opens the Construct menu.' },
usage: 'construct',
callback: (sender) => openMenu(sender)
});
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,
cheatsRequired: false
};
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(MENU_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 !== MENU_ITEM) return;
event.cancel = true;
const builder = Builders.get(event.source.id);
system.run(() => {
if (builder.isFlexibleInstanceMoving())
return;
openMenu(event.source, event);
});
});
function openMenu(player, event = void 0) {
const options = { jumpToInstance: true }
if (event) {
const instanceNames = structureCollection.getInstanceNames();
const instanceName = event.itemStack?.nameTag;
if (instanceNames.includes(instanceName))
options.instanceName = instanceName;
}
new MenuForm(player, options);
}