Skip to content

Commit

Permalink
Merge pull request #526 from JorelAli/dev/velocity-plugin-namespaces
Browse files Browse the repository at this point in the history
Add support for plugin namespaces on Velocity
  • Loading branch information
DerEchtePilz authored Mar 14, 2024
2 parents a653769 + e287520 commit 19247bb
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,13 @@ public Impl setNamespace(String namespace) {
return instance();
}

/**
* Sets whether the CommandAPI should use the plugin's name as the default namespace
* <p>
* If called, any call to {@link CommandAPIConfig#setNamespace(String)} will be ignored
*
* @return this CommandAPIConfig
*/
public abstract Impl usePluginNamespace();

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ExamplePlugin(ProxyServer server, Logger logger) {
this.server = server;
this.logger = logger;

CommandAPI.onLoad(new CommandAPIVelocityConfig(server));
CommandAPI.onLoad(new CommandAPIVelocityConfig(server, this));
}
/* ANCHOR_END: velocityIntro1 */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ public CommandAPIBukkitConfig useMojangMappings(boolean useMojangMappings) {
}

/**
* Sets whether the CommandAPI should use the plugin's name as the default namespace
* <p>
* If called, any call to {@link CommandAPIConfig#setNamespace(String)} will be ignored
*
* @return this CommandAPIBukkitConfig
*/
public CommandAPIBukkitConfig usePluginNamespace() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package dev.jorel.commandapi;

import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.proxy.ProxyServer;
import dev.jorel.commandapi.arguments.Argument;

import java.util.Optional;

public class CommandAPICommand extends AbstractCommandAPICommand<CommandAPICommand, Argument<?>, CommandSource> implements VelocityExecutable<CommandAPICommand> {
/**
* Creates a new command builder
Expand Down Expand Up @@ -41,6 +45,25 @@ public void register(String namespace) {
super.register(namespace);
}

/**
* Registers the command with the given plugin object
*
* @param plugin The plugin instance used to determine this command's namespace
*/
public void register(Object plugin) {
if (plugin == null) {
throw new NullPointerException("Parameter 'plugin' was null while trying to register command /" + meta.commandName + "!");
}
ProxyServer server = CommandAPIVelocity.getConfiguration().getServer();
Optional<PluginContainer> pluginContainerOptional = server.getPluginManager().fromInstance(plugin);
if (pluginContainerOptional.isEmpty()) {
CommandAPI.logInfo("Using the default namespace to register commands since " + plugin.getClass().getSimpleName() + " is not a Velocity plugin!");
super.register();
return;
}
super.register(pluginContainerOptional.get().getDescription().getId());
}

@Override
public CommandAPICommand instance() {
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
package dev.jorel.commandapi;

import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.proxy.ProxyServer;

import java.util.Optional;

/**
* A class that contains information needed to configure the CommandAPI on Velocity-based servers.
*/
public class CommandAPIVelocityConfig extends CommandAPIConfig<CommandAPIVelocityConfig> {
ProxyServer server;
Object plugin;

/**
* Creates a new CommandAPIVelocityConfig object. Variables in this
* constructor are required to load the CommandAPI on Velocity properly.
*
* @param server The {@link ProxyServer} that the CommandAPI is running on.
* @param plugin The plugin that is loading the CommandAPI.
*/
public CommandAPIVelocityConfig(ProxyServer server) {
public CommandAPIVelocityConfig(ProxyServer server, Object plugin) {
this.server = server;
this.plugin = plugin;
super.setNamespace("");
}

/**
* @return this CommandAPIVelocityConfig
*/
@Override
public CommandAPIVelocityConfig usePluginNamespace() {
Optional<PluginContainer> pluginContainerOptional = server.getPluginManager().fromInstance(plugin);
if (pluginContainerOptional.isEmpty()) {
CommandAPI.logInfo("Cannot use plugin namespace because " + plugin.getClass().getSimpleName() + " is not a Velocity plugin! The currently set namespace wasn't changed.");
return instance();
}
super.setNamespace(pluginContainerOptional.get().getDescription().getId());
super.usePluginNamespace = true;
return instance();
}

@Override
public CommandAPIVelocityConfig setNamespace(String namespace) {
if (namespace == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package dev.jorel.commandapi;

import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.proxy.ProxyServer;
import dev.jorel.commandapi.arguments.Argument;

import java.util.Optional;

public class CommandTree extends AbstractCommandTree<CommandTree, Argument<?>, CommandSource> implements VelocityExecutable<CommandTree> {
/**
* Creates a main root node for a command tree with a given command name
Expand All @@ -27,6 +31,25 @@ public void register(String namespace) {
super.register(namespace);
}

/**
* Registers the command with the given plugin object
*
* @param plugin The plugin instance used to determine this command's namespace
*/
public void register(Object plugin) {
if (plugin == null) {
throw new NullPointerException("Parameter 'plugin' was null while trying to register command /" + meta.commandName + "!");
}
ProxyServer server = CommandAPIVelocity.getConfiguration().getServer();
Optional<PluginContainer> pluginContainerOptional = server.getPluginManager().fromInstance(plugin);
if (pluginContainerOptional.isEmpty()) {
CommandAPI.logInfo("Using the default namespace to register commands since " + plugin.getClass().getSimpleName() + " is not a Velocity plugin!");
super.register();
return;
}
super.register(pluginContainerOptional.get().getDescription().getId());
}

@Override
public CommandTree instance() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class InternalVelocityConfig extends InternalConfig {
// The server that the CommandAPI is running on
private final ProxyServer server;

// The plugin that loads the CommandAPI
private final Object plugin;

/**
* Creates an {@link InternalVelocityConfig} from a {@link CommandAPIVelocityConfig}
*
Expand All @@ -19,6 +22,7 @@ public class InternalVelocityConfig extends InternalConfig {
public InternalVelocityConfig(CommandAPIVelocityConfig config) {
super(config);
this.server = config.server;
this.plugin = config.plugin;
}

/**
Expand All @@ -27,4 +31,11 @@ public InternalVelocityConfig(CommandAPIVelocityConfig config) {
public ProxyServer getServer() {
return server;
}

/**
* @return The plugin that loads the CommandAPI
*/
public Object getPlugin() {
return plugin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public CommandAPIMain(ProxyServer server, Logger logger, @DataDirectory Path dat
}

// Configure the CommandAPI
CommandAPIVelocityConfig config = new CommandAPIVelocityConfig(server)
CommandAPIVelocityConfig config = new CommandAPIVelocityConfig(server, this)
.verboseOutput(configYAML.getNode("verbose-outputs").getBoolean())
.silentLogs(configYAML.getNode("silent-logs").getBoolean())
.missingExecutorImplementationMessage(configYAML.getNode("messages", "missing-executor-implementation").getString())
Expand Down
2 changes: 1 addition & 1 deletion docssrc/src/setup_shading.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class CommandAPIConfig {
CommandAPIConfig missingExecutorImplementationMessage(String value); // Set message to display when executor implementation is missing
CommandAPIConfig dispatcherFile(File file); // If not null, the CommandAPI will create a JSON file with Brigadier's command tree
CommandAPIConfig setNamespace(String namespace); // The namespace to use when the CommandAPI registers a command
CommandAPIConfig usePluginNamespace(); // Whether the CommandAPI should use the name of the plugin passed into the CommandAPIConfig implementation as the default namespace for commands

<T> CommandAPIConfig initializeNBTAPI(Class<T> nbtContainerClass, Function<Object, T> nbtContainerConstructor); // Initializes hooks with an NBT API. See NBT arguments documentation page for more info
}
Expand All @@ -51,7 +52,6 @@ public class CommandAPIBukkitConfig extends CommandAPIConfig {
CommandAPIBukkitConfig(JavaPlugin plugin);

CommandAPIBukkitConfig shouldHookPaperReload(boolean hooked); // Whether the CommandAPI should hook into the Paper-exclusive ServerResourcesReloadedEvent
CommandAPIBukkitConfig usePluginNamespace(); // Whether the CommandAPI should use the name of the plugin passed into the CommandAPIBukkitConfig as the default namespace for commands
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Main(ProxyServer server, Logger logger, @DataDirectory Path dataFolder) {
// Load the CommandAPI first
CommandAPI.onLoad(
// Configure the CommandAPI
new CommandAPIVelocityConfig(server)
new CommandAPIVelocityConfig(server, this)
// Turn on verbose output for command registration logs
.verboseOutput(true)
// Give file where Brigadier's command registration tree should be dumped
Expand Down

0 comments on commit 19247bb

Please sign in to comment.