Skip to content

Commit 3a7434f

Browse files
committed
Convert all commands to brigadier
1 parent 07f9eab commit 3a7434f

File tree

14 files changed

+263
-216
lines changed

14 files changed

+263
-216
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--- a/src/main/java/org/bukkit/command/defaults/HelpCommand.java
2+
+++ b/src/main/java/org/bukkit/command/defaults/HelpCommand.java
3+
@@ -27,10 +_,11 @@
4+
5+
public class HelpCommand extends BukkitCommand {
6+
public HelpCommand() {
7+
- super("help");
8+
+ // Deepslate start - Custom help command
9+
+ super("?");
10+
this.description = "Shows the help menu";
11+
- this.usageMessage = "/help <pageNumber>\n/help <topic>\n/help <topic> <pageNumber>";
12+
- this.setAliases(Arrays.asList(new String[]{"?"}));
13+
+ this.usageMessage = "/? <pageNumber>\n/? <topic>\n/? <topic> <pageNumber>";
14+
+ // Deepslate end - Custom help command
15+
this.setPermission("bukkit.command.help");
16+
}
17+

deepslateMC-server/minecraft-patches/sources/net/minecraft/server/ReloadableServerResources.java.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
this.commands = new Commands(commandSelection, CommandBuildContext.simple(registries, enabledFeatures), true); // Paper - Brigadier Command API - use modern alias registration
55
io.papermc.paper.command.brigadier.PaperCommands.INSTANCE.setDispatcher(this.commands, CommandBuildContext.simple(registries, enabledFeatures)); // Paper - Brigadier Command API
66
io.papermc.paper.command.PaperCommands.registerCommands(); // Paper
7-
+ de.pascalpex.deepslatemc.commands.DeepslateCmdRegisterer.registerBrigadierCommands(); // Deepslate
7+
+ de.pascalpex.deepslatemc.commands.DeepslateCmdRegisterer.registerCommands(); // Deepslate
88
this.advancements = new ServerAdvancementManager(registries);
99
this.functionLibrary = new ServerFunctionLibrary(permissions, this.commands.getDispatcher());
1010
}

deepslateMC-server/minecraft-patches/sources/net/minecraft/server/dedicated/DedicatedServer.java.patch

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
--- a/net/minecraft/server/dedicated/DedicatedServer.java
22
+++ b/net/minecraft/server/dedicated/DedicatedServer.java
3-
@@ -285,6 +_,8 @@
3+
@@ -285,6 +_,7 @@
44
org.spigotmc.WatchdogThread.doStart(org.spigotmc.SpigotConfig.timeoutTime, org.spigotmc.SpigotConfig.restartOnCrash); // Paper - start watchdog thread
55
thread.start(); // Paper - Enhance console tab completions for brigadier commands; start console thread after MinecraftServer.console & PaperConfig are initialized
66
io.papermc.paper.command.PaperCommands.registerCommands(this); // Paper - setup /paper command
7-
+ de.pascalpex.deepslatemc.commands.DeepslateCmdRegisterer.registerCommands(this); // Deepslate - register commands
87
+ de.pascalpex.deepslatemc.util.ActionbarUtil.reloadActionbar(); // Deepslate - load actionbar
98
this.server.spark.registerCommandBeforePlugins(this.server); // Paper - spark
109
com.destroystokyo.paper.Metrics.PaperMetrics.startMetrics(); // Paper - start metrics

deepslateMC-server/paper-patches/files/src/main/java/org/bukkit/craftbukkit/CraftServer.java.patch

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,42 @@
11
package de.pascalpex.deepslatemc.commands;
22

3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.context.CommandContext;
5+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6+
import com.mojang.brigadier.tree.LiteralCommandNode;
37
import de.pascalpex.deepslatemc.files.Config;
48
import de.pascalpex.deepslatemc.files.MessagesEntry;
59
import de.pascalpex.deepslatemc.files.MessagesFile;
10+
import io.papermc.paper.command.brigadier.CommandSourceStack;
11+
import io.papermc.paper.command.brigadier.Commands;
612
import net.kyori.adventure.text.Component;
713
import org.bukkit.Bukkit;
8-
import org.bukkit.World;
9-
import org.bukkit.command.Command;
1014
import org.bukkit.command.CommandSender;
1115
import org.bukkit.entity.Player;
12-
import org.jetbrains.annotations.NotNull;
1316

14-
public class BuildworldCommand extends Command {
17+
public class BuildworldCommand implements Command<CommandSourceStack> {
1518

16-
public BuildworldCommand(String name) {
17-
super(name);
18-
this.description = "Teleports builders to the buildworld";
19-
this.usageMessage = "/buildworld";
20-
this.setPermission("deepslate.buildworld");
19+
public static LiteralCommandNode<CommandSourceStack> create() {
20+
return Commands.literal("buildworld")
21+
.executes(new BuildworldCommand())
22+
.requires(commandSourceStack -> commandSourceStack.getSender().hasPermission("deepslate.buildworld"))
23+
.build();
2124
}
2225

2326
@Override
24-
public boolean execute(@NotNull CommandSender sender, @NotNull String label, String @NotNull [] args) {
27+
public int run(CommandContext<CommandSourceStack> commandContext) throws CommandSyntaxException {
2528
Component prefix = MessagesFile.getMessage(MessagesEntry.PREFIX).appendSpace();
29+
CommandSender sender = commandContext.getSource().getSender();
2630
if (sender instanceof Player player) {
27-
if(label.equalsIgnoreCase("setbuildworld")) {
28-
if(player.hasPermission("deepslate.setbuildworld")) {
29-
World world = player.getLocation().getWorld();
30-
Config.setBuildworld(world.getName());
31-
player.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.BUILDWORLD_SET)));
32-
} else {
33-
player.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.NO_PERMISSIONS)));
34-
}
35-
}
36-
if(label.equalsIgnoreCase("buildworld")) {
37-
if(player.hasPermission("deepslate.buildworld")) {
38-
try {
39-
player.teleport(Bukkit.getWorld(Config.getBuildworld()).getSpawnLocation());
40-
player.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.BUILDWORLD_WELCOME)));
41-
} catch (IllegalArgumentException e) {
42-
player.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.BUILDWORLD_NOT_SET)));
43-
}
44-
} else {
45-
player.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.NO_PERMISSIONS)));
46-
}
31+
try {
32+
player.teleport(Bukkit.getWorld(Config.getBuildworld()).getSpawnLocation());
33+
player.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.BUILDWORLD_WELCOME)));
34+
} catch (IllegalArgumentException e) {
35+
player.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.BUILDWORLD_NOT_SET)));
4736
}
4837
} else {
4938
sender.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.ONLY_FOR_PLAYERS)));
5039
}
51-
return true;
40+
return SINGLE_SUCCESS;
5241
}
5342
}
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
package de.pascalpex.deepslatemc.commands;
22

3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.context.CommandContext;
5+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6+
import com.mojang.brigadier.tree.LiteralCommandNode;
37
import de.pascalpex.deepslatemc.files.MessagesEntry;
48
import de.pascalpex.deepslatemc.files.MessagesFile;
9+
import io.papermc.paper.command.brigadier.CommandSourceStack;
10+
import io.papermc.paper.command.brigadier.Commands;
511
import net.kyori.adventure.text.Component;
612
import net.kyori.adventure.text.TextReplacementConfig;
713
import org.bukkit.Bukkit;
8-
import org.bukkit.command.Command;
9-
import org.bukkit.command.CommandSender;
10-
import org.jetbrains.annotations.NotNull;
1114

12-
public class ClearchatCommand extends Command {
13-
public ClearchatCommand(String name) {
14-
super(name);
15-
this.description = "Clears the chat";
16-
this.usageMessage = "/clearchat";
17-
this.setPermission("deepslate.clearchat");
15+
public class ClearchatCommand implements Command<CommandSourceStack> {
16+
17+
public static LiteralCommandNode<CommandSourceStack> create() {
18+
return Commands.literal("clearchat")
19+
.executes(new ClearchatCommand())
20+
.requires(commandSourceStack -> commandSourceStack.getSender().hasPermission("deepslate.clearchat"))
21+
.build();
1822
}
1923

2024
@Override
21-
public boolean execute(@NotNull CommandSender sender, String commandLabel, String @NotNull [] args) {
25+
public int run(CommandContext<CommandSourceStack> commandContext) throws CommandSyntaxException {
2226
Component prefix = MessagesFile.getMessage(MessagesEntry.PREFIX).appendSpace();
23-
if (commandLabel.equalsIgnoreCase("cc") || commandLabel.equalsIgnoreCase("clearchat")) {
24-
if(sender.hasPermission("deepslate.clearchat")) {
25-
for (int x = 0; x < 150; x++){
26-
Bukkit.broadcast(Component.empty());
27-
}
28-
Component clearMessage = MessagesFile.getMessage(MessagesEntry.CLEARED_CHAT).replaceText(TextReplacementConfig.builder().match("%clearer%").replacement(sender.getName()).build());
29-
Bukkit.broadcast(prefix.append(clearMessage));
30-
} else {
31-
sender.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.NO_PERMISSIONS)));
32-
}
27+
for (int x = 0; x < 150; x++){
28+
Bukkit.broadcast(Component.empty());
3329
}
34-
return true;
30+
Component clearMessage = MessagesFile.getMessage(MessagesEntry.CLEARED_CHAT).replaceText(TextReplacementConfig.builder().match("%clearer%").replacement(commandContext.getSource().getSender().getName()).build());
31+
Bukkit.broadcast(prefix.append(clearMessage));
32+
return SINGLE_SUCCESS;
3533
}
3634
}

deepslateMC-server/src/main/java/de/pascalpex/deepslatemc/commands/DeepslateCmdRegisterer.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,30 @@
22

33
import com.mojang.brigadier.tree.LiteralCommandNode;
44
import io.papermc.paper.command.brigadier.CommandSourceStack;
5-
import net.minecraft.server.MinecraftServer;
6-
import org.bukkit.command.SimpleCommandMap;
75

86
import java.util.List;
97
import java.util.Set;
108

119
public class DeepslateCmdRegisterer {
1210

13-
public static void registerCommands(final MinecraftServer server) {
14-
SimpleCommandMap commandMap = server.server.getCommandMap();
15-
16-
commandMap.register("deepslate", "Deepslate", new DeepslateCommand("deepslate"));
17-
commandMap.register("discord", "Deepslate", new DiscordCommand("discord"));
18-
commandMap.register("dc", "Deepslate", new DiscordCommand("dc"));
19-
commandMap.register("cc", "Deepslate", new ClearchatCommand("cc"));
20-
commandMap.register("clearchat", "Deepslate", new ClearchatCommand("clearchat"));
21-
commandMap.register("setbuildworld", "Deepslate", new BuildworldCommand("setbuildworld"));
22-
commandMap.register("buildworld", "Deepslate", new BuildworldCommand("buildworld"));
23-
commandMap.register("maintenance", "Deepslate", new MaintenanceMode("maintenance"));
24-
commandMap.register("help", "Deepslate", new HelpCommand("help"));
25-
commandMap.register("setspawn", "Deepslate", new SpawnCommand("setspawn"));
26-
commandMap.register("spawn", "Deepslate", new SpawnCommand("spawn"));
27-
commandMap.register("lobby", "Deepslate", new SpawnCommand("lobby"));
28-
}
29-
30-
public static void registerBrigadierCommands() {
11+
public static void registerCommands() {
3112
registerInternalCommand(UnbreakableCommand.create(), "Makes an item unbreakable", List.of("unbreakable"));
13+
registerInternalCommand(HelpCommand.create(), "Shows the configured help page", List.of("help"));
14+
registerInternalCommand(DeepslateCommand.create(), "Main DeepslateMC command", List.of("deepslate"));
15+
registerInternalCommand(DiscordCommand.create(), "Shows the Discord link of this server", List.of("discord", "dc"));
16+
registerInternalCommand(ClearchatCommand.create(), "Clears the chat", List.of("clearchat", "cc"));
17+
registerInternalCommand(SpawnCommand.create(), "Teleports you to the spawn", List.of("spawn"));
18+
registerInternalCommand(SetspawnCommand.create(), "Clears the chat", List.of("setspawn"));
19+
registerInternalCommand(BuildworldCommand.create(), "Teleports builders to the buildworld", List.of("buildworld"));
20+
registerInternalCommand(SetbuildworldCommand.create(), "Sets the buildworld for builders", List.of("setbuildworld"));
21+
registerInternalCommand(MaintenanceMode.create(), "Toggles the maintenance mode", List.of("maintenance"));
3222
}
3323

3424
private static void registerInternalCommand(final LiteralCommandNode<CommandSourceStack> node, final String description, final List<String> aliases) {
3525
io.papermc.paper.command.brigadier.PaperCommands.INSTANCE.registerWithFlagsInternal(
3626
null,
3727
"deepslate",
38-
"Paper",
28+
"DeepslateMC",
3929
node,
4030
description,
4131
aliases,
Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,71 @@
11
package de.pascalpex.deepslatemc.commands;
22

3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.context.CommandContext;
5+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6+
import com.mojang.brigadier.tree.LiteralCommandNode;
37
import de.pascalpex.deepslatemc.files.Config;
48
import de.pascalpex.deepslatemc.files.MessagesEntry;
59
import de.pascalpex.deepslatemc.files.MessagesFile;
610
import de.pascalpex.deepslatemc.util.ActionbarUtil;
711
import de.pascalpex.deepslatemc.util.BossbarUtil;
812
import de.pascalpex.deepslatemc.util.ServerLinkUtil;
913
import de.pascalpex.deepslatemc.util.TablistUtil;
14+
import io.papermc.paper.command.brigadier.CommandSourceStack;
15+
import io.papermc.paper.command.brigadier.Commands;
1016
import net.kyori.adventure.text.Component;
1117
import net.kyori.adventure.text.format.NamedTextColor;
12-
import org.bukkit.Location;
13-
import org.bukkit.command.Command;
18+
import org.bukkit.Bukkit;
1419
import org.bukkit.command.CommandSender;
15-
import org.jetbrains.annotations.NotNull;
1620

17-
import java.util.Collections;
18-
import java.util.List;
19-
import java.util.stream.Collectors;
20-
import java.util.stream.Stream;
21+
public class DeepslateCommand implements Command<CommandSourceStack> {
2122

22-
public class DeepslateCommand extends Command {
23-
24-
public DeepslateCommand(String name) {
25-
super(name);
26-
this.description = "Deepslate command";
27-
this.usageMessage = "/deepslate [reload | version]";
23+
public static LiteralCommandNode<CommandSourceStack> create() {
24+
return Commands.literal("deepslate")
25+
.executes(new DeepslateCommand())
26+
.then(Commands.literal("reload")
27+
.requires(commandSourceStack -> commandSourceStack.getSender().hasPermission("deepslate.command"))
28+
.executes(new DeepslateReloadCommand()))
29+
.then(Commands.literal("version")
30+
.requires(commandSourceStack -> commandSourceStack.getSender().hasPermission("deepslate.command"))
31+
.executes(new DeepslateVersionCommand()))
32+
.build();
2833
}
2934

30-
@Override
31-
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, String[] args, Location location) throws IllegalArgumentException {
32-
if (args.length == 1 && sender.hasPermission("deepslate.command")) {
33-
return Stream.of("reload", "version")
34-
.filter(arg -> arg.startsWith(args[0].toLowerCase()))
35-
.collect(Collectors.toList());
35+
private static class DeepslateReloadCommand implements Command<CommandSourceStack> {
36+
@Override
37+
public int run(CommandContext<CommandSourceStack> commandContext) {
38+
MessagesFile.load();
39+
Config.load();
40+
TablistUtil.reloadTablist();
41+
BossbarUtil.reloadBossbar();
42+
ActionbarUtil.reloadActionbar();
43+
ServerLinkUtil.loadLinks();
44+
Component prefix = MessagesFile.getMessage(MessagesEntry.PREFIX).appendSpace();
45+
commandContext.getSource().getSender().sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.CONFIG_RELOADED)));
46+
return SINGLE_SUCCESS;
3647
}
37-
return Collections.emptyList();
3848
}
3949

40-
@Override
41-
public boolean execute(@NotNull CommandSender sender, String commandLabel, String @NotNull [] args) {
42-
if (commandLabel.equalsIgnoreCase("deepslate")) {
50+
private static class DeepslateVersionCommand implements Command<CommandSourceStack> {
51+
@Override
52+
public int run(CommandContext<CommandSourceStack> commandContext) {
4353
Component prefix = MessagesFile.getMessage(MessagesEntry.PREFIX).appendSpace();
44-
if (sender.hasPermission("deepslate.command")) {
45-
if (args.length == 0) {
46-
sender.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.WRONG_SYNTAX)));
47-
}
48-
if (args.length == 1) {
49-
if (args[0].equalsIgnoreCase("reload")) {
50-
MessagesFile.load();
51-
Config.load();
52-
TablistUtil.reloadTablist();
53-
BossbarUtil.reloadBossbar();
54-
ActionbarUtil.reloadActionbar();
55-
ServerLinkUtil.loadLinks();
56-
prefix = MessagesFile.getMessage(MessagesEntry.PREFIX).appendSpace();
57-
sender.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.CONFIG_RELOADED)));
58-
} else {
59-
if (args[0].equalsIgnoreCase("version")) {
60-
String deepslateVersion = this.getClass().getPackage().getImplementationVersion().replace('"', ' ').replace(" ", "");
61-
sender.sendMessage(prefix.append(Component.text("This server is running DeepslateMC from Pascalpex: " + deepslateVersion).color(NamedTextColor.GOLD)));
62-
} else {
63-
sender.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.WRONG_SYNTAX)));
64-
}
65-
}
66-
}
67-
if (args.length >= 2) {
68-
sender.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.WRONG_SYNTAX)));
69-
}
70-
} else {
71-
sender.sendMessage(prefix.append(Component.text("This server is running DeepslateMC from Pascalpex").color(NamedTextColor.GOLD)));
72-
}
54+
String deepslateVersion = Bukkit.getVersionMessage();
55+
commandContext.getSource().getSender().sendMessage(prefix.append(Component.text(deepslateVersion + " made by Pascalpex").color(NamedTextColor.GOLD)));
56+
return SINGLE_SUCCESS;
7357
}
74-
return true;
7558
}
7659

60+
@Override
61+
public int run(CommandContext<CommandSourceStack> commandContext) throws CommandSyntaxException {
62+
Component prefix = MessagesFile.getMessage(MessagesEntry.PREFIX).appendSpace();
63+
CommandSender sender = commandContext.getSource().getSender();
64+
if(commandContext.getSource().getSender().hasPermission("deepslate.command")) {
65+
sender.sendMessage(prefix.append(MessagesFile.getMessage(MessagesEntry.WRONG_SYNTAX)));
66+
} else {
67+
sender.sendMessage(prefix.append(Component.text("This server is running DeepslateMC made by Pascalpex").color(NamedTextColor.GOLD)));
68+
}
69+
return SINGLE_SUCCESS;
70+
}
7771
}

0 commit comments

Comments
 (0)