EquipmentFormatFix.java

net.minecraft.util.datafix.fixes.EquipmentFormatFix

信息

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

    TODO

字段/常量

内部类/嵌套类型

构造器

public EquipmentFormatFix(Schema outputSchema) @ L19

  • 构造器名:EquipmentFormatFix
  • 源码定位:L19
  • 修饰符:public

参数:

  • outputSchema: Schema

说明:

TODO

方法

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

protected TypeRewriteRule makeRule() @ L23

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

参数:

说明:

TODO

private <ItemStackOld,ItemStackNew> TypeRewriteRule fix(Type<ItemStackOld> oldItemStackType, Type<ItemStackNew> newItemStackType, OpticFinder<?> idFinder) @ L31

  • 方法名:fix
  • 源码定位:L31
  • 返回类型:<ItemStackOld,ItemStackNew> TypeRewriteRule
  • 修饰符:private

参数:

  • oldItemStackType: Type
  • newItemStackType: Type
  • idFinder: OpticFinder<?>

说明:

TODO

private static boolean areAllEmpty(Either<?,Unit>... fields) @ L121

  • 方法名:areAllEmpty
  • 源码定位:L121
  • 返回类型:boolean
  • 修饰符:private static

参数:

  • fields: Either<?,Unit>…

说明:

TODO

private static <ItemStack> Either<ItemStack,Unit> getItemFromList(int index, List<ItemStack> items, Predicate<ItemStack> isPlaceholder) @ L132

  • 方法名:getItemFromList
  • 源码定位:L132
  • 返回类型: Either<ItemStack,Unit>
  • 修饰符:private static

参数:

  • index: int
  • items: List
  • isPlaceholder: Predicate

说明:

TODO

代码

public class EquipmentFormatFix extends DataFix {
    public EquipmentFormatFix(Schema outputSchema) {
        super(outputSchema, true);
    }
 
    @Override
    protected TypeRewriteRule makeRule() {
        Type<?> oldItemStackType = this.getInputSchema().getTypeRaw(References.ITEM_STACK);
        Type<?> newItemStackType = this.getOutputSchema().getTypeRaw(References.ITEM_STACK);
        OpticFinder<?> idFinder = oldItemStackType.findField("id");
        return this.fix(oldItemStackType, newItemStackType, idFinder);
    }
 
    private <ItemStackOld, ItemStackNew> TypeRewriteRule fix(Type<ItemStackOld> oldItemStackType, Type<ItemStackNew> newItemStackType, OpticFinder<?> idFinder) {
        Type<Pair<String, Pair<Either<List<ItemStackOld>, Unit>, Pair<Either<List<ItemStackOld>, Unit>, Pair<Either<ItemStackOld, Unit>, Either<ItemStackOld, Unit>>>>>> oldEquipmentType = DSL.named(
            References.ENTITY_EQUIPMENT.typeName(),
            DSL.and(
                DSL.optional(DSL.field("ArmorItems", DSL.list(oldItemStackType))),
                DSL.optional(DSL.field("HandItems", DSL.list(oldItemStackType))),
                DSL.optional(DSL.field("body_armor_item", oldItemStackType)),
                DSL.optional(DSL.field("saddle", oldItemStackType))
            )
        );
        Type<Pair<String, Either<Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Pair<Either<ItemStackNew, Unit>, Dynamic<?>>>>>>>>>, Unit>>> newEquipmentType = DSL.named(
            References.ENTITY_EQUIPMENT.typeName(),
            DSL.optional(
                DSL.field(
                    "equipment",
                    DSL.and(
                        DSL.optional(DSL.field("mainhand", newItemStackType)),
                        DSL.optional(DSL.field("offhand", newItemStackType)),
                        DSL.optional(DSL.field("feet", newItemStackType)),
                        DSL.and(
                            DSL.optional(DSL.field("legs", newItemStackType)),
                            DSL.optional(DSL.field("chest", newItemStackType)),
                            DSL.optional(DSL.field("head", newItemStackType)),
                            DSL.and(DSL.optional(DSL.field("body", newItemStackType)), DSL.optional(DSL.field("saddle", newItemStackType)), DSL.remainderType())
                        )
                    )
                )
            )
        );
        if (!oldEquipmentType.equals(this.getInputSchema().getType(References.ENTITY_EQUIPMENT))) {
            throw new IllegalStateException("Input entity_equipment type does not match expected");
        } else if (!newEquipmentType.equals(this.getOutputSchema().getType(References.ENTITY_EQUIPMENT))) {
            throw new IllegalStateException("Output entity_equipment type does not match expected");
        } else {
            return this.fixTypeEverywhere(
                "EquipmentFormatFix",
                oldEquipmentType,
                newEquipmentType,
                ops -> {
                    Predicate<ItemStackOld> isPlaceholder = itemStack -> {
                        Typed<ItemStackOld> typed = new Typed<>(oldItemStackType, ops, itemStack);
                        return typed.getOptional(idFinder).isEmpty();
                    };
                    return namedOldEquipment -> {
                        String typeName = namedOldEquipment.getFirst();
                        Pair<Either<List<ItemStackOld>, Unit>, Pair<Either<List<ItemStackOld>, Unit>, Pair<Either<ItemStackOld, Unit>, Either<ItemStackOld, Unit>>>> oldEquipment = namedOldEquipment.getSecond();
                        List<ItemStackOld> armorItems = oldEquipment.getFirst().map(Function.identity(), ignored -> List.of());
                        List<ItemStackOld> handItems = oldEquipment.getSecond().getFirst().map(Function.identity(), ignored -> List.of());
                        Either<ItemStackOld, Unit> body = oldEquipment.getSecond().getSecond().getFirst();
                        Either<ItemStackOld, Unit> saddle = oldEquipment.getSecond().getSecond().getSecond();
                        Either<ItemStackOld, Unit> feet = getItemFromList(0, armorItems, isPlaceholder);
                        Either<ItemStackOld, Unit> legs = getItemFromList(1, armorItems, isPlaceholder);
                        Either<ItemStackOld, Unit> chest = getItemFromList(2, armorItems, isPlaceholder);
                        Either<ItemStackOld, Unit> head = getItemFromList(3, armorItems, isPlaceholder);
                        Either<ItemStackOld, Unit> mainhand = getItemFromList(0, handItems, isPlaceholder);
                        Either<ItemStackOld, Unit> offhand = getItemFromList(1, handItems, isPlaceholder);
                        return areAllEmpty(body, saddle, feet, legs, chest, head, mainhand, offhand)
                            ? Pair.of(typeName, Either.right(Unit.INSTANCE))
                            : Pair.of(
                                typeName,
                                Either.left(
                                    Pair.of(
                                        (Either<ItemStackNew, Unit>)mainhand,
                                        Pair.of(
                                            (Either<ItemStackNew, Unit>)offhand,
                                            Pair.of(
                                                (Either<ItemStackNew, Unit>)feet,
                                                Pair.of(
                                                    (Either<ItemStackNew, Unit>)legs,
                                                    Pair.of(
                                                        (Either<ItemStackNew, Unit>)chest,
                                                        Pair.of(
                                                            (Either<ItemStackNew, Unit>)head,
                                                            Pair.of(
                                                                (Either<ItemStackNew, Unit>)body, Pair.of((Either<ItemStackNew, Unit>)saddle, new Dynamic(ops))
                                                            )
                                                        )
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )
                            );
                    };
                }
            );
        }
    }
 
    @SafeVarargs
    private static boolean areAllEmpty(Either<?, Unit>... fields) {
        for (Either<?, Unit> field : fields) {
            if (field.right().isEmpty()) {
                return false;
            }
        }
 
        return true;
    }
 
    private static <ItemStack> Either<ItemStack, Unit> getItemFromList(int index, List<ItemStack> items, Predicate<ItemStack> isPlaceholder) {
        if (index >= items.size()) {
            return Either.right(Unit.INSTANCE);
        } else {
            ItemStack item = items.get(index);
            return isPlaceholder.test(item) ? Either.right(Unit.INSTANCE) : Either.left(item);
        }
    }
}

引用的其他类