ChunkBedBlockEntityInjecterFix.java

net.minecraft.util.datafix.fixes.ChunkBedBlockEntityInjecterFix

信息

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

    TODO

字段/常量

内部类/嵌套类型

构造器

public ChunkBedBlockEntityInjecterFix(Schema outputSchema, boolean changesType) @ L20

  • 构造器名:ChunkBedBlockEntityInjecterFix
  • 源码定位:L20
  • 修饰符:public

参数:

  • outputSchema: Schema
  • changesType: boolean

说明:

TODO

方法

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

public TypeRewriteRule makeRule() @ L24

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

参数:

说明:

TODO

private <TE> TypeRewriteRule cap(Type<?> levelType, ListType<TE> tileEntityListType) @ L35

  • 方法名:cap
  • 源码定位:L35
  • 返回类型: TypeRewriteRule
  • 修饰符:private

参数:

  • levelType: Type<?>
  • tileEntityListType: ListType

说明:

TODO

代码

public class ChunkBedBlockEntityInjecterFix extends DataFix {
    public ChunkBedBlockEntityInjecterFix(Schema outputSchema, boolean changesType) {
        super(outputSchema, changesType);
    }
 
    @Override
    public TypeRewriteRule makeRule() {
        Type<?> chunkType = this.getOutputSchema().getType(References.CHUNK);
        Type<?> levelType = chunkType.findFieldType("Level");
        if (!(levelType.findFieldType("TileEntities") instanceof ListType<?> tileEntityListType)) {
            throw new IllegalStateException("Tile entity type is not a list type.");
        } else {
            return this.cap(levelType, tileEntityListType);
        }
    }
 
    private <TE> TypeRewriteRule cap(Type<?> levelType, ListType<TE> tileEntityListType) {
        Type<TE> tileEntityType = tileEntityListType.getElement();
        OpticFinder<?> levelF = DSL.fieldFinder("Level", levelType);
        OpticFinder<List<TE>> tileEntitiesF = DSL.fieldFinder("TileEntities", tileEntityListType);
        int bedId = 416;
        return TypeRewriteRule.seq(
            this.fixTypeEverywhere(
                "InjectBedBlockEntityType",
                (com.mojang.datafixers.types.templates.TaggedChoice.TaggedChoiceType<String>)this.getInputSchema().findChoiceType(References.BLOCK_ENTITY),
                (com.mojang.datafixers.types.templates.TaggedChoice.TaggedChoiceType<String>)this.getOutputSchema().findChoiceType(References.BLOCK_ENTITY),
                ops -> v -> v
            ),
            this.fixTypeEverywhereTyped(
                "BedBlockEntityInjecter",
                this.getOutputSchema().getType(References.CHUNK),
                input -> {
                    Typed<?> level = input.getTyped(levelF);
                    Dynamic<?> levelTag = level.get(DSL.remainderFinder());
                    int chunkX = levelTag.get("xPos").asInt(0);
                    int chunkZ = levelTag.get("zPos").asInt(0);
                    List<TE> tileEntities = Lists.newArrayList(level.getOrCreate(tileEntitiesF));
 
                    for (Dynamic<?> sectionTag : levelTag.get("Sections").asList(Function.identity())) {
                        int pos = sectionTag.get("Y").asInt(0);
                        Streams.mapWithIndex(sectionTag.get("Blocks").asIntStream(), (block, index) -> {
                                if (416 == (block & 0xFF) << 4) {
                                    int p = (int)index;
                                    int xx = p & 15;
                                    int yy = p >> 8 & 15;
                                    int zz = p >> 4 & 15;
                                    Map<Dynamic<?>, Dynamic<?>> bedTag = Maps.newHashMap();
                                    bedTag.put(sectionTag.createString("id"), sectionTag.createString("minecraft:bed"));
                                    bedTag.put(sectionTag.createString("x"), sectionTag.createInt(xx + (chunkX << 4)));
                                    bedTag.put(sectionTag.createString("y"), sectionTag.createInt(yy + (pos << 4)));
                                    bedTag.put(sectionTag.createString("z"), sectionTag.createInt(zz + (chunkZ << 4)));
                                    bedTag.put(sectionTag.createString("color"), sectionTag.createShort((short)14));
                                    return bedTag;
                                } else {
                                    return null;
                                }
                            })
                            .forEachOrdered(
                                bedTag -> {
                                    if (bedTag != null) {
                                        tileEntities.add(
                                            tileEntityType.read(sectionTag.createMap((Map<? extends Dynamic<?>, ? extends Dynamic<?>>)bedTag))
                                                .result()
                                                .orElseThrow(() -> new IllegalStateException("Could not parse newly created bed block entity."))
                                                .getFirst()
                                        );
                                    }
                                }
                            );
                    }
 
                    return !tileEntities.isEmpty() ? input.set(levelF, level.set(tileEntitiesF, tileEntities)) : input;
                }
            )
        );
    }
}

引用的其他类