#how to create custom commands?

1 messages · Page 1 of 1 (latest)

twilit bear
#

here's my code:

world.beforeEvents.chatSend.subscribe((eventData) => {
    const player = eventData.sender;
    switch (eventData.message) {
        case '!gmc':
            eventData.cancel = true;
            player.runCommandAsync('gamemode c');
            player.runCommandAsync('title @s title §a§lCreative');
            player.runCommandAsync('playsound random.toast @s');
            break;
        case '!gms':
            eventData.cancel = true;
            player.runCommandAsync('gamemode s');
            player.runCommandAsync('title @s title §4§lSurvival');
            player.runCommandAsync('playsound random.toast @s');
            break;
        case '!gmsp':
            eventData.cancel = true;
            player.runCommandAsync('gamemode spectator');
            player.runCommandAsync('title @s title §7§lSpectator');
            player.runCommandAsync('playsound random.toast @s');
            break;
        default: break;
    }
});``` . When I load my world(with all experiments and education edition turned on), there's message in chat, that says smth like "at least one addon isn't loaded". I don't know anything about code(copied this one from the wiki). Please can anyone fix the errors?
dawn marten
#
import { world, system } from '@minecraft/server';

const prefix = '!';

world.beforeEvents.chatSend.subscribe((eventData) => {
    const sender = eventData.sender;
    const message = eventData.message;

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

    eventData.cancel = true;
    
    switch (message) {
        case 'gmc':
            sender.runCommandAsync('gamemode c');
            sender.runCommandAsync('title @s title §a§lCreative');
            sender.runCommandAsync('playsound random.toast @s');
            break;
        case 'gms':
            sender.runCommandAsync('gamemode s');
            sender.runCommandAsync('title @s title §4§lSurvival');
            sender.runCommandAsync('playsound random.toast @s');
            break;
        case 'gmsp':
            sender.runCommandAsync('gamemode spectator');
            sender.runCommandAsync('title @s title §7§lSpectator');
            sender.runCommandAsync('playsound random.toast @s');
            break;
        default: break;
    }
});

Use this @twilit bear

twilit bear
#

thx! I will try this one

dawn marten
#

If this doesn't working then maybe is your .mcpack or .mcaddon problems

twilit bear
#

It doesn't work.. Thanks for trying to help me anyways

dawn marten
#

Full pack

#

Fact the code U copied on wiki that also can working if not thats your addons problem for exp : manifest.json

#

@twilit bear so if can U send the mcpack here

twilit bear
#

alr

dawn marten
#

Wait a moment I will check u pack and find error

#

@twilit bear hey

#

Can I ask a question

#

Where the manifest U get

twilit bear
#

wiki

dawn marten
#

U use the module version incorrectly

twilit bear
#

oh

dawn marten
#

"Maybe"

#

Wait I will send the revamped pack for you

dawn marten
twilit bear
#

oh. Still doesn't work.

dawn marten
#

Send the screenshot for the error text

twilit bear
#

ok, 1min

dawn marten
#

Umm

#

I nerver see this error but I will find the reason

#

Sry I have no idea of this error ,U can go to ask #1067535608660107284

twilit bear
#

Alr. Thanks!

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

const prefix = '!';

world.beforeEvents.chatSend.subscribe((eventData) => {
    const sender = eventData.sender;
    const message = eventData.message;

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

    eventData.cancel = true;
    
    switch (message) {
        case 'gmc':
            sender.setGameMode('creative');
            sender.onScreenDisplay.setTitle(' ', { subtitle: '§a§lCreative' });
            sender.playSound('random.toast');
            break;
        case 'gms':
            sender.setGameMode('survival');
            sender.onScreenDisplay.setTitle(' ', { subtitle: '§4§lSurvival' });
            sender.playSound('random.toast');
            break;
        case 'gmsp':
            sender.setGameMode('spectator');
            sender.onScreenDisplay.setTitle(' ', { subtitle: '§7§lSpectator' });
            sender.playSound('random.toast');
            break;
        default: break;
    }
});
dawn marten
main galleon
#

I know, was just making code better.

dawn marten