FoodToConsumableFix.java

net.minecraft.util.datafix.fixes.FoodToConsumableFix

信息

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

    TODO

字段/常量

内部类/嵌套类型

构造器

public FoodToConsumableFix(Schema outputSchema) @ L11

  • 构造器名:FoodToConsumableFix
  • 源码定位:L11
  • 修饰符:public

参数:

  • outputSchema: Schema

说明:

TODO

方法

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

protected TypeRewriteRule makeRule() @ L15

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

参数:

说明:

TODO

代码

public class FoodToConsumableFix extends DataFix {
    public FoodToConsumableFix(Schema outputSchema) {
        super(outputSchema, true);
    }
 
    @Override
    protected TypeRewriteRule makeRule() {
        return this.writeFixAndRead(
            "Food to consumable fix",
            this.getInputSchema().getType(References.DATA_COMPONENTS),
            this.getOutputSchema().getType(References.DATA_COMPONENTS),
            components -> {
                Optional<? extends Dynamic<?>> foodComponent = components.get("minecraft:food").result();
                if (foodComponent.isPresent()) {
                    float eatSeconds = foodComponent.get().get("eat_seconds").asFloat(1.6F);
                    Stream<? extends Dynamic<?>> effects = foodComponent.get().get("effects").asStream();
                    Stream<? extends Dynamic<?>> onConsumeEffects = effects.map(
                        effect -> effect.emptyMap()
                            .set("type", effect.createString("minecraft:apply_effects"))
                            .set("effects", effect.createList(effect.get("effect").result().stream()))
                            .set("probability", effect.createFloat(effect.get("probability").asFloat(1.0F)))
                    );
                    components = Dynamic.copyField((Dynamic<?>)foodComponent.get(), "using_converts_to", components, "minecraft:use_remainder");
                    components = components.set("minecraft:food", foodComponent.get().remove("eat_seconds").remove("effects").remove("using_converts_to"));
                    return components.set(
                        "minecraft:consumable",
                        components.emptyMap()
                            .set("consume_seconds", components.createFloat(eatSeconds))
                            .set("on_consume_effects", components.createList(onConsumeEffects))
                    );
                } else {
                    return components;
                }
            }
        );
    }
}

引用的其他类