package net.minecraft.util; import java.util.Locale; import java.util.function.Consumer; public class StaticCache2D { private final int minX; private final int minZ; private final int sizeX; private final int sizeZ; private final Object[] cache; public static StaticCache2D create(int centerX, int centerZ, int size, StaticCache2D.Initializer initializer) { int i = centerX - size; int j = centerZ - size; int k = 2 * size + 1; return new StaticCache2D<>(i, j, k, k, initializer); } private StaticCache2D(int minX, int minZ, int sizeX, int sizeZ, StaticCache2D.Initializer initializer) { this.minX = minX; this.minZ = minZ; this.sizeX = sizeX; this.sizeZ = sizeZ; this.cache = new Object[this.sizeX * this.sizeZ]; for (int i = minX; i < minX + sizeX; i++) { for (int j = minZ; j < minZ + sizeZ; j++) { this.cache[this.getIndex(i, j)] = initializer.get(i, j); } } } public void forEach(Consumer action) { for (Object object : this.cache) { action.accept(object); } } public T get(int x, int z) { if (!this.contains(x, z)) { throw new IllegalArgumentException("Requested out of range value (" + x + "," + z + ") from " + this); } else { return (T)this.cache[this.getIndex(x, z)]; } } public boolean contains(int x, int z) { int i = x - this.minX; int j = z - this.minZ; return i >= 0 && i < this.sizeX && j >= 0 && j < this.sizeZ; } public String toString() { return String.format(Locale.ROOT, "StaticCache2D[%d, %d, %d, %d]", this.minX, this.minZ, this.minX + this.sizeX, this.minZ + this.sizeZ); } private int getIndex(int x, int z) { int i = x - this.minX; int j = z - this.minZ; return i * this.sizeZ + j; } @FunctionalInterface public interface Initializer { T get(int i, int j); } }