From 3e284b90eb2bf51261e5e768a7be597137857456 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sat, 3 Nov 2018 11:31:08 +0100 Subject: [PATCH] Fixed protection listeners not being registered to Bukkit Basically removed the "plugin loaded" limitation. --- src/main/java/world/bentobox/bentobox/BentoBox.java | 1 + .../bentobox/bentobox/managers/FlagsManager.java | 7 ++++--- .../bentobox/managers/FlagsManagerTest.java | 13 +------------ 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index aa21ba4f9..8b41a6c2c 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -135,6 +135,7 @@ public void onEnable(){ islandsManager.save(true); }, getSettings().getDatabaseBackupPeriod() * 20 * 60L, getSettings().getDatabaseBackupPeriod() * 20 * 60L); + // Make sure all flag listeners are ready. flagsManager.registerListeners(); // Load metrics diff --git a/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java b/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java index 77594794f..271bbe14a 100644 --- a/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java @@ -53,19 +53,20 @@ public boolean registerFlag(Flag flag) { } /** - * Register any unregistered listeners - called after the plugin is fully loaded + * Register any unregistered listeners. + * This helps to make sure each flag listener is correctly loaded. */ public void registerListeners() { registeredListeners.entrySet().stream().filter(e -> !e.getValue()).map(Map.Entry::getKey).forEach(this::registerListener); } /** - * Tries to register a listener if the plugin is loaded + * Tries to register a listener * @param l - listener */ private void registerListener(Listener l) { registeredListeners.putIfAbsent(l, false); - if (BentoBox.getInstance().isLoaded() && !registeredListeners.get(l)) { + if (!registeredListeners.get(l)) { Bukkit.getServer().getPluginManager().registerEvents(l, plugin); registeredListeners.put(l, true); } diff --git a/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java index 2e097cedd..e100d9fc5 100644 --- a/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java @@ -104,20 +104,9 @@ public void testRegisterDuplicateFlagIcons() { .build()))); } - @Test - public void testRegisteroriginalFlagPluginNotLoaded() { - when(plugin.isLoaded()).thenReturn(false); - FlagsManager fm = new FlagsManager(plugin); - // This should pass - Flag originalFlag = new FlagBuilder().id("ORIGINAL").icon(Material.EMERALD_BLOCK).listener(new BreakBlocksListener()).build(); - assertTrue(fm.registerFlag(originalFlag)); - // Verify no Bukkit listener registered - Mockito.verify(server, Mockito.never()).getPluginManager(); - } - @Test - public void testRegisteroriginalFlagPluginLoadedOriginalListener() { + public void testRegisterOriginalFlagOriginalListener() { when(plugin.isLoaded()).thenReturn(true); FlagsManager fm = new FlagsManager(plugin); // This should pass