8000 Registering commands during server runtime (Bukkit) by willkroboth · Pull Request #417 · CommandAPI/CommandAPI · GitHub
[go: up one dir, main page]

Skip to content

Registering commands during server runtime (Bukkit) #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2c65c44
Implement registering commands during server runtime
willkroboth Mar 31, 2023
ffd5de2
Don't wrap command nodes in CommandAPIBukkit#registerCommandNode
willkroboth Mar 31, 2023
33188cb
Add CommandAPIBukkit#getKnownCommandsMap and use SafeVarHandle instea…
willkroboth Apr 3, 2023
0b10ce1
Clean up logic for unregistering and fix when server is running
willkroboth Apr 4, 2023
6d01d57
Add 1.20 support for registering commands during server runtime
willkroboth Jun 13, 2023
0d0cce9
Use Player#updateCommands instead of `resendPackets`
willkroboth Jul 24, 2023
661ae89
Finish some tasks for Bukkit runtime register PR (https://github.com…
willkroboth Jul 26, 2023
8fedf11
Finish tests for registering/unregistering while server is enabled
willkroboth Jul 27, 2023
428b6bb
Fix formatting error in commandregistration.md
willkroboth Jul 27, 2023
365bf00
Inline `CommandAPIBukkit#getKnownCommands` since it is only being use…
willkroboth Aug 3, 2023
46ea5ac
Rename unregister parameter `force` to `unregisterNamespaces` and add…
willkroboth Aug 6, 2023
7020df1
Fix bug where the namespaced version of a command registered while Bu…
willkroboth Aug 6, 2023
c78c686
Make `CommandAPIBukkit#unregister` static, Tweak `unregister` javadocs
willkroboth Aug 7, 2023
acc0505
Respect `unregisterBukkit` when removing commands from resources disp…
willkroboth Aug 7, 2023
f063702
Add tests for `CommandAPIBukkit#unregister` while server is not enabled
willkroboth Aug 8, 2023
8e49c4a
Document new `CommandAPI#unregister` behavior
willkroboth Aug 11, 2023
cb28042
Add `log` language to unlabeled code block in commandunregistration.md
willkroboth Aug 11, 2023
bb43e1b
Fix `unreigster` typo in commandunregistration.md
willkroboth Aug 11, 2023
073334a
Rename anchors from `commandUnregistration(Purpose)` to `commandUnreg…
willkroboth Aug 11, 2023
c83a880
Add entry for runtime registering/unregistering to Changelog
willkroboth Aug 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename anchors from commandUnregistration(Purpose) to `commandUnreg…
…istration#` to fit with other documentation anchors.

Fix unregistration example alphabetization in relation to `commandTree`
  • Loading branch information
willkroboth 8000 committed Aug 11, 2023
commit 073334adcce32f5ad4535a77eb0e92a68628a17c
Original file line number Diff line number Diff line change
Expand Up @@ -1396,27 +1396,95 @@ void commandRegistration() {
/* ANCHOR_END: commandRegistration1 */
}

class commandTrees extends JavaPlugin {
{
/* ANCHOR: commandTrees1 */
new CommandTree("sayhi")
.executes((sender, args) -> {
sender.sendMessage("Hi!");
})
.then(new PlayerArgument("target")
.executes((sender, args) -> {
Player target = (Player) args.get("target");
target.sendMessage("Hi");
}))
.register();
/* ANCHOR_END: commandTrees1 */

/* ANCHOR: commandTrees2 */
new CommandTree("signedit")
.then(new LiteralArgument("set")
.then(new IntegerArgument("line_number", 1, 4)
.then(new GreedyStringArgument("text")
.executesPlayer((player, args) -> {
// /signedit set <line_number> <text>
Sign sign = getTargetSign(player);
int lineNumber = (int) args.get("line_number");
String text = (String) args.get("text");
sign.setLine(lineNumber - 1, text);
sign.update(true);
}))))
.then(new LiteralArgument("clear")
.then(new IntegerArgument("line_number", 1, 4)
.executesPlayer((player, args) -> {
// /signedit clear <line_number>
Sign sign = getTargetSign(player);
int lineNumber = (int) args.get("line_number");
sign.setLine(lineNumber - 1, "");
sign.update(true);
})))
.then(new LiteralArgument("copy")
.then(new IntegerArgument("line_number", 1, 4)
.executesPlayer((player, args) -> {
// /signedit copy <line_number>
Sign sign = getTargetSign(player);
int lineNumber = (int) args.get("line_number");
player.setMetadata("copied_sign_text", new FixedMetadataValue(this, sign.getLine(lineNumber - 1)));
})))
.then(new LiteralArgument("paste")
.then(new IntegerArgument("line_number", 1, 4)
.executesPlayer((player, args) -> {
// /signedit copy <line_number>
Sign sign = getTargetSign(player);
int lineNumber = (int) args.get("line_number");
sign.setLine(lineNumber - 1, player.getMetadata("copied_sign_text").get(0).asString());
sign.update(true);
})))
.register();
/* ANCHOR_END: commandTrees2 */
}

public Sign getTargetSign(Player player) throws WrapperCommandSyntaxException {
Block block = player.getTargetBlock(null, 256);
if (block.getState() instanceof Sign sign) {
return sign;
} else {
throw CommandAPI.failWithString("You're not looking at a sign!");
}
}
}

class CommandUnregistration {
class UnregistrationBukkit extends JavaPlugin {
/* ANCHOR: commandUnregistrationBukkit */
/* ANCHOR: commandUnregistration1 */
@Override
public void onLoad() {
CommandAPIBukkit.unregister("version", true, true);
}
/* ANCHOR_END: commandUnregistrationBukkit */
/* ANCHOR_END: commandUnregistration1 */
}

class UnregistrationVanilla extends JavaPlugin {
/* ANCHOR: commandUnregistrationVanilla */
/* ANCHOR: commandUnregistration2 */
@Override
public void onEnable() {
CommandAPI.unregister("gamemode");
}
/* ANCHOR_END: commandUnregistrationVanilla */
/* ANCHOR_END: commandUnregistration2 */
}

class UnregistrationReplaceVanilla extends JavaPlugin {
/* ANCHOR: commandUnregistrationReplaceVanilla */
/* ANCHOR: commandUnregistration3 */
@Override
public void onEnable() {
CommandAPI.unregister("gamemode");
Expand All @@ -1429,29 +1497,29 @@ public void onEnable() {
})
.register();
}
/* ANCHOR_END: commandUnregistrationReplaceVanilla */
/* ANCHOR_END: commandUnregistration3 */
}

class UnregistrationPlugin extends JavaPlugin {
/* ANCHOR: commandUnregistrationPlugin */
/* ANCHOR: commandUnregistration4 */
@Override
public void onEnable() {
CommandAPIBukkit.unregister("luckperms:luckperms", false, true);
}
/* ANCHOR_END: commandUnregistrationPlugin */
/* ANCHOR_END: commandUnregistration4 */
}

class UnregistrationCommandAPI extends JavaPlugin {
/* ANCHOR: commandUnregistrationCommandAPI */
/* ANCHOR: commandUnregistration5 */
@Override
public void onEnable() {
CommandAPI.unregister("break");
}
/* ANCHOR_END: commandUnregistrationCommandAPI */
/* ANCHOR_END: commandUnregistration5 */
}

class UnregistrationBukkitHelp extends JavaPlugin {
/* ANCHOR: commandUnregistrationBukkitHelp */
/* ANCHOR: commandUnregistration6 */
@Override
public void onEnable() {
new BukkitRunnable() {
Expand All @@ -1461,11 +1529,11 @@ public void run() {
}
}.runTaskLater(this, 0);
}
/* ANCHOR_END: commandUnregistrationBukkitHelp */
/* ANCHOR_END: commandUnregistration6 */
}

class UnregistrationOnlyVanillaNamespace extends JavaPlugin {
/* ANCHOR: commandUnregistrationOnlyVanillaNamespace */
/* ANCHOR: commandUnregistration7 */
@Override
public void onEnable() {
new BukkitRunnable() {
Expand All @@ -1475,11 +1543,11 @@ public void run() {
}
}.runTaskLater(this, 0);
}
/* ANCHOR_END: commandUnregistrationOnlyVanillaNamespace */
/* ANCHOR_END: commandUnregistration7 */
}

class UnregistrationDealyedVanillaBad extends JavaPlugin {
/* ANCHOR: commandUnregistrationDealyedVanillaBad */
class UnregistrationDelayedVanillaBad extends JavaPlugin {
/* ANCHOR: commandUnregistration8 */
// NOT RECOMMENDED
@Override
public void onEnable() {
Expand All @@ -1490,11 +1558,11 @@ public void run() {
}
}.runTaskLater(this, 0);
}
/* ANCHOR_END: commandUnregistrationDealyedVanillaBad */
/* ANCHOR_END: commandUnregistration8 */
}

class UnregistrationDealyedVanillaBetter extends JavaPlugin {
/* ANCHOR: commandUnregistrationDealyedVanillaBetter */
class UnregistrationDelayedVanillaBetter extends JavaPlugin {
/* ANCHOR: commandUnregistration9 */
@Override
public void onEnable() {
new BukkitRunnable() {
Expand All @@ -1504,75 +1572,7 @@ public void run() {
}
}.runTaskLater(this, 0);
}
/* ANCHOR_END: commandUnregistrationDealyedVanillaBetter */
}
}

class commandTrees extends JavaPlugin {
{
/* ANCHOR: commandTrees1 */
new CommandTree("sayhi")
.executes((sender, args) -> {
sender.sendMessage("Hi!");
})
.then(new PlayerArgument("target")
.executes((sender, args) -> {
Player target = (Player) args.get("target");
target.sendMessage("Hi");
}))
.register();
/* ANCHOR_END: commandTrees1 */

/* ANCHOR: commandTrees2 */
new CommandTree("signedit")
.then(new LiteralArgument("set")
.then(new IntegerArgument("line_number", 1, 4)
.then(new GreedyStringArgument("text")
.executesPlayer((player, args) -> {
// /signedit set <line_number> <text>
Sign sign = getTargetSign(player);
int lineNumber = (int) args.get("line_number");
String text = (String) args.get("text");
sign.setLine(lineNumber - 1, text);
sign.update(true);
}))))
.then(new LiteralArgument("clear")
.then(new IntegerArgument("line_number", 1, 4)
.executesPlayer((player, args) -> {
// /signedit clear <line_number>
Sign sign = getTargetSign(player);
int lineNumber = (int) args.get("line_number");
sign.setLine(lineNumber - 1, "");
sign.update(true);
})))
.then(new LiteralArgument("copy")
.then(new IntegerArgument("line_number", 1, 4)
.executesPlayer((player, args) -> {
// /signedit copy <line_number>
Sign sign = getTargetSign(player);
int lineNumber = (int) args.get("line_number");
player.setMetadata("copied_sign_text", new FixedMetadataValue(this, sign.getLine(lineNumber - 1)));
})))
.then(new LiteralArgument("paste")
.then(new IntegerArgument("line_number", 1, 4)
.executesPlayer((player, args) -> {
// /signedit copy <line_number>
Sign sign = getTargetSign(player);
int lineNumber = (int) args.get("line_number");
sign.setLine(lineNumber - 1, player.getMetadata("copied_sign_text").get(0).asString());
sign.update(true);
})))
.register();
/* ANCHOR_END: commandTrees2 */
}

public Sign getTargetSign(Player player) throws WrapperCommandSyntaxException {
Block block = player.getTargetBlock(null, 256);
if (block.getState() instanceof Sign sign) {
return sign;
} else {
throw CommandAPI.failWithString("You're not looking at a sign!");
}
/* ANCHOR_END: commandUnregistration9 */
}
}

Expand Down
Loading
0