#Custom commands not working?

1 messages · Page 1 of 1 (latest)

worn bay
#

One message removed from a suspended account.

#

One message removed from a suspended account.

mint tartan
#

are you on stable or beta?

#

because this is the old syntax

#

@worn bay you'll need to use this instead: world.beforeEvents.chatSend.subscribe

#

btw, are you trying to do a slot selection system for each inventory slot?

worn bay
worn bay
mint tartan
mint tartan
worn bay
#

One message removed from a suspended account.

mint tartan
#

ah, it's outdated

worn bay
#

One message removed from a suspended account.

#

One message removed from a suspended account.

mint tartan
#

use this ```json
"capabilities": [
"script_eval"
],
"dependencies": [
{
"module_name": "@minecraft/server",
"version": "1.3.0-beta"
},
{
"module_name": "@minecraft/server-ui",
"version": "1.1.0-beta"
}
]

#

Actually, here, just edit the other stuff to your liking: ```json
{
"format_version": 2,
"header": {
"name": "§6Avatar §3Addon §8[1.20.1] [BP]§r",
"description": "Adds the bending seen in avatar the last airbender to minecraft! All credit to GlitchTurtle32!",
"uuid": "13fbd8d8-7ff3-492d-9b92-94ff4a1855c2",
"version": [
3,
1,
0
],
"min_engine_version": [
1,
20,
0
]
},
"modules": [
{
"type": "script",
"language": "javascript",
"uuid": "b7055392-4154-4aa8-813f-afa6291f1056",
"entry": "scripts/index.js",
"version": [
0,
1,
0
]
}
],
"capabilities": [
"script_eval"
],
"dependencies": [
{
"module_name": "@minecraft/server",
"version": "1.3.0-beta"
},
{
"module_name": "@minecraft/server-ui",
"version": "1.1.0-beta"
}
]
}

#

again, how many chat commands are you going to create? Cause it might be a good idea to implement a handler for those

#

instead of just a really long switch statment

worn bay
#

One message removed from a suspended account.

mint tartan
#

also, for tick event, you're gonna need to do ```js
system.run(function runnable() {
system.run(runnable);
tickEvent();
});

worn bay
#

One message removed from a suspended account.

mint tartan
#

it's definitely a manifest thing

#

give me a minute

#

i'll fix the code

#

just found the issue

#

chatsend is a before event, so it's readonly

#

to run commands from that, you'll need a system.run

worn bay
mint tartan
#

I'll send it back working

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

// Events
world.beforeEvents.chatSend.subscribe(eventData => chatSend(eventData));

// Tick event
system.run(function runnable() { 
    system.run(runnable);
    tickEvent();
});

// ----- in a different file probably

function tickEvent() {
    for (const player of world.getPlayers()) {
        let slot = player.selectedSlot;
        setScore(player, "hotbar", slot);
    }
};

// ----- in a different file probably

const commands = {
    help: helpCommand,
}

function chatSend(eventData) {
    const player = eventData.sender;
    const message = eventData.message;

    if (!message.startsWith("!")) return;

    const args = message.slice(1).split(/ +/);
    const commandName = args.shift().toLowerCase();

    system.run(() => {
        if (!(commandName in commands)) return player.sendMessage("That's not a commmand you can use!")
        commands[commandName](player, args);
        message.cancel = true;
    });
};

// ----- in a different file probably

function helpCommand(player, args) {
    console.warn(args);
}

// ----- in a different file probably

/**
 * Set the score of a player or other entity
 * @param {Entity|string} target The entity or dummy player to change the score of.
 * @param {string} objective The objective to change the score on.
 * @param {number} amount The amount to change the score by.
 * @param {boolean} add If the score should stack on top of the previous value set.
 * @returns {number} The new score that was set, or NaN if no objective or initial entity score was found or set.
 */
export function setScore(target, objective, amount, add = false) {
    try {
        const scoreObj = world.scoreboard.getObjective(objective);
        const score = (add ? scoreObj.getScore(target.scoreboardIdentity) : 0) + amount;
        target.scoreboardIdentity.setScore(scoreObj, score);
        return score;
    } catch (error) { 
        return undefined; 
    };
};
#

there's a very basic command handler and args system

#

in this example, the help command gets an array of the arguments, like if the user typed !help flame passive it would give the help command ['flame', 'passive'] which should make it much eaiser to sort through

#

@worn bay

worn bay
#

One message removed from a suspended account.

#

One message removed from a suspended account.

worn bay
mint tartan
worn bay
mint tartan
#

For passives, if it’s list of text, I’d use a dictionary data structure

#

I can’t help right now, but I’ll send an example in a couple minutes

worn bay
#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

fiery jay
#

args with " working here

#

just need to split your message

mint tartan
#

updated code ```js
import { world, system } from '@minecraft/server';

// Events
world.beforeEvents.chatSend.subscribe(eventData => chatSend(eventData));

// Tick event
system.run(function runnable() {
system.run(runnable);
tickEvent();
});

// ----- in a different file probably

function tickEvent() {
for (const player of world.getPlayers()) {
let slot = player.selectedSlot;
setScore(player, "hotbar", slot);
}
};

// ----- in a different file probably

const commands = {
help: helpCommand,
}

function chatSend(eventData) {
const player = eventData.sender;
const message = eventData.message;

if (!message.startsWith("!")) return;
eventData.cancel = true;

const args = message.slice(1).split(/ +/);
const commandName = args.shift().toLowerCase();

system.run(() => {
    if (!(commandName in commands)) return player.sendMessage("That's not a commmand you can use!")
    commands[commandName](player, args);
});

};

// ----- in a different file probably

const help = {
flame: {
passive: "test text goes here",
heavy: "other stuff idk"
}
}

function helpCommand(player, args) {
console.warn(args);
player.sendMessage(help[args[0]][args[1]]);
}

#

notice the ```js
const help = {
flame: {
passive: "test text goes here",
heavy: "other stuff idk"
}
}

#

it navigates the tree based on your args

#

like if you put !help flame heavy, it would send "other stuff idk"

#

if you ever need help with gametest, feel free to send me a DM

worn bay
mint tartan
#

Sure, you’ll have to change how you call it though. If you’re using just tellraw though, it’s much better this way

#

If you want to insert player name or score, you can do that super easily with the current way

worn bay
#

One message removed from a suspended account.

mint tartan
#

So run command then?

#

Switch sendMessage for runCommand and suddenly the descriptions are commands

worn bay
#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

fiery jay
mint tartan
fiery jay
#

with a config of course , to add help things

#

my command parser ?

mint tartan
#

ah nice

fiery jay
#

but it get the args between ", like if I want to add a perm to a player with space

#

so you have the solution in it 🙂

mint tartan
#

Oh that makes sense

#

I don’t actually use the command handler, it was just an example

#

I use the one from paradox anticheat

#

It’s very nice