Close all panels on plugin disable (#743)

* Close all panels on plugin disable
https://github.com/BentoBoxWorld/BentoBox/issues/726

* Adds panel closing to bbox reload and fixes errors.
This commit is contained in:
Florian CUNY 2019-06-09 04:13:19 +02:00 committed by tastybento
parent eeb3e64a97
commit 272e99bc12
3 changed files with 27 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import java.util.stream.Collectors;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.PanelListenerManager;
/**
* Reloads settings, addons and localization.
@ -35,6 +36,9 @@ public class BentoBoxReloadCommand extends ConfirmableCommand {
public boolean execute(User user, String label, List<String> args) {
if (args.isEmpty()) {
this.askConfirmation(user, user.getTranslation("commands.bentobox.reload.warning"), () -> {
// Close all open panels
PanelListenerManager.closeAllPanels();
// Reload settings
getPlugin().loadSettings();
user.sendMessage("commands.bentobox.reload.settings-reloaded");

View File

@ -3,7 +3,9 @@ package world.bentobox.bentobox.listeners;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@ -11,6 +13,7 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.inventory.InventoryView;
import world.bentobox.bentobox.BentoBox;
@ -74,6 +77,22 @@ public class PanelListenerManager implements Listener {
openPanels.remove(event.getPlayer().getUniqueId());
}
@EventHandler(priority = EventPriority.NORMAL)
public void onPluginDisable(PluginDisableEvent event) {
if (event.getPlugin().getName().equals("BentoBox")) {
closeAllPanels();
}
}
/**
* Closes all open BentoBox panels
*/
public static void closeAllPanels() {
// Use stream clones to avoid concurrent modification exceptions
openPanels.values().stream().collect(Collectors.toList()).forEach(p ->
p.getInventory().getViewers().stream().collect(Collectors.toList()).forEach(HumanEntity::closeInventory));
}
/**
* @return the openPanels
*/

View File

@ -35,6 +35,7 @@ import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.AddonDescription;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.PanelListenerManager;
import world.bentobox.bentobox.managers.AddonsManager;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
@ -44,7 +45,7 @@ import world.bentobox.bentobox.managers.LocalesManager;
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
@PrepareForTest({Bukkit.class, BentoBox.class, User.class, PanelListenerManager.class })
public class BentoBoxReloadCommandTest {
@Mock
@ -103,6 +104,8 @@ public class BentoBoxReloadCommandTest {
// User
when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgumentAt(0, String.class));
// Panels
PowerMockito.mockStatic(PanelListenerManager.class);
// Command
reload = new BentoBoxReloadCommand(ac);