package xyz.spaceio.hooks; import java.util.Optional; import java.util.UUID; import java.util.Map.Entry; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.bukkit.Location; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import com.google.common.collect.Maps; public class SkyblockAPICached { private SkyblockAPIHook hook; public SkyblockAPICached(SkyblockAPIHook hook) { this.hook = hook; } LoadingCache, Integer> cachedIslandLevel = CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterWrite(10, TimeUnit.SECONDS) .build( new CacheLoader, Integer>() { public Integer load(Entry key) { return hook.getIslandLevel(key.getKey(), key.getValue()); } }); LoadingCache> cachedIslandOwner = CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterWrite(100, TimeUnit.SECONDS) .build( new CacheLoader>() { public Optional load(Location key) { return hook.getIslandOwner(key); } }); public int getIslandLevel(UUID owner, String world) { Entry entry = Maps.immutableEntry(owner, world); try { return cachedIslandLevel.get(entry); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; } } public Optional getIslandOwner(Location loc){ try { return cachedIslandOwner.get(loc); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public String[] getSkyBlockWorldNames() { return hook.getSkyBlockWorldNames(); } }