2022-03-11 11:34:16 +01:00
|
|
|
package world.bentobox.warps.managers;
|
2020-01-29 02:11:23 +01:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.World;
|
2021-01-09 20:19:35 +01:00
|
|
|
import org.eclipse.jdt.annotation.NonNull;
|
2020-01-29 02:11:23 +01:00
|
|
|
|
|
|
|
import world.bentobox.bentobox.database.Database;
|
2022-03-11 11:34:16 +01:00
|
|
|
import world.bentobox.warps.Warp;
|
2020-01-29 02:11:23 +01:00
|
|
|
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;
|
2020-01-29 02:11:23 +01:00
|
|
|
// 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
|
|
|
|
2020-01-29 02:11:23 +01:00
|
|
|
public SignCacheManager(Warp addon) {
|
|
|
|
this.addon = addon;
|
|
|
|
handler = new Database<>(addon, SignCache.class);
|
|
|
|
// Load the sign caches
|
|
|
|
loadCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadCache() {
|
2021-01-10 00:33:27 +01:00
|
|
|
cachedSigns.clear();
|
2020-01-29 02:11:23 +01:00
|
|
|
handler.loadObjects().forEach(w -> {
|
|
|
|
World world = Bukkit.getWorld(w.getUniqueId());
|
|
|
|
if (world != null) {
|
2021-01-09 20:19:35 +01:00
|
|
|
w.getSigns().values().removeIf(sci -> sci.getType().equals(Material.AIR));
|
2020-01-29 02:11:23 +01:00
|
|
|
cachedSigns.put(world, w.getSigns());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-06-11 06:31:20 +02:00
|
|
|
|
2022-03-11 11:34:16 +01:00
|
|
|
public void saveCache() {
|
2020-06-11 06:31:20 +02:00
|
|
|
cachedSigns.forEach((w, m) -> handler.saveObjectAsync(new SignCache(w, m)));
|
2020-01-29 02:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-09 20:19:35 +01:00
|
|
|
* 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
|
2020-01-29 02:11:23 +01:00
|
|
|
*/
|
2021-01-09 20:19:35 +01:00
|
|
|
@NonNull
|
2022-03-11 11:34:16 +01:00
|
|
|
public SignCacheItem getSignItem(World world, UUID warpOwner) {
|
2020-01-29 02:11:23 +01:00
|
|
|
// Add the worlds if we haven't seen this before
|
|
|
|
cachedSigns.putIfAbsent(world, new HashMap<>());
|
2021-01-09 20:19:35 +01:00
|
|
|
// 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);
|
2020-01-29 02:11:23 +01:00
|
|
|
}
|
2021-01-09 20:19:35 +01:00
|
|
|
return result;
|
2020-01-29 02:11:23 +01:00
|
|
|
}
|
2020-06-11 06:31:20 +02:00
|
|
|
|
2020-01-29 02:11:23 +01:00
|
|
|
/**
|
|
|
|
* Removes sign text from the cache
|
|
|
|
* @param world - world
|
|
|
|
* @param key - uuid of owner
|
2021-01-02 00:36:14 +01:00
|
|
|
* @return true if item is removed from cache
|
2020-01-29 02:11:23 +01:00
|
|
|
*/
|
2022-03-11 11:34:16 +01:00
|
|
|
public boolean removeWarp(World world, UUID key) {
|
2020-05-28 01:48:20 +02:00
|
|
|
if (cachedSigns.containsKey(world)) {
|
2021-01-02 00:36:14 +01:00
|
|
|
return cachedSigns.get(world).remove(key) != null;
|
2020-05-28 01:48:20 +02:00
|
|
|
}
|
2021-01-02 00:36:14 +01:00
|
|
|
return false;
|
2020-01-29 02:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|