diff --git a/src/main/java/net/mackenziemolloy/shopguiplus/sellgui/SellGUI.java b/src/main/java/net/mackenziemolloy/shopguiplus/sellgui/SellGUI.java index 74acdc1..98fb98b 100644 --- a/src/main/java/net/mackenziemolloy/shopguiplus/sellgui/SellGUI.java +++ b/src/main/java/net/mackenziemolloy/shopguiplus/sellgui/SellGUI.java @@ -48,6 +48,10 @@ public void onEnable() { scheduler = foliaLib.getScheduler(); new CommandSellGUI(this).register(); + new net.mackenziemolloy.shopguiplus.sellgui.utility.CommandRegistrar(this).registerAliases(); + getServer().getPluginManager() + .registerEvents(new net.mackenziemolloy.shopguiplus.sellgui.listeners.SellCommandListener(this), this); + Logger logger = getLogger(); checkCompatibility(); diff --git a/src/main/java/net/mackenziemolloy/shopguiplus/sellgui/listeners/SellCommandListener.java b/src/main/java/net/mackenziemolloy/shopguiplus/sellgui/listeners/SellCommandListener.java new file mode 100644 index 0000000..75fac7f --- /dev/null +++ b/src/main/java/net/mackenziemolloy/shopguiplus/sellgui/listeners/SellCommandListener.java @@ -0,0 +1,36 @@ +package net.mackenziemolloy.shopguiplus.sellgui.listeners; + +import net.mackenziemolloy.shopguiplus.sellgui.SellGUI; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; + +import java.util.Locale; + +public class SellCommandListener implements Listener { + + private final SellGUI plugin; + + public SellCommandListener(SellGUI plugin) { + this.plugin = plugin; + } + + @EventHandler(priority = EventPriority.LOWEST) + public void onCommandPreProcess(PlayerCommandPreprocessEvent event) { + String message = event.getMessage(); + String[] args = message.split(" "); + + if (args.length != 1) { + return; + } + + String command = args[0].substring(1).toLowerCase(Locale.US); + java.util.List aliases = plugin.getConfiguration().getStringList("options.commands.aliases"); + + if (aliases.contains(command)) { + event.setCancelled(true); + plugin.getCommand("sellgui").execute(event.getPlayer(), "sellgui", new String[0]); + } + } +} diff --git a/src/main/java/net/mackenziemolloy/shopguiplus/sellgui/utility/CommandRegistrar.java b/src/main/java/net/mackenziemolloy/shopguiplus/sellgui/utility/CommandRegistrar.java new file mode 100644 index 0000000..5624b6e --- /dev/null +++ b/src/main/java/net/mackenziemolloy/shopguiplus/sellgui/utility/CommandRegistrar.java @@ -0,0 +1,94 @@ +package net.mackenziemolloy.shopguiplus.sellgui.utility; + +import net.mackenziemolloy.shopguiplus.sellgui.SellGUI; +import org.bukkit.Bukkit; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.PluginCommand; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.SimplePluginManager; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.util.List; +import java.util.logging.Level; + +public class CommandRegistrar { + + private final SellGUI plugin; + + public CommandRegistrar(SellGUI plugin) { + this.plugin = plugin; + } + + public void registerAliases() { + List aliases = plugin.getConfiguration().getStringList("options.commands.aliases"); + if (aliases == null || aliases.isEmpty()) { + return; + } + + CommandMap commandMap = getCommandMap(); + if (commandMap == null) { + plugin.getLogger().severe("Could not retrieve CommandMap. Custom aliases will not work."); + return; + } + + PluginCommand sellGuiCommand = plugin.getCommand("sellgui"); + if (sellGuiCommand == null) { + plugin.getLogger().severe("The main 'sellgui' command is not registered!"); + return; + } + + for (String alias : aliases) { + Command command = commandMap.getCommand(alias); + if (command != null && !command.getLabel().equalsIgnoreCase(alias)) { + // If the command exists but under a fallback prefix (plugin:cmd), we might + // still want to register ours? + // Bukkit's getCommand returns the first match. + // If Essentials has 'sell', getCommand('sell') returns it. + } + + // We only skip if there is a command that directly matches the alias + if (command != null + && (command.getLabel().equalsIgnoreCase(alias) || command.getAliases().contains(alias))) { + // plugin.getLogger().info("Skipping registration of alias '" + alias + "' as it + // is already registered."); + continue; + } + + PluginCommand aliasCommand = createPluginCommand(alias, plugin); + if (aliasCommand != null) { + aliasCommand.setExecutor(sellGuiCommand.getExecutor()); + aliasCommand.setTabCompleter(sellGuiCommand.getTabCompleter()); + aliasCommand.setDescription(sellGuiCommand.getDescription()); + + commandMap.register(plugin.getDescription().getName(), aliasCommand); + } + } + } + + private CommandMap getCommandMap() { + try { + if (Bukkit.getPluginManager() instanceof SimplePluginManager) { + Field f = SimplePluginManager.class.getDeclaredField("commandMap"); + f.setAccessible(true); + return (CommandMap) f.get(Bukkit.getPluginManager()); + } + } catch (Exception e) { + plugin.getLogger().log(Level.SEVERE, "Failed to get command map", e); + } + return null; + } + + private PluginCommand createPluginCommand(String name, Plugin plugin) { + try { + Constructor c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); + c.setAccessible(true); + return c.newInstance(name, plugin); + } catch (Exception e) { + plugin.getLogger().log(Level.SEVERE, "Failed to create PluginCommand for alias: " + name, e); + return null; + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 689b53a..e09df66 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -17,6 +17,18 @@ # options: + # + # Command Configuration + # + commands: + # + # Custom aliases for /sellgui + # + aliases: + - "sell" + - "sg" + - "sellg" + # # 0 - None # 1 - Hover message (Adds "receipt_text" to the end of the message)