Added dynamic command registration

This commit is contained in:
Auxilor
2022-10-29 19:06:15 +01:00
parent 54b2b42512
commit 2efc040a8e
9 changed files with 147 additions and 3 deletions

View File

@@ -498,6 +498,11 @@ public interface Eco {
@Nullable
Menu getOpenMenu(@NotNull Player player);
/**
* Sync commands.
*/
void syncCommands();
/**
* Get the instance of eco; the bridge between the api frontend
* and the implementation backend.

View File

@@ -0,0 +1,44 @@
package com.willfp.eco.core.command.impl;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* Delegates a bukkit command to an eco command (for registrations).
*/
public final class DelegatedBukkitCommand extends Command implements TabCompleter {
/**
* The delegate command.
*/
private final PluginCommand delegate;
/**
* Create a new delegated command.
*
* @param delegate The delegate.
*/
public DelegatedBukkitCommand(@NotNull final PluginCommand delegate) {
super(delegate.getName());
this.delegate = delegate;
}
@Override
public boolean execute(@NotNull final CommandSender commandSender,
@NotNull final String label,
@NotNull final String[] args) {
return delegate.onCommand(commandSender, this, label, args);
}
@Override
public List<String> onTabComplete(@NotNull final CommandSender commandSender,
@NotNull final Command command,
@NotNull final String label,
@NotNull final String[] args) {
return delegate.onTabComplete(commandSender, command, label, args);
}
}

View File

@@ -1,14 +1,17 @@
package com.willfp.eco.core.command.impl;
import com.willfp.eco.core.Eco;
import com.willfp.eco.core.EcoPlugin;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Field;
import java.util.List;
/**
@@ -43,9 +46,21 @@ public abstract class PluginCommand extends HandledCommand implements CommandExe
*/
public final void register() {
org.bukkit.command.PluginCommand command = Bukkit.getPluginCommand(this.getName());
assert command != null;
command.setExecutor(this);
command.setTabCompleter(this);
if (command != null) {
command.setExecutor(this);
command.setTabCompleter(this);
} else {
CommandMap commandMap = getCommandMap();
Command bukkit = commandMap.getCommand(this.getName());
if (bukkit != null) {
bukkit.unregister(commandMap);
}
commandMap.register(this.getName(), new DelegatedBukkitCommand(this));
Eco.get().syncCommands();
}
}
/**
@@ -93,4 +108,19 @@ public abstract class PluginCommand extends HandledCommand implements CommandExe
return this.handleTabCompletion(sender, args);
}
/**
* Get the internal server CommandMap.
*
* @return The CommandMap.
*/
public static CommandMap getCommandMap() {
try {
Field field = Bukkit.getServer().getClass().getDeclaredField("commandMap");
field.setAccessible(true);
return (CommandMap) field.get(Bukkit.getServer());
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new NullPointerException("Command map wasn't found!");
}
}
}