#custom rank command

1 messages · Page 1 of 1 (latest)

cerulean obsidian
#

im trying to make a custom rank command. it's supposed to look like this: !rank-add playername rank, where playername is the name of the player, and rank is the rank im adding. Is anyone able to help?

#

I have a bit of code for it,

case "rank-add": {
                    const player = world.getPlayers({ name: event.message.split(" ").slice(1).join(" ") })[0];
                    if (player === undefined) return void event.sender.sendMessage(`§cPlayer not found`);
                    player.addTag(`Rank:${event.message}`)
                }; break;
#

this gets rid of the ! and tests for the player, but im not sure how to get rid of everything after the player name

peak cargo
#

ill try

#

i hope ths works

#

@cerulean obsidian


world.beforeEvents.chatSend.subscribe(({ message, player, cancel }) => {
    if (message.startsWith('!')) {
        cancel = true;
        const args = message.split(/"[^"]+"|[\s]+/)
        const cmd = args.shift().slice(1); // 1 = prefix length
    
        // usage: !rank-add "player name" "rank name"
        // use "" when arguments contains space

        switch (cmd) {
            case "rank-add": {
                const player = world.getPlayers({ name: args[0] })[0];
                if (player === undefined) return void player.sendMessage(`§cPlayer not found`);
                const rank = `Rank:${args[1]}`;
                if (player.hasTag(rank)) {
                    player.sendMessage(`§cPlayer already owns this rank`);
                } else {
                    system.run(() => { player.addTag(rank); });
                }
                break;
            };

            case "rank-remove": {
                const player = world.getPlayers({ name: args[0] })[0];
                if (player === undefined) return void player.sendMessage(`§cPlayer not found`);
                const rank = `Rank:${args[1]}`;
                if (!player.hasTag(rank)) {
                    player.sendMessage(`§cPlayer don't owns this rank`);
                } else {
                    system.run(() => { player.removeTag(rank); });
                }
                break;
            };

            default: {
                player.sendMessage(`§cCommand not found`);
                break;
            }
        }
    }
})
cedar pilot
#

ju strikes again

peak cargo
#

wait i got a mistake

cerulean obsidian
#

oh no

peak cargo
#

ill fix it wait

cerulean obsidian
#

also mines in world.beforeEvents.chatSend.subscribe(function(event) {}) am i going to need to change that

peak cargo
#

its okay

#

so:

#
world.beforeEvents.chatSend.subscribe(({ message, player, cancel }) => {
    if (message.startsWith('!')) {
        cancel = true;
        const args = message.match(/"[^"]+"|[^\s]+/g).map((item) => {
            if (item.startsWith('"') && item.endsWith('"')) {
                return item.slice(1, -1);
            }
            return item;
        })
        
        const cmd = args.shift().slice(1); // 1 = prefix length
    
        // usage: !rank-add "player name" "rank name"
        // use "" when arguments contains space

        switch (cmd) {
            case "rank-add": {
                const player = world.getPlayers({ name: args[0] })[0];
                if (player === undefined) return void player.sendMessage(`§cPlayer not found`);
                const rank = `Rank:${args[1]}`;
                if (player.hasTag(rank)) {
                    player.sendMessage(`§cPlayer already owns this rank`);
                } else {
                    system.run(() => { player.addTag(rank); });
                }
                break;
            };

            case "rank-remove": {
                const player = world.getPlayers({ name: args[0] })[0];
                if (player === undefined) return void player.sendMessage(`§cPlayer not found`);
                const rank = `Rank:${args[1]}`;
                if (!player.hasTag(rank)) {
                    player.sendMessage(`§cPlayer don't owns this rank`);
                } else {
                    system.run(() => { player.removeTag(rank); });
                }
                break;
            };

            default: {
                player.sendMessage(`§cCommand not found`);
                break;
            }
        }
    }
})```
#

here

cerulean obsidian
#

man you got me lost at args = lol

peak cargo
#

😄

#

it just splits a message:

#
// input: !rank-add "playerWith this name 221829" "Best Rank Ever" =>
// cmd = "rank-add"
// args = ["playerWith this name"221829", "Best Rank Ever"]
cerulean obsidian
peak cargo
#

uhhh let me do that

cerulean obsidian
#

alright thanks lol

#

i was working on a code for some guy and he gave me like 200 things to do :/

peak cargo
#

xd

#

a command builder would be better for that

cerulean obsidian
#

its not just custom commands i have to make alot of entities that do stuff like crates

peak cargo
#

oaky oaky

cerulean obsidian
#

lol

peak cargo
#

sooo

peak cargo
cedar pilot
#

nah

#

just watching posts lol

peak cargo
#

ahh okay

cerulean obsidian
#

ima put it in a new pack so i dont mess anything up lol

peak cargo
#

sure

#

actually you should do that, i dnt know if my code is working cuz i only write it and didnt tested it

cerulean obsidian
#

my worst enemy has appeared

peak cargo
#

wtf

#

uuhmmm

#

wich version are you ussing

#

in the manifest

cerulean obsidian
#

1.20

#

and 1.0.0

#

and 1.11.0-beta

peak cargo
#

uhmmm

#

okay okay

#

weird

cerulean obsidian
#
{
    "format_version": 2,
    "header": {
        "name": "test",
        "description": "Custom Commands for Nebula factions",
        "uuid": "b5e239f5-195c-48c9-88e7-b4b6a5629932",
        "min_engine_version": [1, 20, 0],
        "version": [1, 0, 0]
    },
    "modules": [
        {
            "description": "Behavior Pack Module",
            "type": "data",
            "uuid": "86a475a8-306f-4f87-9b6e-2554637b77da",
            "version": [1, 0, 0]
        },
        {
            "description": "Gametest Module",
            "type": "script",
            "language": "javascript",
            "entry": "scripts/message.js",
            "uuid": "04924889-bf41-46cb-b427-0854ed6449ec",
            "version": [0, 1, 0]
        }
    ],
    "dependencies": [
        {
            "module_name": "@minecraft/server",
            "version": "1.11.0-beta"
        }
  ]
}
#

i just copied it and changed the uuid

peak cargo
#

okay <player>.isOp() isnt available then

#

add this:

#
Player.prototype.isOp = function () {
    return this.hasTag('isOp');
}
#

then the admin just needs the tag isOp

cerulean obsidian
#

replace this with it?

peak cargo
#

no jus add this function to your code anywhere

#

i added a method to player

#

so you can just call isOp()

#

brb

cerulean obsidian
verbal trench
#

Import the player

#
import  { Player } from "@minecraft/server";

To use the prototype

cerulean obsidian
verbal trench
#

Player is undefined

#

What do you have in the event

cerulean obsidian
#

Player.prototype.isOp = function () {
return this.hasTag('isOp');
}

verbal trench
#

Oh nah

#

Not this

#

I meant chatSend event

#

Yall have it player when its called sender

cerulean obsidian
#
event.sender.sendMessage(`§i${player.name} muted.`);

example from my code

#

im just following this man idk what im doing

#

lol

#
world.beforeEvents.chatSend.subscribe(function (event) {

this is how i made the chatSend

verbal trench
#

He put it player directly

#

Its called sender

cerulean obsidian
#
world.beforeEvents.chatSend.subscribe(({ message, player, cancel }) => {

should i change player to sender?

verbal trench
#

Ye

peak cargo
#

ohh i didnt seen it mb

verbal trench
#

But

#

That will also break the scripts also yall have player defined twice

peak cargo
#
world.beforeEvents.chatSend.subscribe(({ message, sender: player, cancel }) => {
#

like that

verbal trench
#

Ye

peak cargo
#

or was it```js
world.beforeEvents.chatSend.subscribe(({ message, player: sender, cancel }) => {

verbal trench
#

Idk

#

Test and find out ig

#

I think its sender: player

peak cargo
#

same

#

its sender: playeer

verbal trench
#

Also there is a const within each case even tho player is defined up

peak cargo
#

btw brb

verbal trench
#

K

cerulean obsidian
#

alright it works now thanks

#

i just gotta make some tweaks to it

verbal trench
#

You're welcome

cerulean obsidian
#

cus it sends chat 2 times xD

verbal trench
#

Just do cancel = true;

cerulean obsidian
#

yeah i know

#

I put cancel its not canceling AAHHH

verbal trench
#

Thats why i tend to get event as data and access stuff individually from it

peak cargo
#

then: data.cancel = true

#

and only get data

cerulean obsidian
#

only get data here? ```js
world.beforeEvents.chatSend.subscribe(({ message, sender: player, cancel }) => {

#

youve lost me

#

rip

#

bro left me on my own lol

verbal trench
#
world.beforeEvents.chatSend.subscribe((data) => {
  let player = data.sender, message = data.message;
/*rest of code like normal, just use ``data.cancel = true;`` instead of ``cancel = true;``*/
cerulean obsidian
#

aight

#

didnt work

verbal trench
#

Wait

#

Send ur current script

cerulean obsidian
verbal trench
#

No

#

Bruh no

cerulean obsidian
#

what

verbal trench
#

Here

cerulean obsidian
#

wow what did you do

carmine lake
#

@verbal trench wtf is this

verbal trench
#

Its @catgirlsbest that made it

carmine lake
#

it overrides the default isOp function in gametest

verbal trench
#

Yeah ik

carmine lake
#

so then

#

why did you use it

verbal trench
#

Not my problem

#

I didn't use it

#

I didn't make the code

#

It was all ju

cerulean obsidian
#

yeah

verbal trench
#

I just changed the {} in the event to data

#

And used it later

carmine lake
cerulean obsidian
#

idek what that does so

carmine lake
# cerulean obsidian idek what that does so

in gametest there is a native function that checks if a player is operator (player.isOp()). They override this function with their own that makes it check if they have a tag called "isOp".

#

which is fundementally stupid

verbal trench
#

Honestly just do isOP

carmine lake
#

like, really stupid

verbal trench
#
Player.prototype.isOP = function () {
    return this.hasTag('isOp');
}
carmine lake
#

thats still really pointless

verbal trench
carmine lake
#

lets say theoretically you needed that

verbal trench
carmine lake
#

why not just do this.hasTag("isOp") where you need it

#

rather than creating an entire one line function

verbal trench
#

I KNOW I KNOW

carmine lake
#

ur chill dw im just complaining to complain

#

just the actual fucking brainrot from scripting api community sometimes

verbal trench
cerulean obsidian
#

lol

peak cargo
carmine lake
#

i think the worse iv seen so far from this community is from jayly a while back when they did

function isOp(player) {
return player.isOp()
}

function isNotOp(player) {
return !player.isOp()
}

cerulean obsidian
#

xsku just went off on you lol

peak cargo
#

@carmine lake so player.isOp() didnt worked so i added it as a player method

carmine lake
#

still that doesnt justify modifying the class prototype

#

not only is it bad practice in general to modify prototypes

#

having it be its own function is still completely and utterly useless

peak cargo
#

"i didnt" it is working now, therefore player was undefined and thats why it throwed an error

carmine lake
#

considering its one short line

peak cargo
#

i forgot to say, isOp() works

#

@cerulean obsidian you can use isOp() again, it shouldnt throw an error if you have player defined

carmine lake
#

💀

#

your function doesnt even solve the problem it tried to solve

#

as player would still be undefined

#

thus yours would still error

#

lmao

peak cargo
#

why should my function throw an error?

verbal trench
carmine lake
#

unless it was referring to something else

peak cargo
carmine lake
#

alright yeah that makes sense, miscommunication

#

but still that functions existance in general lol

peak cargo
#

kinda mb cuz i explained it very worse

cerulean obsidian
#

well it works now

verbal trench
#

player.hasTag('staff'): 🗿

verbal trench
peak cargo
#

should work

verbal trench
#

Won't work*

peak cargo
#

cuz

verbal trench
#

It wont throw error

#

But it won't cancel the message

peak cargo
#

so it works

#

ohh thta again

#

well wait

cerulean obsidian
#

im using the data thingy now so its fixed

verbal trench
#

I already fixed it for him bruh

peak cargo
#

now

#

ohh then nvm*

#

i should read about objects again

cerulean obsidian
#

i should learn more js

peak cargo
#

ohh i know my mistake now tf

cerulean obsidian
#

and watever this is

#

message.match(/"[^"]+"|[^\s]+/g).map((item) => {
if (item.startsWith('"') && item.endsWith('"')) {
return item.slice(1, -1);

#

fried my brain

peak cargo
#

regex and methods

verbal trench
#

Bruh all you did was change my thing from ```js
let player = data.sender, message = data.message;
data.cancel = true;

To
```js
let { player, message } = data;
data.cancel = true;

I have no idea what it does

#

Like bruh

#

Its the same

peak cargo
#

yes

#

i just did it like that

verbal trench
#

Preferences huh

peak cargo
#

@cerulean obsidian

  • RegExp
  • String methods - match | startsWith | endsWith | slice
  • array method - map
cerulean obsidian
#

something i should know and will also never look into lol

peak cargo
#

RegExp is very useful

verbal trench
peak cargo
#

same

cerulean obsidian
#

aight

carmine lake
#

as it creates more varaibles that the javascript gc has to clean up

verbal trench
#

Yeah ik

#

Minato already told me that

#

But honestly I'd use it anyway

peak cargo
#
const myMap = new Map();

myMap.set('keyName', 'anyValue')
myMap.get('keyName') // returns string: anyValue
myMap.has('keyName') // returns boolean: true

myMap.delete('keyName') // returns boolean: true

myMap.get('keyName') // returns undefined: undefined
myMap.has('keyName') // returns boolean: false

myMap.size // returns number: 0
myMap.clear() // clears it
myMap.forEach((value, key, map) => { /* ... */ })

for (const [key, value] of myMap) {
    // idk if this is working
}```