Skip to content

Commit

Permalink
Update dev/command-build-rewrite after rebasing #509 and #526
Browse files Browse the repository at this point in the history
  • Loading branch information
willkroboth committed Apr 30, 2024
1 parent 4a634bb commit 958ac7b
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,25 @@ public CommandAPIPlatform<Argument, CommandSender, Source> getPlatform() {
// SECTION: Creating commands //
////////////////////////////////

/**
* Registers a command with a given namespace. This is intended to be called by {@link ExecutableCommand#register(String)}
*
* @param command The command to register.
* @param namespace The namespace of this command. This cannot be null, and each platform may impose additional requirements.
* See {@link CommandAPIPlatform#validateNamespace(ExecutableCommand, String)}.
* @throws NullPointerException if the namespace is null.
*/
public void registerCommand(ExecutableCommand<?, CommandSender> command, String namespace) {
// Validate parameters
if (namespace == null) {
throw new NullPointerException("Parameter 'namespace' was null when registering command /" + command.getName() + "!");
}
namespace = platform.validateNamespace(command, namespace);

// Do plaform-specific pre-registration tasks
platform.preCommandRegistration(command.getName());

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

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

// Do platform-specific post-registration tasks
platform.postCommandRegistration(registeredCommandInformation, resultantNode, aliasNodes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ public interface CommandAPIPlatform<Argument
// Some commands have existing suggestion providers
public abstract SuggestionProvider<Source> getSuggestionProvider(SuggestionProviders suggestionProvider);

/**
* Ensures the given String is a valid command namespace on this platform. If the namespace
* is not valid, this method will return a String that should be used instead.
*
* @param command The command being registered with the given namespace.
* @param namespace The String that wants to be used as a namespace.
* @return The String that should be used as the namespace. If the given String is a valid namespace, it will be returned.
*/
public abstract String validateNamespace(ExecutableCommand<?, CommandSender> command, String namespace);

/**
* Stuff to run before a command is generated. For Bukkit, this involves checking
* if a command was declared in the plugin.yml when it isn't supposed to be.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package dev.jorel.commandapi;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;

import dev.jorel.commandapi.commandsenders.AbstractCommandSender;
import dev.jorel.commandapi.exceptions.InvalidCommandNameException;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
* This is a base class for {@link AbstractCommandAPICommand} and {@link AbstractCommandTree} command definitions
*
Expand Down Expand Up @@ -317,15 +318,13 @@ public void register() {
}

/**
* Registers the command with a given namespace.
* Registers the command with the given namespace.
*
* @param namespace The namespace of this command. This cannot be null
* @throws NullPointerException if the namespace is null
* @param namespace The namespace for this command. This cannot be null, and each platform may impose additional requirements.
* See {@link CommandAPIPlatform#validateNamespace(ExecutableCommand, String)}.
* @throws NullPointerException if the namespace is null.
*/
public void register(String namespace) {
if (namespace == null) {
throw new NullPointerException("Parameter 'namespace' was null when registering command /" + this.name + "!");
}
((CommandAPIHandler<?, CommandSender, ?>) CommandAPIHandler.getInstance()).registerCommand(this, namespace);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,25 @@ public BukkitCommandSender<? extends CommandSender> wrapCommandSender(CommandSen
@Unimplemented(because = REQUIRES_MINECRAFT_SERVER)
public abstract SuggestionProvider<Source> getSuggestionProvider(SuggestionProviders suggestionProvider);

/**
* {@inheritDoc}
* On Bukkit, namespaces must not be empty, and can only contain 0-9, a-z, underscores, periods, and hyphens.
*/
@Override
public String validateNamespace(ExecutableCommand<?, CommandSender> command, String namespace) {
if (namespace.isEmpty()) {
CommandAPI.logNormal("Registering command '" + command.getName() + "' using the default namespace because an empty namespace was given!");
return config.getNamespace();
}
if (!CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
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!");
return config.getNamespace();
}

// Namespace is good, return it
return namespace;
}

@Override
public void preCommandRegistration(String commandName) {
// Warn if the command we're registering already exists in this plugin's
Expand Down Expand Up @@ -925,20 +944,4 @@ protected void registerBukkitRecipesSafely(Iterator<Recipe> recipes) {
}
}
}

boolean isInvalidNamespace(String commandName, String namespace) {
if (namespace == null) {
throw new NullPointerException("Parameter 'namespace' was null when registering command /" + commandName + "!");
}
if (namespace.isEmpty()) {
CommandAPI.logNormal("Registering command '" + commandName + "' using the default namespace because an empty namespace was given!");
return true;
}
if (!CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
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!");
return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import io.papermc.paper.event.server.ServerResourcesReloadedEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.regex.Pattern;

/**
* A class that contains information needed to configure the CommandAPI on Bukkit-based servers.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,13 @@ public CommandAPICommand withHelp(HelpTopic helpTopic) {
}

/**
* Registers the command with a given namespace.
*
* @param namespace The namespace of this command. This cannot be null or empty.
* Registers this command with the given namespace.
*
* @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.
* @throws NullPointerException if the namespace is null.
*/
@Override
public void register(String namespace) {
if (CommandAPIBukkit.get().isInvalidNamespace(this.name, namespace)) {
super.register();
return;
}
super.register(namespace);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@ public CommandTree withHelp(HelpTopic helpTopic) {
}

/**
* Registers the command with a given namespace
* Registers this command with the given namespace.
*
* @param namespace The namespace of this command. This cannot be null or empty
* @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.
* @throws NullPointerException if the namespace is null.
*/
@Override
public void register(String namespace) {
if (CommandAPIBukkit.get().isInvalidNamespace(this.name, namespace)) {
super.register();
return;
}
super.register(namespace);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ public CommandAPICommand(String commandName) {
/**
* Registers the command with a given namespace
*
* @param namespace The namespace of this command. This cannot be null or empty
*
* @param namespace The namespace of this command. This cannot be null and can only contain 0-9, a-z, underscores, periods, and hyphens.
*/
@Override
public void register(String namespace) {
if (!namespace.isEmpty() && !CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
super.register();
return;
}
super.register(namespace);
}

Expand All @@ -38,7 +34,7 @@ public void register(String namespace) {
*/
public void register(Object plugin) {
if (plugin == null) {
throw new NullPointerException("Parameter 'plugin' was null while trying to register command /" + meta.commandName + "!");
throw new NullPointerException("Parameter 'plugin' was null while trying to register command /" + this.getName() + "!");
}
ProxyServer server = CommandAPIVelocity.getConfiguration().getServer();
Optional<PluginContainer> pluginContainerOptional = server.getPluginManager().fromInstance(plugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ public SuggestionProvider<CommandSource> getSuggestionProvider(SuggestionProvide
return (context, builder) -> Suggestions.empty();
}

/**
* {@inheritDoc}
* On Velocity, namespaces may be empty, but can only contain 0-9, a-z, underscores, periods, and hyphens.
*/
@Override
public String validateNamespace(ExecutableCommand<?, CommandSource> command, String namespace) {
if (!CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
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!");
return config.getNamespace();
}

// Namespace is good, return it
return namespace;
}

@Override
public void preCommandRegistration(String commandName) {
// Nothing to do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ public CommandTree(String commandName) {
/**
* Registers the command with a given namespace
*
* @param namespace The namespace of this command. This cannot be null or empty
*
* @param namespace The namespace of this command. This cannot be null and can only contain 0-9, a-z, underscores, periods, and hyphens.
*/
@Override
public void register(String namespace) {
if (!namespace.isEmpty() && !CommandAPIHandler.NAMESPACE_PATTERN.matcher(namespace).matches()) {
super.register();
return;
}
super.register(namespace);
}

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

0 comments on commit 958ac7b

Please sign in to comment.