ChunkToProtochunkFix.java

net.minecraft.util.datafix.fixes.ChunkToProtochunkFix

信息

  • 全限定名:net.minecraft.util.datafix.fixes.ChunkToProtochunkFix
  • 类型:public class
  • 包:net.minecraft.util.datafix.fixes
  • 源码路径:src/main/java/net/minecraft/util/datafix/fixes/ChunkToProtochunkFix.java
  • 起始行号:L15
  • 继承:DataFix
  • 职责:

    TODO

字段/常量

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

      TODO

内部类/嵌套类型

构造器

public ChunkToProtochunkFix(Schema outputSchema, boolean changesType) @ L18

  • 构造器名:ChunkToProtochunkFix
  • 源码定位:L18
  • 修饰符:public

参数:

  • outputSchema: Schema
  • changesType: boolean

说明:

TODO

方法

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

public TypeRewriteRule makeRule() @ L22

  • 方法名:makeRule
  • 源码定位:L22
  • 返回类型:TypeRewriteRule
  • 修饰符:public

参数:

说明:

TODO

private static <T> Dynamic<T> fixChunkData(Dynamic<T> tag) @ L32

  • 方法名:fixChunkData
  • 源码定位:L32
  • 返回类型: Dynamic
  • 修饰符:private static

参数:

  • tag: Dynamic

说明:

TODO

private static <T> Dynamic<T> repackBiomes(Dynamic<T> tag) @ L49

  • 方法名:repackBiomes
  • 源码定位:L49
  • 返回类型: Dynamic
  • 修饰符:private static

参数:

  • tag: Dynamic

说明:

TODO

private static <T> Dynamic<T> repackTicks(Dynamic<T> tag) @ L63

  • 方法名:repackTicks
  • 源码定位:L63
  • 返回类型: Dynamic
  • 修饰符:private static

参数:

  • tag: Dynamic

说明:

TODO

private static short packOffsetCoordinates(int x, int y, int z) @ L89

  • 方法名:packOffsetCoordinates
  • 源码定位:L89
  • 返回类型:short
  • 修饰符:private static

参数:

  • x: int
  • y: int
  • z: int

说明:

TODO

代码

public class ChunkToProtochunkFix extends DataFix {
    private static final int NUM_SECTIONS = 16;
 
    public ChunkToProtochunkFix(Schema outputSchema, boolean changesType) {
        super(outputSchema, changesType);
    }
 
    @Override
    public TypeRewriteRule makeRule() {
        return this.writeFixAndRead(
            "ChunkToProtoChunkFix",
            this.getInputSchema().getType(References.CHUNK),
            this.getOutputSchema().getType(References.CHUNK),
            chunk -> chunk.update("Level", ChunkToProtochunkFix::fixChunkData)
        );
    }
 
    private static <T> Dynamic<T> fixChunkData(Dynamic<T> tag) {
        boolean terrainPopulated = tag.get("TerrainPopulated").asBoolean(false);
        boolean lightPopulated = tag.get("LightPopulated").asNumber().result().isEmpty() || tag.get("LightPopulated").asBoolean(false);
        String status;
        if (terrainPopulated) {
            if (lightPopulated) {
                status = "mobs_spawned";
            } else {
                status = "decorated";
            }
        } else {
            status = "carved";
        }
 
        return repackTicks(repackBiomes(tag)).set("Status", tag.createString(status)).set("hasLegacyStructureData", tag.createBoolean(true));
    }
 
    private static <T> Dynamic<T> repackBiomes(Dynamic<T> tag) {
        return tag.update("Biomes", biomes -> DataFixUtils.orElse(biomes.asByteBufferOpt().result().map(buffer -> {
            int[] newBiomes = new int[256];
 
            for (int i = 0; i < newBiomes.length; i++) {
                if (i < buffer.capacity()) {
                    newBiomes[i] = buffer.get(i) & 255;
                }
            }
 
            return tag.createIntList(Arrays.stream(newBiomes));
        }), biomes));
    }
 
    private static <T> Dynamic<T> repackTicks(Dynamic<T> tag) {
        return DataFixUtils.orElse(
            tag.get("TileTicks")
                .asStreamOpt()
                .result()
                .map(
                    ticks -> {
                        List<ShortList> toBeTickedTag = IntStream.range(0, 16).mapToObj(i -> new ShortArrayList()).collect(Collectors.toList());
                        ticks.forEach(pendingTickTag -> {
                            int x = pendingTickTag.get("x").asInt(0);
                            int y = pendingTickTag.get("y").asInt(0);
                            int z = pendingTickTag.get("z").asInt(0);
                            short packedOffset = packOffsetCoordinates(x, y, z);
                            toBeTickedTag.get(y >> 4).add(packedOffset);
                        });
                        return tag.remove("TileTicks")
                            .set(
                                "ToBeTicked",
                                tag.createList(toBeTickedTag.stream().map(l -> tag.createList(l.intStream().mapToObj(v -> tag.createShort((short)v)))))
                            );
                    }
                ),
            tag
        );
    }
 
    private static short packOffsetCoordinates(int x, int y, int z) {
        return (short)(x & 15 | (y & 15) << 4 | (z & 15) << 8);
    }
}

引用的其他类