Skip to content

Commit

Permalink
Fixed protection listeners not being registered to Bukkit
Browse files Browse the repository at this point in the history
Basically removed the "plugin loaded" limitation.
  • Loading branch information
Poslovitch committed Nov 3, 2018
1 parent 6108277 commit 3e284b9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/main/java/world/bentobox/bentobox/BentoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3e284b9

Please sign in to comment.