| 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.DataFixUtils;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.DSL.TypeReference;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.datafixers.types.Type;
|
| 10 |
|
| 11 | public class AddFlagIfNotPresentFix extends DataFix {
|
| 12 | private final String name;
|
| 13 | private final boolean flagValue;
|
| 14 | private final String flagKey;
|
| 15 | private final TypeReference typeReference;
|
| 16 |
|
| 17 | public AddFlagIfNotPresentFix(final Schema outputSchema, final TypeReference typeReference, final String flagKey, final boolean flagValue) {
|
| 18 | super(outputSchema, true);
|
| 19 | this.flagValue = flagValue;
|
| 20 | this.flagKey = flagKey;
|
| 21 | this.name = "AddFlagIfNotPresentFix_" + this.flagKey + "=" + this.flagValue + " for " + outputSchema.getVersionKey();
|
| 22 | this.typeReference = typeReference;
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | protected TypeRewriteRule makeRule() {
|
| 27 | Type<?> worldGenSettingsType = this.getInputSchema().getType(this.typeReference);
|
| 28 | return this.fixTypeEverywhereTyped(
|
| 29 | this.name,
|
| 30 | worldGenSettingsType,
|
| 31 | settings -> settings.update(
|
| 32 | DSL.remainderFinder(), tag -> tag.set(this.flagKey, DataFixUtils.orElseGet(tag.get(this.flagKey).result(), () -> tag.createBoolean(this.flagValue)))
|
| 33 | )
|
| 34 | );
|
| 35 | }
|
| 36 | }
|
| 37 |
|