72 lines
2.3 KiB
Java
72 lines
2.3 KiB
Java
package tokarotik.giftapi.cache;
|
|
|
|
import net.minecraft.server.v1_6_R3.EntityPlayer;
|
|
import org.bukkit.entity.Player;
|
|
import tokarotik.giftapi.NBT.nbt.BasicNBT;
|
|
import tokarotik.giftapi.NBT.nbt.WorldNBTSynchronizer;
|
|
import tokarotik.giftapi.NBT.pages.PagesManager;
|
|
|
|
import java.util.Map;
|
|
|
|
public class CacheManager
|
|
{
|
|
private final CacheUtil cacheUtil;
|
|
private final int inventory_slots;
|
|
|
|
public CacheManager(int inventory_slots)
|
|
{
|
|
this.inventory_slots = inventory_slots;
|
|
cacheUtil = new CacheUtil(inventory_slots);
|
|
}
|
|
|
|
public synchronized PagesManager load(Player player) {
|
|
String playerWorld = player.getWorld().getName();
|
|
Map<Player, PagesManager> worldMap = cacheUtil.getWorlMap(playerWorld);
|
|
EntityPlayer entityPlayer = BasicNBT.getPlayer(player);
|
|
|
|
// Return cached PageManager if already loaded
|
|
if (worldMap.containsKey(player)) {
|
|
System.out.print("Is already loaded");
|
|
return worldMap.get(player);
|
|
}
|
|
|
|
// Attempt to load from current world
|
|
if (WorldNBTSynchronizer.hasAll(entityPlayer, playerWorld)) {
|
|
System.out.print("Load from current world");
|
|
return this.cacheUtil.loadAndCachePageManager(player, playerWorld, entityPlayer, worldMap);
|
|
}
|
|
|
|
// Attempt to load from another world if present
|
|
String alternateWorld = WorldNBTSynchronizer.getWorldWhatHasPlayer(entityPlayer);
|
|
if (alternateWorld != null) {
|
|
System.out.print("Load from another world");
|
|
return this.cacheUtil.loadAndCachePageManager(player, alternateWorld, entityPlayer, worldMap);
|
|
}
|
|
|
|
// Fallback to new empty PageManager
|
|
PagesManager defaultPages = new PagesManager(this.inventory_slots);
|
|
worldMap.put(player, defaultPages);
|
|
|
|
System.out.print("New empty PageManager");
|
|
|
|
return defaultPages;
|
|
}
|
|
|
|
public synchronized void save(Player player, PagesManager pagesManager)
|
|
{
|
|
Map<Player, PagesManager> map = this.cacheUtil.getWorlMap(player.getWorld().getName());
|
|
|
|
map.put(player, pagesManager);
|
|
}
|
|
|
|
public synchronized void disable()
|
|
{
|
|
cacheUtil.saveAllHard();
|
|
}
|
|
|
|
public synchronized void playerQuit(Player player)
|
|
{
|
|
this.cacheUtil.saveHard(player);
|
|
}
|
|
}
|