1
0
mirror of https://github.com/BentoBoxWorld/Warps.git synced 2024-09-27 14:52:55 +02:00
addon-welcomewarpsigns/src/main/java/world/bentobox/warps/managers/SignCacheManager.java

82 lines
2.5 KiB
Java
Raw Normal View History

package world.bentobox.warps.managers;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.database.Database;
import world.bentobox.warps.Warp;
import world.bentobox.warps.objects.SignCache;
public class SignCacheManager {
2021-08-09 03:09:28 +02:00
private final Map<World, Map<UUID, SignCacheItem>> cachedSigns = new HashMap<>();
private final Warp addon;
// Database handler for level data
2021-08-09 03:09:28 +02:00
private final Database<SignCache> handler;
2020-06-11 06:31:20 +02:00
public SignCacheManager(Warp addon) {
this.addon = addon;
handler = new Database<>(addon, SignCache.class);
// Load the sign caches
loadCache();
}
private void loadCache() {
cachedSigns.clear();
handler.loadObjects().forEach(w -> {
World world = Bukkit.getWorld(w.getUniqueId());
if (world != null) {
w.getSigns().values().removeIf(sci -> sci.getType().equals(Material.AIR));
cachedSigns.put(world, w.getSigns());
}
});
}
2020-06-11 06:31:20 +02:00
public void saveCache() {
2020-06-11 06:31:20 +02:00
cachedSigns.forEach((w, m) -> handler.saveObjectAsync(new SignCache(w, m)));
}
/**
* Get the sign item from cache or get it from the world if it is not in the cache
* @param world - world
* @param warpOwner - warp owner
* @return SignCacheItem
*/
@NonNull
public SignCacheItem getSignItem(World world, UUID warpOwner) {
// Add the worlds if we haven't seen this before
cachedSigns.putIfAbsent(world, new HashMap<>());
// Get from cache if available
if (cachedSigns.get(world).containsKey(warpOwner)) {
return cachedSigns.get(world).get(warpOwner);
}
// Generate and add to cache
SignCacheItem result = addon.getWarpSignsManager().getSignInfo(world, warpOwner);
if (result.isReal()) {
cachedSigns.get(world).put(warpOwner, result);
} else {
2021-01-09 20:37:41 +01:00
cachedSigns.get(world).remove(warpOwner);
}
return result;
}
2020-06-11 06:31:20 +02:00
/**
* Removes sign text from the cache
* @param world - world
* @param key - uuid of owner
* @return true if item is removed from cache
*/
public boolean removeWarp(World world, UUID key) {
if (cachedSigns.containsKey(world)) {
return cachedSigns.get(world).remove(key) != null;
}
return false;
}
}