package me.konsolas.aac.util; import java.util.HashMap; import java.util.Map; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; public class BlockCache { private final double originX; private final double originY; private final double originZ; private final World world; private final Map cachedBlocks = new HashMap<>(); public BlockCache(Location location) { this.originX = location.getX(); this.originY = location.getY(); this.originZ = location.getZ(); this.world = location.getWorld(); } public Block getOffsetBlock(double dx, double dy, double dz) { return this.getBlockAt( (int) Math.floor(this.originX + dx), (int) Math.floor(this.originY + dy), (int) Math.floor(this.originZ + dz) ); } public Block getNegativeOffsetBlock(double dx, double dy, double dz) { return this.getBlockAt( (int) Math.floor(this.originX - dx), (int) Math.floor(this.originY - dy), (int) Math.floor(this.originZ - dz) ); } public Material getOffsetMaterial(double dx, double dy, double dz) { return this.getOffsetBlock(dx, dy, dz).getType(); } public Material getNegativeOffsetMaterial(double dx, double dy, double dz) { return this.getNegativeOffsetBlock(dx, dy, dz).getType(); } private Block getBlockAt(int x, int y, int z) { BlockPosition position = new BlockPosition(x, y, z); Block cached = this.cachedBlocks.get(position); if (cached != null) { return cached; } Block block = this.world.getBlockAt(x, y, z); this.cachedBlocks.put(position, block); return block; } public Block getOriginBlock() { return this.getBlockAt((int) this.originX, (int) this.originY, (int) this.originZ); } private static final class BlockPosition { private final int x; private final int y; private final int z; private BlockPosition(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } @Override public boolean equals(Object other) { if (other == this) { return true; } if (!(other instanceof BlockPosition)) { return false; } BlockPosition position = (BlockPosition) other; return position.x == this.x && position.y == this.y && position.z == this.z; } @Override public int hashCode() { int result = 17; result = 31 * result + this.x; result = 31 * result + this.y; return 31 * result + this.z; } } }