refactor(cli): migrate construct command into Command pipeline and split itemUse handler
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -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();
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export const PACK_IDENTIFIER = 'construct';
|
||||
export const MENU_ITEM = 'construct:menu';
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user