-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create index.js * Create README.md * Update README.md * Update README.md * Update index.js * Command Builder * Command Builder * Command Builder
- Loading branch information
1 parent
d79d5cd
commit 0de2f8e
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
); |