| 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.OpticFinder;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.schemas.Schema;
|
| 8 | import com.mojang.serialization.Dynamic;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 14 |
|
| 15 | public class InlineBlockPosFormatFix extends DataFix {
|
| 16 | public InlineBlockPosFormatFix(final Schema outputSchema) {
|
| 17 | super(outputSchema, false);
|
| 18 | }
|
| 19 |
|
| 20 | @Override
|
| 21 | public TypeRewriteRule makeRule() {
|
| 22 | OpticFinder<?> vexFinder = this.entityFinder("minecraft:vex");
|
| 23 | OpticFinder<?> phantomFinder = this.entityFinder("minecraft:phantom");
|
| 24 | OpticFinder<?> turtleFinder = this.entityFinder("minecraft:turtle");
|
| 25 | List<OpticFinder<?>> blockAttachedFinders = List.of(
|
| 26 | this.entityFinder("minecraft:item_frame"),
|
| 27 | this.entityFinder("minecraft:glow_item_frame"),
|
| 28 | this.entityFinder("minecraft:painting"),
|
| 29 | this.entityFinder("minecraft:leash_knot")
|
| 30 | );
|
| 31 | return TypeRewriteRule.seq(
|
| 32 | this.fixTypeEverywhereTyped(
|
| 33 | "InlineBlockPosFormatFix - player", this.getInputSchema().getType(References.PLAYER), player -> player.update(DSL.remainderFinder(), this::fixPlayer)
|
| 34 | ),
|
| 35 | this.fixTypeEverywhereTyped(
|
| 36 | "InlineBlockPosFormatFix - entity",
|
| 37 | this.getInputSchema().getType(References.ENTITY),
|
| 38 | entity -> {
|
| 39 | entity = entity.update(DSL.remainderFinder(), this::fixLivingEntity)
|
| 40 | .updateTyped(vexFinder, vex -> vex.update(DSL.remainderFinder(), this::fixVex))
|
| 41 | .updateTyped(phantomFinder, phantom -> phantom.update(DSL.remainderFinder(), this::fixPhantom))
|
| 42 | .updateTyped(turtleFinder, turtle -> turtle.update(DSL.remainderFinder(), this::fixTurtle));
|
| 43 |
|
| 44 | for (OpticFinder<?> blockAttachedFinder : blockAttachedFinders) {
|
| 45 | entity = entity.updateTyped(blockAttachedFinder, blockAttached -> blockAttached.update(DSL.remainderFinder(), this::fixBlockAttached));
|
| 46 | }
|
| 47 |
|
| 48 | return entity;
|
| 49 | }
|
| 50 | )
|
| 51 | );
|
| 52 | }
|
| 53 |
|
| 54 | private OpticFinder<?> entityFinder(final String choiceName) {
|
| 55 | return DSL.namedChoice(choiceName, this.getInputSchema().getChoiceType(References.ENTITY, choiceName));
|
| 56 | }
|
| 57 |
|
| 58 | private Dynamic<?> fixPlayer(Dynamic<?> tag) {
|
| 59 | tag = this.fixLivingEntity(tag);
|
| 60 | Optional<Number> spawnX = tag.get("SpawnX").asNumber().result();
|
| 61 | Optional<Number> spawnY = tag.get("SpawnY").asNumber().result();
|
| 62 | Optional<Number> spawnZ = tag.get("SpawnZ").asNumber().result();
|
| 63 | if (spawnX.isPresent() && spawnY.isPresent() && spawnZ.isPresent()) {
|
| 64 | Dynamic<?> respawn = tag.createMap(
|
| 65 | Map.of(tag.createString("pos"), ExtraDataFixUtils.createBlockPos(tag, spawnX.get().intValue(), spawnY.get().intValue(), spawnZ.get().intValue()))
|
| 66 | );
|
| 67 | respawn = Dynamic.copyField(tag, "SpawnAngle", respawn, "angle");
|
| 68 | respawn = Dynamic.copyField(tag, "SpawnDimension", respawn, "dimension");
|
| 69 | respawn = Dynamic.copyField(tag, "SpawnForced", respawn, "forced");
|
| 70 | tag = tag.remove("SpawnX").remove("SpawnY").remove("SpawnZ").remove("SpawnAngle").remove("SpawnDimension").remove("SpawnForced");
|
| 71 | tag = tag.set("respawn", respawn);
|
| 72 | }
|
| 73 |
|
| 74 | Optional<? extends Dynamic<?>> enteredNetherPos = tag.get("enteredNetherPosition").result();
|
| 75 | if (enteredNetherPos.isPresent()) {
|
| 76 | tag = tag.remove("enteredNetherPosition")
|
| 77 | .set(
|
| 78 | "entered_nether_pos",
|
| 79 | tag.createList(
|
| 80 | Stream.of(
|
| 81 | tag.createDouble(enteredNetherPos.get().get("x").asDouble(0.0)),
|
| 82 | tag.createDouble(enteredNetherPos.get().get("y").asDouble(0.0)),
|
| 83 | tag.createDouble(enteredNetherPos.get().get("z").asDouble(0.0))
|
| 84 | )
|
| 85 | )
|
| 86 | );
|
| 87 | }
|
| 88 |
|
| 89 | return tag;
|
| 90 | }
|
| 91 |
|
| 92 | private Dynamic<?> fixLivingEntity(final Dynamic<?> tag) {
|
| 93 | return ExtraDataFixUtils.fixInlineBlockPos(tag, "SleepingX", "SleepingY", "SleepingZ", "sleeping_pos");
|
| 94 | }
|
| 95 |
|
| 96 | private Dynamic<?> fixVex(final Dynamic<?> tag) {
|
| 97 | return ExtraDataFixUtils.fixInlineBlockPos(tag.renameField("LifeTicks", "life_ticks"), "BoundX", "BoundY", "BoundZ", "bound_pos");
|
| 98 | }
|
| 99 |
|
| 100 | private Dynamic<?> fixPhantom(final Dynamic<?> tag) {
|
| 101 | return ExtraDataFixUtils.fixInlineBlockPos(tag.renameField("Size", "size"), "AX", "AY", "AZ", "anchor_pos");
|
| 102 | }
|
| 103 |
|
| 104 | private Dynamic<?> fixTurtle(Dynamic<?> tag) {
|
| 105 | tag = tag.remove("TravelPosX").remove("TravelPosY").remove("TravelPosZ");
|
| 106 | tag = ExtraDataFixUtils.fixInlineBlockPos(tag, "HomePosX", "HomePosY", "HomePosZ", "home_pos");
|
| 107 | return tag.renameField("HasEgg", "has_egg");
|
| 108 | }
|
| 109 |
|
| 110 | private Dynamic<?> fixBlockAttached(final Dynamic<?> tag) {
|
| 111 | return ExtraDataFixUtils.fixInlineBlockPos(tag, "TileX", "TileY", "TileZ", "block_pos");
|
| 112 | }
|
| 113 | }
|
| 114 |
|