Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8826219

Browse files
committed
Document new CommandAPI#unregister behavior
1 parent 200ba45 commit 8826219

File tree

6 files changed

+484
-65
lines changed

6 files changed

+484
-65
lines changed

commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java

Lines changed: 111 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.bukkit.plugin.java.JavaPlugin;
6868
import org.bukkit.potion.PotionEffect;
6969
import org.bukkit.potion.PotionEffectType;
70+
import org.bukkit.scheduler.BukkitRunnable;
7071
import org.bukkit.scoreboard.DisplaySlot;
7172
import org.bukkit.scoreboard.Objective;
7273
import org.bukkit.scoreboard.Scoreboard;
@@ -1393,19 +1394,118 @@ void commandRegistration() {
13931394
})
13941395
.register();
13951396
/* ANCHOR_END: commandRegistration1 */
1397+
}
13961398

1397-
/* ANCHOR: commandRegistration2 */
1398-
// Unregister the gamemode command from the server (by force)
1399-
CommandAPI.unregister("gamemode", true);
1399+
class CommandUnregistration {
1400+
class UnregistrationBukkit extends JavaPlugin {
1401+
/* ANCHOR: commandUnregistrationBukkit */
1402+
@Override
1403+
public void onLoad() {
1404+
CommandAPIBukkit.unregister("version", true, true);
1405+
}
1406+
/* ANCHOR_END: commandUnregistrationBukkit */
1407+
}
14001408

1401-
// Register our new /gamemode, with survival, creative, adventure and spectator
1402-
new CommandAPICommand("gamemode")
1403-
.withArguments(new MultiLiteralArgument("gamemodes", "survival", "creative", "adventure", "spectator"))
1404-
.executes((sender, args) -> {
1405-
// Implementation of our /gamemode command
1406-
})
1407-
.register();
1408-
/* ANCHOR_END: commandRegistration2 */
1409+
class UnregistrationVanilla extends JavaPlugin {
1410+
/* ANCHOR: commandUnregistrationVanilla */
1411+
@Override
1412+
public void onEnable() {
1413+
CommandAPI.unregister("gamemode");
1414+
}
1415+
/* ANCHOR_END: commandUnregistrationVanilla */
1416+
}
1417+
1418+
class UnregistrationReplaceVanilla extends JavaPlugin {
1419+
/* ANCHOR: commandUnregistrationReplaceVanilla */
1420+
@Override
1421+
public void onEnable() {
1422+
CommandAPI.unregister("gamemode");
1423+
1424+
// Register our new /gamemode, with survival, creative, adventure and spectator
1425+
new CommandAPICommand("gamemode")
1426+
.withArguments(new MultiLiteralArgument("gamemodes", "survival", "creative", "adventure", "spectator"))
1427+
.executes((sender, args) -> {
1428+
// Implementation of our /gamemode command
1429+
})
1430+
.register();
1431+
}
1432+
/* ANCHOR_END: commandUnregistrationReplaceVanilla */
1433+
}
1434+
1435+
class UnregistrationPlugin extends JavaPlugin {
1436+
/* ANCHOR: commandUnregistrationPlugin */
1437+
@Override
1438+
public void onEnable() {
1439+
CommandAPIBukkit.unregister("luckperms:luckperms", false, true);
1440+
}
1441+
/* ANCHOR_END: commandUnregistrationPlugin */
1442+
}
1443+
1444+
class UnregistrationCommandAPI extends JavaPlugin {
1445+
/* ANCHOR: commandUnregistrationCommandAPI */
1446+
@Override
1447+
public void onEnable() {
1448+
CommandAPI.unregister("break");
1449+
}
1450+
/* ANCHOR_END: commandUnregistrationCommandAPI */
1451+
}
1452+
1453+
class UnregistrationBukkitHelp extends JavaPlugin {
1454+
/* ANCHOR: commandUnregistrationBukkitHelp */
1455+
@Override
1456+
public void onEnable() {
1457+
new BukkitRunnable() {
1458+
@Override
1459+
public void run() {
1460+
CommandAPIBukkit.unregister("help", false, true);
1461+
}
1462+
}.runTaskLater(this, 0);
1463+
}
1464+
/* ANCHOR_END: commandUnregistrationBukkitHelp */
1465+
}
1466+
1467+
class UnregistrationOnlyVanillaNamespace extends JavaPlugin {
1468+
/* ANCHOR: commandUnregistrationOnlyVanillaNamespace */
1469+
@Override
1470+
public void onEnable() {
1471+
new BukkitRunnable() {
1472+
@Override
1473+
public void run() {
1474+
CommandAPI.unregister("minecraft:gamemode");
1475+
}
1476+
}.runTaskLater(this, 0);
1477+
}
1478+
/* ANCHOR_END: commandUnregistrationOnlyVanillaNamespace */
1479+
}
1480+
1481+
class UnregistrationDealyedVanillaBad extends JavaPlugin {
1482+
/* ANCHOR: commandUnregistrationDealyedVanillaBad */
1483+
// NOT RECOMMENDED
1484+
@Override
1485+
public void onEnable() {
1486+
new BukkitRunnable() {
1487+
@Override
1488+
public void run() {
1489+
CommandAPI.unregister("gamemode");
1490+
}
1491+
}.runTaskLater(this, 0);
1492+
}
1493+
/* ANCHOR_END: commandUnregistrationDealyedVanillaBad */
1494+
}
1495+
1496+
class UnregistrationDealyedVanillaBetter extends JavaPlugin {
1497+
/* ANCHOR: commandUnregistrationDealyedVanillaBetter */
1498+
@Override
1499+
public void onEnable() {
1500+
new BukkitRunnable() {
1501+
@Override
1502+
public void run() {
1503+
CommandAPI.unregister("gamemode", true);
1504+
}
1505+
}.runTaskLater(this, 0);
1506+
}
1507+
/* ANCHOR_END: commandUnregistrationDealyedVanillaBetter */
1508+
}
14091509
}
14101510

14111511
class commandTrees extends JavaPlugin {

commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import org.bukkit.metadata.FixedMetadataValue
4343
import org.bukkit.plugin.java.JavaPlugin
4444
import org.bukkit.potion.PotionEffect
4545
import org.bukkit.potion.PotionEffectType
46+
import org.bukkit.scheduler.BukkitRunnable
4647
import org.bukkit.scoreboard.DisplaySlot
4748
import org.bukkit.scoreboard.Objective
4849
import org.bukkit.scoreboard.Team
@@ -1309,19 +1310,105 @@ CommandAPICommand("broadcastmsg")
13091310
})
13101311
.register()
13111312
/* ANCHOR_END: commandRegistration1 */
1313+
}
13121314

1313-
/* ANCHOR: commandRegistration2 */
1314-
// Unregister the gamemode command from the server (by force)
1315-
CommandAPI.unregister("gamemode", true)
1315+
class CommandUnregistration {
1316+
class UnregistrationBukkit : JavaPlugin() {
1317+
/* ANCHOR: commandUnregistrationBukkit */
1318+
override fun onLoad() {
1319+
CommandAPIBukkit.unregister("version", false, true)
1320+
}
1321+
/* ANCHOR_END: commandUnregistrationBukkit */
1322+
}
13161323

1317-
// Register our new /gamemode, with survival, creative, adventure and spectator
1318-
CommandAPICommand("gamemode")
1319-
.withArguments(MultiLiteralArgument("gamemodes", "survival", "creative", "adventure", "spectator"))
1320-
.executes(CommandExecutor { sender, args ->
1321-
// Implementation of our /gamemode command
1322-
})
1323-
.register()
1324-
/* ANCHOR_END: commandRegistration2 */
1324+
class UnregistrationVanilla : JavaPlugin() {
1325+
/* ANCHOR: commandUnregistrationVanilla */
1326+
override fun onEnable() {
1327+
CommandAPI.unregister("gamemode")
1328+
}
1329+
/* ANCHOR_END: commandUnregistrationVanilla */
1330+
}
1331+
1332+
class UnregistrationReplaceVanilla : JavaPlugin() {
1333+
/* ANCHOR: commandUnregistrationReplaceVanilla */
1334+
override fun onEnable() {
1335+
CommandAPI.unregister("gamemode");
1336+
1337+
// Register our new /gamemode, with survival, creative, adventure and spectator
1338+
CommandAPICommand("gamemode")
1339+
.withArguments(MultiLiteralArgument("gamemodes", "survival", "creative", "adventure", "spectator"))
1340+
.executes(CommandExecutor { sender, args ->
1341+
// Implementation of our /gamemode command
1342+
})
1343+
.register()
1344+
}
1345+
/* ANCHOR_END: commandUnregistrationReplaceVanilla */
1346+
}
1347+
1348+
class UnregistrationPlugin : JavaPlugin() {
1349+
/* ANCHOR: commandUnregistrationPlugin */
1350+
override fun onEnable() {
1351+
CommandAPIBukkit.unregister("luckperms:luckperms", false, true)
1352+
}
1353+
/* ANCHOR_END: commandUnregistrationPlugin */
1354+
}
1355+
1356+
class UnregistrationCommandAPI : JavaPlugin() {
1357+
/* ANCHOR: commandUnregistrationCommandAPI */
1358+
override fun onEnable() {
1359+
CommandAPI.unregister("break")
1360+
}
1361+
/* ANCHOR_END: commandUnregistrationCommandAPI */
1362+
}
1363+
1364+
class UnregistrationBukkitHelp : JavaPlugin() {
1365+
/* ANCHOR: commandUnregistrationBukkitHelp */
1366+
override fun onEnable() {
1367+
object : BukkitRunnable() {
1368+
override fun run() {
1369+
CommandAPIBukkit.unregister("help", false, true)
1370+
}
1371+
}.runTaskLater(this, 0)
1372+
}
1373+
/* ANCHOR_END: commandUnregistrationBukkitHelp */
1374+
}
1375+
1376+
class UnregistrationOnlyVanillaNamespace : JavaPlugin() {
1377+
/* ANCHOR: commandUnregistrationOnlyVanillaNamespace */
1378+
override fun onEnable() {
1379+
object : BukkitRunnable() {
1380+
override fun run() {
1381+
CommandAPI.unregister("minecraft:gamemode")
1382+
}
1383+
}.runTaskLater(this, 0)
1384+
}
1385+
/* ANCHOR_END: commandUnregistrationOnlyVanillaNamespace */
1386+
}
1387+
1388+
class UnregistrationDealyedVanillaBad : JavaPlugin() {
1389+
/* ANCHOR: commandUnregistrationDealyedVanillaBad */
1390+
// NOT RECOMMENDED
1391+
override fun onEnable() {
1392+
object : BukkitRunnable() {
1393+
override fun run() {
1394+
CommandAPI.unregister("gamemode")
1395+
}
1396+
}.runTaskLater(this, 0)
1397+
}
1398+
/* ANCHOR_END: commandUnregistrationDealyedVanillaBad */
1399+
}
1400+
1401+
class UnregistrationDealyedVanillaBetter : JavaPlugin() {
1402+
/* ANCHOR: commandUnregistrationDealyedVanillaBetter */
1403+
override fun onEnable() {
1404+
object : BukkitRunnable() {
1405+
override fun run() {
1406+
CommandAPI.unregister("gamemode", true)
1407+
}
1408+
}.runTaskLater(this, 0)
1409+
}
1410+
/* ANCHOR_END: commandUnregistrationDealyedVanillaBetter */
1411+
}
13251412
}
13261413

13271414
class commandTrees : JavaPlugin() {

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/OnEnableTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ void testOnEnableRegisterAndUnregisterCommand() {
276276
// You would expect namespace to succeed since it is in the CommandMap
277277
// However, running that command simply tells the Brig dispatcher to run the original command
278278
// The command was removed from the Brig dispacter, so it doesn't actually know how to do that
279-
// I'm going to say this is not a bug, just an example why you should be careful when unregistering commands
279+
// This behavior is documented at the bottom of the docs page for Command unregistration
280280
// As a result, this test doesn't actually pass
281281
// // Namespace command should still work
282282
// assertStoresResult(runCommandsPlayer, "minecraft:command argument", results, "argument");

docssrc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# Creating Commands
2626

2727
- [Command registration](./commandregistration.md)
28+
- [Command unregistration](./commandunregistration.md)
2829
- [Command executors](./commandexecutors.md)
2930
- [Normal command executors](./normalexecutors.md)
3031
- [Proxied commandsenders](./proxysender.md)

docssrc/src/commandregistration.md

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -179,49 +179,8 @@ Registers the command.
179179
It is recommended to register commands in either the `onLoad()` or `onEnable()` method. With the CommandAPI, depending on whether you use `onLoad()` or `onEnable()` to load your commands depends on whether your plugin is used with Minecraft's functions:
180180

181181
| When to load | What to do |
182-
| ------------------- | -------------------------------------------------------------------------------------------------------------- |
182+
|---------------------|----------------------------------------------------------------------------------------------------------------|
183183
| `onLoad()` method | Register commands to be used in Minecraft functions ([see the Function section for more info](functions.html)) |
184184
| `onEnable()` method | Register regular commands |
185185

186186
The CommandAPI does support registering commands outside of these methods while the server is running. Commands registered after the server is done loading _should_ work the same as commands registered in `onEnable`.
187-
188-
-----
189-
190-
## Command unregistration
191-
192-
The CommandAPI has support to unregister commands completely from Minecraft's command list. This includes Minecraft built in commands!
193-
194-
<div class="warning">
195-
196-
**Developer's Note:**
197-
198-
Command unregistration, although powerful, is not recommended! It is the CommandAPI's most "dangerous" feature as it can cause unexpected side effects, such as command blocks executing commands you wouldn't expect them to. In almost every case, I'd recommend just creating a new command instead of unregistering one to replace it.
199-
200-
For instance, instead of unregistering `/gamemode`, you could register a command `/gm` or `/changegamemode`.
201-
202-
</div>
203-
204-
| Method | Result |
205-
| -------------------------------------------------- | ------------------------------------------------------------ |
206-
| `CommandAPI.unregister(String cmd)` | Unregisters a command from the game |
207-
| `CommandAPI.unregister(String cmd, boolean force)` | Attempts to unregister a command from the game by force. This includes `/minecraft:cmd`, `/bukkit:cmd` and `/spigot:cmd` commands as well. |
208-
209-
<div class="example">
210-
211-
### Example - Replacing Minecraft's `/gamemode` command
212-
213-
To replace a command, we can first unregister it and then register our implementation of that command.
214-
215-
<div class="multi-pre">
216-
217-
```java,Java
218-
{{#include ../../commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java:commandRegistration2}}
219-
```
220-
221-
```kotlin,Kotlin
222-
{{#include ../../commandapi-documentation-code/src/main/kotlin/dev/jorel/commandapi/examples/kotlin/Examples.kt:commandRegistration2}}
223-
```
224-
225-
</div>
226-
227-
</div>

0 commit comments

Comments
 (0)