Skip to content

Commit

Permalink
Added saving of the warp sign cache to database.
Browse files Browse the repository at this point in the history
Relates to #68
  • Loading branch information
tastybento committed Jan 29, 2020
1 parent ef523d4 commit 79b8c0c
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@

import org.bukkit.Material;

import com.google.gson.annotations.Expose;

/**
* Stores info on a warp sign
* @author tastybento
*
*/
public class SignCache {
public class SignCacheItem {
@Expose
private final List<String> signText;
@Expose
private final Material type;
/**
* @param signText
* @param type
*/
public SignCache(List<String> signText, Material type) {
public SignCacheItem(List<String> signText, Material type) {
this.signText = signText;
this.type = type;
}
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/world/bentobox/warps/SignCacheManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package world.bentobox.warps;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;

import world.bentobox.bentobox.database.Database;
import world.bentobox.warps.objects.SignCache;

public class SignCacheManager {
private Map<World, Map<UUID, SignCacheItem>> cachedSigns = new HashMap<>();
private Warp addon;
// Database handler for level data
private Database<SignCache> handler;

public SignCacheManager(Warp addon) {
this.addon = addon;
handler = new Database<>(addon, SignCache.class);
// Load the sign caches
loadCache();
}

private void loadCache() {
handler.loadObjects().forEach(w -> {
World world = Bukkit.getWorld(w.getUniqueId());
if (world != null) {
cachedSigns.put(world, w.getSigns());
}
});
}

void saveCache() {
cachedSigns.forEach((w, m) -> handler.saveObject(new SignCache(w, m)));
}

Material getSignIcon(World world, UUID warpOwner) {
// 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
SignCacheItem sc = addon.getWarpSignsManager().getSignInfo(world, warpOwner);
cachedSigns.get(world).put(warpOwner, sc);
return sc.getType();
}

/**
* Gets sign text and cache it
* @param playerUUID
* @return sign text in a list
*/
List<String> getSign(World world, UUID playerUUID) {
// 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).getSignText();
}
SignCacheItem result = addon.getWarpSignsManager().getSignInfo(world, playerUUID);
cachedSigns.get(world).put(playerUUID, result);
return result.getSignText();
}

/**
* Removes sign text from the cache
* @param world - world
* @param key - uuid of owner
*/
void removeWarp(World world, UUID key) {
cachedSigns.putIfAbsent(world, new HashMap<>());
cachedSigns.get(world).remove(key);
}

}
4 changes: 3 additions & 1 deletion src/main/java/world/bentobox/warps/Warp.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public void onDisable(){
// Save the warps
if (warpSignsManager != null)
warpSignsManager.saveWarpList();
if (warpPanelManager != null)
warpPanelManager.saveCache();
}


Expand All @@ -172,7 +174,7 @@ private boolean loadSettings() {

/**
* Get warp panel manager
* @return
* @return Warp Panel Manager
*/
public WarpPanelManager getWarpPanelManager() {
return warpPanelManager;
Expand Down
50 changes: 11 additions & 39 deletions src/main/java/world/bentobox/warps/WarpPanelManager.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package world.bentobox.warps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

Expand All @@ -21,20 +19,19 @@ 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, SignCache>> cachedSigns = new HashMap<>();


private SignCacheManager signCacheManager;

public WarpPanelManager(Warp addon) {
this.addon = addon;
signCacheManager = new SignCacheManager(addon);
}

private PanelItem getPanelItem(World world, UUID warpOwner) {
PanelItemBuilder pib = new PanelItemBuilder()
.name(addon.getSettings().getNameFormat() + addon.getPlugin().getPlayers().getName(warpOwner))
.description(getSign(world, warpOwner))
.description(signCacheManager.getSign(world, warpOwner))
.clickHandler((panel, clicker, click, slot) -> hander(world, clicker, warpOwner));
Material icon = getSignIcon(world, warpOwner);
Material icon = signCacheManager.getSignIcon(world, warpOwner);
if (icon.equals(Material.PLAYER_HEAD)) {
return pib.icon(addon.getPlayers().getName(warpOwner)).build();
} else {
Expand All @@ -59,35 +56,6 @@ private PanelItem getRandomButton(World world, User user, UUID warpOwner) {
.icon(Material.END_CRYSTAL).build();
}

private Material getSignIcon(World world, UUID warpOwner) {
// 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
* @return sign text in a list
*/
private List<String> getSign(World world, UUID playerUUID) {
// 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).getSignText();
}
SignCache result = addon.getWarpSignsManager().getSignInfo(world, playerUUID);
cachedSigns.get(world).put(playerUUID, result);
return result.getSignText();
}

/**
* Show the warp panel for the user
* @param world - world
Expand Down Expand Up @@ -148,11 +116,15 @@ public void showWarpPanel(World world, User user, int index) {

/**
* Removes sign text from the cache
* @param key
* @param world - world
* @param key - uuid of owner
*/
public void removeWarp(World world, UUID key) {
cachedSigns.putIfAbsent(world, new HashMap<>());
cachedSigns.get(world).remove(key);
signCacheManager.removeWarp(world, key);
}

public void saveCache() {
signCacheManager.saveCache();
}

}
10 changes: 5 additions & 5 deletions src/main/java/world/bentobox/warps/WarpSignsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public WarpSignsManager(Warp addon, BentoBox plugin) {
this.addon = addon;
this.plugin = plugin;
// Set up the database handler
// Note that these are saved by the BSkyBlock database
// Note that these are saved by the BentoBox database
handler = new Database<>(addon, WarpsData.class);
// Load the warps
loadWarpList();
Expand Down Expand Up @@ -263,7 +263,7 @@ public void saveWarpList() {
* @return Sign's content and type
*/
@NonNull
public SignCache getSignInfo(@NonNull World world, @NonNull UUID uuid) {
public SignCacheItem getSignInfo(@NonNull World world, @NonNull UUID uuid) {
List<String> result = new ArrayList<>();
//get the sign info
Location signLocation = getWarp(world, uuid);
Expand Down Expand Up @@ -297,14 +297,14 @@ public SignCache getSignInfo(@NonNull World world, @NonNull UUID uuid) {
}

if (icon == null || icon.name().contains("SIGN")) {
return new SignCache(result, Material.valueOf(sign.getType().name().replace("WALL_", "")));
return new SignCacheItem(result, Material.valueOf(sign.getType().name().replace("WALL_", "")));
} else {
return new SignCache(result, icon);
return new SignCacheItem(result, icon);
}
} else {
addon.getWarpSignsManager().removeWarp(world, uuid);
}
return new SignCache(Collections.emptyList(), Material.AIR);
return new SignCacheItem(Collections.emptyList(), Material.AIR);
}

/**
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/world/bentobox/warps/objects/SignCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package world.bentobox.warps.objects;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.bukkit.World;

import com.google.gson.annotations.Expose;

import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.warps.SignCacheItem;

public class SignCache implements DataObject {

@Expose
private String uniqueId = "";
@Expose
private Map<UUID, SignCacheItem> signs = new HashMap<>();

public SignCache() {
// Required by YAML database
}

public SignCache(World w, Map<UUID, SignCacheItem> m) {
this.uniqueId = w.getName();
this.signs = m;
}

@Override
public String getUniqueId() {
return uniqueId;
}

@Override
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}

/**
* @return the signs
*/
public Map<UUID, SignCacheItem> getSigns() {
return signs;
}

/**
* @param signs the signs to set
*/
public void setSigns(Map<UUID, SignCacheItem> signs) {
this.signs = signs;
}

}
21 changes: 19 additions & 2 deletions src/test/java/world/bentobox/warps/WarpPanelManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
Expand All @@ -37,6 +38,8 @@

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.DatabaseSetup;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.warps.config.Settings;

Expand All @@ -45,7 +48,7 @@
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class})
@PrepareForTest({Bukkit.class, DatabaseSetup.class})
public class WarpPanelManagerTest {

@Mock
Expand All @@ -63,6 +66,20 @@ public class WarpPanelManagerTest {
private UUID uuid;
@Mock
private Settings settings;
@Mock
private static AbstractDatabaseHandler<Object> handler;

@SuppressWarnings("unchecked")
@BeforeClass
public static void beforeClass() {
// This has to be done beforeClass otherwise the tests will interfere with each other
handler = mock(AbstractDatabaseHandler.class);
// Database
PowerMockito.mockStatic(DatabaseSetup.class);
DatabaseSetup dbSetup = mock(DatabaseSetup.class);
when(DatabaseSetup.getDatabase()).thenReturn(dbSetup);
when(dbSetup.getHandler(any())).thenReturn(handler);
}

/**
* @throws java.lang.Exception
Expand Down Expand Up @@ -127,7 +144,7 @@ public String answer(InvocationOnMock invocation) throws Throwable {
when(wsm.getWarp(any(), any())).thenReturn(location);

// Sign cache
SignCache sc = mock(SignCache.class);
SignCacheItem sc = mock(SignCacheItem.class);
when(sc.getSignText()).thenReturn(Collections.singletonList("[welcome]"));
when(sc.getType()).thenReturn(sign_type);
when(wsm.getSignInfo(any(), any())).thenReturn(sc);
Expand Down
7 changes: 2 additions & 5 deletions src/test/java/world/bentobox/warps/WarpSignsManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,7 @@ public void setUp() throws Exception {

// WarpPanelManager
when(addon.getWarpPanelManager()).thenReturn(wpm);

// User



wsm = new WarpSignsManager(addon, plugin);
}

Expand Down Expand Up @@ -417,7 +414,7 @@ public void testSaveWarpList() throws Exception {
*/
@Test
public void testGetSignInfo() {
SignCache sc = wsm.getSignInfo(world, uuid);
SignCacheItem sc = wsm.getSignInfo(world, uuid);
assertEquals(Material.ACACIA_SIGN, sc.getType());
}

Expand Down

0 comments on commit 79b8c0c

Please sign in to comment.