DropChancesFormatFix.java

net.minecraft.util.datafix.fixes.DropChancesFormatFix

信息

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

    TODO

字段/常量

  • ARMOR_SLOT_NAMES

    • 类型: List<String>
    • 修饰符: private static final
    • 源码定位: L12
    • 说明:

      TODO

  • HAND_SLOT_NAMES

    • 类型: List<String>
    • 修饰符: private static final
    • 源码定位: L13
    • 说明:

      TODO

  • DEFAULT_CHANCE

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

      TODO

内部类/嵌套类型

构造器

public DropChancesFormatFix(Schema outputSchema) @ L16

  • 构造器名:DropChancesFormatFix
  • 源码定位:L16
  • 修饰符:public

参数:

  • outputSchema: Schema

说明:

TODO

方法

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

protected TypeRewriteRule makeRule() @ L20

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

参数:

说明:

TODO

private static Dynamic<?> addSlotChances(Dynamic<?> output, List<Float> chances, List<String> slotNames) @ L40

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

参数:

  • output: Dynamic<?>
  • chances: List
  • slotNames: List

说明:

TODO

private static List<Float> parseDropChances(OptionalDynamic<?> value) @ L52

  • 方法名:parseDropChances
  • 源码定位:L52
  • 返回类型:List
  • 修饰符:private static

参数:

  • value: OptionalDynamic<?>

说明:

TODO

代码

public class DropChancesFormatFix extends DataFix {
    private static final List<String> ARMOR_SLOT_NAMES = List.of("feet", "legs", "chest", "head");
    private static final List<String> HAND_SLOT_NAMES = List.of("mainhand", "offhand");
    private static final float DEFAULT_CHANCE = 0.085F;
 
    public DropChancesFormatFix(Schema outputSchema) {
        super(outputSchema, false);
    }
 
    @Override
    protected TypeRewriteRule makeRule() {
        return this.fixTypeEverywhereTyped(
            "DropChancesFormatFix", this.getInputSchema().getType(References.ENTITY), input -> input.update(DSL.remainderFinder(), remainder -> {
                List<Float> armorDropChances = parseDropChances(remainder.get("ArmorDropChances"));
                List<Float> handDropChances = parseDropChances(remainder.get("HandDropChances"));
                float bodyArmorDropChance = remainder.get("body_armor_drop_chance").asNumber().result().map(Number::floatValue).orElse(0.085F);
                remainder = remainder.remove("ArmorDropChances").remove("HandDropChances").remove("body_armor_drop_chance");
                Dynamic<?> slotChances = remainder.emptyMap();
                slotChances = addSlotChances(slotChances, armorDropChances, ARMOR_SLOT_NAMES);
                slotChances = addSlotChances(slotChances, handDropChances, HAND_SLOT_NAMES);
                if (bodyArmorDropChance != 0.085F) {
                    slotChances = slotChances.set("body", remainder.createFloat(bodyArmorDropChance));
                }
 
                return !slotChances.equals(remainder.emptyMap()) ? remainder.set("drop_chances", slotChances) : remainder;
            })
        );
    }
 
    private static Dynamic<?> addSlotChances(Dynamic<?> output, List<Float> chances, List<String> slotNames) {
        for (int i = 0; i < slotNames.size() && i < chances.size(); i++) {
            String slot = slotNames.get(i);
            float chance = chances.get(i);
            if (chance != 0.085F) {
                output = output.set(slot, output.createFloat(chance));
            }
        }
 
        return output;
    }
 
    private static List<Float> parseDropChances(OptionalDynamic<?> value) {
        return value.asStream().map(dynamic -> dynamic.asFloat(0.085F)).toList();
    }
}

引用的其他类