| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DSL;
|
| 4 | import com.mojang.datafixers.DataFix;
|
| 5 | import com.mojang.datafixers.TypeRewriteRule;
|
| 6 | import com.mojang.datafixers.schemas.Schema;
|
| 7 | import com.mojang.serialization.Dynamic;
|
| 8 | import com.mojang.serialization.OptionalDynamic;
|
| 9 | import java.util.List;
|
| 10 |
|
| 11 | public class DropChancesFormatFix extends DataFix {
|
| 12 | private static final List<String> ARMOR_SLOT_NAMES = List.of("feet", "legs", "chest", "head");
|
| 13 | private static final List<String> HAND_SLOT_NAMES = List.of("mainhand", "offhand");
|
| 14 | private static final float DEFAULT_CHANCE = 0.085F;
|
| 15 |
|
| 16 | public DropChancesFormatFix(final Schema outputSchema) {
|
| 17 | super(outputSchema, false);
|
| 18 | }
|
| 19 |
|
| 20 | @Override
|
| 21 | protected TypeRewriteRule makeRule() {
|
| 22 | return this.fixTypeEverywhereTyped(
|
| 23 | "DropChancesFormatFix", this.getInputSchema().getType(References.ENTITY), input -> input.update(DSL.remainderFinder(), remainder -> {
|
| 24 | List<Float> armorDropChances = parseDropChances(remainder.get("ArmorDropChances"));
|
| 25 | List<Float> handDropChances = parseDropChances(remainder.get("HandDropChances"));
|
| 26 | float bodyArmorDropChance = remainder.get("body_armor_drop_chance").asNumber().result().map(Number::floatValue).orElse(0.085F);
|
| 27 | remainder = remainder.remove("ArmorDropChances").remove("HandDropChances").remove("body_armor_drop_chance");
|
| 28 | Dynamic<?> slotChances = remainder.emptyMap();
|
| 29 | slotChances = addSlotChances(slotChances, armorDropChances, ARMOR_SLOT_NAMES);
|
| 30 | slotChances = addSlotChances(slotChances, handDropChances, HAND_SLOT_NAMES);
|
| 31 | if (bodyArmorDropChance != 0.085F) {
|
| 32 | slotChances = slotChances.set("body", remainder.createFloat(bodyArmorDropChance));
|
| 33 | }
|
| 34 |
|
| 35 | return !slotChances.equals(remainder.emptyMap()) ? remainder.set("drop_chances", slotChances) : remainder;
|
| 36 | })
|
| 37 | );
|
| 38 | }
|
| 39 |
|
| 40 | private static Dynamic<?> addSlotChances(Dynamic<?> output, final List<Float> chances, final List<String> slotNames) {
|
| 41 | for (int i = 0; i < slotNames.size() && i < chances.size(); i++) {
|
| 42 | String slot = slotNames.get(i);
|
| 43 | float chance = chances.get(i);
|
| 44 | if (chance != 0.085F) {
|
| 45 | output = output.set(slot, output.createFloat(chance));
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | return output;
|
| 50 | }
|
| 51 |
|
| 52 | private static List<Float> parseDropChances(final OptionalDynamic<?> value) {
|
| 53 | return value.asStream().map(dynamic -> dynamic.asFloat(0.085F)).toList();
|
| 54 | }
|
| 55 | }
|
| 56 |
|