| 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.datafixers.types.Type;
|
| 9 | import com.mojang.serialization.Dynamic;
|
| 10 |
|
| 11 | public class EntityPaintingItemFrameDirectionFix extends DataFix {
|
| 12 | private static final int[][] DIRECTIONS = new int[][]{{0, 0, 1}, {-1, 0, 0}, {0, 0, -1}, {1, 0, 0}};
|
| 13 |
|
| 14 | public EntityPaintingItemFrameDirectionFix(final Schema outputSchema, final boolean changesType) {
|
| 15 | super(outputSchema, changesType);
|
| 16 | }
|
| 17 |
|
| 18 | private Dynamic<?> doFix(Dynamic<?> input, final boolean isPainting, final boolean isItemFrame) {
|
| 19 | if ((isPainting || isItemFrame) && input.get("Facing").asNumber().result().isEmpty()) {
|
| 20 | int direction;
|
| 21 | if (input.get("Direction").asNumber().result().isPresent()) {
|
| 22 | direction = input.get("Direction").asByte((byte)0) % DIRECTIONS.length;
|
| 23 | int[] steps = DIRECTIONS[direction];
|
| 24 | input = input.set("TileX", input.createInt(input.get("TileX").asInt(0) + steps[0]));
|
| 25 | input = input.set("TileY", input.createInt(input.get("TileY").asInt(0) + steps[1]));
|
| 26 | input = input.set("TileZ", input.createInt(input.get("TileZ").asInt(0) + steps[2]));
|
| 27 | input = input.remove("Direction");
|
| 28 | if (isItemFrame && input.get("ItemRotation").asNumber().result().isPresent()) {
|
| 29 | input = input.set("ItemRotation", input.createByte((byte)(input.get("ItemRotation").asByte((byte)0) * 2)));
|
| 30 | }
|
| 31 | } else {
|
| 32 | direction = input.get("Dir").asByte((byte)0) % DIRECTIONS.length;
|
| 33 | input = input.remove("Dir");
|
| 34 | }
|
| 35 |
|
| 36 | input = input.set("Facing", input.createByte((byte)direction));
|
| 37 | }
|
| 38 |
|
| 39 | return input;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public TypeRewriteRule makeRule() {
|
| 44 | Type<?> paintingType = this.getInputSchema().getChoiceType(References.ENTITY, "Painting");
|
| 45 | OpticFinder<?> paintingF = DSL.namedChoice("Painting", paintingType);
|
| 46 | Type<?> itemFrameType = this.getInputSchema().getChoiceType(References.ENTITY, "ItemFrame");
|
| 47 | OpticFinder<?> itemFrameF = DSL.namedChoice("ItemFrame", itemFrameType);
|
| 48 | Type<?> entityType = this.getInputSchema().getType(References.ENTITY);
|
| 49 | TypeRewriteRule paintingRule = this.fixTypeEverywhereTyped(
|
| 50 | "EntityPaintingFix",
|
| 51 | entityType,
|
| 52 | input -> input.updateTyped(paintingF, paintingType, entity -> entity.update(DSL.remainderFinder(), tag -> this.doFix(tag, true, false)))
|
| 53 | );
|
| 54 | TypeRewriteRule itemFrameRule = this.fixTypeEverywhereTyped(
|
| 55 | "EntityItemFrameFix",
|
| 56 | entityType,
|
| 57 | input -> input.updateTyped(itemFrameF, itemFrameType, entity -> entity.update(DSL.remainderFinder(), tag -> this.doFix(tag, false, true)))
|
| 58 | );
|
| 59 | return TypeRewriteRule.seq(paintingRule, itemFrameRule);
|
| 60 | }
|
| 61 | }
|
| 62 |
|