Loading Commands

easy

Sometimes when creating an add-on you need to run commands on a players first join or on the first time a world has been loaded. We will go through both.

Player Join

In order to check if a new player has joined the world we need to run commands every tick. For this we use tick.json.

Here is what your file could look like:

BP/functions/tick.jsonCopy
{
    "values": [
        "load_commands"
    ]
}
1
2
3
4
5

load_commands is name of our function that is going to check for new players.

BP/functions/load_commands.mcfunctionCopy
msg @a[tag=!joined] Welcome
tag @a add joined
1
2

Here the player gets a welcome message. You can swap it out for anything you'd like that targets a player without the joined tag such as teleporting them somewhere, giving them something or spawning an entity. You can also make a delay with scoreboard and tags so player is fully loaded into the world and will see it.

First World Load

Unlike entities, worlds can not be applied tags. Instead we have to use scoreboards. Because of that, it is a bit more complicated.

BP/functions/tick.jsonCopy
{
    "values": [
        "load_check"
    ]
}
1
2
3
4
5
BP/functions/load_check.mcfunctionCopy
execute @a[c=1] ~ ~ ~ function load_commands
1

Here, we are running it from one player, this could be from any entity though.

BP/functions/load_commands.mcfunctionCopy
scoreboard objectives add loaded dummy
scoreboard players add global loaded 0
scoreboard players operation @s loaded = global loaded
execute @s[scores={loaded=0}] ~ ~ ~ say The world is loaded!
scoreboard players set global loaded 1
1
2
3
4
5

The above function seems rather complicated, but it is actually quite simple. The first two lines check that a scoreboard called loaded has been created and that a player called global exists on it. The third line copies the value that the player global has. The fourth line executes commands after the world is loaded, and the last line sets the value of loaded to 1.

Contributors

MedicalJewel105HatchibombotarSmokeyStack