| 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.Optional;
|
| 13 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 14 |
|
| 15 | public class ItemWaterPotionFix extends DataFix {
|
| 16 | public ItemWaterPotionFix(final Schema outputSchema, final boolean changesType) {
|
| 17 | super(outputSchema, changesType);
|
| 18 | }
|
| 19 |
|
| 20 | @Override
|
| 21 | public TypeRewriteRule makeRule() {
|
| 22 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 23 | OpticFinder<Pair<String, String>> idF = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
|
| 24 | OpticFinder<?> tagF = itemStackType.findField("tag");
|
| 25 | return this.fixTypeEverywhereTyped(
|
| 26 | "ItemWaterPotionFix",
|
| 27 | itemStackType,
|
| 28 | input -> {
|
| 29 | Optional<Pair<String, String>> idOpt = input.getOptional(idF);
|
| 30 | if (idOpt.isPresent()) {
|
| 31 | String id = idOpt.get().getSecond();
|
| 32 | if ("minecraft:potion".equals(id)
|
| 33 | || "minecraft:splash_potion".equals(id)
|
| 34 | || "minecraft:lingering_potion".equals(id)
|
| 35 | || "minecraft:tipped_arrow".equals(id)) {
|
| 36 | Typed<?> tag = input.getOrCreateTyped(tagF);
|
| 37 | Dynamic<?> tagRest = tag.get(DSL.remainderFinder());
|
| 38 | if (tagRest.get("Potion").asString().result().isEmpty()) {
|
| 39 | tagRest = tagRest.set("Potion", tagRest.createString("minecraft:water"));
|
| 40 | }
|
| 41 |
|
| 42 | return input.set(tagF, tag.set(DSL.remainderFinder(), tagRest));
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | return input;
|
| 47 | }
|
| 48 | );
|
| 49 | }
|
| 50 | }
|
| 51 |
|