| 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.TypeRewriteRule;
|
| 6 | import com.mojang.datafixers.schemas.Schema;
|
| 7 | import com.mojang.datafixers.types.Type;
|
| 8 | import com.mojang.serialization.Dynamic;
|
| 9 | import java.util.Optional;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public abstract class DataComponentRemainderFix extends DataFix {
|
| 13 | private final String name;
|
| 14 | private final String componentId;
|
| 15 | private final String newComponentId;
|
| 16 |
|
| 17 | public DataComponentRemainderFix(final Schema outputSchema, final String name, final String componentId) {
|
| 18 | this(outputSchema, name, componentId, componentId);
|
| 19 | }
|
| 20 |
|
| 21 | public DataComponentRemainderFix(final Schema outputSchema, final String name, final String componentId, final String newComponentId) {
|
| 22 | super(outputSchema, false);
|
| 23 | this.name = name;
|
| 24 | this.componentId = componentId;
|
| 25 | this.newComponentId = newComponentId;
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public final TypeRewriteRule makeRule() {
|
| 30 | Type<?> dataComponentsType = this.getInputSchema().getType(References.DATA_COMPONENTS);
|
| 31 | return this.fixTypeEverywhereTyped(this.name, dataComponentsType, components -> components.update(DSL.remainderFinder(), remainder -> {
|
| 32 | Optional<? extends Dynamic<?>> component = remainder.get(this.componentId).result();
|
| 33 | if (component.isEmpty()) {
|
| 34 | return remainder;
|
| 35 | }
|
| 36 |
|
| 37 | Dynamic<?> newComponent = this.fixComponent((Dynamic<?>)component.get());
|
| 38 | return remainder.remove(this.componentId).setFieldIfPresent(this.newComponentId, Optional.ofNullable(newComponent));
|
| 39 | }));
|
| 40 | }
|
| 41 |
|
| 42 | protected abstract <T> @Nullable Dynamic<T> fixComponent(Dynamic<T> input);
|
| 43 | }
|
| 44 |
|