| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import com.mojang.datafixers.DataFix;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.schemas.Schema;
|
| 8 | import com.mojang.serialization.DataResult;
|
| 9 | import com.mojang.serialization.Dynamic;
|
| 10 | import com.mojang.serialization.OptionalDynamic;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.Set;
|
| 14 | import java.util.function.Function;
|
| 15 | import java.util.regex.Matcher;
|
| 16 | import java.util.regex.Pattern;
|
| 17 | import java.util.stream.Stream;
|
| 18 |
|
| 19 | public class SavedDataFeaturePoolElementFix extends DataFix {
|
| 20 | private static final Pattern INDEX_PATTERN = Pattern.compile("\\[(\\d+)\\]");
|
| 21 | private static final Set<String> PIECE_TYPE = Sets.newHashSet(
|
| 22 | "minecraft:jigsaw", "minecraft:nvi", "minecraft:pcp", "minecraft:bastionremnant", "minecraft:runtime"
|
| 23 | );
|
| 24 | private static final Set<String> FEATURES = Sets.newHashSet("minecraft:tree", "minecraft:flower", "minecraft:block_pile", "minecraft:random_patch");
|
| 25 |
|
| 26 | public SavedDataFeaturePoolElementFix(final Schema outputSchema) {
|
| 27 | super(outputSchema, false);
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public TypeRewriteRule makeRule() {
|
| 32 | return this.writeFixAndRead(
|
| 33 | "SavedDataFeaturePoolElementFix",
|
| 34 | this.getInputSchema().getType(References.STRUCTURE_FEATURE),
|
| 35 | this.getOutputSchema().getType(References.STRUCTURE_FEATURE),
|
| 36 | SavedDataFeaturePoolElementFix::fixTag
|
| 37 | );
|
| 38 | }
|
| 39 |
|
| 40 | private static <T> Dynamic<T> fixTag(final Dynamic<T> input) {
|
| 41 | return input.update("Children", SavedDataFeaturePoolElementFix::updateChildren);
|
| 42 | }
|
| 43 |
|
| 44 | private static <T> Dynamic<T> updateChildren(final Dynamic<T> input) {
|
| 45 | return input.asStreamOpt().map(SavedDataFeaturePoolElementFix::updateChildren).map(input::createList).result().orElse(input);
|
| 46 | }
|
| 47 |
|
| 48 | private static Stream<? extends Dynamic<?>> updateChildren(final Stream<? extends Dynamic<?>> stream) {
|
| 49 | return stream.map(
|
| 50 | child -> {
|
| 51 | String id = child.get("id").asString("");
|
| 52 | if (!PIECE_TYPE.contains(id)) {
|
| 53 | return child;
|
| 54 | }
|
| 55 |
|
| 56 | OptionalDynamic<?> poolElement = child.get("pool_element");
|
| 57 | return !poolElement.get("element_type").asString("").equals("minecraft:feature_pool_element")
|
| 58 | ? child
|
| 59 | : child.update("pool_element", pool -> pool.update("feature", SavedDataFeaturePoolElementFix::fixFeature));
|
| 60 | }
|
| 61 | );
|
| 62 | }
|
| 63 |
|
| 64 | private static <T> OptionalDynamic<T> get(final Dynamic<T> input, final String... path) {
|
| 65 | if (path.length == 0) {
|
| 66 | throw new IllegalArgumentException("Missing path");
|
| 67 | }
|
| 68 |
|
| 69 | OptionalDynamic<T> output = input.get(path[0]);
|
| 70 |
|
| 71 | for (int i = 1; i < path.length; i++) {
|
| 72 | String element = path[i];
|
| 73 | Matcher matcher = INDEX_PATTERN.matcher(element);
|
| 74 | if (matcher.matches()) {
|
| 75 | int id = Integer.parseInt(matcher.group(1));
|
| 76 | List<? extends Dynamic<T>> dynamics = output.asList(Function.identity());
|
| 77 | if (id >= 0 && id < dynamics.size()) {
|
| 78 | output = new OptionalDynamic<>(input.getOps(), DataResult.success((Dynamic<T>)dynamics.get(id)));
|
| 79 | } else {
|
| 80 | output = new OptionalDynamic<>(input.getOps(), DataResult.error(() -> "Missing id:" + id));
|
| 81 | }
|
| 82 | } else {
|
| 83 | output = output.get(element);
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | return output;
|
| 88 | }
|
| 89 |
|
| 90 | @VisibleForTesting
|
| 91 | protected static Dynamic<?> fixFeature(final Dynamic<?> value) {
|
| 92 | Optional<String> replacement = getReplacement(
|
| 93 | get(value, "type").asString(""),
|
| 94 | get(value, "name").asString(""),
|
| 95 | get(value, "config", "state_provider", "type").asString(""),
|
| 96 | get(value, "config", "state_provider", "state", "Name").asString(""),
|
| 97 | get(value, "config", "state_provider", "entries", "[0]", "data", "Name").asString(""),
|
| 98 | get(value, "config", "foliage_placer", "type").asString(""),
|
| 99 | get(value, "config", "leaves_provider", "state", "Name").asString("")
|
| 100 | );
|
| 101 | return replacement.isPresent() ? value.createString(replacement.get()) : value;
|
| 102 | }
|
| 103 |
|
| 104 | private static Optional<String> getReplacement(
|
| 105 | final String type,
|
| 106 | final String name,
|
| 107 | final String stateProviderType,
|
| 108 | final String stateProviderState,
|
| 109 | final String stateProviderFirstWeighedState,
|
| 110 | final String foliagePlacerType,
|
| 111 | final String leavesProviderState
|
| 112 | ) {
|
| 113 | String feature;
|
| 114 | if (!type.isEmpty()) {
|
| 115 | feature = type;
|
| 116 | } else {
|
| 117 | if (name.isEmpty()) {
|
| 118 | return Optional.empty();
|
| 119 | }
|
| 120 |
|
| 121 | if ("minecraft:normal_tree".equals(name)) {
|
| 122 | feature = "minecraft:tree";
|
| 123 | } else {
|
| 124 | feature = name;
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | if (FEATURES.contains(feature)) {
|
| 129 | if ("minecraft:random_patch".equals(feature)) {
|
| 130 | if ("minecraft:simple_state_provider".equals(stateProviderType)) {
|
| 131 | if ("minecraft:sweet_berry_bush".equals(stateProviderState)) {
|
| 132 | return Optional.of("minecraft:patch_berry_bush");
|
| 133 | }
|
| 134 |
|
| 135 | if ("minecraft:cactus".equals(stateProviderState)) {
|
| 136 | return Optional.of("minecraft:patch_cactus");
|
| 137 | }
|
| 138 | } else if ("minecraft:weighted_state_provider".equals(stateProviderType)
|
| 139 | && ("minecraft:grass".equals(stateProviderFirstWeighedState) || "minecraft:fern".equals(stateProviderFirstWeighedState))) {
|
| 140 | return Optional.of("minecraft:patch_taiga_grass");
|
| 141 | }
|
| 142 | } else if ("minecraft:block_pile".equals(feature)) {
|
| 143 | if (!"minecraft:simple_state_provider".equals(stateProviderType) && !"minecraft:rotated_block_provider".equals(stateProviderType)) {
|
| 144 | if ("minecraft:weighted_state_provider".equals(stateProviderType)) {
|
| 145 | if ("minecraft:packed_ice".equals(stateProviderFirstWeighedState) || "minecraft:blue_ice".equals(stateProviderFirstWeighedState)) {
|
| 146 | return Optional.of("minecraft:pile_ice");
|
| 147 | }
|
| 148 |
|
| 149 | if ("minecraft:jack_o_lantern".equals(stateProviderFirstWeighedState) || "minecraft:pumpkin".equals(stateProviderFirstWeighedState)) {
|
| 150 | return Optional.of("minecraft:pile_pumpkin");
|
| 151 | }
|
| 152 | }
|
| 153 | } else {
|
| 154 | if ("minecraft:hay_block".equals(stateProviderState)) {
|
| 155 | return Optional.of("minecraft:pile_hay");
|
| 156 | }
|
| 157 |
|
| 158 | if ("minecraft:melon".equals(stateProviderState)) {
|
| 159 | return Optional.of("minecraft:pile_melon");
|
| 160 | }
|
| 161 |
|
| 162 | if ("minecraft:snow".equals(stateProviderState)) {
|
| 163 | return Optional.of("minecraft:pile_snow");
|
| 164 | }
|
| 165 | }
|
| 166 | } else {
|
| 167 | if ("minecraft:flower".equals(feature)) {
|
| 168 | return Optional.of("minecraft:flower_plain");
|
| 169 | }
|
| 170 |
|
| 171 | if ("minecraft:tree".equals(feature)) {
|
| 172 | if ("minecraft:acacia_foliage_placer".equals(foliagePlacerType)) {
|
| 173 | return Optional.of("minecraft:acacia");
|
| 174 | }
|
| 175 |
|
| 176 | if ("minecraft:blob_foliage_placer".equals(foliagePlacerType) && "minecraft:oak_leaves".equals(leavesProviderState)) {
|
| 177 | return Optional.of("minecraft:oak");
|
| 178 | }
|
| 179 |
|
| 180 | if ("minecraft:pine_foliage_placer".equals(foliagePlacerType)) {
|
| 181 | return Optional.of("minecraft:pine");
|
| 182 | }
|
| 183 |
|
| 184 | if ("minecraft:spruce_foliage_placer".equals(foliagePlacerType)) {
|
| 185 | return Optional.of("minecraft:spruce");
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | return Optional.empty();
|
| 192 | }
|
| 193 | }
|
| 194 |
|