| 1 | package net.minecraft.world.level.block.piston;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.HolderLookup;
|
| 7 | import net.minecraft.core.registries.Registries;
|
| 8 | import net.minecraft.nbt.CompoundTag;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.entity.MoverType;
|
| 12 | import net.minecraft.world.level.BlockGetter;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.block.Block;
|
| 15 | import net.minecraft.world.level.block.Blocks;
|
| 16 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 17 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 18 | import net.minecraft.world.level.block.state.BlockState;
|
| 19 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 20 | import net.minecraft.world.level.block.state.properties.PistonType;
|
| 21 | import net.minecraft.world.level.material.PushReaction;
|
| 22 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 23 | import net.minecraft.world.level.storage.ValueInput;
|
| 24 | import net.minecraft.world.level.storage.ValueOutput;
|
| 25 | import net.minecraft.world.phys.AABB;
|
| 26 | import net.minecraft.world.phys.Vec3;
|
| 27 | import net.minecraft.world.phys.shapes.Shapes;
|
| 28 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 29 |
|
| 30 | public class PistonMovingBlockEntity extends BlockEntity {
|
| 31 | private static final int TICKS_TO_EXTEND = 2;
|
| 32 | private static final double PUSH_OFFSET = 0.01;
|
| 33 | public static final double TICK_MOVEMENT = 0.51;
|
| 34 | private static final BlockState DEFAULT_BLOCK_STATE = Blocks.AIR.defaultBlockState();
|
| 35 | private static final float DEFAULT_PROGRESS = 0.0F;
|
| 36 | private static final boolean DEFAULT_EXTENDING = false;
|
| 37 | private static final boolean DEFAULT_SOURCE = false;
|
| 38 | private BlockState movedState = DEFAULT_BLOCK_STATE;
|
| 39 | private Direction direction;
|
| 40 | private boolean extending = false;
|
| 41 | private boolean isSourcePiston = false;
|
| 42 | private static final ThreadLocal<Direction> NOCLIP = ThreadLocal.withInitial(() -> null);
|
| 43 | private float progress = 0.0F;
|
| 44 | private float progressO = 0.0F;
|
| 45 | private long lastTicked;
|
| 46 | private int deathTicks;
|
| 47 |
|
| 48 | public PistonMovingBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 49 | super(BlockEntityTypes.PISTON, worldPosition, blockState);
|
| 50 | }
|
| 51 |
|
| 52 | public PistonMovingBlockEntity(
|
| 53 | final BlockPos worldPosition,
|
| 54 | final BlockState blockState,
|
| 55 | final BlockState movedState,
|
| 56 | final Direction direction,
|
| 57 | final boolean extending,
|
| 58 | final boolean isSourcePiston
|
| 59 | ) {
|
| 60 | this(worldPosition, blockState);
|
| 61 | this.movedState = movedState;
|
| 62 | this.direction = direction;
|
| 63 | this.extending = extending;
|
| 64 | this.isSourcePiston = isSourcePiston;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 69 | return this.saveCustomOnly(registries);
|
| 70 | }
|
| 71 |
|
| 72 | public boolean isExtending() {
|
| 73 | return this.extending;
|
| 74 | }
|
| 75 |
|
| 76 | public Direction getDirection() {
|
| 77 | return this.direction;
|
| 78 | }
|
| 79 |
|
| 80 | public boolean isSourcePiston() {
|
| 81 | return this.isSourcePiston;
|
| 82 | }
|
| 83 |
|
| 84 | public float getProgress(float a) {
|
| 85 | if (a > 1.0F) {
|
| 86 | a = 1.0F;
|
| 87 | }
|
| 88 |
|
| 89 | return Mth.lerp(a, this.progressO, this.progress);
|
| 90 | }
|
| 91 |
|
| 92 | public float getXOff(final float a) {
|
| 93 | return this.direction.getStepX() * this.getExtendedProgress(this.getProgress(a));
|
| 94 | }
|
| 95 |
|
| 96 | public float getYOff(final float a) {
|
| 97 | return this.direction.getStepY() * this.getExtendedProgress(this.getProgress(a));
|
| 98 | }
|
| 99 |
|
| 100 | public float getZOff(final float a) {
|
| 101 | return this.direction.getStepZ() * this.getExtendedProgress(this.getProgress(a));
|
| 102 | }
|
| 103 |
|
| 104 | private float getExtendedProgress(final float progress) {
|
| 105 | return this.extending ? progress - 1.0F : 1.0F - progress;
|
| 106 | }
|
| 107 |
|
| 108 | private BlockState getCollisionRelatedBlockState() {
|
| 109 | return !this.isExtending() && this.isSourcePiston() && this.movedState.getBlock() instanceof PistonBaseBlock
|
| 110 | ? Blocks.PISTON_HEAD
|
| 111 | .defaultBlockState()
|
| 112 | .setValue(PistonHeadBlock.SHORT, this.progress > 0.25F)
|
| 113 | .setValue(PistonHeadBlock.TYPE, this.movedState.is(Blocks.STICKY_PISTON) ? PistonType.STICKY : PistonType.DEFAULT)
|
| 114 | .setValue(PistonHeadBlock.FACING, this.movedState.getValue(PistonBaseBlock.FACING))
|
| 115 | : this.movedState;
|
| 116 | }
|
| 117 |
|
| 118 | private static void moveCollidedEntities(final Level level, final BlockPos pos, final float newProgress, final PistonMovingBlockEntity self) {
|
| 119 | Direction movement = self.getMovementDirection();
|
| 120 | double deltaProgress = newProgress - self.progress;
|
| 121 | VoxelShape shape = self.getCollisionRelatedBlockState().getCollisionShape(level, pos);
|
| 122 | if (!shape.isEmpty()) {
|
| 123 | AABB aabb = moveByPositionAndProgress(pos, shape.bounds(), self);
|
| 124 | List<Entity> entities = level.getEntities(null, PistonMath.getMovementArea(aabb, movement, deltaProgress).minmax(aabb));
|
| 125 | if (!entities.isEmpty()) {
|
| 126 | List<AABB> shapeAabbs = shape.toAabbs();
|
| 127 | boolean causeBounce = self.movedState.is(Blocks.SLIME_BLOCK);
|
| 128 |
|
| 129 | for (Entity entity : entities) {
|
| 130 | if (entity.getPistonPushReaction() != PushReaction.IGNORE_ENTITY) {
|
| 131 | if (causeBounce && entity.canSimulateMovement()) {
|
| 132 | Vec3 deltaMovement = entity.getDeltaMovement();
|
| 133 | double dx = deltaMovement.x;
|
| 134 | double dy = deltaMovement.y;
|
| 135 | double dz = deltaMovement.z;
|
| 136 | switch (movement.getAxis()) {
|
| 137 | case X:
|
| 138 | dx = movement.getStepX();
|
| 139 | break;
|
| 140 | case Y:
|
| 141 | dy = movement.getStepY();
|
| 142 | break;
|
| 143 | case Z:
|
| 144 | dz = movement.getStepZ();
|
| 145 | }
|
| 146 |
|
| 147 | entity.setDeltaMovement(dx, dy, dz);
|
| 148 | }
|
| 149 |
|
| 150 | double delta = 0.0;
|
| 151 |
|
| 152 | for (AABB shapeAabb : shapeAabbs) {
|
| 153 | AABB movingAABB = PistonMath.getMovementArea(moveByPositionAndProgress(pos, shapeAabb, self), movement, deltaProgress);
|
| 154 | AABB entityAabb = entity.getBoundingBox();
|
| 155 | if (movingAABB.intersects(entityAabb)) {
|
| 156 | delta = Math.max(delta, getMovement(movingAABB, movement, entityAabb));
|
| 157 | if (delta >= deltaProgress) {
|
| 158 | break;
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | if (!(delta <= 0.0)) {
|
| 164 | delta = Math.min(delta, deltaProgress) + 0.01;
|
| 165 | moveEntityByPiston(movement, entity, delta, movement);
|
| 166 | if (!self.extending && self.isSourcePiston) {
|
| 167 | fixEntityWithinPistonBase(pos, entity, movement, deltaProgress);
|
| 168 | }
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | private static void moveEntityByPiston(final Direction pistonDirection, final Entity entity, final double delta, final Direction movement) {
|
| 177 | NOCLIP.set(pistonDirection);
|
| 178 | Vec3 previousPos = entity.position();
|
| 179 | entity.move(MoverType.PISTON, new Vec3(delta * movement.getStepX(), delta * movement.getStepY(), delta * movement.getStepZ()));
|
| 180 | entity.applyEffectsFromBlocks(previousPos, entity.position());
|
| 181 | entity.removeLatestMovementRecording();
|
| 182 | NOCLIP.set(null);
|
| 183 | }
|
| 184 |
|
| 185 | private static void moveStuckEntities(final Level level, final BlockPos pos, final float newProgress, final PistonMovingBlockEntity self) {
|
| 186 | if (self.isStickyForEntities()) {
|
| 187 | Direction movement = self.getMovementDirection();
|
| 188 | if (movement.getAxis().isHorizontal()) {
|
| 189 | double stickyTop = self.movedState.getCollisionShape(level, pos).max(Direction.Axis.Y);
|
| 190 | AABB aabb = moveByPositionAndProgress(pos, new AABB(0.0, stickyTop, 0.0, 1.0, 1.5000010000000001, 1.0), self);
|
| 191 | double deltaProgress = newProgress - self.progress;
|
| 192 |
|
| 193 | for (Entity entity : level.getEntities((Entity)null, aabb, entityx -> matchesStickyCritera(aabb, entityx, pos))) {
|
| 194 | moveEntityByPiston(movement, entity, deltaProgress, movement);
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | private static boolean matchesStickyCritera(final AABB aabb, final Entity entity, final BlockPos pos) {
|
| 201 | return entity.getPistonPushReaction() == PushReaction.PUSH_PULL
|
| 202 | && entity.onGround()
|
| 203 | && (entity.isSupportedBy(pos) || entity.getX() >= aabb.minX && entity.getX() <= aabb.maxX && entity.getZ() >= aabb.minZ && entity.getZ() <= aabb.maxZ);
|
| 204 | }
|
| 205 |
|
| 206 | private boolean isStickyForEntities() {
|
| 207 | return this.movedState.is(Blocks.HONEY_BLOCK);
|
| 208 | }
|
| 209 |
|
| 210 | public Direction getMovementDirection() {
|
| 211 | return this.extending ? this.direction : this.direction.getOpposite();
|
| 212 | }
|
| 213 |
|
| 214 | private static double getMovement(final AABB aabbToBeOutsideOf, final Direction movement, final AABB aabb) {
|
| 215 | switch (movement) {
|
| 216 | case EAST:
|
| 217 | return aabbToBeOutsideOf.maxX - aabb.minX;
|
| 218 | case WEST:
|
| 219 | return aabb.maxX - aabbToBeOutsideOf.minX;
|
| 220 | case UP:
|
| 221 | default:
|
| 222 | return aabbToBeOutsideOf.maxY - aabb.minY;
|
| 223 | case DOWN:
|
| 224 | return aabb.maxY - aabbToBeOutsideOf.minY;
|
| 225 | case SOUTH:
|
| 226 | return aabbToBeOutsideOf.maxZ - aabb.minZ;
|
| 227 | case NORTH:
|
| 228 | return aabb.maxZ - aabbToBeOutsideOf.minZ;
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | private static AABB moveByPositionAndProgress(final BlockPos pos, final AABB aabb, final PistonMovingBlockEntity entity) {
|
| 233 | double currentPosition = entity.getExtendedProgress(entity.progress);
|
| 234 | return aabb.move(
|
| 235 | pos.getX() + currentPosition * entity.direction.getStepX(),
|
| 236 | pos.getY() + currentPosition * entity.direction.getStepY(),
|
| 237 | pos.getZ() + currentPosition * entity.direction.getStepZ()
|
| 238 | );
|
| 239 | }
|
| 240 |
|
| 241 | private static void fixEntityWithinPistonBase(final BlockPos pos, final Entity entity, final Direction direction, final double deltaProgress) {
|
| 242 | AABB entityAabb = entity.getBoundingBox();
|
| 243 | AABB box = Shapes.block().bounds().move(pos);
|
| 244 | if (entityAabb.intersects(box)) {
|
| 245 | Direction opposite = direction.getOpposite();
|
| 246 | double delta = getMovement(box, opposite, entityAabb) + 0.01;
|
| 247 | double deltaIntersected = getMovement(box, opposite, entityAabb.intersect(box)) + 0.01;
|
| 248 | if (Math.abs(delta - deltaIntersected) < 0.01) {
|
| 249 | delta = Math.min(delta, deltaProgress) + 0.01;
|
| 250 | moveEntityByPiston(direction, entity, delta, opposite);
|
| 251 | }
|
| 252 | }
|
| 253 | }
|
| 254 |
|
| 255 | public BlockState getMovedState() {
|
| 256 | return this.movedState;
|
| 257 | }
|
| 258 |
|
| 259 | public void finalTick() {
|
| 260 | if (this.level != null && (this.progressO < 1.0F || this.level.isClientSide())) {
|
| 261 | this.progress = 1.0F;
|
| 262 | this.progressO = this.progress;
|
| 263 | this.level.removeBlockEntity(this.worldPosition);
|
| 264 | this.setRemoved();
|
| 265 | if (this.level.getBlockState(this.worldPosition).is(Blocks.MOVING_PISTON)) {
|
| 266 | BlockState newState;
|
| 267 | if (this.isSourcePiston) {
|
| 268 | newState = Blocks.AIR.defaultBlockState();
|
| 269 | } else {
|
| 270 | newState = Block.updateFromNeighbourShapes(this.movedState, this.level, this.worldPosition);
|
| 271 | }
|
| 272 |
|
| 273 | this.level.setBlockAndUpdate(this.worldPosition, newState);
|
| 274 | this.level
|
| 275 | .neighborChanged(this.worldPosition, newState.getBlock(), ExperimentalRedstoneUtils.initialOrientation(this.level, this.getPushDirection(), null));
|
| 276 | }
|
| 277 | }
|
| 278 | }
|
| 279 |
|
| 280 | @Override
|
| 281 | public void preRemoveSideEffects(final BlockPos pos, final BlockState state) {
|
| 282 | this.finalTick();
|
| 283 | }
|
| 284 |
|
| 285 | public Direction getPushDirection() {
|
| 286 | return this.extending ? this.direction : this.direction.getOpposite();
|
| 287 | }
|
| 288 |
|
| 289 | public static void tick(final Level level, final BlockPos pos, final BlockState state, final PistonMovingBlockEntity entity) {
|
| 290 | entity.lastTicked = level.getGameTime();
|
| 291 | entity.progressO = entity.progress;
|
| 292 | if (entity.progressO >= 1.0F) {
|
| 293 | if (level.isClientSide() && entity.deathTicks < 5) {
|
| 294 | entity.deathTicks++;
|
| 295 | } else {
|
| 296 | level.removeBlockEntity(pos);
|
| 297 | entity.setRemoved();
|
| 298 | if (level.getBlockState(pos).is(Blocks.MOVING_PISTON)) {
|
| 299 | BlockState newState = Block.updateFromNeighbourShapes(entity.movedState, level, pos);
|
| 300 | if (newState.isAir()) {
|
| 301 | level.setBlock(pos, entity.movedState, 340);
|
| 302 | Block.updateOrDestroy(entity.movedState, newState, level, pos, 3);
|
| 303 | } else {
|
| 304 | if (newState.hasProperty(BlockStateProperties.WATERLOGGED) && newState.getValue(BlockStateProperties.WATERLOGGED)) {
|
| 305 | newState = newState.setValue(BlockStateProperties.WATERLOGGED, false);
|
| 306 | }
|
| 307 |
|
| 308 | level.setBlock(pos, newState, 67);
|
| 309 | level.neighborChanged(pos, newState.getBlock(), ExperimentalRedstoneUtils.initialOrientation(level, entity.getPushDirection(), null));
|
| 310 | }
|
| 311 | }
|
| 312 | }
|
| 313 | } else {
|
| 314 | float newProgress = entity.progress + 0.5F;
|
| 315 | moveCollidedEntities(level, pos, newProgress, entity);
|
| 316 | moveStuckEntities(level, pos, newProgress, entity);
|
| 317 | entity.progress = newProgress;
|
| 318 | if (entity.progress >= 1.0F) {
|
| 319 | entity.progress = 1.0F;
|
| 320 | }
|
| 321 | }
|
| 322 | }
|
| 323 |
|
| 324 | @Override
|
| 325 | protected void loadAdditional(final ValueInput input) {
|
| 326 | super.loadAdditional(input);
|
| 327 | this.movedState = input.read("blockState", BlockState.CODEC).orElse(DEFAULT_BLOCK_STATE);
|
| 328 | this.direction = input.read("facing", Direction.LEGACY_ID_CODEC).orElse(Direction.DOWN);
|
| 329 | this.progress = input.getFloatOr("progress", 0.0F);
|
| 330 | this.progressO = this.progress;
|
| 331 | this.extending = input.getBooleanOr("extending", false);
|
| 332 | this.isSourcePiston = input.getBooleanOr("source", false);
|
| 333 | }
|
| 334 |
|
| 335 | @Override
|
| 336 | protected void saveAdditional(final ValueOutput output) {
|
| 337 | super.saveAdditional(output);
|
| 338 | output.store("blockState", BlockState.CODEC, this.movedState);
|
| 339 | output.store("facing", Direction.LEGACY_ID_CODEC, this.direction);
|
| 340 | output.putFloat("progress", this.progressO);
|
| 341 | output.putBoolean("extending", this.extending);
|
| 342 | output.putBoolean("source", this.isSourcePiston);
|
| 343 | }
|
| 344 |
|
| 345 | public VoxelShape getCollisionShape(final BlockGetter level, final BlockPos pos) {
|
| 346 | VoxelShape pistonHeadShape;
|
| 347 | if (!this.extending && this.isSourcePiston && this.movedState.getBlock() instanceof PistonBaseBlock) {
|
| 348 | pistonHeadShape = this.movedState.setValue(PistonBaseBlock.EXTENDED, true).getCollisionShape(level, pos);
|
| 349 | } else {
|
| 350 | pistonHeadShape = Shapes.empty();
|
| 351 | }
|
| 352 |
|
| 353 | Direction noClipDirection = NOCLIP.get();
|
| 354 | if (this.progress < 1.0 && noClipDirection == this.getMovementDirection()) {
|
| 355 | return pistonHeadShape;
|
| 356 | }
|
| 357 |
|
| 358 | BlockState blockState;
|
| 359 | if (this.isSourcePiston()) {
|
| 360 | blockState = Blocks.PISTON_HEAD
|
| 361 | .defaultBlockState()
|
| 362 | .setValue(PistonHeadBlock.FACING, this.direction)
|
| 363 | .setValue(PistonHeadBlock.SHORT, this.extending != 1.0F - this.progress < 0.25F);
|
| 364 | } else {
|
| 365 | blockState = this.movedState;
|
| 366 | }
|
| 367 |
|
| 368 | float extendedProgress = this.getExtendedProgress(this.progress);
|
| 369 | double dx = this.direction.getStepX() * extendedProgress;
|
| 370 | double dy = this.direction.getStepY() * extendedProgress;
|
| 371 | double dz = this.direction.getStepZ() * extendedProgress;
|
| 372 | return Shapes.or(pistonHeadShape, blockState.getCollisionShape(level, pos).move(dx, dy, dz));
|
| 373 | }
|
| 374 |
|
| 375 | public long getLastTicked() {
|
| 376 | return this.lastTicked;
|
| 377 | }
|
| 378 |
|
| 379 | @Override
|
| 380 | public void setLevel(final Level level) {
|
| 381 | super.setLevel(level);
|
| 382 | if (level.holderLookup(Registries.BLOCK).get(this.movedState.getBlock().builtInRegistryHolder().key()).isEmpty()) {
|
| 383 | this.movedState = Blocks.AIR.defaultBlockState();
|
| 384 | }
|
| 385 | }
|
| 386 | }
|
| 387 |
|