Fixed protection listeners not being registered to Bukkit

Basically removed the "plugin loaded" limitation.
This commit is contained in:
Florian CUNY 2018-11-03 11:31:08 +01:00
parent 6108277b3c
commit 3e284b90eb
3 changed files with 6 additions and 15 deletions

View File

@ -135,6 +135,7 @@ public class BentoBox extends JavaPlugin {
islandsManager.save(true);
}, getSettings().getDatabaseBackupPeriod() * 20 * 60L, getSettings().getDatabaseBackupPeriod() * 20 * 60L);
// Make sure all flag listeners are ready.
flagsManager.registerListeners();
// Load metrics

View File

@ -53,19 +53,20 @@ public class FlagsManager {
}
/**
* 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);
}

View File

@ -104,20 +104,9 @@ public class FlagsManagerTest {
.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