| 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.DataFixUtils;
|
| 6 | import com.mojang.datafixers.OpticFinder;
|
| 7 | import com.mojang.datafixers.TypeRewriteRule;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.datafixers.types.Type;
|
| 10 | import com.mojang.serialization.Dynamic;
|
| 11 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 12 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 13 | import java.util.Optional;
|
| 14 |
|
| 15 | public class ItemStackEnchantmentNamesFix extends DataFix {
|
| 16 | private static final Int2ObjectMap<String> MAP = DataFixUtils.make(new Int2ObjectOpenHashMap<>(), map -> {
|
| 17 | map.put(0, "minecraft:protection");
|
| 18 | map.put(1, "minecraft:fire_protection");
|
| 19 | map.put(2, "minecraft:feather_falling");
|
| 20 | map.put(3, "minecraft:blast_protection");
|
| 21 | map.put(4, "minecraft:projectile_protection");
|
| 22 | map.put(5, "minecraft:respiration");
|
| 23 | map.put(6, "minecraft:aqua_affinity");
|
| 24 | map.put(7, "minecraft:thorns");
|
| 25 | map.put(8, "minecraft:depth_strider");
|
| 26 | map.put(9, "minecraft:frost_walker");
|
| 27 | map.put(10, "minecraft:binding_curse");
|
| 28 | map.put(16, "minecraft:sharpness");
|
| 29 | map.put(17, "minecraft:smite");
|
| 30 | map.put(18, "minecraft:bane_of_arthropods");
|
| 31 | map.put(19, "minecraft:knockback");
|
| 32 | map.put(20, "minecraft:fire_aspect");
|
| 33 | map.put(21, "minecraft:looting");
|
| 34 | map.put(22, "minecraft:sweeping");
|
| 35 | map.put(32, "minecraft:efficiency");
|
| 36 | map.put(33, "minecraft:silk_touch");
|
| 37 | map.put(34, "minecraft:unbreaking");
|
| 38 | map.put(35, "minecraft:fortune");
|
| 39 | map.put(48, "minecraft:power");
|
| 40 | map.put(49, "minecraft:punch");
|
| 41 | map.put(50, "minecraft:flame");
|
| 42 | map.put(51, "minecraft:infinity");
|
| 43 | map.put(61, "minecraft:luck_of_the_sea");
|
| 44 | map.put(62, "minecraft:lure");
|
| 45 | map.put(65, "minecraft:loyalty");
|
| 46 | map.put(66, "minecraft:impaling");
|
| 47 | map.put(67, "minecraft:riptide");
|
| 48 | map.put(68, "minecraft:channeling");
|
| 49 | map.put(70, "minecraft:mending");
|
| 50 | map.put(71, "minecraft:vanishing_curse");
|
| 51 | });
|
| 52 |
|
| 53 | public ItemStackEnchantmentNamesFix(final Schema outputSchema, final boolean changesType) {
|
| 54 | super(outputSchema, changesType);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected TypeRewriteRule makeRule() {
|
| 59 | Type<?> item = this.getInputSchema().getType(References.ITEM_STACK);
|
| 60 | OpticFinder<?> tagFinder = item.findField("tag");
|
| 61 | return this.fixTypeEverywhereTyped(
|
| 62 | "ItemStackEnchantmentFix", item, input -> input.updateTyped(tagFinder, tag -> tag.update(DSL.remainderFinder(), this::fixTag))
|
| 63 | );
|
| 64 | }
|
| 65 |
|
| 66 | private Dynamic<?> fixTag(Dynamic<?> tag) {
|
| 67 | Optional<? extends Dynamic<?>> newEnch = tag.get("ench")
|
| 68 | .asStreamOpt()
|
| 69 | .map(s -> s.map(element -> element.set("id", element.createString(MAP.getOrDefault(element.get("id").asInt(0), "null")))))
|
| 70 | .map(tag::createList)
|
| 71 | .result();
|
| 72 | if (newEnch.isPresent()) {
|
| 73 | tag = tag.remove("ench").set("Enchantments", (Dynamic<?>)newEnch.get());
|
| 74 | }
|
| 75 |
|
| 76 | return tag.update(
|
| 77 | "StoredEnchantments",
|
| 78 | list -> DataFixUtils.orElse(
|
| 79 | list.asStreamOpt()
|
| 80 | .map(l -> l.map(enchant -> enchant.set("id", enchant.createString(MAP.getOrDefault(enchant.get("id").asInt(0), "null")))))
|
| 81 | .map(list::createList)
|
| 82 | .result(),
|
| 83 | list
|
| 84 | )
|
| 85 | );
|
| 86 | }
|
| 87 | }
|
| 88 |
|