From 4d07e2e1ae1757fc26af2e878841b1b99da19910 Mon Sep 17 00:00:00 2001 From: ForestOfLight Date: Tue, 19 May 2026 21:21:32 -0700 Subject: [PATCH] refactor(cli): migrate construct command into Command pipeline and split itemUse handler --- .../classes/Instance/FlexibleInstanceMove.js | 2 +- packs/BP/scripts/classes/MenuItemHandler.js | 27 ++++++++++ packs/BP/scripts/commands/ConstructCommand.js | 38 ++++++++++++++ packs/BP/scripts/commands/construct.js | 52 ------------------- packs/BP/scripts/consts.js | 2 + packs/BP/scripts/main.js | 5 +- 6 files changed, 72 insertions(+), 54 deletions(-) create mode 100644 packs/BP/scripts/classes/MenuItemHandler.js create mode 100644 packs/BP/scripts/commands/ConstructCommand.js delete mode 100644 packs/BP/scripts/commands/construct.js create mode 100644 packs/BP/scripts/consts.js diff --git a/packs/BP/scripts/classes/Instance/FlexibleInstanceMove.js b/packs/BP/scripts/classes/Instance/FlexibleInstanceMove.js index 34cf36b..80086de 100644 --- a/packs/BP/scripts/classes/Instance/FlexibleInstanceMove.js +++ b/packs/BP/scripts/classes/Instance/FlexibleInstanceMove.js @@ -1,6 +1,6 @@ import { InputPermissionCategory, world, system } from "@minecraft/server"; import { Outliner } from "../Outliner"; -import { MENU_ITEM } from "../../commands/construct"; +import { MENU_ITEM } from "../../consts"; import { Vector } from "../../lib/Vector"; import { PlayerMovement } from "../PlayerMovement"; import { Builders } from "../Builder/Builders"; diff --git a/packs/BP/scripts/classes/MenuItemHandler.js b/packs/BP/scripts/classes/MenuItemHandler.js new file mode 100644 index 0000000..8ed9f9f --- /dev/null +++ b/packs/BP/scripts/classes/MenuItemHandler.js @@ -0,0 +1,27 @@ +import { world, system } from '@minecraft/server'; +import { MENU_ITEM } from '../consts'; +import { MenuForm } from './MenuForm'; +import { structureCollection } from './Structure/StructureCollection'; +import { Builders } from './Builder/Builders'; + +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); +} diff --git a/packs/BP/scripts/commands/ConstructCommand.js b/packs/BP/scripts/commands/ConstructCommand.js new file mode 100644 index 0000000..04e4ba2 --- /dev/null +++ b/packs/BP/scripts/commands/ConstructCommand.js @@ -0,0 +1,38 @@ +import { CommandPermissionLevel, CustomCommandStatus, EntityComponentTypes, ItemStack, system } from '@minecraft/server'; +import { Command } from '../classes/Commands/Command'; +import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin'; +import { requirePlayer } from '../classes/Commands/lib/requirePlayer'; +import { commandError } from '../classes/Commands/lib/commandError'; +import { MENU_ITEM } from '../consts'; + +export class ConstructCommand extends Command { + constructor() { + super({ + name: 'construct', + description: 'construct.commands.construct', + permissionLevel: CommandPermissionLevel.Any, + cheatsRequired: false, + allowedSources: [PlayerCommandOrigin], + callback: (source) => this.run(source) + }); + } + + run(source) { + try { + const player = requirePlayer(source); + system.run(() => { + const remaining = player.getComponent(EntityComponentTypes.Inventory) + ?.container?.addItem(new ItemStack(MENU_ITEM)); + if (remaining) + player.sendMessage({ translate: 'construct.commands.construct.fail' }); + else + player.sendMessage({ translate: 'construct.commands.construct.success' }); + }); + return { status: CustomCommandStatus.Success }; + } catch (err) { + return commandError(source, err); + } + } +} + +export const constructCommand = new ConstructCommand(); diff --git a/packs/BP/scripts/commands/construct.js b/packs/BP/scripts/commands/construct.js deleted file mode 100644 index 05b3ab8..0000000 --- a/packs/BP/scripts/commands/construct.js +++ /dev/null @@ -1,52 +0,0 @@ -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'; - -system.beforeEvents.startup.subscribe((event) => { - const command = { - name: 'construct:construct', - description: 'construct.commands.construct', - 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: 'construct.commands.construct.denyorigin' }; - system.run(() => { - const givenItemStack = player.getComponent(EntityComponentTypes.Inventory)?.container?.addItem(new ItemStack(MENU_ITEM)); - if (givenItemStack) - player.sendMessage({ translate: 'construct.commands.construct.fail' }); - else - player.sendMessage({ translate: 'construct.commands.construct.success' }); - }); - 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); -} \ No newline at end of file diff --git a/packs/BP/scripts/consts.js b/packs/BP/scripts/consts.js new file mode 100644 index 0000000..b6094db --- /dev/null +++ b/packs/BP/scripts/consts.js @@ -0,0 +1,2 @@ +export const PACK_IDENTIFIER = 'construct'; +export const MENU_ITEM = 'construct:menu'; diff --git a/packs/BP/scripts/main.js b/packs/BP/scripts/main.js index 508d3f0..ba6b6f9 100644 --- a/packs/BP/scripts/main.js +++ b/packs/BP/scripts/main.js @@ -6,8 +6,11 @@ import './options/easyPlace'; import './options/fastEasyPlace'; import './options/materialGrabber'; +// Menu item handler +import './classes/MenuItemHandler'; + // Commands -import './commands/construct'; +import './commands/ConstructCommand'; // Other import './classes/BlockInfo';