feat(cli): add command helpers and NotAPlayerError
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
import { CustomCommandStatus } from '@minecraft/server';
|
||||||
|
import { NotAPlayerError } from '../../Errors/NotAPlayerError';
|
||||||
|
|
||||||
|
export function commandError(source, err) {
|
||||||
|
if (err instanceof NotAPlayerError) {
|
||||||
|
return { status: CustomCommandStatus.Failure, message: 'construct.commands.error.notAPlayer' };
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { system } from '@minecraft/server';
|
||||||
|
import { structureCollection } from '../../Structure/StructureCollection';
|
||||||
|
|
||||||
|
export function findInstance(source, name) {
|
||||||
|
if (!structureCollection.has(name)) {
|
||||||
|
system.run(() => source.sendMessage({
|
||||||
|
rawtext: [{ translate: 'construct.commands.error.instanceNotFound', with: [name] }]
|
||||||
|
}));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return structureCollection.get(name);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { PlayerCommandOrigin } from '../PlayerCommandOrigin';
|
||||||
|
import { NotAPlayerError } from '../../Errors/NotAPlayerError';
|
||||||
|
|
||||||
|
export function requirePlayer(source) {
|
||||||
|
if (!(source instanceof PlayerCommandOrigin))
|
||||||
|
throw new NotAPlayerError();
|
||||||
|
return source.getSource();
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
export class NotAPlayerError extends Error {
|
||||||
|
constructor(message = 'Command requires a player source.') {
|
||||||
|
super(message);
|
||||||
|
this.name = 'NotAPlayerError';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -106,3 +106,7 @@ construct.option.materialgrabber.howto=Interact with inventories using the Mater
|
|||||||
construct.option.materialgrabber.grabbed.zero=§7Grabbed 0 items.
|
construct.option.materialgrabber.grabbed.zero=§7Grabbed 0 items.
|
||||||
construct.option.materialgrabber.grabbed.one=§aGrabbed 1 item.
|
construct.option.materialgrabber.grabbed.one=§aGrabbed 1 item.
|
||||||
construct.option.materialgrabber.grabbed.many=§aGrabbed %s items. ## Insert string: number of items transferred to the player
|
construct.option.materialgrabber.grabbed.many=§aGrabbed %s items. ## Insert string: number of items transferred to the player
|
||||||
|
|
||||||
|
## CLI shared errors
|
||||||
|
construct.commands.error.instanceNotFound=§cInstance "%1" not found.
|
||||||
|
construct.commands.error.notAPlayer=§cThis command can only be used by players.
|
||||||
|
|||||||
@@ -106,3 +106,7 @@ construct.option.materialgrabber.howto=使用"材料收集器"物品与箱子等
|
|||||||
construct.option.materialgrabber.grabbed.zero=§7已收集 0 个物品
|
construct.option.materialgrabber.grabbed.zero=§7已收集 0 个物品
|
||||||
construct.option.materialgrabber.grabbed.one=§a已收集 1 个物品
|
construct.option.materialgrabber.grabbed.one=§a已收集 1 个物品
|
||||||
construct.option.materialgrabber.grabbed.many=§a已收集 %s 个物品 ## 插入字符串: 收集给玩家的物品数
|
construct.option.materialgrabber.grabbed.many=§a已收集 %s 个物品 ## 插入字符串: 收集给玩家的物品数
|
||||||
|
|
||||||
|
## CLI shared errors
|
||||||
|
construct.commands.error.instanceNotFound=§cInstance "%1" not found.
|
||||||
|
construct.commands.error.notAPlayer=§cThis command can only be used by players.
|
||||||
|
|||||||
Reference in New Issue
Block a user