| 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 java.util.Set;
|
| 14 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 15 |
|
| 16 | public class EffectDurationFix extends DataFix {
|
| 17 | private static final Set<String> POTION_ITEMS = Set.of("minecraft:potion", "minecraft:splash_potion", "minecraft:lingering_potion", "minecraft:tipped_arrow");
|
| 18 |
|
| 19 | public EffectDurationFix(final Schema outputSchema) {
|
| 20 | super(outputSchema, false);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | protected TypeRewriteRule makeRule() {
|
| 25 | Schema inputSchema = this.getInputSchema();
|
| 26 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 27 | OpticFinder<Pair<String, String>> idFinder = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
|
| 28 | OpticFinder<?> tagFinder = itemStackType.findField("tag");
|
| 29 | return TypeRewriteRule.seq(
|
| 30 | this.fixTypeEverywhereTyped("EffectDurationEntity", inputSchema.getType(References.ENTITY), input -> input.update(DSL.remainderFinder(), this::updateEntity)),
|
| 31 | this.fixTypeEverywhereTyped("EffectDurationPlayer", inputSchema.getType(References.PLAYER), input -> input.update(DSL.remainderFinder(), this::updateEntity)),
|
| 32 | this.fixTypeEverywhereTyped("EffectDurationItem", itemStackType, input -> {
|
| 33 | if (input.getOptional(idFinder).filter(typeAndIdPair -> POTION_ITEMS.contains(typeAndIdPair.getSecond())).isPresent()) {
|
| 34 | Optional<? extends Typed<?>> tag = input.getOptionalTyped(tagFinder);
|
| 35 | if (tag.isPresent()) {
|
| 36 | Dynamic<?> tagRest = tag.get().get(DSL.remainderFinder());
|
| 37 | Typed<?> newTag = tag.get().set(DSL.remainderFinder(), tagRest.update("CustomPotionEffects", this::fix));
|
| 38 | return input.set(tagFinder, newTag);
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | return input;
|
| 43 | })
|
| 44 | );
|
| 45 | }
|
| 46 |
|
| 47 | private Dynamic<?> fixEffect(final Dynamic<?> effect) {
|
| 48 | return effect.update("FactorCalculationData", factorData -> {
|
| 49 | int timestamp = factorData.get("effect_changed_timestamp").asInt(-1);
|
| 50 | factorData = factorData.remove("effect_changed_timestamp");
|
| 51 | int duration = effect.get("Duration").asInt(-1);
|
| 52 | int ticksActive = timestamp - duration;
|
| 53 | return factorData.set("ticks_active", factorData.createInt(ticksActive));
|
| 54 | });
|
| 55 | }
|
| 56 |
|
| 57 | private Dynamic<?> fix(final Dynamic<?> input) {
|
| 58 | return input.createList(input.asStream().map(this::fixEffect));
|
| 59 | }
|
| 60 |
|
| 61 | private Dynamic<?> updateEntity(Dynamic<?> data) {
|
| 62 | data = data.update("Effects", this::fix);
|
| 63 | data = data.update("ActiveEffects", this::fix);
|
| 64 | return data.update("CustomPotionEffects", this::fix);
|
| 65 | }
|
| 66 | }
|
| 67 |
|