| 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.OpticFinder;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.Typed;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.datafixers.types.Type;
|
| 10 | import com.mojang.datafixers.util.Pair;
|
| 11 | import com.mojang.serialization.Dynamic;
|
| 12 | import java.util.Objects;
|
| 13 | import java.util.Optional;
|
| 14 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 15 |
|
| 16 | public class ItemStackMapIdFix extends DataFix {
|
| 17 | public ItemStackMapIdFix(final Schema outputSchema, final boolean changesType) {
|
| 18 | super(outputSchema, changesType);
|
| 19 | }
|
| 20 |
|
| 21 | @Override
|
| 22 | public TypeRewriteRule makeRule() {
|
| 23 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 24 | OpticFinder<Pair<String, String>> idF = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
|
| 25 | OpticFinder<?> tagF = itemStackType.findField("tag");
|
| 26 | return this.fixTypeEverywhereTyped("ItemInstanceMapIdFix", itemStackType, input -> {
|
| 27 | Optional<Pair<String, String>> id = input.getOptional(idF);
|
| 28 | if (id.isPresent() && Objects.equals(id.get().getSecond(), "minecraft:filled_map")) {
|
| 29 | Dynamic<?> rest = input.get(DSL.remainderFinder());
|
| 30 | Typed<?> tag = input.getOrCreateTyped(tagF);
|
| 31 | Dynamic<?> tagRest = tag.get(DSL.remainderFinder());
|
| 32 | tagRest = tagRest.set("map", tagRest.createInt(rest.get("Damage").asInt(0)));
|
| 33 | return input.set(tagF, tag.set(DSL.remainderFinder(), tagRest));
|
| 34 | } else {
|
| 35 | return input;
|
| 36 | }
|
| 37 | });
|
| 38 | }
|
| 39 | }
|
| 40 |
|