| 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.Typed;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.datafixers.types.Type;
|
| 10 | import com.mojang.datafixers.util.Pair;
|
| 11 | import com.mojang.serialization.DataResult;
|
| 12 | import com.mojang.serialization.Dynamic;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.stream.Stream;
|
| 15 |
|
| 16 | public class MobSpawnerEntityIdentifiersFix extends DataFix {
|
| 17 | public MobSpawnerEntityIdentifiersFix(final Schema outputSchema, final boolean changesType) {
|
| 18 | super(outputSchema, changesType);
|
| 19 | }
|
| 20 |
|
| 21 | private Dynamic<?> fix(Dynamic<?> input) {
|
| 22 | if (!"MobSpawner".equals(input.get("id").asString(""))) {
|
| 23 | return input;
|
| 24 | }
|
| 25 |
|
| 26 | Optional<String> entityId = input.get("EntityId").asString().result();
|
| 27 | if (entityId.isPresent()) {
|
| 28 | Dynamic<?> spawnData = DataFixUtils.orElse(input.get("SpawnData").result(), input.emptyMap());
|
| 29 | spawnData = spawnData.set("id", spawnData.createString(entityId.get().isEmpty() ? "Pig" : entityId.get()));
|
| 30 | input = input.set("SpawnData", spawnData);
|
| 31 | input = input.remove("EntityId");
|
| 32 | }
|
| 33 |
|
| 34 | Optional<? extends Stream<? extends Dynamic<?>>> spawnPotentials = input.get("SpawnPotentials").asStreamOpt().result();
|
| 35 | if (spawnPotentials.isPresent()) {
|
| 36 | input = input.set(
|
| 37 | "SpawnPotentials",
|
| 38 | input.createList(
|
| 39 | spawnPotentials.get()
|
| 40 | .map(
|
| 41 | spawnPotential -> {
|
| 42 | Optional<String> type = spawnPotential.get("Type").asString().result();
|
| 43 | if (type.isPresent()) {
|
| 44 | Dynamic<?> spawnData = DataFixUtils.orElse(spawnPotential.get("Properties").result(), spawnPotential.emptyMap())
|
| 45 | .set("id", spawnPotential.createString(type.get()));
|
| 46 | return spawnPotential.set("Entity", spawnData).remove("Type").remove("Properties");
|
| 47 | } else {
|
| 48 | return spawnPotential;
|
| 49 | }
|
| 50 | }
|
| 51 | )
|
| 52 | )
|
| 53 | );
|
| 54 | }
|
| 55 |
|
| 56 | return input;
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public TypeRewriteRule makeRule() {
|
| 61 | Type<?> newType = this.getOutputSchema().getType(References.UNTAGGED_SPAWNER);
|
| 62 | return this.fixTypeEverywhereTyped("MobSpawnerEntityIdentifiersFix", this.getInputSchema().getType(References.UNTAGGED_SPAWNER), newType, input -> {
|
| 63 | Dynamic<?> tag = input.get(DSL.remainderFinder());
|
| 64 | tag = tag.set("id", tag.createString("MobSpawner"));
|
| 65 | DataResult<? extends Pair<? extends Typed<?>, ?>> fixed = newType.readTyped(this.fix(tag));
|
| 66 | return fixed.result().isEmpty() ? input : fixed.result().get().getFirst();
|
| 67 | });
|
| 68 | }
|
| 69 | }
|
| 70 |
|