UNPKG

minecraft-module

Version:

Module For Minecraft ScriptApi

123 lines (109 loc) 3.43 kB
import { system, world } from "@minecraft/server"; const prefix = "!"; export class CommandBuilder { constructor() { this._registrationInformation = []; } register(register, callback) { let form = { name: register.name.toLowerCase(), aliases: register.aliases ? register.aliases.map((v) => v.toLowerCase()) : [], description: register.description, usage: register.usage || null, admin: register.admin || false, permission: register.permission || null, callback, }; this._registrationInformation.push(form); } unregister(name) { const index = this._registrationInformation.findIndex( (element) => element.name.toLowerCase() === name.toLowerCase() ); if (index !== -1) { this._registrationInformation.splice(index, 1); } } msg(text) { for (const player of world.getPlayers()) { player.sendMessage(text); } } error(text) { for (const player of world.getPlayers()) { player.sendMessage(`§cCOMMAND §7>> §cError! ${text}`); } } valid(command, player) { const cmd = this.getRegistration(command.toLowerCase()); if (!cmd) { system.run(() => { player.sendMessage( `§cCOMMAND §7>> §cCommand §g${prefix}${command} §cnot found. Please check that command using §g${prefix}help §ccommand.` ); player.playSound("note.bass"); }); return false; } if (cmd.admin && !player.hasTag("Admin")) { system.run(() => { player.sendMessage( `§cCOMMAND §7>> §cYou do not have permission to use the §g${prefix}${command} §ccommand.` ); player.playSound("note.bass"); }); return false; } if (cmd.permission && !player.hasTag("Admin")) { const permissions = Array.isArray(cmd.permission) ? cmd.permission : [cmd.permission]; const hasPermission = permissions.some((perm) => player.hasTag(perm)); if (!hasPermission) { system.run(() => { player.sendMessage( `§cCOMMAND §7>> §cYou do not have permission to use the §g${prefix}${command} §ccommand.` ); player.playSound("note.bass"); }); return false; } } return true; } get() { const commands = []; this._registrationInformation.forEach((element) => { if (!element.private) { commands.push(element.name); } }); return commands; } getAllRegistration() { return this._registrationInformation; } getRegistration(name) { return this._registrationInformation.find( (element) => element.name.toLowerCase() === name || (element.aliases && element.aliases.includes(name)) ); } } const CommandBuild = new CommandBuilder(); world.beforeEvents.chatSend.subscribe((data) => { if (data.message.startsWith(prefix)) { let args = data.message.slice(prefix.length).trim().split(" "); let command = args.shift().toLowerCase(); const player = data.sender; data.cancel = true; if (CommandBuild.valid(command, player)) { let getCommand = CommandBuild.getRegistration(command); getCommand.callback(data, player, args, getCommand); } } }); export { CommandBuild }