Skip to content

Commit 53b1654

Browse files
committed
ERootCommand
1 parent 2bdc0c6 commit 53b1654

3 files changed

Lines changed: 166 additions & 45 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencies {
4747
}
4848

4949
group = "de.erethon"
50-
version = "1.5.8"
50+
version = "1.5.9"
5151
description = "Bedrock"
5252
java.sourceCompatibility = JavaVersion.VERSION_21
5353

src/main/java/de/erethon/bedrock/command/ECommandCache.java

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -83,58 +83,28 @@ public void register(JavaPlugin plugin) {
8383
if (!command.isRegisterSeparately()) {
8484
continue;
8585
}
86-
registerCommand(plugin, command);
87-
}
88-
Constructor<PluginCommand> pluginCommand;
89-
try {
90-
pluginCommand = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
91-
} catch (NoSuchMethodException e) {
92-
throw new RuntimeException(e);
93-
}
94-
pluginCommand.setAccessible(true);
95-
PluginCommand pluginCommandInstance;
96-
try {
97-
pluginCommandInstance = pluginCommand.newInstance(label, plugin);
98-
} catch (Exception e) {
99-
MessageUtil.log("Couldn't register command '" + label + "' cause: " + e.getMessage());
100-
return;
101-
}
102-
pluginCommandInstance.setExecutor(executor);
103-
if (tabCompletion) {
104-
pluginCommandInstance.setTabCompleter(this);
86+
registerCommand(command);
10587
}
88+
ERootCommand labelCommand = new ERootCommand(label);
89+
labelCommand.setExecutor(executor);
90+
labelCommand.setTabCompleter(this);
91+
Bukkit.getCommandMap().register(label, labelCommand);
10692
}
10793

108-
private void registerCommand(JavaPlugin plugin, ECommand command) {
94+
private void registerCommand(ECommand command) {
10995
if (command.getHelp() == null) {
11096
command.setDefaultHelp();
11197
}
112-
registerNewCommand(plugin, command);
98+
registerRootCommand(command);
11399
}
114100

115-
private void registerNewCommand(JavaPlugin plugin, ECommand command) {
116-
try {
117-
final Constructor<PluginCommand> c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
118-
c.setAccessible(true);
119-
final PluginCommand bukkitCommand = c.newInstance(command.getCommand(), plugin);
120-
if (command.getAliases() != null) {
121-
bukkitCommand.setAliases(new ArrayList<>(command.getAliases()));
122-
}
123-
if (command.getDescription() != null) {
124-
bukkitCommand.setDescription(command.getDescription());
125-
}
126-
if (command.getUsage() != null) {
127-
bukkitCommand.setUsage(command.getUsage());
128-
}
129-
bukkitCommand.setPermission(command.getPermission());
130-
bukkitCommand.permissionMessage(BedrockMessage.CMD_NO_PERMISSION.message());
131-
bukkitCommand.setTabCompleter(command);
132-
bukkitCommand.setExecutor(command);
133-
134-
Bukkit.getCommandMap().register(label, bukkitCommand);
135-
} catch (Exception e) {
136-
MessageUtil.log("Couldn't register command '" + command.getCommand() + "' cause: " + e.getMessage());
137-
}
101+
public void registerRootCommand(ECommand eCommand) {
102+
List<String> aliases = new ArrayList<>(eCommand.getAliases());
103+
ERootCommand rootCommand = new ERootCommand(eCommand.getCommand(), eCommand.getDescription(), eCommand.getUsage(), aliases);
104+
Bukkit.getCommandMap().register(eCommand.getCommand(), rootCommand);
105+
rootCommand.setTabCompleter(eCommand);
106+
rootCommand.setExecutor(eCommand);
107+
rootCommand.setPermission(eCommand.getPermission());
138108
}
139109

140110
@Override
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package de.erethon.bedrock.command;
2+
3+
import com.google.common.base.Preconditions;
4+
import org.bukkit.command.Command;
5+
import org.bukkit.command.CommandException;
6+
import org.bukkit.command.CommandExecutor;
7+
import org.bukkit.command.CommandSender;
8+
import org.bukkit.command.TabCompleter;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import java.util.List;
13+
14+
public class ERootCommand extends Command {
15+
16+
private CommandExecutor executor;
17+
private TabCompleter completer;
18+
19+
20+
protected ERootCommand(String label) {
21+
super(label);
22+
this.executor = null;
23+
this.completer = null;
24+
}
25+
26+
protected ERootCommand(@NotNull String name, @NotNull String description, @NotNull String usageMessage, @NotNull List<String> aliases) {
27+
super(name, description, usageMessage, aliases);
28+
}
29+
30+
/**
31+
* Executes the command, returning its success
32+
*
33+
* @param sender Source object which is executing this command
34+
* @param commandLabel The alias of the command used
35+
* @param args All arguments passed to the command, split via ' '
36+
* @return true if the command was successful, otherwise false
37+
*/
38+
@Override
39+
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
40+
boolean success = false;
41+
42+
if (!testPermission(sender)) {
43+
return true;
44+
}
45+
46+
try {
47+
success = executor.onCommand(sender, this, commandLabel, args);
48+
} catch (Throwable ex) {
49+
throw new CommandException("Unhandled exception executing command '" + commandLabel, ex);
50+
}
51+
52+
if (!success && usageMessage.length() > 0) {
53+
for (String line : usageMessage.replace("<command>", commandLabel).split("\n")) {
54+
sender.sendMessage(line);
55+
}
56+
}
57+
58+
return success;
59+
}
60+
61+
/**
62+
* Sets the {@link CommandExecutor} to run when parsing this command
63+
*
64+
* @param executor New executor to run
65+
*/
66+
public void setExecutor(@Nullable CommandExecutor executor) {
67+
this.executor = executor;
68+
}
69+
70+
/**
71+
* Gets the {@link CommandExecutor} associated with this command
72+
*
73+
* @return CommandExecutor object linked to this command
74+
*/
75+
@NotNull
76+
public CommandExecutor getExecutor() {
77+
return executor;
78+
}
79+
80+
/**
81+
* Sets the {@link TabCompleter} to run when tab-completing this command.
82+
* <p>
83+
* If no TabCompleter is specified, and the command's executor implements
84+
* TabCompleter, then the executor will be used for tab completion.
85+
*
86+
* @param completer New tab completer
87+
*/
88+
public void setTabCompleter(@Nullable TabCompleter completer) {
89+
this.completer = completer;
90+
}
91+
92+
/**
93+
* Gets the {@link TabCompleter} associated with this command.
94+
*
95+
* @return TabCompleter object linked to this command
96+
*/
97+
@Nullable
98+
public TabCompleter getTabCompleter() {
99+
return completer;
100+
}
101+
102+
103+
/**
104+
* {@inheritDoc}
105+
* <p>
106+
* Delegates to the tab completer if present.
107+
* <p>
108+
* If it is not present or returns null, will delegate to the current
109+
* command executor if it implements {@link TabCompleter}. If a non-null
110+
* list has not been found, will default to standard player name
111+
* completion in {@link
112+
* Command#tabComplete(CommandSender, String, String[])}.
113+
* <p>
114+
* This method does not consider permissions.
115+
*
116+
* @throws CommandException if the completer or executor throw an
117+
* exception during the process of tab-completing.
118+
* @throws IllegalArgumentException if sender, alias, or args is null
119+
*/
120+
@NotNull
121+
@Override
122+
public java.util.List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws CommandException, IllegalArgumentException {
123+
Preconditions.checkArgument(sender != null, "Sender cannot be null");
124+
Preconditions.checkArgument(args != null, "Arguments cannot be null");
125+
Preconditions.checkArgument(alias != null, "Alias cannot be null");
126+
127+
List<String> completions = null;
128+
try {
129+
if (completer != null) {
130+
completions = completer.onTabComplete(sender, this, alias, args);
131+
}
132+
if (completions == null && executor instanceof TabCompleter) {
133+
completions = ((TabCompleter) executor).onTabComplete(sender, this, alias, args);
134+
}
135+
} catch (Throwable ex) {
136+
StringBuilder message = new StringBuilder();
137+
message.append("Unhandled exception during tab completion for command '/").append(alias).append(' ');
138+
for (String arg : args) {
139+
message.append(arg).append(' ');
140+
}
141+
message.deleteCharAt(message.length() - 1).append("' in plugin ");
142+
throw new CommandException(message.toString(), ex);
143+
}
144+
145+
if (completions == null) {
146+
return super.tabComplete(sender, alias, args);
147+
}
148+
return completions;
149+
}
150+
151+
}

0 commit comments

Comments
 (0)