127 lines
3.5 KiB
JavaScript
127 lines
3.5 KiB
JavaScript
// priority: 100
|
|
|
|
/**
|
|
* Converts a hex color to RGB.
|
|
* @param {string} hex - The hex color.
|
|
* @returns {{r: number, g: number, b: number}} - The resulting RGB.
|
|
*/
|
|
function hexToRgb(hex) {
|
|
const h = hex.replace("#", "");
|
|
return {
|
|
r: parseInt(h.substring(0, 2), 16),
|
|
g: parseInt(h.substring(2, 4), 16),
|
|
b: parseInt(h.substring(4, 6), 16),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Iterates over a player's inventory.
|
|
* @param {Internal.Player} player
|
|
* @param {(item: Internal.ItemStack) => void} callback
|
|
*/
|
|
function iterateInventory(player, callback) {
|
|
player.inventory.allItems.forEach(
|
|
/** @type {any} */ (
|
|
(/** @type {Internal.ItemStack} */ item) => {
|
|
callback(item);
|
|
}
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Filters entities in a level.
|
|
* @param {Internal.ServerLevel} level
|
|
* @param {(entity: Internal.Entity) => boolean} callback
|
|
* @returns {Internal.EntityArrayList} - The filtered entities.
|
|
*/
|
|
function filterEntities(level, callback) {
|
|
return level
|
|
.getEntities()
|
|
.filter(
|
|
/** @type {any} */ (
|
|
(/** @type {Internal.Entity} */ entity) => callback(entity)
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Sends a title message to a player.
|
|
* @param {Internal.MinecraftServer} server - The server instance.
|
|
* @param {string} username - The player's username.
|
|
* @param {string} message - The message to send.
|
|
*/
|
|
function title(server, username, message) {
|
|
server.runCommandSilent("title @a times 20 200 20");
|
|
server.runCommandSilent(`title ${username} title ["${message}"]`);
|
|
}
|
|
|
|
/**
|
|
* Registers a command.
|
|
* @param {Internal.CommandRegistryEventJS} event
|
|
* @param {{
|
|
* command: string,
|
|
* operator?: boolean,
|
|
* handler?: (ctx: Internal.CommandContext<Internal.CommandSourceStack>) => void,
|
|
* parameters?: Array<{
|
|
* name: string,
|
|
* type: Internal.ArgumentTypeWrappers,
|
|
* handler?: (ctx: Internal.CommandContext<Internal.CommandSourceStack>) => void,
|
|
* }>
|
|
* }} options
|
|
*/
|
|
function registerCommand(event, options) {
|
|
const command = options.command;
|
|
const operator = options.operator !== undefined ? options.operator : false;
|
|
const parameters = options.parameters !== undefined ? options.parameters : [];
|
|
const handler = options.handler !== undefined ? options.handler : null;
|
|
|
|
const cmd = event.commands.literal(command);
|
|
if (operator)
|
|
cmd.requires(
|
|
/** @type {any} */ (
|
|
(/** @type {Internal.CommandSourceStack} */ source) =>
|
|
source.hasPermission(2)
|
|
),
|
|
);
|
|
|
|
if (handler !== null)
|
|
cmd.executes(
|
|
/** @type {any} */ (
|
|
/** @param {Internal.CommandContext<Internal.CommandSourceStack>} ctx */
|
|
(ctx) => {
|
|
handler(ctx);
|
|
return 1;
|
|
}
|
|
),
|
|
);
|
|
|
|
/** @type { Internal.LiteralArgumentBuilder<Internal.CommandSourceStack> | Internal.RequiredArgumentBuilder<Internal.CommandSourceStack, any> } */
|
|
let current = cmd;
|
|
parameters.forEach((param, index) => {
|
|
const name = param.name;
|
|
const type = param.type;
|
|
const paramHandler = param.handler;
|
|
const arg = event.commands.argument(name, type.create(event));
|
|
const isLast = index === parameters.length - 1;
|
|
|
|
const finalHandler = paramHandler ? paramHandler : isLast ? handler : null;
|
|
|
|
if (finalHandler) {
|
|
arg.executes(
|
|
/** @type {any} */ (
|
|
/** @param {Internal.CommandContext<Internal.CommandSourceStack>} ctx */
|
|
(ctx) => {
|
|
finalHandler(ctx);
|
|
return 1;
|
|
}
|
|
),
|
|
);
|
|
}
|
|
|
|
current.then(arg);
|
|
current = arg;
|
|
});
|
|
|
|
event.register(cmd);
|
|
}
|