| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import com.mojang.datafixers.DSL;
|
| 5 | import com.mojang.datafixers.OpticFinder;
|
| 6 | import com.mojang.datafixers.Typed;
|
| 7 | import com.mojang.datafixers.schemas.Schema;
|
| 8 | import com.mojang.datafixers.types.Type;
|
| 9 | import com.mojang.datafixers.util.Pair;
|
| 10 | import java.util.function.Supplier;
|
| 11 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 12 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 13 |
|
| 14 | public class ThrownPotionSplitFix extends EntityRenameFix {
|
| 15 | private final Supplier<ThrownPotionSplitFix.ItemIdFinder> itemIdFinder = Suppliers.memoize(
|
| 16 | () -> {
|
| 17 | Type<?> potionType = this.getInputSchema().getChoiceType(References.ENTITY, "minecraft:potion");
|
| 18 | Type<?> patchedPotionType = ExtraDataFixUtils.patchSubType(
|
| 19 | potionType, this.getInputSchema().getType(References.ENTITY), this.getOutputSchema().getType(References.ENTITY)
|
| 20 | );
|
| 21 | OpticFinder<?> itemFinder = patchedPotionType.findField("Item");
|
| 22 | OpticFinder<Pair<String, String>> itemIdFinder = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
|
| 23 | return new ThrownPotionSplitFix.ItemIdFinder(itemFinder, itemIdFinder);
|
| 24 | }
|
| 25 | );
|
| 26 |
|
| 27 | public ThrownPotionSplitFix(final Schema outputSchema) {
|
| 28 | super("ThrownPotionSplitFix", outputSchema, true);
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | protected Pair<String, Typed<?>> fix(final String name, final Typed<?> entity) {
|
| 33 | if (!name.equals("minecraft:potion")) {
|
| 34 | return Pair.of(name, entity);
|
| 35 | }
|
| 36 |
|
| 37 | String itemId = this.itemIdFinder.get().getItemId(entity);
|
| 38 | return "minecraft:lingering_potion".equals(itemId) ? Pair.of("minecraft:lingering_potion", entity) : Pair.of("minecraft:splash_potion", entity);
|
| 39 | }
|
| 40 |
|
| 41 | private record ItemIdFinder(OpticFinder<?> itemFinder, OpticFinder<Pair<String, String>> itemIdFinder) {
|
| 42 | public String getItemId(final Typed<?> entity) {
|
| 43 | return entity.getOptionalTyped(this.itemFinder)
|
| 44 | .flatMap(item -> item.getOptional(this.itemIdFinder))
|
| 45 | .map(Pair::getSecond)
|
| 46 | .map(NamespacedSchema::ensureNamespaced)
|
| 47 | .orElse("");
|
| 48 | }
|
| 49 | }
|
| 50 | }
|
| 51 |
|