| 1 | package net.minecraft.world.level.block.piston;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.world.level.Level;
|
| 8 | import net.minecraft.world.level.block.Blocks;
|
| 9 | import net.minecraft.world.level.block.state.BlockState;
|
| 10 | import net.minecraft.world.level.material.PushReaction;
|
| 11 |
|
| 12 | public class PistonStructureResolver {
|
| 13 | public static final int MAX_PUSH_DEPTH = 12;
|
| 14 | private final Level level;
|
| 15 | private final BlockPos pistonPos;
|
| 16 | private final boolean extending;
|
| 17 | private final BlockPos startPos;
|
| 18 | private final Direction pushDirection;
|
| 19 | private final List<BlockPos> toPush = Lists.newArrayList();
|
| 20 | private final List<BlockPos> toDestroy = Lists.newArrayList();
|
| 21 | private final Direction pistonDirection;
|
| 22 |
|
| 23 | public PistonStructureResolver(final Level level, final BlockPos pistonPos, final Direction direction, final boolean extending) {
|
| 24 | this.level = level;
|
| 25 | this.pistonPos = pistonPos;
|
| 26 | this.pistonDirection = direction;
|
| 27 | this.extending = extending;
|
| 28 | if (extending) {
|
| 29 | this.pushDirection = direction;
|
| 30 | this.startPos = pistonPos.relative(direction);
|
| 31 | } else {
|
| 32 | this.pushDirection = direction.getOpposite();
|
| 33 | this.startPos = pistonPos.relative(direction, 2);
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | public boolean resolve() {
|
| 38 | this.toPush.clear();
|
| 39 | this.toDestroy.clear();
|
| 40 | BlockState nextState = this.level.getBlockState(this.startPos);
|
| 41 | if (!PistonBaseBlock.isPushable(nextState, this.level, this.startPos, this.pushDirection, false, this.pistonDirection)) {
|
| 42 | if (this.extending && nextState.getPistonPushReaction() == PushReaction.POPPED) {
|
| 43 | this.toDestroy.add(this.startPos);
|
| 44 | return true;
|
| 45 | } else {
|
| 46 | return false;
|
| 47 | }
|
| 48 | } else {
|
| 49 | if (!this.addBlockLine(this.startPos, this.pushDirection)) {
|
| 50 | return false;
|
| 51 | }
|
| 52 |
|
| 53 | for (int i = 0; i < this.toPush.size(); i++) {
|
| 54 | BlockPos pos = this.toPush.get(i);
|
| 55 | if (isSticky(this.level.getBlockState(pos)) && !this.addBranchingBlocks(pos)) {
|
| 56 | return false;
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | return true;
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | private static boolean isSticky(final BlockState state) {
|
| 65 | return state.is(Blocks.SLIME_BLOCK) || state.is(Blocks.HONEY_BLOCK);
|
| 66 | }
|
| 67 |
|
| 68 | private static boolean canStickToEachOther(final BlockState state1, final BlockState state2) {
|
| 69 | if (state1.is(Blocks.HONEY_BLOCK) && state2.is(Blocks.SLIME_BLOCK)) {
|
| 70 | return false;
|
| 71 | } else {
|
| 72 | return state1.is(Blocks.SLIME_BLOCK) && state2.is(Blocks.HONEY_BLOCK) ? false : isSticky(state1) || isSticky(state2);
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | private boolean addBlockLine(final BlockPos start, final Direction direction) {
|
| 77 | BlockState nextState = this.level.getBlockState(start);
|
| 78 | if (nextState.isAir()) {
|
| 79 | return true;
|
| 80 | }
|
| 81 |
|
| 82 | if (!PistonBaseBlock.isPushable(nextState, this.level, start, this.pushDirection, false, direction)) {
|
| 83 | return true;
|
| 84 | }
|
| 85 |
|
| 86 | if (start.equals(this.pistonPos)) {
|
| 87 | return true;
|
| 88 | }
|
| 89 |
|
| 90 | if (this.toPush.contains(start)) {
|
| 91 | return true;
|
| 92 | }
|
| 93 |
|
| 94 | int blockCount = 1;
|
| 95 | if (blockCount + this.toPush.size() > 12) {
|
| 96 | return false;
|
| 97 | }
|
| 98 |
|
| 99 | while (isSticky(nextState)) {
|
| 100 | BlockPos pos = start.relative(this.pushDirection.getOpposite(), blockCount);
|
| 101 | BlockState previousState = nextState;
|
| 102 | nextState = this.level.getBlockState(pos);
|
| 103 | if (nextState.isAir()
|
| 104 | || !canStickToEachOther(previousState, nextState)
|
| 105 | || !PistonBaseBlock.isPushable(nextState, this.level, pos, this.pushDirection, false, this.pushDirection.getOpposite())
|
| 106 | || pos.equals(this.pistonPos)) {
|
| 107 | break;
|
| 108 | }
|
| 109 |
|
| 110 | if (++blockCount + this.toPush.size() > 12) {
|
| 111 | return false;
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | int blocksAdded = 0;
|
| 116 |
|
| 117 | for (int i = blockCount - 1; i >= 0; i--) {
|
| 118 | this.toPush.add(start.relative(this.pushDirection.getOpposite(), i));
|
| 119 | blocksAdded++;
|
| 120 | }
|
| 121 |
|
| 122 | int i = 1;
|
| 123 |
|
| 124 | while (true) {
|
| 125 | BlockPos pos = start.relative(this.pushDirection, i);
|
| 126 | int collisionPos = this.toPush.indexOf(pos);
|
| 127 | if (collisionPos > -1) {
|
| 128 | this.reorderListAtCollision(blocksAdded, collisionPos);
|
| 129 |
|
| 130 | for (int j = 0; j <= collisionPos + blocksAdded; j++) {
|
| 131 | BlockPos blockPos = this.toPush.get(j);
|
| 132 | if (isSticky(this.level.getBlockState(blockPos)) && !this.addBranchingBlocks(blockPos)) {
|
| 133 | return false;
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | return true;
|
| 138 | }
|
| 139 |
|
| 140 | nextState = this.level.getBlockState(pos);
|
| 141 | if (nextState.isAir()) {
|
| 142 | return true;
|
| 143 | }
|
| 144 |
|
| 145 | if (!PistonBaseBlock.isPushable(nextState, this.level, pos, this.pushDirection, true, this.pushDirection) || pos.equals(this.pistonPos)) {
|
| 146 | return false;
|
| 147 | }
|
| 148 |
|
| 149 | if (nextState.getPistonPushReaction() == PushReaction.POPPED) {
|
| 150 | this.toDestroy.add(pos);
|
| 151 | return true;
|
| 152 | }
|
| 153 |
|
| 154 | if (this.toPush.size() >= 12) {
|
| 155 | return false;
|
| 156 | }
|
| 157 |
|
| 158 | this.toPush.add(pos);
|
| 159 | blocksAdded++;
|
| 160 | i++;
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | private void reorderListAtCollision(final int blocksAdded, final int collisionPos) {
|
| 165 | List<BlockPos> head = Lists.newArrayList();
|
| 166 | List<BlockPos> lastLineAdded = Lists.newArrayList();
|
| 167 | List<BlockPos> collisionToLine = Lists.newArrayList();
|
| 168 | head.addAll(this.toPush.subList(0, collisionPos));
|
| 169 | lastLineAdded.addAll(this.toPush.subList(this.toPush.size() - blocksAdded, this.toPush.size()));
|
| 170 | collisionToLine.addAll(this.toPush.subList(collisionPos, this.toPush.size() - blocksAdded));
|
| 171 | this.toPush.clear();
|
| 172 | this.toPush.addAll(head);
|
| 173 | this.toPush.addAll(lastLineAdded);
|
| 174 | this.toPush.addAll(collisionToLine);
|
| 175 | }
|
| 176 |
|
| 177 | private boolean addBranchingBlocks(final BlockPos fromPos) {
|
| 178 | BlockState fromState = this.level.getBlockState(fromPos);
|
| 179 |
|
| 180 | for (Direction direction : Direction.values()) {
|
| 181 | if (direction.getAxis() != this.pushDirection.getAxis()) {
|
| 182 | BlockPos neighbourPos = fromPos.relative(direction);
|
| 183 | BlockState neighbourState = this.level.getBlockState(neighbourPos);
|
| 184 | if (canStickToEachOther(neighbourState, fromState) && !this.addBlockLine(neighbourPos, direction)) {
|
| 185 | return false;
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | return true;
|
| 191 | }
|
| 192 |
|
| 193 | public Direction getPushDirection() {
|
| 194 | return this.pushDirection;
|
| 195 | }
|
| 196 |
|
| 197 | public List<BlockPos> getToPush() {
|
| 198 | return this.toPush;
|
| 199 | }
|
| 200 |
|
| 201 | public List<BlockPos> getToDestroy() {
|
| 202 | return this.toDestroy;
|
| 203 | }
|
| 204 | }
|
| 205 |
|