From 3b62db46b68a3d63278e2528ed447c0be6587f34 Mon Sep 17 00:00:00 2001 From: Jikoo Date: Sun, 28 Jul 2024 09:20:40 -0400 Subject: [PATCH] Extract registrations to helper methods --- .../main/java/com/lishid/openinv/OpenInv.java | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/plugin/src/main/java/com/lishid/openinv/OpenInv.java b/plugin/src/main/java/com/lishid/openinv/OpenInv.java index 55597a1e..3a6cfea6 100644 --- a/plugin/src/main/java/com/lishid/openinv/OpenInv.java +++ b/plugin/src/main/java/com/lishid/openinv/OpenInv.java @@ -126,27 +126,11 @@ public void onEnable() { // Update existing configuration. May require internal access. new ConfigUpdater(this).checkForUpdates(); - // Get plugin manager - PluginManager pm = this.getServer().getPluginManager(); - // Register listeners - if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 21))) { - pm.registerEvents(new LegacyInventoryListener(this), this); - } - pm.registerEvents(playerLoader, this); - pm.registerEvents(inventoryManager, this); - pm.registerEvents(new ContainerListener(accessor, languageManager), this); - pm.registerEvents(new ToggleListener(), this); - - // Register commands to their executors - this.setCommandExecutor(new OpenInvCommand(this, config, inventoryManager, languageManager, playerLoader), "openinv", "openender"); - this.setCommandExecutor(new SearchContainerCommand(this, languageManager), "searchcontainer"); - this.setCommandExecutor(new SearchInvCommand(languageManager), "searchinv", "searchender"); - this.setCommandExecutor(new SearchEnchantCommand(languageManager), "searchenchant"); - - ContainerSettingCommand settingCommand = new ContainerSettingCommand(languageManager); - for (PlayerToggle toggle : PlayerToggles.get()) { - setCommandExecutor(settingCommand, toggle.getName()); - } + // Register relevant event listeners. + registerEvents(); + + // Register commands to their executors. + registerCommands(); } else { this.sendVersionError(this.getLogger()::warning); @@ -154,6 +138,30 @@ public void onEnable() { } + private void registerEvents() { + PluginManager pluginManager = this.getServer().getPluginManager(); + // Legacy: extra listener for permission handling and self-view issue prevention. + if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 21))) { + pluginManager.registerEvents(new LegacyInventoryListener(this), this); + } + pluginManager.registerEvents(playerLoader, this); + pluginManager.registerEvents(inventoryManager, this); + pluginManager.registerEvents(new ContainerListener(accessor, languageManager), this); + pluginManager.registerEvents(new ToggleListener(), this); + } + + private void registerCommands() { + this.setCommandExecutor(new OpenInvCommand(this, config, inventoryManager, languageManager, playerLoader), "openinv", "openender"); + this.setCommandExecutor(new SearchContainerCommand(this, languageManager), "searchcontainer"); + this.setCommandExecutor(new SearchInvCommand(languageManager), "searchinv", "searchender"); + this.setCommandExecutor(new SearchEnchantCommand(languageManager), "searchenchant"); + + ContainerSettingCommand settingCommand = new ContainerSettingCommand(languageManager); + for (PlayerToggle toggle : PlayerToggles.get()) { + setCommandExecutor(settingCommand, toggle.getName()); + } + } + private void setCommandExecutor(@NotNull CommandExecutor executor, String @NotNull ... commands) { for (String commandName : commands) { PluginCommand command = this.getCommand(commandName);