refactor(cli): migrate construct command into Command pipeline and split itemUse handler

This commit is contained in:
ForestOfLight
2026-05-19 21:21:32 -07:00
Unverified
parent aae9fd2f4b
commit 4d07e2e1ae
6 changed files with 72 additions and 54 deletions
@@ -1,6 +1,6 @@
import { InputPermissionCategory, world, system } from "@minecraft/server"; import { InputPermissionCategory, world, system } from "@minecraft/server";
import { Outliner } from "../Outliner"; import { Outliner } from "../Outliner";
import { MENU_ITEM } from "../../commands/construct"; import { MENU_ITEM } from "../../consts";
import { Vector } from "../../lib/Vector"; import { Vector } from "../../lib/Vector";
import { PlayerMovement } from "../PlayerMovement"; import { PlayerMovement } from "../PlayerMovement";
import { Builders } from "../Builder/Builders"; import { Builders } from "../Builder/Builders";
@@ -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);
}
@@ -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();
-52
View File
@@ -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);
}
+2
View File
@@ -0,0 +1,2 @@
export const PACK_IDENTIFIER = 'construct';
export const MENU_ITEM = 'construct:menu';
+4 -1
View File
@@ -6,8 +6,11 @@ import './options/easyPlace';
import './options/fastEasyPlace'; import './options/fastEasyPlace';
import './options/materialGrabber'; import './options/materialGrabber';
// Menu item handler
import './classes/MenuItemHandler';
// Commands // Commands
import './commands/construct'; import './commands/ConstructCommand';
// Other // Other
import './classes/BlockInfo'; import './classes/BlockInfo';