Caches sign type as well as sign text.
https://github.com/BentoBoxWorld/addon-welcomewarpsigns/issues/27
This commit is contained in:
parent
29b9c3421e
commit
4621772fe8
2
pom.xml
2
pom.xml
|
@ -6,7 +6,7 @@
|
|||
|
||||
<groupId>world.bentobox</groupId>
|
||||
<artifactId>warps</artifactId>
|
||||
<version>1.5.0</version>
|
||||
<version>1.5.1-SNAPSHOT</version>
|
||||
|
||||
<name>WelcomeWarpSigns</name>
|
||||
<description>WelcomeWarpSigns is an add-on for BentoBox, an expandable Minecraft Bukkit plugin for island-type games like ASkyBlock or AcidIsland.</description>
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package world.bentobox.warps;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
/**
|
||||
* Stores info on a warp sign
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
public class SignCache {
|
||||
private final List<String> signText;
|
||||
private final Material type;
|
||||
/**
|
||||
* @param signText
|
||||
* @param type
|
||||
*/
|
||||
public SignCache(List<String> signText, Material type) {
|
||||
this.signText = signText;
|
||||
this.type = type;
|
||||
}
|
||||
/**
|
||||
* @return the signText
|
||||
*/
|
||||
public List<String> getSignText() {
|
||||
return signText;
|
||||
}
|
||||
/**
|
||||
* @return the type
|
||||
*/
|
||||
public Material getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
|
@ -15,13 +15,13 @@ import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
|
|||
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
|
||||
|
||||
public class WarpPanelManager {
|
||||
|
||||
private static final int PANEL_MAX_SIZE = 52;
|
||||
private Warp addon;
|
||||
// This is a cache of signs
|
||||
private Map<World, Map<UUID, List<String>>> cachedSigns = new HashMap<>();
|
||||
private Map<World, Map<UUID, SignCache>> cachedSigns = new HashMap<>();
|
||||
|
||||
|
||||
|
||||
public WarpPanelManager(Warp addon) {
|
||||
|
@ -43,10 +43,18 @@ public class WarpPanelManager {
|
|||
}
|
||||
|
||||
private Material getSignIcon(World world, UUID warpOwner) {
|
||||
Material type = addon.getWarpSignsManager().getWarp(world, warpOwner).getBlock().getType();
|
||||
return Material.valueOf(type.toString().replace("WALL_", ""));
|
||||
// Add the worlds if we haven't seen this before
|
||||
cachedSigns.putIfAbsent(world, new HashMap<>());
|
||||
if (cachedSigns.get(world).containsKey(warpOwner)) {
|
||||
return cachedSigns.get(world).get(warpOwner).getType();
|
||||
}
|
||||
// Not in cache
|
||||
SignCache sc = addon.getWarpSignsManager().getSignInfo(world, warpOwner);
|
||||
cachedSigns.get(world).put(warpOwner, sc);
|
||||
return sc.getType();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets sign text and cache it
|
||||
* @param playerUUID
|
||||
|
@ -56,11 +64,11 @@ public class WarpPanelManager {
|
|||
// Add the worlds if we haven't seen this before
|
||||
cachedSigns.putIfAbsent(world, new HashMap<>());
|
||||
if (cachedSigns.get(world).containsKey(playerUUID)) {
|
||||
return cachedSigns.get(world).get(playerUUID);
|
||||
return cachedSigns.get(world).get(playerUUID).getSignText();
|
||||
}
|
||||
List<String> result = addon.getWarpSignsManager().getSignText(world, playerUUID);
|
||||
SignCache result = addon.getWarpSignsManager().getSignInfo(world, playerUUID);
|
||||
cachedSigns.get(world).put(playerUUID, result);
|
||||
return result;
|
||||
return result.getSignText();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,6 +3,7 @@ package world.bentobox.warps;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
@ -17,6 +18,7 @@ import java.util.stream.Collectors;
|
|||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
|
@ -242,13 +244,13 @@ public class WarpSignsManager {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the warp sign text for player's UUID in world
|
||||
* Gets the warp sign text and material type for player's UUID in world
|
||||
*
|
||||
* @param world - world to look in
|
||||
* @param uuid - player's uuid
|
||||
* @return List of lines
|
||||
* @return Sign's content and type
|
||||
*/
|
||||
public List<String> getSignText(World world, UUID uuid) {
|
||||
public SignCache getSignInfo(World world, UUID uuid) {
|
||||
List<String> result = new ArrayList<>();
|
||||
//get the sign info
|
||||
Location signLocation = getWarp(world, uuid);
|
||||
|
@ -259,10 +261,13 @@ public class WarpSignsManager {
|
|||
result.remove(0);
|
||||
// Remove any trailing blank lines
|
||||
result.removeIf(String::isEmpty);
|
||||
// Get the sign type
|
||||
Material type = Material.valueOf(sign.getType().name().replace("WALL_", ""));
|
||||
return new SignCache(result, type);
|
||||
} else {
|
||||
addon.getWarpSignsManager().removeWarp(world, uuid);
|
||||
}
|
||||
return result;
|
||||
return new SignCache(Collections.emptyList(), Material.AIR);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
package world.bentobox.warps;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
|
@ -67,8 +70,8 @@ public class WarpPanelManagerTest {
|
|||
// One final one
|
||||
uuid = UUID.randomUUID();
|
||||
list.add(uuid);
|
||||
|
||||
when(wsm.getSortedWarps(Mockito.any())).thenReturn(list);
|
||||
|
||||
when(wsm.getSortedWarps(any())).thenReturn(list);
|
||||
|
||||
user = mock(User.class);
|
||||
player = mock(Player.class);
|
||||
|
@ -79,32 +82,32 @@ public class WarpPanelManagerTest {
|
|||
public String answer(InvocationOnMock invocation) throws Throwable {
|
||||
return invocation.getArgumentAt(0, String.class);
|
||||
}});
|
||||
|
||||
|
||||
// World
|
||||
world = mock(World.class);
|
||||
|
||||
|
||||
// BentoBox
|
||||
BentoBox plugin = mock(BentoBox.class);
|
||||
PlayersManager pm = mock(PlayersManager.class);
|
||||
when(pm.getName(Mockito.any())).thenReturn("name");
|
||||
when(pm.getName(any())).thenReturn("name");
|
||||
when(plugin.getPlayers()).thenReturn(pm);
|
||||
when(addon.getPlugin()).thenReturn(plugin);
|
||||
|
||||
|
||||
// Bukkit
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
ItemFactory itemF = mock(ItemFactory.class);
|
||||
ItemMeta imeta = mock(ItemMeta.class);
|
||||
when(itemF.getItemMeta(Mockito.any())).thenReturn(imeta);
|
||||
when(itemF.getItemMeta(any())).thenReturn(imeta);
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemF);
|
||||
|
||||
top = mock(Inventory.class);
|
||||
when(top.getSize()).thenReturn(9);
|
||||
|
||||
when(Bukkit.createInventory(Mockito.any(), Mockito.anyInt(), Mockito.any())).thenReturn(top);
|
||||
|
||||
when(Bukkit.createInventory(any(), Mockito.anyInt(), any())).thenReturn(top);
|
||||
|
||||
Settings settings = mock(Settings.class);
|
||||
when(settings.getIcon()).thenReturn("SIGN");
|
||||
when(addon.getSettings()).thenReturn(settings);
|
||||
when(addon.getSettings()).thenReturn(settings);
|
||||
|
||||
Location location = mock(Location.class);
|
||||
Block block = mock(Block.class);
|
||||
|
@ -117,8 +120,14 @@ public class WarpPanelManagerTest {
|
|||
when(block.getType()).thenReturn(sign_type);
|
||||
when(location.getBlock()).thenReturn(block);
|
||||
// Sign block
|
||||
when(wsm.getWarp(Mockito.any(), Mockito.any())).thenReturn(location);
|
||||
}
|
||||
when(wsm.getWarp(any(), any())).thenReturn(location);
|
||||
|
||||
// Sign cache
|
||||
SignCache sc = mock(SignCache.class);
|
||||
when(sc.getSignText()).thenReturn(Collections.singletonList("[welcome]"));
|
||||
when(sc.getType()).thenReturn(sign_type);
|
||||
when(wsm.getSignInfo(any(), any())).thenReturn(sc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link WarpPanelManager#showWarpPanel(org.bukkit.World, world.bentobox.bbox.api.user.User, int)}.
|
||||
|
@ -127,11 +136,11 @@ public class WarpPanelManagerTest {
|
|||
public void testShowWarpPanelFirst() {
|
||||
WarpPanelManager wpm = new WarpPanelManager(addon);
|
||||
wpm.showWarpPanel(world, user, 0);
|
||||
Mockito.verify(player).openInventory(Mockito.eq(top));
|
||||
verify(player).openInventory(Mockito.eq(top));
|
||||
// Just next sign
|
||||
Mockito.verify(top, Mockito.times(53)).setItem(Mockito.anyInt(), Mockito.any(ItemStack.class));
|
||||
verify(top, Mockito.times(53)).setItem(Mockito.anyInt(), Mockito.any(ItemStack.class));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test method for {@link WarpPanelManager#showWarpPanel(org.bukkit.World, world.bentobox.bbox.api.user.User, int)}.
|
||||
*/
|
||||
|
@ -139,9 +148,9 @@ public class WarpPanelManagerTest {
|
|||
public void testShowWarpPanelMiddle() {
|
||||
WarpPanelManager wpm = new WarpPanelManager(addon);
|
||||
wpm.showWarpPanel(world, user, 1);
|
||||
Mockito.verify(player).openInventory(Mockito.eq(top));
|
||||
verify(player).openInventory(Mockito.eq(top));
|
||||
// includes previous and next signs
|
||||
Mockito.verify(top, Mockito.times(54)).setItem(Mockito.anyInt(), Mockito.any(ItemStack.class));
|
||||
verify(top, Mockito.times(54)).setItem(Mockito.anyInt(), Mockito.any(ItemStack.class));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -151,9 +160,9 @@ public class WarpPanelManagerTest {
|
|||
public void testShowWarpPanelLast() {
|
||||
WarpPanelManager wpm = new WarpPanelManager(addon);
|
||||
wpm.showWarpPanel(world, user, 3);
|
||||
Mockito.verify(player).openInventory(Mockito.eq(top));
|
||||
verify(player).openInventory(Mockito.eq(top));
|
||||
// Final amount, just previous sign
|
||||
Mockito.verify(top, Mockito.times(46)).setItem(Mockito.anyInt(), Mockito.any(ItemStack.class));
|
||||
verify(top, Mockito.times(46)).setItem(Mockito.anyInt(), Mockito.any(ItemStack.class));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -167,10 +176,10 @@ public class WarpPanelManagerTest {
|
|||
// Get the panel again
|
||||
wpm.showWarpPanel(world, user, 3);
|
||||
// Should only check this 45 times because the sign text is cached
|
||||
Mockito.verify(wsm, Mockito.times(45)).getSignText(Mockito.any(), Mockito.any());
|
||||
verify(wsm, Mockito.times(45)).getSignInfo(any(), any());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Test method for {@link WarpPanelManager#removeWarp(org.bukkit.World, java.util.UUID)}.
|
||||
*/
|
||||
|
@ -182,8 +191,8 @@ public class WarpPanelManagerTest {
|
|||
wpm.removeWarp(world, uuid);
|
||||
wpm.showWarpPanel(world, user, 3);
|
||||
// Removing the UUID should force a refresh and therefore 46 lookups
|
||||
Mockito.verify(wsm, Mockito.times(46)).getSignText(Mockito.any(), Mockito.any());
|
||||
|
||||
verify(wsm, Mockito.times(46)).getSignInfo(any(), any());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue