8000 Update `dev/command-build-rewrite` after rebasing #509 and #526 · CommandAPI/CommandAPI@ed4d57e · GitHub
[go: up one dir, main page]

Skip to content

Commit ed4d57e

Browse files
committed
Update dev/command-build-rewrite after rebasing #509 and #526
1 parent 95f9ded commit ed4d57e

File tree

10 files changed

+84
-55
lines changed

10 files changed

+84
-55
lines changed

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIHandler.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,25 @@ public CommandAPIPlatform<Argument, CommandSender, Source> getPlatform() {
153153
// SECTION: Creating commands //
154154
////////////////////////////////
155155

156+
/**
157+
* Registers a command with a given namespace. This is intended to be called by {@link ExecutableCommand#register(String)}
158+
*
159+
* @param command The command to register.
160+
* @param namespace The namespace of this command. This cannot be null, and each platform may impose additional requirements.
161+
* See {@link CommandAPIPlatform#validateNamespace(ExecutableCommand, String)}.
162+
* @throws NullPointerException if the namespace is null.
163+
*/
156164
public void registerCommand(ExecutableCommand<?, CommandSender> command, String namespace) {
165+
// Validate parameters
166+
if (namespace == null) {
167+
throw new NullPointerException("Parameter 'namespace' was null when registering command /" + command.getName() + "!");
168+
}
169+
namespace = platform.validateNamespace(command, namespace);
170+
171+
// Do plaform-specific pre-registration tasks
157172
platform.preCommandRegistration(command.getName());
158173

174+
// Record the commands that are registered
159175
List<RegisteredCommand> registeredCommandInformation = RegisteredCommand.fromExecutableCommand(command, namespace);
160176
registeredCommands.addAll(registeredCommandInformation);
161177

@@ -191,6 +207,7 @@ public void registerCommand(ExecutableCommand<?, CommandSender> command, String
191207
// partial) command registration. Generate the dispatcher file!
192208
writeDispatcherToFile();
193209

210+
// Do platform-specific post-registration tasks
194211
platform.postCommandRegistration(registeredCommandInformation, resultantNode, aliasNodes);
195212
}
196213

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIPlatform.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ public interface CommandAPIPlatform<Argument
8383
// Some commands have existing suggestion providers
8484
public abstract SuggestionProvider<Source> getSuggestionProvider(SuggestionProviders suggestionProvider);
8585

86+
/**
87+
* Ensures the given String is a valid command namespace on this platform. If the namespace
88+
* is not valid, this method will return a String that should be used instead.
89+
*
90+
* @param command The command being registered with the given namespace.
91+
* @param namespace The String that wants to be used as a namespace.
92+
* @return The String that should be used as the namespace. If the given String is a valid namespace, it will be returned.
93+
*/
94+
public abstract String validateNamespace(ExecutableCommand<?, CommandSender> command, String namespace);
95+
8696
/**
8797
* Stuff to run before a command is generated. For Bukkit, this involves checking
8898
* if a command was declared in the plugin.yml when it isn't supposed to be.

commandapi-core/src/main/java/dev/jorel/commandapi/ExecutableCommand.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package dev.jorel.commandapi;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.function.Predicate;
6+
37
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
48
import com.mojang.brigadier.tree.LiteralCommandNode;
9+
510
import dev.jorel.commandapi.commandsenders.AbstractCommandSender;
611
import dev.jorel.commandapi.exceptions.InvalidCommandNameException;
712

8-
import java.util.ArrayList;
9-
import java.util.List;
10-
import java.util.function.Predicate;
11-
1213
/**
1314
* This is a base class for {@link AbstractCommandAPICommand} and {@link AbstractCommandTree} command definitions
1415
*
@@ -302,15 +303,13 @@ public void register() {
302303
}
303304

304305
/**
305-
* Registers the command with a given namespace.
306+
* Registers the command with the given namespace.
306307
*
307-
* @param namespace The namespace of this command. This cannot be null
308-
* @throws NullPointerException if the namespace is null
308+
* @param namespace The namespace for this command. This cannot be null, and each platform may impose additional requirements.
309+
* See {@link CommandAPIPlatform#validateNamespace(ExecutableCommand, String)}.
310+
* @throws NullPointerException if the namespace is null.
309311
*/
310312
public void register(String namespace) {
311-
if (namespace == null) {
312-
throw new NullPointerException("Parameter 'namespace' was null when registering command /" + this.name + "!");
313-
}
314313
((CommandAPIHandler<?, CommandSender, ?>) CommandAPIHandler.getInstance()).registerCommand(this, namespace);
315314
}
316315

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/CommandAPIBukkit.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,25 @@ public BukkitCommandSender<? extends CommandSender> wrapCommandSender(CommandSen
453453
@Unimplemented(because = REQUIRES_MINECRAFT_SERVER)
454454
public abstract SuggestionProvider<Source> getSuggestionProvider(SuggestionProviders suggestionProvider);
455455

456+
/**
457+
* {@inheritDoc}
458+
* On Bukkit, namespaces must not be empty, and can only contain 0-9, a-z, underscores, periods, and hyphens.
459+
*/
460+
@Override
461+
public String validateNamespace(ExecutableCommand<?, CommandSender> command, String namespace) {
462+
if (namespace.isEmpty()) {
463+
CommandAPI.logNormal("Registering command '" + command.getName() + "' using the default namespace because an empty namespace was given!");
464+
return config.getNamespace();
465+
}
466+
if (!CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
467+
CommandAPI.logNormal("Registering comand '" + command.getName() + "' using the default namespace because an invalid namespace (" + namespace + ") was given. Only 0-9, a-z, underscores, periods and hyphens are allowed!");
468+
return config.getNamespace();
469+
}
470+
471+
// Namespace is good, return it
472+
return namespace;
473+
}
474+
456475
@Override
457476
public void preCommandRegistration(String commandName) {
458477
// Warn if the command we're registering already exists in this plugin's
@@ -862,20 +881,4 @@ protected void registerBukkitRecipesSafely(Iterator<Recipe> recipes) {
862881
}
863882
}
864883
}
865-
866-
boolean isInvalidNamespace(String commandName, String namespace) {
867-
if (namespace == null) {
868-
throw new NullPointerException("Parameter 'namespace' was null when registering command /" + commandName + "!");
869-
}
870-
if (namespace.isEmpty()) {
871-
CommandAPI.logNormal("Registering command '" + commandName + "' using the default namespace because an empty namespace was given!");
872-
return true;
873-
}
874-
if (!CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
875-
CommandAPI.logNormal("Registering comand '" + commandName + "' using the default namespace because an invalid namespace (" + namespace + ") was given. Only 0-9, a-z, underscores, periods and hyphens are allowed!");
876-
return true;
877-
}
878-
return false;
879-
}
880-
881884
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/CommandAPIBukkitConfig.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import io.papermc.paper.event.server.ServerResourcesReloadedEvent;
44
import org.bukkit.plugin.java.JavaPlugin;
55

6-
import java.util.regex.Pattern;
7-
86
/**
97
* A class that contains information needed to configure the CommandAPI on Bukkit-based servers.
108
*/

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/CommandAPICommand.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@ public CommandAPICommand instance() {
2020
}
2121

2222
/**
23-
* Registers the command with a given namespace.
24-
*
25-
* @param namespace The namespace of this command. This cannot be null or empty.
23+
* Registers this command with the given namespace.
2624
*
25+
* @param namespace The namespace for this command. This cannot be null or empty, and can only contain 0-9, a-z, underscores, periods, and hyphens.
26+
* @throws NullPointerException if the namespace is null.
2727
*/
28+
@Override
2829
public void register(String namespace) {
29-
if (CommandAPIBukkit.get().isInvalidNamespace(this.name, namespace)) {
30-
super.register();
31-
return;
32-
}
3330
super.register(namespace);
3431
}
3532

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/CommandTree.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ public CommandTree instance() {
2020
}
2121

2222
/**
23-
* Registers the command with a given namespace
23+
* Registers this command with the given namespace.
2424
*
25-
* @param namespace The namespace of this command. This cannot be null or empty
25+
* @param namespace The namespace for this command. This cannot be null or empty, and can only contain 0-9, a-z, underscores, periods, and hyphens.
26+
* @throws NullPointerException if the namespace is null.
2627
*/
28+
@Override
2729
public void register(String namespace) {
28-
if (CommandAPIBukkit.get().isInvalidNamespace(this.name, namespace)) {
29-
super.register();
30-
return;
31-
}
3230
super.register(namespace);
3331
}
3432

commandapi-platforms/commandapi-velocity/commandapi-velocity-core/src/main/java/dev/jorel/commandapi/CommandAPICommand.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ public CommandAPICommand(String commandName) {
2020
/**
2121
* Registers the command with a given namespace
2222
*
23-
* @param namespace The namespace of this command. This cannot be null or empty
24-
*
23+
* @param namespace The namespace of this command. This cannot be null and can only contain 0-9, a-z, underscores, periods, and hyphens.
2524
*/
25+
@Override
2626
public void register(String namespace) {
27-
if (!namespace.isEmpty() && !CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
28-
super.register();
29-
return;
30-
}
3127
super.register(namespace);
3228
}
3329

@@ -38,7 +34,7 @@ public void register(String namespace) {
3834
*/
3935
public void register(Object plugin) {
4036
if (plugin == null) {
41-
throw new NullPointerException("Parameter 'plugin' was null while trying to register command /" + meta.commandName + "!");
37+
throw new NullPointerException("Parameter 'plugin' was null while trying to register command /" + this.getName() + "!");
4238
}
4339
ProxyServer server = CommandAPIVelocity.getConfiguration().getServer();
4440
Optional<PluginContainer> pluginContainerOptional = server.getPluginManager().fromInstance(plugin);

commandapi-platforms/commandapi-velocity/commandapi-velocity-core/src/main/java/dev/jorel/commandapi/CommandAPIVelocity.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,21 @@ public SuggestionProvider<CommandSource> getSuggestionProvider(SuggestionProvide
198198
return (context, builder) -> Suggestions.empty();
199199
}
200200

201+
/**
202+
* {@inheritDoc}
203+
* On Velocity, namespaces may be empty, but can only contain 0-9, a-z, underscores, periods, and hyphens.
204+
*/
205+
@Override
206+
public String validateNamespace(ExecutableCommand<?, CommandSource> command, String namespace) {
207+
if (!CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
208+
CommandAPI.logNormal("Registering comand '" + command.getName() + "' using the default namespace because an invalid namespace (" + namespace + ") was given. Only 0-9, a-z, underscores, periods and hyphens are allowed!");
209+
return config.getNamespace();
210+
}
211+
212+
// Namespace is good, return it
213+
return namespace;
214+
}
215+
201216
@Override
202217
public void preCommandRegistration(String commandName) {
203218
// Nothing to do

commandapi-platforms/commandapi-velocity/commandapi-velocity-core/src/main/java/dev/jorel/commandapi/CommandTree.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ public CommandTree(String commandName) {
2020
/**
2121
* Registers the command with a given namespace
2222
*
23-
* @param namespace The namespace of this command. This cannot be null or empty
24-
*
23+
* @param namespace The namespace of this command. This cannot be null and can only contain 0-9, a-z, underscores, periods, and hyphens.
2524
*/
25+
@Override
2626
public void register(String namespace) {
27-
if (!namespace.isEmpty() && !CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
28-
super.register();
29-
return;
30-
}
3127
super.register(namespace);
3228
}
3329

@@ -38,7 +34,7 @@ public void register(String namespace) {
3834
*/
3935
public void register(Object plugin) {
4036
if (plugin == null) {
41-
throw new NullPointerException("Parameter 'plugin' was null while trying to register command /" + meta.commandName + "!");
37+
throw new NullPointerException("Parameter 'plugin' was null while trying to register command /" + this.getName() + "!");
4238
}
4339
ProxyServer server = CommandAPIVelocity.getConfiguration().getServer();
4440
Optional<PluginContainer> pluginContainerOptional = server.getPluginManager().fromInstance(plugin);

0 commit comments

Comments
 (0)
0