Skip to content

Commit 732bdcd

Browse files
committed
New command system completed
1 parent 4bceef5 commit 732bdcd

43 files changed

Lines changed: 625 additions & 406 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/net/blueva/core/Main.java

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525

2626
package net.blueva.core;
2727

28-
import net.blueva.core.commands.legacy.*;
2928
import net.blueva.core.commands.main.CommandHandler;
29+
import net.blueva.core.commands.main.command.BlueCoreAliasRegister;
3030
import net.blueva.core.commands.main.command.BlueCoreCommand;
31-
import net.blueva.core.commands.main.subcommands.core.HelpSubCommand;
32-
import net.blueva.core.commands.main.subcommands.core.InfoSubCommand;
31+
import net.blueva.core.commands.main.subcommands.core.*;
3332
import net.blueva.core.configuration.ConfigManager;
3433
import net.blueva.core.libraries.bstats.Metrics;
3534
import net.blueva.core.modules.economy.vault.EconomyImplementer;
@@ -80,9 +79,13 @@ public void onEnable() {
8079
ConfigManager.generateFolders();
8180
ConfigManager.registerDocuments();
8281
registerEvents();
83-
registerCommands();
82+
try {
83+
registerCommands();
84+
} catch (IOException e) {
85+
throw new RuntimeException(e);
86+
}
8487

85-
prefix = ConfigManager.language.getString("prefix");
88+
prefix = ConfigManager.language.getString("prefix");
8689
currency_symbol = ConfigManager.Modules.economy.getString("economy.currency_symbol");
8790

8891
if(ConfigManager.settings.getBoolean("metrics")) {
@@ -164,55 +167,54 @@ private void registerEvents() {
164167
pm.registerEvents(new PlayerQuitListener(this), this);
165168
}
166169

167-
private void registerCommands() {
170+
private void registerCommands() throws IOException {
168171
CommandHandler bc = new CommandHandler();
169172

170173
bc.register("bluecore", new BlueCoreCommand());
171174
bc.register("help", new HelpSubCommand());
172175
bc.register("info", new InfoSubCommand());
176+
bc.register("actionbar", new ActionBarCommand(this));
177+
bc.register("adventure", new AdventureCommand(this));
178+
bc.register("clearchat", new ClearChatCommand(this));
179+
bc.register("createkit", new CreateKitCommand(this));
180+
bc.register("creative", new CreativeCommand(this));
181+
bc.register("day", new DayCommand(this));
182+
bc.register("deletewarp", new DeleteWarpCommand(this));
183+
bc.register("economy", new EconomyCommand(this));
184+
bc.register("enderchest", new EnderChestCommand(this));
185+
bc.register("feed", new FeedCommand(this));
186+
bc.register("fly", new FlyCommand(this));
187+
bc.register("gamemode", new GamemodeCommand(this));
188+
bc.register("god", new GodCommand(this));
189+
bc.register("heal", new HealCommand(this));
190+
bc.register("kill", new KillCommand(this));
191+
bc.register("kit", new KitCommand(this));
192+
bc.register("message", new MessageCommand(this));
193+
bc.register("midnight", new MidnightCommand(this));
194+
bc.register("modifykit", new ModifyKitCommand(this));
195+
bc.register("money", new MoneyCommand(this));
196+
bc.register("night", new NightCommand(this));
197+
bc.register("noon", new NoonCommand(this));
198+
bc.register("pay", new PayCommand(this));
199+
bc.register("setspawn", new SetSpawnCommand(this));
200+
bc.register("setwarp", new SetWarpCommand(this));
201+
bc.register("spawn", new SpawnCommand(this));
202+
bc.register("spectator", new SpectatorCommand(this));
203+
bc.register("speed", new SpeedCommand(this));
204+
bc.register("storm", new StormCommand(this));
205+
bc.register("sudo", new SudoCommand(this));
206+
bc.register("suicide", new SuicideCommand(this));
207+
bc.register("sun", new SunCommand(this));
208+
bc.register("survival", new SurvivalCommand(this));
209+
bc.register("teleport", new TeleportCommand(this));
210+
bc.register("title", new TitleCommand(this));
211+
bc.register("updatewarp", new UpdateWarpCommand(this));
212+
bc.register("warp", new WarpCommand(this));
213+
bc.register("workbench", new WorkbenchCommand(this));
214+
bc.register("world", new WorldCommand(this));
173215
Objects.requireNonNull(getCommand("bluecore")).setExecutor(bc);
216+
new BlueCoreAliasRegister(this, bc).registerAliases();
174217
//Objects.requireNonNull(getCommand("bluecore")).setTabCompleter(new ...);
175-
176-
// LEGACY
177-
Objects.requireNonNull(this.getCommand("actionbar")).setExecutor(new ActionBarCommand(this));
178-
Objects.requireNonNull(this.getCommand("adventure")).setExecutor(new AdventureCommand(this));
179-
Objects.requireNonNull(this.getCommand("clearchat")).setExecutor(new ClearChatCommand(this));
180-
Objects.requireNonNull(this.getCommand("createkit")).setExecutor(new CreateKitCommand(this));
181-
Objects.requireNonNull(this.getCommand("creative")).setExecutor(new CreativeCommand(this));
182-
Objects.requireNonNull(this.getCommand("day")).setExecutor(new DayCommand(this));
183-
Objects.requireNonNull(this.getCommand("deletewarp")).setExecutor(new DeleteWarpCommand(this));
184-
Objects.requireNonNull(this.getCommand("economy")).setExecutor(new EconomyCommand(this));
185-
Objects.requireNonNull(this.getCommand("enderchest")).setExecutor(new EnderChestCommand(this));
186-
Objects.requireNonNull(this.getCommand("feed")).setExecutor(new FeedCommand(this));
187-
Objects.requireNonNull(this.getCommand("fly")).setExecutor(new FlyCommand(this));
188-
Objects.requireNonNull(this.getCommand("gamemode")).setExecutor(new GamemodeCommand(this));
189-
Objects.requireNonNull(this.getCommand("god")).setExecutor(new GodCommand(this));
190-
Objects.requireNonNull(this.getCommand("heal")).setExecutor(new HealCommand(this));
191-
Objects.requireNonNull(this.getCommand("kill")).setExecutor(new KillCommand(this));
192-
Objects.requireNonNull(this.getCommand("kit")).setExecutor(new KitCommand(this));
193-
Objects.requireNonNull(this.getCommand("message")).setExecutor(new MessageCommand(this));
194-
Objects.requireNonNull(this.getCommand("midnight")).setExecutor(new MidnightCommand(this));
195-
Objects.requireNonNull(this.getCommand("modifykit")).setExecutor(new ModifyKitCommand(this));
196-
Objects.requireNonNull(this.getCommand("money")).setExecutor(new MoneyCommand(this));
197-
Objects.requireNonNull(this.getCommand("night")).setExecutor(new NightCommand(this));
198-
Objects.requireNonNull(this.getCommand("noon")).setExecutor(new NoonCommand(this));
199-
Objects.requireNonNull(this.getCommand("pay")).setExecutor(new PayCommand(this));
200-
Objects.requireNonNull(this.getCommand("setspawn")).setExecutor(new SetSpawnCommand(this));
201-
Objects.requireNonNull(this.getCommand("setwarp")).setExecutor(new SetWarpCommand(this));
202-
Objects.requireNonNull(this.getCommand("spawn")).setExecutor(new SpawnCommand(this));
203-
Objects.requireNonNull(this.getCommand("spectator")).setExecutor(new SpectatorCommand(this));
204-
Objects.requireNonNull(this.getCommand("speed")).setExecutor(new SpeedCommand(this));
205-
Objects.requireNonNull(this.getCommand("storm")).setExecutor(new StormCommand(this));
206-
Objects.requireNonNull(this.getCommand("sudo")).setExecutor(new SudoCommand(this));
207-
Objects.requireNonNull(this.getCommand("suicide")).setExecutor(new SuicideCommand(this));
208-
Objects.requireNonNull(this.getCommand("sun")).setExecutor(new SunCommand(this));
209-
Objects.requireNonNull(this.getCommand("survival")).setExecutor(new SurvivalCommand(this));
210-
Objects.requireNonNull(this.getCommand("teleport")).setExecutor(new TeleportCommand(this));
211-
Objects.requireNonNull(this.getCommand("title")).setExecutor(new TitleCommand(this));
212-
Objects.requireNonNull(this.getCommand("updatewarp")).setExecutor(new UpdateWarpCommand(this));
213-
Objects.requireNonNull(this.getCommand("warp")).setExecutor(new WarpCommand(this));
214-
Objects.requireNonNull(this.getCommand("workbench")).setExecutor(new WorkbenchCommand(this));
215-
Objects.requireNonNull(this.getCommand("world")).setExecutor(new WorldCommand(this));
216218
}
217219

218220
public @NonNull BukkitAudiences adventure() {

src/main/java/net/blueva/core/commands/main/CommandHandler.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.spongepowered.configurate.serialize.SerializationException;
3838

3939
import java.io.IOException;
40+
import java.util.Arrays;
4041
import java.util.HashMap;
4142

4243
public class CommandHandler implements CommandExecutor
@@ -49,6 +50,32 @@ public void register(String name, CommandInterface cmd) {
4950
commands.put(name, cmd);
5051
}
5152

53+
public void registerAlias(String alias, String commandName) {
54+
if (commands.containsKey(alias)) {
55+
return;
56+
}
57+
58+
CommandInterface executor = commands.get(commandName);
59+
if (executor != null) {
60+
commands.put(alias, executor);
61+
}
62+
}
63+
64+
public boolean executeSubcommand(CommandSender sender, Command cmd, String commandLabel, String subcommand, String[] args) {
65+
if (exists(subcommand)) {
66+
try {
67+
getExecutor(subcommand).onCommand(sender, cmd, commandLabel, args);
68+
} catch (IOException e) {
69+
throw new RuntimeException(e);
70+
}
71+
return true;
72+
}
73+
74+
Main.getPlugin().adventure().sender(sender).sendMessage(MessagesUtils.format(Bukkit.getPlayer(sender.getName()), ConfigManager.language.getString("messages.error.unknown_command")
75+
.replace("{command}", "bw")));
76+
return true;
77+
}
78+
5279
public boolean exists(String name) {
5380
return commands.containsKey(name);
5481
}
@@ -71,19 +98,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
7198
return true;
7299
}
73100

74-
if(exists(args[0])){
75-
76-
try {
77-
getExecutor(args[0]).onCommand(sender, cmd, commandLabel, args);
78-
} catch (IOException e) {
79-
throw new RuntimeException(e);
80-
}
81-
} else {
82-
83-
Main.getPlugin().adventure().sender(sender).sendMessage(MessagesUtils.format(Bukkit.getPlayer(sender.getName()), ConfigManager.language.getString("messages.error.unknown_command")
84-
.replace("{command}", "bw")));
85-
}
86-
return true;
101+
String[] subArgs = Arrays.copyOfRange(args, 1, args.length);
102+
return executeSubcommand(sender, cmd, commandLabel, args[0], subArgs);
87103
}
88104

89-
}
105+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* ____ _ ____
3+
* | __ )| |_ _ ___ / ___|___ _ __ ___
4+
* | _ \| | | | |/ _ | | / _ \| '__/ _ \
5+
* | |_) | | |_| | __| |__| (_) | | | __/
6+
* |____/|_|\__,_|\___|\____\___/|_| \___|
7+
*
8+
* This file is part of Blue Core.
9+
*
10+
* Blue Core is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License version 3 as
12+
* published by the Free Software Foundation.
13+
*
14+
* Blue Core is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License version 3 for more details.
18+
*
19+
* Blue Core plugin developed by Blueva Development.
20+
* Website: https://blueva.net/
21+
* GitHub repository: https://github.com/BluevaDevelopment/BlueCore
22+
*
23+
* Copyright (c) 2026 Blueva Development. All rights reserved.
24+
*/
25+
26+
package net.blueva.core.commands.main.command;
27+
28+
import net.blueva.core.Main;
29+
import net.blueva.core.commands.main.CommandHandler;
30+
import net.blueva.core.configuration.ConfigManager;
31+
import org.bukkit.Bukkit;
32+
import org.bukkit.command.CommandMap;
33+
import org.bukkit.command.defaults.BukkitCommand;
34+
import org.jetbrains.annotations.NotNull;
35+
36+
import java.lang.reflect.Field;
37+
import java.util.List;
38+
import java.util.Locale;
39+
import java.util.Objects;
40+
import java.util.Set;
41+
42+
public class BlueCoreAliasRegister {
43+
44+
private final Main plugin;
45+
private final CommandHandler commandHandler;
46+
47+
public BlueCoreAliasRegister(Main plugin, CommandHandler commandHandler) {
48+
this.plugin = plugin;
49+
this.commandHandler = commandHandler;
50+
}
51+
52+
public void registerAliases() {
53+
if (!ConfigManager.Modules.commands.contains("commands")) {
54+
return;
55+
}
56+
57+
Set<Object> commandKeys = Objects.requireNonNull(ConfigManager.Modules.commands.getSection("commands")).getKeys();
58+
for (Object key : commandKeys) {
59+
String commandName = key.toString();
60+
commandHandler.registerAlias(commandName, commandName);
61+
62+
List<String> aliases = ConfigManager.Modules.commands.getStringList("commands." + commandName + ".aliases");
63+
registerSubcommandAliases(commandName, aliases);
64+
65+
boolean hasStandaloneSetting = ConfigManager.Modules.commands.contains("commands." + commandName + ".register_standalone");
66+
boolean registerStandalone = ConfigManager.Modules.commands.getBoolean("commands." + commandName + ".register_standalone", true);
67+
if (!hasStandaloneSetting) {
68+
registerStandalone = true;
69+
}
70+
71+
if (registerStandalone) {
72+
registerStandaloneAlias(commandName, commandName);
73+
for (String alias : aliases) {
74+
if (alias == null || alias.isBlank()) {
75+
continue;
76+
}
77+
registerStandaloneAlias(alias, commandName);
78+
}
79+
}
80+
}
81+
}
82+
83+
private void registerSubcommandAliases(String commandName, List<String> aliases) {
84+
for (String alias : aliases) {
85+
if (alias == null || alias.isBlank()) {
86+
continue;
87+
}
88+
commandHandler.registerAlias(alias.toLowerCase(Locale.ROOT), commandName);
89+
}
90+
}
91+
92+
private void registerStandaloneAlias(String alias, String commandName) {
93+
if (alias == null || alias.isBlank() || commandName == null || commandName.isBlank()) {
94+
return;
95+
}
96+
String normalizedAlias = alias.toLowerCase(Locale.ROOT);
97+
CommandMap commandMap = getCommandMap();
98+
if (commandMap == null || commandMap.getCommand(normalizedAlias) != null) {
99+
return;
100+
}
101+
BukkitCommand standaloneCommand = new BlueCoreStandaloneAliasCommand(normalizedAlias, commandName.toLowerCase(Locale.ROOT), commandHandler);
102+
commandMap.register(plugin.getDescription().getName(), standaloneCommand);
103+
}
104+
105+
private CommandMap getCommandMap() {
106+
try {
107+
Field commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
108+
commandMapField.setAccessible(true);
109+
return (CommandMap) commandMapField.get(Bukkit.getPluginManager());
110+
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException | SecurityException e) {
111+
e.printStackTrace();
112+
return null;
113+
}
114+
}
115+
116+
private static class BlueCoreStandaloneAliasCommand extends BukkitCommand {
117+
118+
private final CommandHandler commandHandler;
119+
private final String targetSubcommand;
120+
121+
public BlueCoreStandaloneAliasCommand(@NotNull String name, @NotNull String targetSubcommand, CommandHandler commandHandler) {
122+
super(name);
123+
this.commandHandler = commandHandler;
124+
this.targetSubcommand = targetSubcommand;
125+
}
126+
127+
@Override
128+
public boolean execute(@NotNull org.bukkit.command.CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
129+
return commandHandler.executeSubcommand(sender, this, commandLabel, targetSubcommand, args);
130+
}
131+
}
132+
}

src/main/java/net/blueva/core/commands/legacy/ActionBarCommand.java renamed to src/main/java/net/blueva/core/commands/main/subcommands/core/ActionBarCommand.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,31 @@
2323
* Copyright (c) 2026 Blueva Development. All rights reserved.
2424
*/
2525

26-
package net.blueva.core.commands.legacy;
26+
package net.blueva.core.commands.main.subcommands.core;
2727

2828
import net.blueva.core.Main;
2929
import net.blueva.core.configuration.ConfigManager;
3030
import net.blueva.core.utils.MessagesUtils;
3131
import org.bukkit.Bukkit;
3232
import org.bukkit.command.Command;
33-
import org.bukkit.command.CommandExecutor;
33+
import net.blueva.core.commands.CommandInterface;
3434
import org.bukkit.command.CommandSender;
3535
import org.bukkit.entity.Player;
3636
import org.jetbrains.annotations.NotNull;
37+
import java.io.IOException;
3738

38-
public class ActionBarCommand implements CommandExecutor {
39+
public class ActionBarCommand implements CommandInterface {
3940

4041
private final Main main;
4142

4243
public ActionBarCommand(Main main) {
4344
this.main = main;
4445
}
4546

46-
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args){
47+
public void onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) throws IOException {
4748
if (args.length < 2) {
4849
MessagesUtils.sendToSender(sender, ConfigManager.language.getString("messages.other.use_actionbar_command"));
49-
return true;
50+
return;
5051
}
5152
if(args[0].equalsIgnoreCase("broadcast")){
5253
if(sender.hasPermission("bluecore.actionbar.broadcast")){
@@ -66,7 +67,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
6667
if(sender.hasPermission("bluecore.title.send")){
6768
if(args.length < 3) {
6869
MessagesUtils.sendToSender(sender, ConfigManager.language.getString("messages.other.use_actionbar_command"));
69-
return true;
70+
return;
7071
}
7172

7273
StringBuilder messageBuilder = new StringBuilder();
@@ -87,6 +88,6 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
8788
}else{
8889
MessagesUtils.sendToSender(sender, ConfigManager.language.getString("messages.other.use_actionbar_command"));
8990
}
90-
return true;
91+
return;
9192
}
9293
}

0 commit comments

Comments
 (0)