Ignores colors when checking panel names

Fixes https://github.com/BentoBoxWorld/BentoBox/issues/1202

Black is a default color so there can be a mismatch between the
inventory title and the expected title if colors are used. In general,
it is safer not to check colors for GUIs.
This commit is contained in:
tastybento 2020-02-21 18:53:20 -08:00
parent 65dbd530d5
commit b189ebde65
2 changed files with 47 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.ChatColor;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -37,8 +38,8 @@ public class PanelListenerManager implements Listener {
// uncancel it. If gui was from our environment, then cancel event anyway.
event.setCancelled(true);
// Check the name of the panel
if (view.getTitle().equals(openPanels.get(user.getUniqueId()).getName())) {
// Check the name of the panel - strip colors. Note that black is removed from titles automatically by the server.
if (ChatColor.stripColor(view.getTitle()).equals(ChatColor.stripColor(openPanels.get(user.getUniqueId()).getName()))) {
// Close inventory if clicked outside and if setting is true
if (BentoBox.getInstance().getSettings().isClosePanelOnClickOutside() && event.getSlotType().equals(SlotType.OUTSIDE)) {
event.getWhoClicked().closeInventory();
@ -63,6 +64,8 @@ public class PanelListenerManager implements Listener {
} else {
// Wrong name - delete this panel
openPanels.remove(user.getUniqueId());
// This avoids GUIs being left open so that items can be taken.
user.closeInventory();
}
}
}

View File

@ -3,7 +3,11 @@ package world.bentobox.bentobox.listeners;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
@ -12,6 +16,7 @@ import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
@ -25,7 +30,6 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@ -52,6 +56,7 @@ import world.bentobox.bentobox.util.Util;
@PrepareForTest({BentoBox.class, Util.class, Bukkit.class })
public class PanelListenerManagerTest {
private static final String PANEL_NAME = "name";
@Mock
private Player player;
private InventoryView view;
@ -90,7 +95,7 @@ public class PanelListenerManagerTest {
User.getInstance(player);
// Inventory view
view = new MyView("name");
view = new MyView(ChatColor.RED + PANEL_NAME);
type = SlotType.CONTAINER;
click = ClickType.LEFT;
@ -136,6 +141,14 @@ public class PanelListenerManagerTest {
this.name = name;
}
/**
* @param name
*/
public MyView(String name, Inventory inventory) {
top = inventory;
this.name = name;
}
@Override
public Inventory getTopInventory() {
return top;
@ -178,7 +191,7 @@ public class PanelListenerManagerTest {
SlotType type = SlotType.OUTSIDE;
InventoryClickEvent e = new InventoryClickEvent(view, type, 0, click, inv);
plm.onInventoryClick(e);
Mockito.verify(player, Mockito.never()).closeInventory();
verify(player, never()).closeInventory();
}
/**
@ -191,7 +204,7 @@ public class PanelListenerManagerTest {
SlotType type = SlotType.OUTSIDE;
InventoryClickEvent e = new InventoryClickEvent(view, type, 0, click, inv);
plm.onInventoryClick(e);
Mockito.verify(player).closeInventory();
verify(player).closeInventory();
}
/**
@ -202,22 +215,40 @@ public class PanelListenerManagerTest {
InventoryClickEvent e = new InventoryClickEvent(view, type, 0, click, inv);
plm.onInventoryClick(e);
// Nothing should happen
Mockito.verify(player, Mockito.never()).closeInventory();
verify(player, never()).closeInventory();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
*/
@Ignore("Logic needs to be rewritten")
@Test
public void testOnInventoryClickOpenPanelsWrongPanel() {
PanelListenerManager.getOpenPanels().put(uuid, panel);
InventoryClickEvent e = new InventoryClickEvent(new MyView("another"), type, 0, click, inv);
// Use another name for this panel
InventoryView otherView = new MyView("another", panel.getInventory());
InventoryClickEvent e = new InventoryClickEvent(otherView, type, 0, click, inv);
plm.onInventoryClick(e);
// Panel should be removed
assertTrue(PanelListenerManager.getOpenPanels().isEmpty());
verify(player).closeInventory();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
*/
@Test
public void testOnInventoryClickOpenPanelsDifferentColorPanel() {
PanelListenerManager.getOpenPanels().put(uuid, panel);
// Use another name for this panel
InventoryView otherView = new MyView(ChatColor.BLACK + PANEL_NAME, panel.getInventory());
InventoryClickEvent e = new InventoryClickEvent(otherView, type, 0, click, inv);
plm.onInventoryClick(e);
// Check that the onClick is called
verify(ch).onClick(eq(panel), any(User.class), eq(click), eq(0));
verify(pl).onInventoryClick(any(), any());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
*/
@ -228,7 +259,7 @@ public class PanelListenerManagerTest {
InventoryClickEvent e = new InventoryClickEvent(view, type, 1, click, inv);
plm.onInventoryClick(e);
assertTrue(e.isCancelled());
Mockito.verify(pl).onInventoryClick(Mockito.any(), Mockito.any());
verify(pl).onInventoryClick(any(), any());
}
/**
@ -241,8 +272,8 @@ public class PanelListenerManagerTest {
InventoryClickEvent e = new InventoryClickEvent(view, type, 0, click, inv);
plm.onInventoryClick(e);
// Check that the onClick is called
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());
verify(ch).onClick(eq(panel), any(User.class), eq(click), eq(0));
verify(pl).onInventoryClick(any(), any());
}
/**
@ -268,7 +299,7 @@ public class PanelListenerManagerTest {
InventoryCloseEvent event = new InventoryCloseEvent(view);
plm.onInventoryClose(event);
assertTrue(PanelListenerManager.getOpenPanels().isEmpty());
Mockito.verify(pl).onInventoryClose(event);
verify(pl).onInventoryClose(event);
}