ChunkRenamesFix.java

net.minecraft.util.datafix.fixes.ChunkRenamesFix

信息

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

    TODO

字段/常量

内部类/嵌套类型

构造器

public ChunkRenamesFix(Schema outputSchema) @ L20

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

参数:

  • outputSchema: Schema

说明:

TODO

方法

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

protected TypeRewriteRule makeRule() @ L24

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

参数:

说明:

TODO

private static Typed<?> renameField(Typed<?> input, String oldName, String newName) @ L45

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

参数:

  • input: Typed<?>
  • oldName: String
  • newName: String

说明:

TODO

private static <A> Typed<?> renameFieldHelper(Typed<?> input, String oldName, String newName, Type<A> fieldType) @ L49

参数:

说明:

TODO

private static <A> Typed<Pair<String,A>> appendChunkName(Typed<A> input) @ L55

参数:

说明:

TODO

private static <T> Dynamic<T> mergeRemainders(Typed<?> chunk, Dynamic<T> levelRemainder) @ L59

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

参数:

  • chunk: Typed<?>
  • levelRemainder: Dynamic

说明:

TODO

代码

public class ChunkRenamesFix extends DataFix {
    public ChunkRenamesFix(Schema outputSchema) {
        super(outputSchema, true);
    }
 
    @Override
    protected TypeRewriteRule makeRule() {
        Type<?> chunkType = this.getInputSchema().getType(References.CHUNK);
        OpticFinder<?> levelFinder = chunkType.findField("Level");
        OpticFinder<?> structureFinder = levelFinder.type().findField("Structures");
        Type<?> newChunkType = this.getOutputSchema().getType(References.CHUNK);
        Type<?> newStructuresType = newChunkType.findFieldType("structures");
        return this.fixTypeEverywhereTyped("Chunk Renames; purge Level-tag", chunkType, newChunkType, chunk -> {
            Typed<?> level = chunk.getTyped(levelFinder);
            Typed<?> chunkTyped = appendChunkName(level);
            chunkTyped = chunkTyped.set(DSL.remainderFinder(), mergeRemainders(chunk, level.get(DSL.remainderFinder())));
            chunkTyped = renameField(chunkTyped, "TileEntities", "block_entities");
            chunkTyped = renameField(chunkTyped, "TileTicks", "block_ticks");
            chunkTyped = renameField(chunkTyped, "Entities", "entities");
            chunkTyped = renameField(chunkTyped, "Sections", "sections");
            chunkTyped = chunkTyped.updateTyped(structureFinder, newStructuresType, structure -> renameField(structure, "Starts", "starts"));
            chunkTyped = renameField(chunkTyped, "Structures", "structures");
            return chunkTyped.update(DSL.remainderFinder(), remainder -> remainder.remove("Level"));
        });
    }
 
    private static Typed<?> renameField(Typed<?> input, String oldName, String newName) {
        return renameFieldHelper(input, oldName, newName, input.getType().findFieldType(oldName)).update(DSL.remainderFinder(), tag -> tag.remove(oldName));
    }
 
    private static <A> Typed<?> renameFieldHelper(Typed<?> input, String oldName, String newName, Type<A> fieldType) {
        Type<Either<A, Unit>> oldType = DSL.optional(DSL.field(oldName, fieldType));
        Type<Either<A, Unit>> newType = DSL.optional(DSL.field(newName, fieldType));
        return input.update(oldType.finder(), newType, Function.identity());
    }
 
    private static <A> Typed<Pair<String, A>> appendChunkName(Typed<A> input) {
        return new Typed<>(DSL.named("chunk", input.getType()), input.getOps(), Pair.of("chunk", input.getValue()));
    }
 
    private static <T> Dynamic<T> mergeRemainders(Typed<?> chunk, Dynamic<T> levelRemainder) {
        DynamicOps<T> ops = levelRemainder.getOps();
        Dynamic<T> chunkRemainder = chunk.get(DSL.remainderFinder()).convert(ops);
        DataResult<T> toMap = ops.getMap(levelRemainder.getValue()).flatMap(map -> ops.mergeToMap(chunkRemainder.getValue(), (MapLike<T>)map));
        return toMap.result().map(v -> new Dynamic<>(ops, (T)v)).orElse(levelRemainder);
    }
}

引用的其他类