Running Commands
Preface
Sometimes, you might want to run a command (such as /tell @a Hi!
), in your code.
Most always, there is better method, but sometimes, you just don't want to learn more complicated topics, and just run a command.
Basic Usage
The most basic usage would be to call runCommand()
from a server
class.
Utils.server.runCommand(`tell @a Hi!`)
If this command returns a message (usually an error) that is normally placed chat, it will be logged. This is not desired outside of debugging situations.
So instead you can use the following to not log these messages.
Utils.server.runCommandSilent(`tell @a Hi!`)
If the server is not loaded at the time this is ran, then the code will not work.
Although you can use player.runCommandSilent()
, it is not recommend as the command runs with the players permission level.
Using the execute command
Commands are ran in the default dimension (the overworld usually) at 0, 0, 0
To get around this, you can use the execute command:
//This example makes a bedrock box around creepers when they spawn
onEvent('entity.spawned', event => {
if (event.entity.type != "minecraft:creeper") return // the following code only runs when creepers are spawned
event.server.runCommandSilent(`execute in ${event.entity.level.dimension} positioned ${event.entity.x} ${event.entity.y} ${event.entity.z} run fill ~-1 ~-1 ~-1 ~1 ~2 ~1 bedrock hollow`)
})