#CustomCommandラッパー

1 messages · Page 1 of 1 (latest)

torpid mantle
#

CustomCommandBuilder.ts


import {CommandPermissionLevel, CustomCommand, CustomCommandParameter} from "@minecraft/server";

/**
 * @example commandRegistry.registerEnum("test:my_enum,["foo","bar","baz"]);
 * const testCommand = new CustomCommandBuilder("test:test","Used for test.","CustomCommandPermissionLevel.Any").setMandatoryParam([
 * {type: CustomCommandParamType.Enum,
 * name: "test:my_enum"
 * }])
 */

export class CustomCommandBuilder implements CustomCommand {
  name: string;
  description: string;
  permissionLevel: CommandPermissionLevel;
  /**
   *@description コマンドを実行する際に必須のパラメータ。
   *@example /namespace:name mandatory_enum1 mandatory_enum2 optional_enum1
   *@remarks mandatory_enum1に応じてmandatory_enum2を変えることができません。共通のenumを使用することになります。
   */
  mandatoryParameters?: CustomCommandParameter[] | undefined;
  /**
   * @description コマンドを実行する際の省略可能のパラメータ。
   *@example /namespace:name mandatory_enum1 mandatory_enum2 optional_enum1
   *@remarks mandatory_enum1に応じてmandatory_enum2を変えることができません。共通のenumを使用することになります。
   */
  optionalParameters?: CustomCommandParameter[] | undefined;
  cheatsRequired?: boolean | undefined;

  constructor(name: string, description: string, permissionLevel?: CommandPermissionLevel) {
    this.name = name;
    this.description = description;
    if (!permissionLevel) {
      this.permissionLevel = CommandPermissionLevel.Any;
    } else {
      this.permissionLevel = permissionLevel;
    }
  }
  setMandatoryParam(mandatoryParameters: CustomCommandParameter[]) {
    this.mandatoryParameters = mandatoryParameters;
  }

  setOptionalParam(optionalParameters: CustomCommandParameter[]) {
    this.optionalParameters = optionalParameters;
  }

  setCheatsRequired(cheatsRequired: boolean) {
    this.cheatsRequired = cheatsRequired;
  }
}

#

CustomCommand.ts

import {
  CommandPermissionLevel,
  CustomCommandOrigin,
  CustomCommandRegistry,
  CustomCommandResult,
} from "@minecraft/server";
import {CustomCommandBuilder} from "./CustomCommandBuilder";

export abstract class CustomCommand {
  public readonly data: CustomCommandBuilder;
  constructor(name: string, description: string, permissionLevel?: CommandPermissionLevel) {
    this.data = new CustomCommandBuilder(name, description, permissionLevel);
  }

  /**
   *
   * @example execute(){
   * return {message?:string,state:CustomCommandStatus}: CustomCommandResult
   * }
   *
   */
  abstract execute(origin: CustomCommandOrigin, ...args: unknown[]): CustomCommandResult;

  public register(registry: CustomCommandRegistry) {
    registry.registerCommand(this.data, this.execute.bind(this));
    console.warn(`Registered Custom Command: /${this.data.name}`);
  }
}
#

CustomCommandEnum.ts

import {CustomCommandRegistry} from "@minecraft/server";

export class CustomCommandEnum {
  name: string;
  values: string[];
  constructor(name: string, values: {[key: string]: string}) {
    this.name = name;
    this.values = Object.values(values);
  }
  public register(registry: CustomCommandRegistry) {
    registry.registerEnum(this.name, this.values);
    console.warn(`Registered Custom Command Enum: /${this.name}`);
  }
}
#

CustomCommandクラスのregisterメソッドにCustomCommandRegistryを渡してあげるようなRegistryServiceを作ってあげれば作成と登録が簡単になります
管理もしやすいと思います

#

カスタムコマンドよりカスタムコマンドenumを先に登録する必要があることには注意してください