Skip to content

Commit

Permalink
Finish some tasks for Bukkit runtime register PR (JorelAli#417)
Browse files Browse the repository at this point in the history
Remove messages that registering/unregistering while the server is running does not work

Remove test commands

Update relevant documentation
  • Loading branch information
willkroboth committed Jul 26, 2023
1 parent 2c86bd1 commit 5c9e85f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,6 @@ private void checkHasExecutors() {

@Override
public void register() {
if (!CommandAPI.canRegister()) {
CommandAPI.logWarning("Command /" + meta.commandName + " is being registered after the server had loaded. Undefined behavior ahead!");
}

@SuppressWarnings("unchecked")
Argument[] argumentsArray = (Argument[]) (arguments == null ? new AbstractArgument[0] : arguments.toArray(AbstractArgument[]::new));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,6 @@ public static void unregister(String command) {
* across all plugins as well as minecraft, bukkit and spigot
*/
public static void unregister(String command, boolean force) {
if (!canRegister) {
getLogger().warning("Unexpected unregistering of /" + command
+ ", as server is loaded! Unregistering anyway, but this can lead to unstable results!");
}
CommandAPIHandler.getInstance().getPlatform().unregister(command, force);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,26 @@
*******************************************************************************/
package dev.jorel.commandapi;

import de.tr7zw.changeme.nbtapi.NBTContainer;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
import dev.jorel.commandapi.arguments.GreedyStringArgument;
import dev.jorel.commandapi.arguments.StringArgument;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import de.tr7zw.changeme.nbtapi.NBTContainer;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;

/**
* Main CommandAPI plugin entrypoint
*/
public class CommandAPIMain extends JavaPlugin {

private static final String PLUGINS_TO_CONVERT = "plugins-to-convert";

@Override
Expand Down Expand Up @@ -85,7 +84,7 @@ public void onLoad() {

convertCommands(fileConfig);
}

private void convertCommands(FileConfiguration fileConfig) {
// Load all plugins at the same time
Map<JavaPlugin, String[]> pluginsToConvert = new HashMap<>();
Expand All @@ -102,7 +101,7 @@ private void convertCommands(FileConfiguration fileConfig) {
// Get the plugin, if it doesn't exist, scream in the console (but
// don't crash, we want to continue!)
final JavaPlugin plugin = getAndValidatePlugin((String) map.keySet().iterator().next());
if(plugin != null) {
if (plugin != null) {
pluginsToConvert.put(plugin, pluginCommands);
}
}
Expand All @@ -123,7 +122,7 @@ private void convertCommands(FileConfiguration fileConfig) {
new AdvancedConverter(commandName).convertCommand();
}
}

private JavaPlugin getAndValidatePlugin(String pluginName) {
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
if (plugin != null) {
Expand All @@ -142,49 +141,5 @@ private JavaPlugin getAndValidatePlugin(String pluginName) {
@Override
public void onEnable() {
CommandAPI.onEnable();

new CommandAPICommand("register")
.withArguments(new StringArgument("command"))
.withOptionalArguments(
new GreedyStringArgument("aliases")
)
.executes(info -> {
String name = info.args().getUnchecked("command");
assert name != null;

String aliasString = info.args().getUnchecked("aliases");
String[] aliases;
if(aliasString == null)
aliases = new String[0];
else
aliases = aliasString.split(" ");

new CommandAPICommand(name)
.withAliases(aliases)
.executes(i -> {
i.sender().sendMessage("You ran the " + name + " command!");
})
.withPermission("dynamic." + name)
.withShortDescription("New command!")
.withFullDescription("This command was added while the server was running. Do you see it?")
.register();
})
.register();

new CommandAPICommand("unregister")
.withArguments(new StringArgument("command"))
.executes(info -> {
String name = info.args().getUnchecked("command");
assert name != null;

CommandAPI.unregister(name, true);
})
.register();

new CommandAPICommand("updateRequirements")
.executesPlayer(info -> {
CommandAPI.updateRequirements(info.sender());
})
.register();
}
}
4 changes: 3 additions & 1 deletion docssrc/src/commandregistration.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@ Registers the command.

## Command loading order

In order to register commands properly, **commands must be registered before the server finishes loading**. The CommandAPI will output a warning if you register a command after the server has loaded. This means that all command registration must occur during a plugin's `onLoad()` or `onEnable()` method. With the CommandAPI, depending on whether you use `onLoad()` or `onEnable()` to load your commands depends on whether your plugin is used with Minecraft's functions:
It is recommended to register commands in either the `onLoad()` or `onEnable()` method. With the CommandAPI, depending on whether you use `onLoad()` or `onEnable()` to load your commands depends on whether your plugin is used with Minecraft's functions:

| When to load | What to do |
| ------------------- | -------------------------------------------------------------------------------------------------------------- |
| `onLoad()` method | Register commands to be used in Minecraft functions ([see the Function section for more info](functions.html)) |
| `onEnable()` method | Register regular commands |

The CommandAPI does support registering commands outside of these methods while the server is running. Commands registered after the server is done loading *should* work the same as commands registered in `onEnable`.

Check failure on line 186 in docssrc/src/commandregistration.md

View workflow job for this annotation

GitHub Actions / build (17, ubuntu-20.04)

Emphasis style should be consistent [Expected: underscore; Actual: asterisk]

docssrc/src/commandregistration.md:186:157 MD049/emphasis-style Emphasis style should be consistent [Expected: underscore; Actual: asterisk]

-----

## Command unregistration
Expand Down

0 comments on commit 5c9e85f

Please sign in to comment.