|
| 1 | +/* |
| 2 | + * This file is part of Baritone. |
| 3 | + * |
| 4 | + * Baritone is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * Baritone is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with Baritone. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package baritone.api.schematic; |
| 19 | + |
| 20 | +import net.minecraft.world.level.block.state.BlockState; |
| 21 | +import net.minecraft.world.level.block.Rotation; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +public class RotatedSchematic implements ISchematic { |
| 27 | + |
| 28 | + private final ISchematic schematic; |
| 29 | + private final Rotation rotation; |
| 30 | + private final Rotation inverseRotation; |
| 31 | + |
| 32 | + public RotatedSchematic(ISchematic schematic, Rotation rotation) { |
| 33 | + this.schematic = schematic; |
| 34 | + this.rotation = rotation; |
| 35 | + // I don't think a 14 line switch would improve readability |
| 36 | + this.inverseRotation = rotation.getRotated(rotation).getRotated(rotation); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public boolean inSchematic(int x, int y, int z, BlockState currentState) { |
| 41 | + return schematic.inSchematic( |
| 42 | + rotateX(x, z, widthX(), lengthZ(), inverseRotation), |
| 43 | + y, |
| 44 | + rotateZ(x, z, widthX(), lengthZ(), inverseRotation), |
| 45 | + rotate(currentState, inverseRotation) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public BlockState desiredState(int x, int y, int z, BlockState current, List<BlockState> approxPlaceable) { |
| 51 | + return rotate(schematic.desiredState( |
| 52 | + rotateX(x, z, widthX(), lengthZ(), inverseRotation), |
| 53 | + y, |
| 54 | + rotateZ(x, z, widthX(), lengthZ(), inverseRotation), |
| 55 | + rotate(current, inverseRotation), |
| 56 | + rotate(approxPlaceable, inverseRotation) |
| 57 | + ), rotation); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void reset() { |
| 62 | + schematic.reset(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public int widthX() { |
| 67 | + return flipsCoordinates(rotation) ? schematic.lengthZ() : schematic.widthX(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public int heightY() { |
| 72 | + return schematic.heightY(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int lengthZ() { |
| 77 | + return flipsCoordinates(rotation) ? schematic.widthX() : schematic.lengthZ(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Wether {@code rotation} swaps the x and z components |
| 82 | + */ |
| 83 | + private static boolean flipsCoordinates(Rotation rotation) { |
| 84 | + return rotation == Rotation.CLOCKWISE_90 || rotation == Rotation.COUNTERCLOCKWISE_90; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * The x component of x,y after applying the rotation |
| 89 | + */ |
| 90 | + private static int rotateX(int x, int z, int sizeX, int sizeZ, Rotation rotation) { |
| 91 | + switch (rotation) { |
| 92 | + case NONE: |
| 93 | + return x; |
| 94 | + case CLOCKWISE_90: |
| 95 | + return sizeZ - z - 1; |
| 96 | + case CLOCKWISE_180: |
| 97 | + return sizeX - x - 1; |
| 98 | + case COUNTERCLOCKWISE_90: |
| 99 | + return z; |
| 100 | + } |
| 101 | + throw new IllegalArgumentException("Unknown rotation"); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * The z component of x,y after applying the rotation |
| 106 | + */ |
| 107 | + private static int rotateZ(int x, int z, int sizeX, int sizeZ, Rotation rotation) { |
| 108 | + switch (rotation) { |
| 109 | + case NONE: |
| 110 | + return z; |
| 111 | + case CLOCKWISE_90: |
| 112 | + return x; |
| 113 | + case CLOCKWISE_180: |
| 114 | + return sizeZ - z - 1; |
| 115 | + case COUNTERCLOCKWISE_90: |
| 116 | + return sizeX - x - 1; |
| 117 | + } |
| 118 | + throw new IllegalArgumentException("Unknown rotation"); |
| 119 | + } |
| 120 | + |
| 121 | + private static BlockState rotate(BlockState state, Rotation rotation) { |
| 122 | + if (state == null) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + return state.rotate(rotation); |
| 126 | + } |
| 127 | + |
| 128 | + private static List<BlockState> rotate(List<BlockState> states, Rotation rotation) { |
| 129 | + if (states == null) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + return states.stream() |
| 133 | + .map(s -> rotate(s, rotation)) |
| 134 | + .collect(Collectors.toList()); |
| 135 | + } |
| 136 | +} |
0 commit comments