mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-24 19:55:17 +01:00
Merge pull request #270 from BentoBoxWorld/fix-outside-closing-inventory
Fixed clicking outside of inventory closing even vanilla inventories
This commit is contained in:
commit
13bc945b40
@ -24,18 +24,19 @@ public class PanelListenerManager implements Listener {
|
|||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
public void onInventoryClick(InventoryClickEvent event) {
|
public void onInventoryClick(InventoryClickEvent event) {
|
||||||
// Close inventory if clicked outside and if setting is true
|
|
||||||
if (BentoBox.getInstance().getSettings().isClosePanelOnClickOutside() && event.getSlotType().equals(SlotType.OUTSIDE)) {
|
|
||||||
event.getWhoClicked().closeInventory();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
User user = User.getInstance(event.getWhoClicked()); // The player that clicked the item
|
User user = User.getInstance(event.getWhoClicked()); // The player that clicked the item
|
||||||
Inventory inventory = event.getInventory(); // The inventory that was
|
Inventory inventory = event.getInventory(); // The inventory that was clicked on
|
||||||
// Open the inventory panel that this player has open (they can only ever have one)
|
// Open the inventory panel that this player has open (they can only ever have one)
|
||||||
if (openPanels.containsKey(user.getUniqueId())) {
|
if (openPanels.containsKey(user.getUniqueId())) {
|
||||||
// Check the name of the panel
|
// Check the name of the panel
|
||||||
if (inventory.getName().equals(openPanels.get(user.getUniqueId()).getInventory().getName())) {
|
if (inventory.getName().equals(openPanels.get(user.getUniqueId()).getInventory().getName())) {
|
||||||
// Cancel the event. If they don't want it to be canceled then the click handler(s) should uncancel it
|
// Close inventory if clicked outside and if setting is true
|
||||||
|
if (BentoBox.getInstance().getSettings().isClosePanelOnClickOutside() && event.getSlotType().equals(SlotType.OUTSIDE)) {
|
||||||
|
event.getWhoClicked().closeInventory();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cancel the event. If they don't want it to be cancelled then the click handler(s) should uncancel it
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
// Get the panel itself
|
// Get the panel itself
|
||||||
Panel panel = openPanels.get(user.getUniqueId());
|
Panel panel = openPanels.get(user.getUniqueId());
|
||||||
@ -67,9 +68,7 @@ public class PanelListenerManager implements Listener {
|
|||||||
|
|
||||||
@EventHandler(priority = EventPriority.NORMAL)
|
@EventHandler(priority = EventPriority.NORMAL)
|
||||||
public void onLogOut(PlayerQuitEvent event) {
|
public void onLogOut(PlayerQuitEvent event) {
|
||||||
if (openPanels.containsKey(event.getPlayer().getUniqueId())) {
|
openPanels.remove(event.getPlayer().getUniqueId());
|
||||||
openPanels.remove(event.getPlayer().getUniqueId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package world.bentobox.bentobox.listeners;
|
package world.bentobox.bentobox.listeners;
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ public class PanelListenerManagerTest {
|
|||||||
when(player.getUniqueId()).thenReturn(uuid);
|
when(player.getUniqueId()).thenReturn(uuid);
|
||||||
view = mock(InventoryView.class);
|
view = mock(InventoryView.class);
|
||||||
when(view.getPlayer()).thenReturn(player);
|
when(view.getPlayer()).thenReturn(player);
|
||||||
|
|
||||||
User.getInstance(player);
|
User.getInstance(player);
|
||||||
Inventory top = mock(Inventory.class);
|
Inventory top = mock(Inventory.class);
|
||||||
when(top.getSize()).thenReturn(9);
|
when(top.getSize()).thenReturn(9);
|
||||||
@ -90,7 +90,7 @@ public class PanelListenerManagerTest {
|
|||||||
inv = InventoryAction.UNKNOWN;
|
inv = InventoryAction.UNKNOWN;
|
||||||
|
|
||||||
plm = new PanelListenerManager();
|
plm = new PanelListenerManager();
|
||||||
|
|
||||||
// Panel
|
// Panel
|
||||||
panel = mock(Panel.class);
|
panel = mock(Panel.class);
|
||||||
pl = mock(PanelListener.class);
|
pl = mock(PanelListener.class);
|
||||||
@ -111,17 +111,29 @@ public class PanelListenerManagerTest {
|
|||||||
anotherInv = mock(Inventory.class);
|
anotherInv = mock(Inventory.class);
|
||||||
when(anotherInv.getName()).thenReturn("another_name");
|
when(anotherInv.getName()).thenReturn("another_name");
|
||||||
when(wrongPanel.getInventory()).thenReturn(anotherInv);
|
when(wrongPanel.getInventory()).thenReturn(anotherInv);
|
||||||
|
|
||||||
// Clear the static panels
|
// Clear the static panels
|
||||||
PanelListenerManager.getOpenPanels().clear();
|
PanelListenerManager.getOpenPanels().clear();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
|
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnInventoryClickOutside() {
|
public void testOnInventoryClickOutsideUnknownPanel() {
|
||||||
|
SlotType type = SlotType.OUTSIDE;
|
||||||
|
InventoryClickEvent e = new InventoryClickEvent(view, type, 0, click, inv);
|
||||||
|
plm.onInventoryClick(e);
|
||||||
|
Mockito.verify(player, Mockito.never()).closeInventory();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testOnInventoryClickOutsideKnownPanel() {
|
||||||
|
// Put a panel in the list
|
||||||
|
PanelListenerManager.getOpenPanels().put(uuid, panel);
|
||||||
SlotType type = SlotType.OUTSIDE;
|
SlotType type = SlotType.OUTSIDE;
|
||||||
InventoryClickEvent e = new InventoryClickEvent(view, type, 0, click, inv);
|
InventoryClickEvent e = new InventoryClickEvent(view, type, 0, click, inv);
|
||||||
plm.onInventoryClick(e);
|
plm.onInventoryClick(e);
|
||||||
@ -151,7 +163,7 @@ public class PanelListenerManagerTest {
|
|||||||
// Panel should be removed
|
// Panel should be removed
|
||||||
assertTrue(PanelListenerManager.getOpenPanels().isEmpty());
|
assertTrue(PanelListenerManager.getOpenPanels().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
|
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
|
||||||
*/
|
*/
|
||||||
@ -164,7 +176,7 @@ public class PanelListenerManagerTest {
|
|||||||
assertTrue(e.isCancelled());
|
assertTrue(e.isCancelled());
|
||||||
Mockito.verify(pl).onInventoryClick(Mockito.any(), Mockito.any());
|
Mockito.verify(pl).onInventoryClick(Mockito.any(), Mockito.any());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
|
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
|
||||||
*/
|
*/
|
||||||
@ -178,7 +190,7 @@ public class PanelListenerManagerTest {
|
|||||||
Mockito.verify(ch).onClick(Mockito.eq(panel), Mockito.any(User.class), Mockito.eq(click), Mockito.eq(0));
|
Mockito.verify(ch).onClick(Mockito.eq(panel), Mockito.any(User.class), Mockito.eq(click), Mockito.eq(0));
|
||||||
Mockito.verify(pl).onInventoryClick(Mockito.any(), Mockito.any());
|
Mockito.verify(pl).onInventoryClick(Mockito.any(), Mockito.any());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClose(org.bukkit.event.inventory.InventoryCloseEvent)}.
|
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClose(org.bukkit.event.inventory.InventoryCloseEvent)}.
|
||||||
*/
|
*/
|
||||||
@ -189,9 +201,9 @@ public class PanelListenerManagerTest {
|
|||||||
// No panels for this player
|
// No panels for this player
|
||||||
InventoryCloseEvent event = new InventoryCloseEvent(view);
|
InventoryCloseEvent event = new InventoryCloseEvent(view);
|
||||||
plm.onInventoryClose(event);
|
plm.onInventoryClose(event);
|
||||||
assertTrue(PanelListenerManager.getOpenPanels().size() == 1);
|
assertTrue(PanelListenerManager.getOpenPanels().size() == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClose(org.bukkit.event.inventory.InventoryCloseEvent)}.
|
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClose(org.bukkit.event.inventory.InventoryCloseEvent)}.
|
||||||
*/
|
*/
|
||||||
@ -203,7 +215,7 @@ public class PanelListenerManagerTest {
|
|||||||
plm.onInventoryClose(event);
|
plm.onInventoryClose(event);
|
||||||
assertTrue(PanelListenerManager.getOpenPanels().isEmpty());
|
assertTrue(PanelListenerManager.getOpenPanels().isEmpty());
|
||||||
Mockito.verify(pl).onInventoryClose(event);
|
Mockito.verify(pl).onInventoryClose(event);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -211,16 +223,16 @@ public class PanelListenerManagerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testOnLogOut() {
|
public void testOnLogOut() {
|
||||||
// Add a panel for player
|
// Add a panel for player
|
||||||
PanelListenerManager.getOpenPanels().put(uuid, panel);
|
PanelListenerManager.getOpenPanels().put(uuid, panel);
|
||||||
// Unknown player logs out
|
// Unknown player logs out
|
||||||
|
|
||||||
Player unknown = mock(Player.class);
|
Player unknown = mock(Player.class);
|
||||||
when(unknown.getUniqueId()).thenReturn(UUID.randomUUID());
|
when(unknown.getUniqueId()).thenReturn(UUID.randomUUID());
|
||||||
PlayerQuitEvent event = new PlayerQuitEvent(unknown, "");
|
PlayerQuitEvent event = new PlayerQuitEvent(unknown, "");
|
||||||
plm.onLogOut(event);
|
plm.onLogOut(event);
|
||||||
assertFalse(PanelListenerManager.getOpenPanels().isEmpty());
|
assertFalse(PanelListenerManager.getOpenPanels().isEmpty());
|
||||||
|
|
||||||
// Real log out
|
// Real log out
|
||||||
event = new PlayerQuitEvent(player, "");
|
event = new PlayerQuitEvent(player, "");
|
||||||
plm.onLogOut(event);
|
plm.onLogOut(event);
|
||||||
|
Loading…
Reference in New Issue
Block a user