| 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.schemas.Schema;
|
| 8 | import com.mojang.serialization.Dynamic;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.Set;
|
| 13 | import java.util.stream.Collectors;
|
| 14 |
|
| 15 | public class FeatureFlagRemoveFix extends DataFix {
|
| 16 | private final String name;
|
| 17 | private final Set<String> flagsToRemove;
|
| 18 |
|
| 19 | public FeatureFlagRemoveFix(final Schema outputSchema, final String name, final Set<String> flagsToRemove) {
|
| 20 | super(outputSchema, false);
|
| 21 | this.name = name;
|
| 22 | this.flagsToRemove = flagsToRemove;
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | protected TypeRewriteRule makeRule() {
|
| 27 | return this.fixTypeEverywhereTyped(
|
| 28 | this.name, this.getInputSchema().getType(References.LIGHTWEIGHT_LEVEL), input -> input.update(DSL.remainderFinder(), this::fixTag)
|
| 29 | );
|
| 30 | }
|
| 31 |
|
| 32 | private <T> Dynamic<T> fixTag(final Dynamic<T> tag) {
|
| 33 | List<Dynamic<T>> inactiveFeatures = tag.get("removed_features").asStream().collect(Collectors.toCollection(ArrayList::new));
|
| 34 | Dynamic<T> result = tag.update("enabled_features", features -> DataFixUtils.orElse(features.asStreamOpt().result().map(s -> s.filter(feature -> {
|
| 35 | Optional<String> asString = feature.asString().result();
|
| 36 | if (asString.isEmpty()) {
|
| 37 | return true;
|
| 38 | }
|
| 39 |
|
| 40 | boolean shouldRemove = this.flagsToRemove.contains(asString.get());
|
| 41 | if (shouldRemove) {
|
| 42 | inactiveFeatures.add(tag.createString(asString.get()));
|
| 43 | }
|
| 44 |
|
| 45 | return !shouldRemove;
|
| 46 | })).map(tag::createList), features));
|
| 47 | if (!inactiveFeatures.isEmpty()) {
|
| 48 | result = result.set("removed_features", tag.createList(inactiveFeatures.stream()));
|
| 49 | }
|
| 50 |
|
| 51 | return result;
|
| 52 | }
|
| 53 | }
|
| 54 |
|