#custom rank command
1 messages · Page 1 of 1 (latest)
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
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;
}
}
}
})
ju strikes again
wait i got a mistake
oh no
ill fix it wait
also mines in world.beforeEvents.chatSend.subscribe(function(event) {}) am i going to need to change that
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
man you got me lost at args = lol
😄
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"]
where would i put it in this?
uhhh let me do that
alright thanks lol
i was working on a code for some guy and he gave me like 200 things to do :/
its not just custom commands i have to make alot of entities that do stuff like crates
oaky oaky
lol
yee, you need help too?
ahh okay
ima put it in a new pack so i dont mess anything up lol
sure
actually you should do that, i dnt know if my code is working cuz i only write it and didnt tested it
my worst enemy has appeared
{
"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
okay <player>.isOp() isnt available then
add this:
Player.prototype.isOp = function () {
return this.hasTag('isOp');
}
then the admin just needs the tag isOp
replace this with it?
no jus add this function to your code anywhere
i added a method to player
so you can just call isOp()
brb
Import the player
import { Player } from "@minecraft/server";
To use the prototype
Player.prototype.isOp = function () {
return this.hasTag('isOp');
}
Oh nah
Not this
I meant chatSend event
Yall have it player when its called sender
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
world.beforeEvents.chatSend.subscribe(({ message, player, cancel }) => {
should i change player to sender?
Ye
ohh i didnt seen it mb
world.beforeEvents.chatSend.subscribe(({ message, sender: player, cancel }) => {
like that
Ye
or was it```js
world.beforeEvents.chatSend.subscribe(({ message, player: sender, cancel }) => {
Also there is a const within each case even tho player is defined up
btw brb
K
You're welcome
cus it sends chat 2 times xD
Just do cancel = true;
Thats why i tend to get event as data and access stuff individually from it
only get data here? ```js
world.beforeEvents.chatSend.subscribe(({ message, sender: player, cancel }) => {
youve lost me
rip
bro left me on my own lol
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
what
wow what did you do
@verbal trench wtf is this
Not my shit man
Its @catgirlsbest that made it
it overrides the default isOp function in gametest
Yeah ik
yeah
@peak cargo justify this please
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
Honestly just do isOP
like, really stupid
Player.prototype.isOP = function () {
return this.hasTag('isOp');
}
thats still really pointless
@cerulean obsidian just use this instead
lets say theoretically you needed that
Ik
why not just do this.hasTag("isOp") where you need it
rather than creating an entire one line function
I KNOW I KNOW
ur chill dw im just complaining to complain
just the actual fucking brainrot from scripting api community sometimes
Nah its ok
lol
sorry i was afk
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()
}
xsku just went off on you lol
@carmine lake so player.isOp() didnt worked so i added it as a player method
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
"i didnt" it is working now, therefore player was undefined and thats why it throwed an error
Nahhhh jayly??????
considering its one short line
i forgot to say, isOp() works
@cerulean obsidian you can use isOp() again, it shouldnt throw an error if you have player defined
💀
your function doesnt even solve the problem it tried to solve
as player would still be undefined
thus yours would still error
lmao
why should my function throw an error?
Bruh you said that after i told him the fix and i dont know why isOp fr i was just spectating cuz i dont like to edit or make scripts for others
you just said this
unless it was referring to something else
it was undefined because i accidantely used player instead of sender
alright yeah that makes sense, miscommunication
but still that functions existance in general lol
kinda mb cuz i explained it very worse
well it works now
player.hasTag('staff'): 🗿
Good
Won't work*
cuz
im using the data thingy now so its fixed
I already fixed it for him bruh
i should learn more js
ohh i know my mistake now tf
and watever this is
message.match(/"[^"]+"|[^\s]+/g).map((item) => {
if (item.startsWith('"') && item.endsWith('"')) {
return item.slice(1, -1);
fried my brain
regex and methods
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
Preferences huh
@cerulean obsidian
- RegExp
- String methods - match | startsWith | endsWith | slice
- array method - map
something i should know and will also never look into lol
RegExp is very useful
I'll send u some good tut links to help ya
same
aight
hot take but destructering either way is bad
as it creates more varaibles that the javascript gc has to clean up
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
}```