diff --git a/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java b/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java index 43e52ce7e..bf2973bc8 100644 --- a/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/FlagsManager.java @@ -122,4 +122,16 @@ public class FlagsManager { // Remove flags flags.values().removeIf(addon::equals); } + + /** + * Unregister a specific flag + * @param Flag - flag + * @since 1.14.0 + */ + public void unregister(@NonNull Flag flag) { + // Unregister any listener + flag.getListener().ifPresent(HandlerList::unregisterAll); + // Remove flag + flags.remove(flag); + } } diff --git a/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java index 799e37c5c..3008449ce 100644 --- a/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java @@ -18,6 +18,7 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.World; +import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.meta.SkullMeta; @@ -39,7 +40,7 @@ import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; @RunWith(PowerMockRunner.class) -@PrepareForTest( {BentoBox.class, Bukkit.class, Util.class} ) +@PrepareForTest( {BentoBox.class, Bukkit.class, Util.class, HandlerList.class} ) public class FlagsManagerTest { /** @@ -57,7 +58,7 @@ public class FlagsManagerTest { public void setUp() throws Exception { // Set up plugin Whitebox.setInternalState(BentoBox.class, "instance", plugin); - + // Util class to handle PaperLib PowerMockito.mockStatic(Util.class); when(Util.isPaper()).thenReturn(false); @@ -151,4 +152,25 @@ public class FlagsManagerTest { } + /** + * Test for {@link FlagsManager#unregister(Flag)} + */ + @Test + public void testUnregisterFlag() { + PowerMockito.mockStatic(HandlerList.class); + when(plugin.isLoaded()).thenReturn(true); + FlagsManager fm = new FlagsManager(plugin); + // Listener + OriginalListener ol = new OriginalListener(); + Flag originalFlag = new Flag.Builder("ORIGINAL", Material.EMERALD_BLOCK).listener(ol).build(); + assertTrue(fm.registerFlag(originalFlag)); + assertEquals(originalFlag, fm.getFlag("ORIGINAL").get()); + // Remove + fm.unregister(originalFlag); + assertFalse(fm.getFlag("ORIGINAL").isPresent()); + // Verify the listener was removed + PowerMockito.verifyStatic(HandlerList.class); + HandlerList.unregisterAll(ol); + } + }