updated command registration for usage without static plugin.yml file

This commit is contained in:
2025-04-04 22:51:01 +02:00
parent 4592d53d22
commit 8742f5f631
2 changed files with 14 additions and 57 deletions

View File

@@ -9,9 +9,11 @@ import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@@ -104,16 +106,22 @@ public abstract class Appliance {
}
private void setCommandExecutor(JavaPlugin plugin, String name, ApplianceCommand<?> executor) {
PluginCommand command = plugin.getCommand(name);
if(command != null && executor != null) {
try {
PluginCommand command = this.createPluginCommand(name, plugin);
command.setExecutor(executor);
command.setTabCompleter(executor);
} else {
Main.logger().warning("Command " + name + " is not specified in plugin.yml!");
throw new RuntimeException("All commands must be registered in plugin.yml. Missing command: " + name);
plugin.getServer().getCommandMap().register(plugin.getName(), command);
} catch(Exception e) {
throw new RuntimeException(String.format("Failed to register command '%s'", name), e);
}
}
private PluginCommand createPluginCommand(String name, JavaPlugin plugin) throws Exception {
Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
constructor.setAccessible(true);
return constructor.newInstance(name, plugin);
}
public List<Listener> getListeners() {
return this.listeners;
}