Skip to content

Commit dfd369d

Browse files
committed
feat: Unregistration page
1 parent 808202d commit dfd369d

File tree

7 files changed

+299
-60
lines changed

7 files changed

+299
-60
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Types
3+
order: 5
4+
---

docs/en/create-commands/registration.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@ I think the easiest way to explain it is with an example:
1515

1616
:::tabs
1717
===Java
18-
```java
19-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandRegistration1}}
20-
```
18+
<<< @/../reference-code/src/main/java/createcommands/Registration.java#registrationExample
2119
===Kotlin
22-
```kotlin
23-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandRegistration1}}
24-
```
20+
<<< @/../reference-code/src/main/kotlin/createcommands/Registration.kt#registrationExample
2521
:::
2622

2723
- First, we create a new `CommandAPICommand`, with the name of the command that the sender must enter to run it.

docs/en/create-commands/unregistration.md

Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,9 @@ Since this command exists in the Bukkit CommandMap, we'll need to use `CommandAP
6363

6464
:::tabs
6565
===Java
66-
```java
67-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandUnregistration1}}
68-
```
66+
<<< @/../reference-code/src/main/java/createcommands/Unregistration.java#unregisterBukkitExample
6967
===Kotlin
70-
```kotlin
71-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandUnregistration1}}
72-
```
68+
<<< @/../reference-code/src/main/kotlin/createcommands/Unregistration.kt#unregisterBukkitExample
7369
:::
7470

7571
With this plugin, executing `/version` or `/bukkit:version` will give the unknown command message. Note that aliases like `/ver` and its namespaced version `/bukkit:ver` will still work. To remove aliases as well, you need to unregister each as its own command. For, `/ver`, that would mean calling `CommandAPIBukkit.unregister("ver", true, true)`.
@@ -82,13 +78,9 @@ Since this command exists in the Vanilla CommandDispatcher, we can use `CommandA
8278

8379
:::tabs
8480
===Java
85-
```java
86-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandUnregistration2}}
87-
```
81+
<<< @/../reference-code/src/main/java/createcommands/Unregistration.java#unregisterVanillaExample
8882
===Kotlin
89-
```kotlin
90-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandUnregistration2}}
91-
```
83+
<<< @/../reference-code/src/main/kotlin/createcommands/Unregistration.kt#unregisterVanillaExample
9284
:::
9385

9486
With this code, executing `/gamemode` will give the unknown command exception as expected. However, even though `unregisterNamespaces` was `false`, `/minecraft:gamemode` can also not be run. This happens because Vanilla commands are given their namespace in step 6, after our plugin has removed `/gamemode`.
@@ -101,13 +93,9 @@ To replace a command, first unregister the original command, then register a new
10193

10294
:::tabs
10395
===Java
104-
```java
105-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandUnregistration3}}
106-
```
96+
<<< @/../reference-code/src/main/java/createcommands/Unregistration.java#unregisterVanillaAndReplaceExample
10797
===Kotlin
108-
```kotlin
109-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandUnregistration3}}
110-
```
98+
<<< @/../reference-code/src/main/kotlin/createcommands/Unregistration.kt#unregisterVanillaAndReplaceExample
11199
:::
112100

113101
Now, when `/gamemode` is executed, it will use the new implementation defined using the CommandAPI.
@@ -130,13 +118,9 @@ Since plugin commands are stored in the Bukkit CommandMap, we need to use `Comma
130118

131119
:::tabs
132120
===Java
133-
```java
134-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandUnregistration4}}
135-
```
121+
<<< @/../reference-code/src/main/java/createcommands/Unregistration.java#unregisterPluginExample
136122
===Kotlin
137-
```kotlin
138-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandUnregistration4}}
139-
```
123+
<<< @/../reference-code/src/main/kotlin/createcommands/Unregistration.kt#unregisterPluginExample
140124
:::
141125

142126
Executing `/luckperms` will work as normal, but `/luckperms:luckperms` will give the unknown command message.
@@ -179,13 +163,9 @@ In summary, we will unregister the `/break` command in our plugin's `onEnable`.
179163

180164
:::tabs
181165
===Java
182-
```java
183-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandUnregistration5}}
184-
```
166+
<<< @/../reference-code/src/main/java/createcommands/Unregistration.java#unregisterCommandAPIExample
185167
===Kotlin
186-
```kotlin
187-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandUnregistration5}}
188-
```
168+
<<< @/../reference-code/src/main/kotlin/createcommands/Unregistration.kt#unregisterCommandAPIExample
189169
:::
190170

191171
Now, when you try to execute `/break`, you will just get the unknown command message as if it never existed.
@@ -200,13 +180,9 @@ Since `/help` is in the Bukkit CommandMap, we need to use `CommandAPIBukkit#unre
200180

201181
:::tabs
202182
===Java
203-
```java
204-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandUnregistration6}}
205-
```
183+
<<< @/../reference-code/src/main/java/createcommands/Unregistration.java#unregisterBukkitHelpExample
206184
===Kotlin
207-
```kotlin
208-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandUnregistration6}}
209-
```
185+
<<< @/../reference-code/src/main/kotlin/createcommands/Unregistration.kt#unregisterBukkitHelpExample
210186
:::
211187

212188
Funnily, if you try to execute `/help`, the server will still tell you: `Unknown command. Type "/help" for help.`. Luckily, `unregisterNamespaces` was `false`, so you can still use `/bukkit:help` to figure out your problem.
@@ -223,13 +199,9 @@ Finally, `unregisterNamespaces` should be `false`, and since that's the default
223199

224200
:::tabs
225201
===Java
226-
```java
227-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandUnregistration7}}
228-
```
202+
<<< @/../reference-code/src/main/java/createcommands/Unregistration.java#unregisterVanillaNamespaceOnlyExample
229203
===Kotlin
230-
```kotlin
231-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandUnregistration7}}
232-
```
204+
<<< @/../reference-code/src/main/kotlin/createcommands/Unregistration.kt#unregisterVanillaNamespaceOnlyExample
233205
:::
234206

235207
With this code, `/gamemode` will execute as normal, but `/minecraft:gamemode` will give the unknown command message.
@@ -240,13 +212,9 @@ Doing the opposite action here -- only unregistering `/gamemode` but keeping `/m
240212

241213
:::tabs
242214
===Java
243-
```java
244-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandUnregistration8}}
245-
```
215+
<<< @/../reference-code/src/main/java/createcommands/Unregistration.java#unregisterDelayedVanillaBadExample
246216
===Kotlin
247-
```kotlin
248-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandUnregistration8}}
249-
```
217+
<<< @/../reference-code/src/main/kotlin/createcommands/Unregistration.kt#unregisterDelayedVanillaBadExample
250218
:::
251219

252220
The expected outcome of this code is that `/minecraft:gamemode` would work as expected, and `/gamemode` would give the command not found message. However, that is only true for the player's commands. If you try to use `/minecraft:gamemode` in the console, it *will not work* properly. Specifically, while you can tab-complete the command's label, `minecraft:gamemode` the command's arguments will not have any suggestions. If you try to execute `/minecraft:gamemode` in the console, it will always tell you your command is unknown or incomplete.
@@ -255,13 +223,9 @@ The main point is that if you ever try to unregister a Vanilla command after the
255223

256224
:::tabs
257225
===Java
258-
```java
259-
// todo {{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandUnregistration9}}
260-
```
226+
<<< @/../reference-code/src/main/java/createcommands/Unregistration.java#unregisterDelayedVanillaGoodExample
261227
===Kotlin
262-
```kotlin
263-
// todo {{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandUnregistration9}}
264-
```
228+
<<< @/../reference-code/src/main/kotlin/createcommands/Unregistration.kt#unregisterDelayedVanillaGoodExample
265229
:::
266230

267231
::::
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package createcommands;
2+
3+
import dev.jorel.commandapi.CommandAPICommand;
4+
import dev.jorel.commandapi.CommandPermission;
5+
import dev.jorel.commandapi.arguments.GreedyStringArgument;
6+
import org.bukkit.Bukkit;
7+
8+
class Registration {
9+
{
10+
// #region registrationExample
11+
// Create our command
12+
new CommandAPICommand("broadcastmsg")
13+
.withArguments(new GreedyStringArgument("message")) // The arguments
14+
.withAliases("broadcast", "broadcastmessage") // Command aliases
15+
.withPermission(CommandPermission.OP) // Required permissions
16+
.executes((sender, args) -> {
17+
String message = (String) args.get("message");
18+
Bukkit.getServer().broadcastMessage(message);
19+
})
20+
.register();
21+
// #endregion registrationExample
22+
}
23+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package createcommands;
2+
3+
import dev.jorel.commandapi.CommandAPI;
4+
import dev.jorel.commandapi.CommandAPIBukkit;
5+
import dev.jorel.commandapi.CommandAPICommand;
6+
import dev.jorel.commandapi.arguments.MultiLiteralArgument;
7+
import org.bukkit.plugin.java.JavaPlugin;
8+
import org.bukkit.scheduler.BukkitRunnable;
9+
10+
class Unregistration {
11+
{
12+
new JavaPlugin() {
13+
// #region unregisterBukkitExample
14+
@Override
15+
public void onLoad() {
16+
CommandAPIBukkit.unregister("version", true, true);
17+
}
18+
// #endregion unregisterBukkitExample
19+
};
20+
21+
new JavaPlugin() {
22+
// #region unregisterVanillaExample
23+
@Override
24+
public void onEnable() {
25+
CommandAPI.unregister("gamemode");
26+
}
27+
// #endregion unregisterVanillaExample
28+
};
29+
30+
new JavaPlugin() {
31+
// #region unregisterVanillaAndReplaceExample
32+
@Override
33+
public void onEnable() {
34+
CommandAPI.unregister("gamemode");
35+
36+
// Register our new /gamemode, with survival, creative, adventure and spectator
37+
new CommandAPICommand("gamemode")
38+
.withArguments(new MultiLiteralArgument("gamemodes", "survival", "creative", "adventure", "spectator"))
39+
.executes((sender, args) -> {
40+
// Implementation of our /gamemode command
41+
})
42+
.register();
43+
}
44+
// #endregion unregisterVanillaAndReplaceExample
45+
};
46+
47+
new JavaPlugin() {
48+
// #region unregisterPluginExample
49+
@Override
50+
public void onEnable() {
51+
CommandAPIBukkit.unregister("luckperms:luckperms", false, true);
52+
}
53+
// #endregion unregisterPluginExample
54+
};
55+
56+
new JavaPlugin() {
57+
// #region unregisterCommandAPIExample
58+
@Override
59+
public void onEnable() {
60+
CommandAPI.unregister("break");
61+
}
62+
// #endregion unregisterCommandAPIExample
63+
};
64+
65+
new JavaPlugin() {
66+
// #region unregisterBukkitHelpExample
67+
@Override
68+
public void onEnable() {
69+
new BukkitRunnable() {
70+
@Override
71+
public void run() {
72+
CommandAPIBukkit.unregister("help", false, true);
73+
}
74+
}.runTaskLater(this, 0);
75+
}
76+
// #endregion unregisterBukkitHelpExample
77+
};
78+
79+
new JavaPlugin() {
80+
// #region unregisterVanillaNamespaceOnlyExample
81+
@Override
82+
public void onEnable() {
83+
new BukkitRunnable() {
84+
@Override
85+
public void run() {
86+
CommandAPI.unregister("minecraft:gamemode");
87+
}
88+
}.runTaskLater(this, 0);
89+
}
90+
// #endregion unregisterVanillaNamespaceOnlyExample
91+
};
92+
93+
new JavaPlugin() {
94+
// #region unregisterDelayedVanillaBadExample
95+
// NOT RECOMMENDED
96+
@Override
97+
public void onEnable() {
98+
new BukkitRunnable() {
99+
@Override
100+
public void run() {
101+
CommandAPI.unregister("gamemode");
102+
}
103+
}.runTaskLater(this, 0);
104+
}
105+
// #endregion unregisterDelayedVanillaBadExample
106+
};
107+
108+
new JavaPlugin() {
109+
// #region unregisterDelayedVanillaGoodExample
110+
// NOT RECOMMENDED
111+
@Override
112+
public void onEnable() {
113+
new BukkitRunnable() {
114+
@Override
115+
public void run() {
116+
CommandAPI.unregister("gamemode", true);
117+
}
118+
}.runTaskLater(this, 0);
119+
}
120+
// #endregion unregisterDelayedVanillaGoodExample
121+
};
122+
}
123+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package createcommands
2+
3+
import dev.jorel.commandapi.CommandAPICommand
4+
import dev.jorel.commandapi.CommandPermission
5+
import dev.jorel.commandapi.arguments.GreedyStringArgument
6+
import dev.jorel.commandapi.executors.CommandExecutor
7+
import org.bukkit.Bukkit
8+
9+
fun registrationExample() {
10+
// #region registrationExample
11+
// Create our command
12+
CommandAPICommand("broadcastmsg")
13+
.withArguments(GreedyStringArgument("message")) // The arguments
14+
.withAliases("broadcast", "broadcastmessage") // Command aliases
15+
.withPermission(CommandPermission.OP) // Required permissions
16+
.executes(CommandExecutor { sender, args ->
17+
val message = args["message"] as String
18+
Bukkit.getServer().broadcastMessage(message)
19+
})
20+
.register()
21+
// #endregion registrationExample
22+
}

0 commit comments

Comments
 (0)