| 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.HashMap;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Optional;
|
| 12 |
|
| 13 | public class DebugProfileLookingAtSplitFix extends DataFix {
|
| 14 | public DebugProfileLookingAtSplitFix(final Schema outputSchema) {
|
| 15 | super(outputSchema, false);
|
| 16 | }
|
| 17 |
|
| 18 | @Override
|
| 19 | public TypeRewriteRule makeRule() {
|
| 20 | return this.fixTypeEverywhereTyped(
|
| 21 | "DebugProfileLookingAtSplitFix",
|
| 22 | this.getInputSchema().getType(References.DEBUG_PROFILE),
|
| 23 | input -> input.update(DSL.remainderFinder(), tag -> tag.update("custom", DebugProfileLookingAtSplitFix::updateOptions))
|
| 24 | );
|
| 25 | }
|
| 26 |
|
| 27 | private static <T> Dynamic<T> updateOptions(final Dynamic<T> custom) {
|
| 28 | return DataFixUtils.orElse(
|
| 29 | custom.getMapValues()
|
| 30 | .map(
|
| 31 | map -> {
|
| 32 | Map<Dynamic<T>, Dynamic<T>> newOptions = new HashMap<>();
|
| 33 | map.forEach(
|
| 34 | (key, value) -> renamedKey((Dynamic<T>)key)
|
| 35 | .ifPresentOrElse(newKey -> newOptions.putIfAbsent((Dynamic<T>)newKey, value), () -> newOptions.put((Dynamic<T>)key, (Dynamic<T>)value))
|
| 36 | );
|
| 37 | return custom.createMap(newOptions);
|
| 38 | }
|
| 39 | )
|
| 40 | .result(),
|
| 41 | custom
|
| 42 | );
|
| 43 | }
|
| 44 |
|
| 45 | private static <T> Optional<Dynamic<T>> renamedKey(final Dynamic<T> keyDynamic) {
|
| 46 | return keyDynamic.asString().result().flatMap(key -> {
|
| 47 | return switch (key) {
|
| 48 | case "minecraft:looking_at_block" -> Optional.of("minecraft:looking_at_block_state");
|
| 49 | case "minecraft:looking_at_fluid" -> Optional.of("minecraft:looking_at_fluid_state");
|
| 50 | default -> Optional.empty();
|
| 51 | };
|
| 52 | }).map(keyDynamic::createString);
|
| 53 | }
|
| 54 | }
|
| 55 |
|