Skip to content

Commit d48945f

Browse files
committed
Update functions.md
Also might include a fix for the diff language not being detected correctly
1 parent 2873d18 commit d48945f

File tree

6 files changed

+83
-23
lines changed

6 files changed

+83
-23
lines changed

docs/.vitepress/config.mts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ const vitepressOptions: UserConfig = {
113113
}]
114114
}).then(_ => {
115115
})
116-
shiki.loadLanguage({
117-
name: 'diff',
118-
scopeName: 'source.diff'
119-
}).then(_ => {
120-
})
121116
},
122117
config: ( md) => {
123118
tabsPlugin(md);

docs/en/create-commands/functions-and-tags/functions.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
order: 1
3+
preferences: ["paper-spigot"]
34
authors:
45
- DerEchtePilz
56
- willkroboth
@@ -17,29 +18,58 @@ The CommandAPI has support to use Minecraft's [functions](https://minecraft.wiki
1718

1819
Minecraft 1.16+ changes the way that datapacks are loaded on the server, so that they load before plugins are enabled. This means that non-vanilla commands that are declared in functions and tags will be detected as invalid, causing the server to throw a lot of errors at the very start.
1920

21+
<div class="paper">
22+
23+
To make commands available in datapacks, they have to be registered at the right place. More on that later.
24+
25+
</div>
26+
<div class="spigot">
27+
2028
The CommandAPI reloads datapacks once the server has finished loading using all declared commands, therefore **the error messages at the start of the server can be ignored**.
2129

30+
</div>
31+
2232
:::
2333

2434
## Using custom commands in functions
2535

26-
In order to use a command from your plugin in a `.mcfunction` file, you must register your command in your plugin's `onLoad()` method, instead of the `onEnable()` method. Failure to do so will not allow the command to be registered for Minecraft functions, causing the function file to fail to load during the server startup phase.
36+
<div class="paper">
37+
38+
In order to use a command from your plugin in a `.mcfunction` file, you must register your command in your plugin's `bootstrap(BootstrapContext)` method, instead of the `onLoad()`/`onEnable()` method. Failure to do so will not allow the command to be registered for Minecraft functions, causing the function file to fail to load during the server startup phase.
2739

2840
:::info
2941

30-
In short, if you want to register a command which can be used in Minecraft functions, register it in your plugin's `onLoad()` method.
42+
In short, if you want to register a command which can be used in Minecraft functions, register it in your plugin's `bootstrap(BootstrapContext)` method.
3143

3244
:::
3345

46+
</div>
47+
3448
::::tip Example – Registering command for use in a function
3549

36-
Say we have a command `/killall` that simply kills all entities in all worlds on the server. If we were to register this in our `onLoad()` method, this would allow us to use the `/killall` command in Minecraft functions and tags.
50+
<div class="paper">
51+
52+
Say we have a command `/killall` that simply kills all entities in all worlds on the server. To make it available in a function, we want to register the command in the `bootstrap(BootstrapContext)` method.
3753

3854
:::tabs
3955
===Java
40-
<<< @/../reference-code/bukkit/src/main/java/createcommands/functionsandtags/Functions.java#functionsExample
56+
<<< @/../reference-code/paper/src/main/java/createcommands/functionsandtags/Functions.java#functionsExample
4157
===Kotlin
42-
<<< @/../reference-code/bukkit/src/main/kotlin/createcommands/functionsandtags/Functions.kt#functionsExample
58+
<<< @/../reference-code/paper/src/main/kotlin/createcommands/functionsandtags/Functions.kt#functionsExample
4359
:::
4460

61+
</div>
62+
<div class="spigot">
63+
64+
Say we have a command `/killall` that simply kills all entities in all worlds on the server. It doesn't matter where we register to make the command available in a function, but we'll just use the `onLoad()` method here.
65+
66+
:::tabs
67+
===Java
68+
<<< @/../reference-code/spigot/src/main/java/createcommands/functionsandtags/Functions.java#functionsExample
69+
===Kotlin
70+
<<< @/../reference-code/spigot/src/main/kotlin/createcommands/functionsandtags/Functions.kt#functionsExample
71+
:::
72+
73+
</div>
74+
4575
::::
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package createcommands.functionsandtags;
2+
3+
import dev.jorel.commandapi.CommandAPICommand;
4+
import io.papermc.paper.plugin.bootstrap.BootstrapContext;
5+
import io.papermc.paper.plugin.bootstrap.PluginBootstrap;
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.plugin.java.JavaPlugin;
8+
9+
class Functions {
10+
// #region functionsExample
11+
public class MainBoostrap implements PluginBootstrap {
12+
@Override
13+
public void bootstrap(BootstrapContext context) {
14+
// Commands which will be used in Minecraft functions are registered here
15+
16+
new CommandAPICommand("killall")
17+
.executes((sender, args) -> {
18+
// Kills all enemies in all worlds
19+
Bukkit.getWorlds().forEach(w -> w.getLivingEntities().forEach(e -> e.setHealth(0)));
20+
})
21+
.register();
22+
}
23+
}
24+
// #endregion functionsExample
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package createcommands.functionsandtags
2+
3+
import dev.jorel.commandapi.CommandAPICommand
4+
import dev.jorel.commandapi.executors.CommandExecutor
5+
import io.papermc.paper.plugin.bootstrap.BootstrapContext
6+
import io.papermc.paper.plugin.bootstrap.PluginBootstrap
7+
import org.bukkit.Bukkit
8+
import org.bukkit.plugin.java.JavaPlugin
9+
10+
// #region functionsExample
11+
class MainBootstrap : PluginBootstrap {
12+
override fun bootstrap(context: BootstrapContext) {
13+
// Commands which will be used in Minecraft functions are registered here
14+
15+
CommandAPICommand("killall")
16+
.executes(CommandExecutor { _, _ ->
17+
// Kills all enemies in all worlds
18+
Bukkit.getWorlds().forEach { world -> world.livingEntities.forEach { entity -> entity.health = 0.0 } }
19+
})
20+
.register()
21+
}
22+
}
23+
// #endregion functionsExample

reference-code/bukkit/src/main/java/createcommands/functionsandtags/Functions.java renamed to reference-code/spigot/src/main/java/createcommands/functionsandtags/Functions.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@ class Functions {
99
public class Main extends JavaPlugin {
1010
@Override
1111
public void onLoad() {
12-
// Commands which will be used in Minecraft functions are registered here
13-
1412
new CommandAPICommand("killall")
1513
.executes((sender, args) -> {
1614
// Kills all enemies in all worlds
1715
Bukkit.getWorlds().forEach(w -> w.getLivingEntities().forEach(e -> e.setHealth(0)));
1816
})
1917
.register();
2018
}
21-
22-
@Override
23-
public void onEnable() {
24-
// Register all other commands here
25-
}
2619
}
2720
// #endregion functionsExample
2821
}

reference-code/bukkit/src/main/kotlin/createcommands/functionsandtags/Functions.kt renamed to reference-code/spigot/src/main/kotlin/createcommands/functionsandtags/Functions.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,12 @@ import org.bukkit.plugin.java.JavaPlugin
88
// #region functionsExample
99
class Main : JavaPlugin() {
1010
override fun onLoad() {
11-
// Commands which will be used in Minecraft functions are registered here
12-
1311
CommandAPICommand("killall")
1412
.executes(CommandExecutor { _, _ ->
1513
// Kills all enemies in all worlds
1614
Bukkit.getWorlds().forEach { world -> world.livingEntities.forEach { entity -> entity.health = 0.0 } }
1715
})
1816
.register()
1917
}
20-
21-
override fun onEnable() {
22-
// Register all other commands here
23-
}
2418
}
2519
// #endregion functionsExample

0 commit comments

Comments
 (0)