Skip to content

Instantly share code, notes, and snippets.

@Cryptite
Created November 30, 2018 19:06
Show Gist options
  • Save Cryptite/0c0644b83b0522677ab82956b24f931e to your computer and use it in GitHub Desktop.
Save Cryptite/0c0644b83b0522677ab82956b24f931e to your computer and use it in GitHub Desktop.
Paper-based Asynchronous Cache for looking up blocks.
public class ChunkSnapshotCache {
private final LoadingCache<StringChunk, ChunkSnapshot> cache;
private ChunkSnapshotCache() {
cache = CacheBuilder.newBuilder()
.build(new CacheLoader<StringChunk, ChunkSnapshot>() {
@Override
public ChunkSnapshot load(StringChunk chunk) {
return chunk.getChunk().getChunkSnapshot(false, false, false);
}
});
}
public StringBlock getCleanBlock(StringBlock liveBlock) {
World cleanWorld = Bukkit.getWorld(liveBlock.getWorld() + "_clean");
StringChunk chunk = new StringChunk(cleanWorld, liveBlock.getBlockX() >> 4, liveBlock.getBlockZ() >> 4);
ChunkSnapshot cleanSnapshot = cache.getUnchecked(chunk);
int x = liveBlock.getBlockX();
int y = liveBlock.getBlockY();
int z = liveBlock.getBlockZ();
return new StringBlock(cleanWorld, x, y, z,
cleanSnapshot.getBlockType(x, y, z),
(byte) cleanSnapshot.getBlockData(x, y, z));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment