BitStorageAlignFix.java

net.minecraft.util.datafix.fixes.BitStorageAlignFix

信息

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

    TODO

字段/常量

  • BIT_TO_LONG_SHIFT

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

      TODO

  • SECTION_WIDTH

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

      TODO

  • SECTION_HEIGHT

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

      TODO

  • SECTION_SIZE

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

      TODO

  • HEIGHTMAP_BITS

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

      TODO

  • HEIGHTMAP_SIZE

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

      TODO

内部类/嵌套类型

构造器

public BitStorageAlignFix(Schema schema) @ L26

  • 构造器名:BitStorageAlignFix
  • 源码定位:L26
  • 修饰符:public

参数:

  • schema: Schema

说明:

TODO

方法

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

protected TypeRewriteRule makeRule() @ L30

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

参数:

说明:

TODO

private Typed<?> updateHeightmaps(Typed<?> level) @ L48

  • 方法名:updateHeightmaps
  • 源码定位:L48
  • 返回类型:Typed<?>
  • 修饰符:private

参数:

  • level: Typed<?>

说明:

TODO

private static Typed<?> updateSections(OpticFinder<?> sectionsFinder, OpticFinder<?> sectionFinder, OpticFinder<List<Pair<String,Dynamic<?>>>> paletteFinder, Typed<?> level) @ L57

  • 方法名:updateSections
  • 源码定位:L57
  • 返回类型:Typed<?>
  • 修饰符:private static

参数:

  • sectionsFinder: OpticFinder<?>
  • sectionFinder: OpticFinder<?>
  • paletteFinder: OpticFinder<List<Pair<String,Dynamic<?>>>>
  • level: Typed<?>

说明:

TODO

private static Dynamic<?> updateBitStorage(Dynamic<?> tag, Dynamic<?> storage, int size, int bits) @ L74

  • 方法名:updateBitStorage
  • 源码定位:L74
  • 返回类型:Dynamic<?>
  • 修饰符:private static

参数:

  • tag: Dynamic<?>
  • storage: Dynamic<?>
  • size: int
  • bits: int

说明:

TODO

public static long[] addPadding(int size, int bits, long[] data) @ L80

  • 方法名:addPadding
  • 源码定位:L80
  • 返回类型:long[]
  • 修饰符:public static

参数:

  • size: int
  • bits: int
  • data: long[]

说明:

TODO

代码

public class BitStorageAlignFix extends DataFix {
    private static final int BIT_TO_LONG_SHIFT = 6;
    private static final int SECTION_WIDTH = 16;
    private static final int SECTION_HEIGHT = 16;
    private static final int SECTION_SIZE = 4096;
    private static final int HEIGHTMAP_BITS = 9;
    private static final int HEIGHTMAP_SIZE = 256;
 
    public BitStorageAlignFix(Schema schema) {
        super(schema, false);
    }
 
    @Override
    protected TypeRewriteRule makeRule() {
        Type<?> chunkType = this.getInputSchema().getType(References.CHUNK);
        Type<?> levelType = chunkType.findFieldType("Level");
        OpticFinder<?> levelFinder = DSL.fieldFinder("Level", levelType);
        OpticFinder<?> sectionsFinder = levelFinder.type().findField("Sections");
        Type<?> sectionType = ((ListType)sectionsFinder.type()).getElement();
        OpticFinder<?> sectionFinder = DSL.typeFinder(sectionType);
        Type<Pair<String, Dynamic<?>>> blockStateType = DSL.named(References.BLOCK_STATE.typeName(), DSL.remainderType());
        OpticFinder<List<Pair<String, Dynamic<?>>>> paletteFinder = DSL.fieldFinder("Palette", DSL.list(blockStateType));
        return this.fixTypeEverywhereTyped(
            "BitStorageAlignFix",
            chunkType,
            this.getOutputSchema().getType(References.CHUNK),
            chunk -> chunk.updateTyped(levelFinder, level -> this.updateHeightmaps(updateSections(sectionsFinder, sectionFinder, paletteFinder, level)))
        );
    }
 
    private Typed<?> updateHeightmaps(Typed<?> level) {
        return level.update(
            DSL.remainderFinder(),
            tag -> tag.update(
                "Heightmaps", heightmaps -> heightmaps.updateMapValues(e -> e.mapSecond(heightmap -> updateBitStorage(tag, (Dynamic<?>)heightmap, 256, 9)))
            )
        );
    }
 
    private static Typed<?> updateSections(
        OpticFinder<?> sectionsFinder, OpticFinder<?> sectionFinder, OpticFinder<List<Pair<String, Dynamic<?>>>> paletteFinder, Typed<?> level
    ) {
        return level.updateTyped(
            sectionsFinder,
            sections -> sections.updateTyped(
                sectionFinder,
                section -> {
                    int bits = section.getOptional(paletteFinder).map(palette -> Math.max(4, DataFixUtils.ceillog2(palette.size()))).orElse(0);
                    return bits != 0 && !Mth.isPowerOfTwo(bits)
                        ? section.update(DSL.remainderFinder(), tag -> tag.update("BlockStates", states -> updateBitStorage(tag, states, 4096, bits)))
                        : section;
                }
            )
        );
    }
 
    private static Dynamic<?> updateBitStorage(Dynamic<?> tag, Dynamic<?> storage, int size, int bits) {
        long[] input = storage.asLongStream().toArray();
        long[] output = addPadding(size, bits, input);
        return tag.createLongList(LongStream.of(output));
    }
 
    public static long[] addPadding(int size, int bits, long[] data) {
        int dataLength = data.length;
        if (dataLength == 0) {
            return data;
        } else {
            long mask = (1L << bits) - 1L;
            int valuesPerLong = 64 / bits;
            int requiredLength = (size + valuesPerLong - 1) / valuesPerLong;
            long[] result = new long[requiredLength];
            int outputDataIndex = 0;
            int outputStart = 0;
            long outputData = 0L;
            int currentIndex = 0;
            long current = data[0];
            long next = dataLength > 1 ? data[1] : 0L;
 
            for (int index = 0; index < size; index++) {
                int position = index * bits;
                int startData = position >> 6;
                int endData = (index + 1) * bits - 1 >> 6;
                int startBit = position ^ startData << 6;
                if (startData != currentIndex) {
                    current = next;
                    next = startData + 1 < dataLength ? data[startData + 1] : 0L;
                    currentIndex = startData;
                }
 
                long valueToInsert;
                if (startData == endData) {
                    valueToInsert = current >>> startBit & mask;
                } else {
                    int shiftBits = 64 - startBit;
                    valueToInsert = (current >>> startBit | next << shiftBits) & mask;
                }
 
                int outputEnd = outputStart + bits;
                if (outputEnd >= 64) {
                    result[outputDataIndex++] = outputData;
                    outputData = valueToInsert;
                    outputStart = bits;
                } else {
                    outputData |= valueToInsert << outputStart;
                    outputStart = outputEnd;
                }
            }
 
            if (outputData != 0L) {
                result[outputDataIndex] = outputData;
            }
 
            return result;
        }
    }
}

引用的其他类

  • Schema

    • 引用位置: 参数
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.isPowerOfTwo()
  • ResolvableProfile

    • 引用位置: 参数/返回值