Skip to content

Commit

Permalink
Command Builder (#304)
Browse files Browse the repository at this point in the history
* Create index.js

* Create README.md

* Update README.md

* Update README.md

* Update index.js

* Command Builder

* Command Builder

* Command Builder
  • Loading branch information
defowler2005 authored Aug 5, 2023
1 parent d79d5cd commit 0de2f8e
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
34 changes: 34 additions & 0 deletions scripts/command-builder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# command-builder

## Description

A simple to use command creator.

**Parameters:**

- `info` (Object): An object containing the information about the command.
- `name` (string): The name of the command.
- `description` (string): The description of the command.
- `is_staff` (boolean, default: false): Indicates if the command requires staff privileges.

## Example usage

```js
commandBuild.create(
{
name: 'test',
description: 'Testing command',
is_staff: false,
},
(player, args) => {
console.warn(`${player.name} ${args}`);
},
(player, args) => {
console.warn(`${player.name} ${args}`);
}
);
```

## Credits

These scripts were written by [defowler2005](https://github.com/defowler2005).
100 changes: 100 additions & 0 deletions scripts/command-builder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Script example for ScriptAPI
// Author: defowler2005 <https://github.com/defowler2005>
// Project: https://github.com/JaylyDev/ScriptAPI

/**
* Minecraft Bedrock ScriptAPI Example
* @author defowler2005#4812
* @version 1.0.0
* ---------------------------------------------------------------------------
* Add a commandBuilder to your world, Simple creating!
* ---------------------------------------------------------------------------
*/

import { world, system } from '@minecraft/server';

/**
* Represents a command builder.
* @class
*/
class commandBuilder {

/**
* @constructor
*/
constructor() {
this.commands = [];
}

/**
* Create the command with the provided info, callbacks.
*
* @param {Object} info - The information about the command.
* @param {string} info.name - The name of the command.
* @param {string} [info.description] - The description of the command.
* @param {boolean} [info.is_staff=false] - Indicates if the command requires staff privileges.
* @param {Function} callback - The callback function for the command.
* @param {Function} callbackWM - The callback function for delayed code execution until the player moves after using the command.
*/
create(info, callback, callbackWM) {
const command = {
name: info.name.split(' ')[0],
description: info.description || '',
is_staff: info.is_staff || false,
callback,
callbackWM,
};
this.commands.push(command);
}
};

const commandBuild = new commandBuilder();

/**
* Waits for the player to move and then executes a callback function.
* @param {Object} target - The target player to monitor for movement.
* @param {number} x - The initial X-coordinate of the target player.
* @param {number} y - The initial Y-coordinate of the target player.
* @param {number} z - The initial Z-coordinate of the target player.
* @param {Function} callback - The callback function to execute after the player moves.
*/

function waitMove(target, x, y, z, callback) {
const t = new Map();
t.set(target, [x, y, z]);
system.runInterval(() => {
for (const [target, [xOld, yOld, zOld]] of t) {
const { x: xc, y: yc, z: zc } = target.location;
if (xOld !== xc || yOld !== yc || zOld !== zc) system.run(() => t.delete(target) || callback());
}
})
};

world.beforeEvents.chatSend.subscribe((data) => {
const prefix = 'DF.'; //Replace this prefix to what you want!
const player = data.sender;
const message = data.message;
const { x, y, z } = player.location;
if (!message.startsWith(prefix)) return;
data.cancel = true;
const args = message.slice(prefix.length).split(/\s+/g);
const cmd = args.shift();
const command = commandBuild.commands.find((cmdName) => cmdName.name === cmd);
if (!command) return player.sendMessage('Invalid command!');
if (command.is_staff && !player.hasTag('hosen24576jg')) return player.sendMessage('Invalid permission!')
system.run(() => command.callback(player, args) || waitMove(player, x, y, z, () => command.callbackWM(player, args)));
});

commandBuild.create(
{
name: 'test',
description: 'Testing command',
is_staff: true
},
(player, args) => {
console.warn(`${player.name} ${args}`)
},
(player, args) => {
console.warn(`${player.name} ${args}`)
}
);

0 comments on commit 0de2f8e

Please sign in to comment.