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

Skip to content

Commit 2c3a989

Browse files
committed
Update dev/command-build-rewrite after rebasing #509 and #526
1 parent faed7e6 commit 2c3a989

File tree

9 files changed

+84
-52
lines changed

9 files changed

+84
-52
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
*
@@ -317,15 +318,13 @@ public void register() {
317318
}
318319

319320
/**
320-
* Registers the command with a given namespace.
321+
* Registers the command with the given namespace.
321322
*
322-
* @param namespace The namespace of this command. This cannot be null
323-
* @throws NullPointerException if the namespace is null
323+
* @param namespace The namespace for this command. This cannot be null, and each platform may impose additional requirements.
324+
* See {@link CommandAPIPlatform#validateNamespace(ExecutableCommand, String)}.
325+
* @throws NullPointerException if the namespace is null.
324326
*/
325327
public void register(String namespace) {
326-
if (namespace == null) {
327-
throw new NullPointerException("Parameter 'namespace' was null when registering command /" + this.name + "!");
328-
}
329328
((CommandAPIHandler<?, CommandSender, ?>) CommandAPIHandler.getInstance()).registerCommand(this, namespace);
330329
}
331330

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

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

426+
/**
427+
* {@inheritDoc}
428+
* On Bukkit, namespaces must not be empty, and can only contain 0-9, a-z, underscores, periods, and hyphens.
429+
*/
430+
@Override
431+
public String validateNamespace(ExecutableCommand<?, CommandSender> command, String namespace) {
432+
if (namespace.isEmpty()) {
433+
CommandAPI.logNormal("Registering command '" + command.getName() + "' using the default namespace because an empty namespace was given!");
434+
return config.getNamespace();
435+
}
436+
if (!CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
437+
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!");
438+
return config.getNamespace();
439+
}
440+
441+
// Namespace is good, return it
442+
return namespace;
443+
}
444+
426445
@Override
427446
public void preCommandRegistration(String commandName) {
428447
// Warn if the command we're registering already exists in this plugin's
@@ -641,19 +660,4 @@ protected void registerBukkitRecipesSafely(Iterator<Recipe> recipes) {
641660
}
642661
}
643662
}
644-
645-
boolean isInvalidNamespace(String commandName, String namespace) {
646-
if (namespace == null) {
647-
throw new NullPointerException("Parameter 'namespace' was null when registering command /" + commandName + "!");
648-
}
649-
if (namespace.isEmpty()) {
650-
CommandAPI.logNormal("Registering command '" + commandName + "' using the default namespace because an empty namespace was given!");
651-
return true;
652-
}
653-
if (!CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
654-
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!");
655-
return true;
656-
}
657-
return false;
658-
}
659663
}

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
@@ -42,16 +42,13 @@ public CommandAPICommand withHelp(HelpTopic helpTopic) {
4242
}
4343

4444
/**
45-
* Registers the command with a given namespace.
46-
*
47-
* @param namespace The namespace of this command. This cannot be null or empty.
45+
* Registers this command with the given namespace.
4846
*
47+
* @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.
48+
* @throws NullPointerException if the namespace is null.
4949
*/
50+
@Override
5051
public void register(String namespace) {
51-
if (CommandAPIBukkit.get().isInvalidNamespace(this.name, namespace)) {
52-
super.register();
53-
return;
54-
}
5552
super.register(namespace);
5653
}
5754

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
@@ -42,15 +42,13 @@ public CommandTree withHelp(HelpTopic helpTopic) {
4242
}
4343

4444
/**
45-
* Registers the command with a given namespace
45+
* Registers this command with the given namespace.
4646
*
47-
* @param namespace The namespace of this command. This cannot be null or empty
47+
* @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.
48+
* @throws NullPointerException if the namespace is null.
4849
*/
50+
@Override
4951
public void register(String namespace) {
50-
if (CommandAPIBukkit.get().isInvalidNamespace(this.name, namespace)) {
51-
super.register();
52-
return;
53-
}
5452
super.register(namespace);
5553
}
5654

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