StaticCache2D.java

net.minecraft.util.StaticCache2D

信息

  • 全限定名:net.minecraft.util.StaticCache2D
  • 类型:public class
  • 包:net.minecraft.util
  • 源码路径:src/main/java/net/minecraft/util/StaticCache2D.java
  • 起始行号:L6
  • 职责:

    TODO

字段/常量

  • minX

    • 类型: int
    • 修饰符: private final
    • 源码定位: L7
    • 说明:

      TODO

  • minZ

    • 类型: int
    • 修饰符: private final
    • 源码定位: L8
    • 说明:

      TODO

  • sizeX

    • 类型: int
    • 修饰符: private final
    • 源码定位: L9
    • 说明:

      TODO

  • sizeZ

    • 类型: int
    • 修饰符: private final
    • 源码定位: L10
    • 说明:

      TODO

  • cache

    • 类型: Object[]
    • 修饰符: private final
    • 源码定位: L11
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.util.StaticCache2D.Initializer
    • 类型: interface
    • 修饰符: public
    • 源码定位: L66
    • 说明:

      TODO

构造器

private StaticCache2D(int minX, int minZ, int sizeX, int sizeZ, StaticCache2D.Initializer<T> initializer) @ L20

  • 构造器名:StaticCache2D
  • 源码定位:L20
  • 修饰符:private

参数:

  • minX: int
  • minZ: int
  • sizeX: int
  • sizeZ: int
  • initializer: StaticCache2D.Initializer

说明:

TODO

方法

下面的方法块按源码顺序生成。

public static <T> StaticCache2D<T> create(int centerX, int centerZ, int range, StaticCache2D.Initializer<T> initializer) @ L13

  • 方法名:create
  • 源码定位:L13
  • 返回类型: StaticCache2D
  • 修饰符:public static

参数:

  • centerX: int
  • centerZ: int
  • range: int
  • initializer: StaticCache2D.Initializer

说明:

TODO

public void forEach(Consumer<T> consumer) @ L34

  • 方法名:forEach
  • 源码定位:L34
  • 返回类型:void
  • 修饰符:public

参数:

  • consumer: Consumer

说明:

TODO

public T get(int x, int z) @ L40

  • 方法名:get
  • 源码定位:L40
  • 返回类型:T
  • 修饰符:public

参数:

  • x: int
  • z: int

说明:

TODO

public boolean contains(int x, int z) @ L48

  • 方法名:contains
  • 源码定位:L48
  • 返回类型:boolean
  • 修饰符:public

参数:

  • x: int
  • z: int

说明:

TODO

public String toString() @ L54

  • 方法名:toString
  • 源码定位:L54
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

private int getIndex(int x, int z) @ L59

  • 方法名:getIndex
  • 源码定位:L59
  • 返回类型:int
  • 修饰符:private

参数:

  • x: int
  • z: int

说明:

TODO

代码

public class StaticCache2D<T> {
    private final int minX;
    private final int minZ;
    private final int sizeX;
    private final int sizeZ;
    private final Object[] cache;
 
    public static <T> StaticCache2D<T> create(int centerX, int centerZ, int range, StaticCache2D.Initializer<T> initializer) {
        int minX = centerX - range;
        int minZ = centerZ - range;
        int size = 2 * range + 1;
        return new StaticCache2D<>(minX, minZ, size, size, initializer);
    }
 
    private StaticCache2D(int minX, int minZ, int sizeX, int sizeZ, StaticCache2D.Initializer<T> initializer) {
        this.minX = minX;
        this.minZ = minZ;
        this.sizeX = sizeX;
        this.sizeZ = sizeZ;
        this.cache = new Object[this.sizeX * this.sizeZ];
 
        for (int x = minX; x < minX + sizeX; x++) {
            for (int z = minZ; z < minZ + sizeZ; z++) {
                this.cache[this.getIndex(x, z)] = initializer.get(x, z);
            }
        }
    }
 
    public void forEach(Consumer<T> consumer) {
        for (Object o : this.cache) {
            consumer.accept((T)o);
        }
    }
 
    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 deltaX = x - this.minX;
        int deltaZ = z - this.minZ;
        return deltaX >= 0 && deltaX < this.sizeX && deltaZ >= 0 && deltaZ < this.sizeZ;
    }
 
    @Override
    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 deltaX = x - this.minX;
        int deltaZ = z - this.minZ;
        return deltaX * this.sizeZ + deltaZ;
    }
 
    @FunctionalInterface
    public interface Initializer<T> {
        T get(int x, int z);
    }
}

引用的其他类