diff --git a/src/main/java/net/smoofyuniverse/superpiston/api/structure/calculator/DefaultStructureCalculator.java b/src/main/java/net/smoofyuniverse/superpiston/api/structure/calculator/DefaultStructureCalculator.java index 80b83ee..910d703 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/api/structure/calculator/DefaultStructureCalculator.java +++ b/src/main/java/net/smoofyuniverse/superpiston/api/structure/calculator/DefaultStructureCalculator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2022 Hugo Dupanloup (Yeregorix) + * Copyright (c) 2018-2024 Hugo Dupanloup (Yeregorix) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -98,13 +98,17 @@ public PistonStructure calculateStructure() { @SuppressWarnings("ForLoopReplaceableByForEach") protected boolean calculate(Vector3i origin) { - BlockState state = this.world.block(origin); + if (isPositionBlocked(origin)) + return false; + BlockState state = this.world.block(origin); MovementReaction reaction = getReaction(state, origin); + if (reaction == MovementReaction.DESTROY) { this.toDestroy.add(origin); return true; } + if (reaction == MovementReaction.BLOCK) return false; @@ -121,15 +125,15 @@ protected boolean calculate(Vector3i origin) { } protected boolean addBlockLine(Vector3i origin, Direction dir) { - BlockState state = this.world.block(origin); - - if (BlockUtil.isAir(state)) + if (this.toMoveSet.contains(origin)) return true; - if (this.toMoveSet.contains(origin)) + if (this.piston.position().equals(origin) || isPositionBlocked(origin)) return true; - if (this.piston.position().equals(origin)) + BlockState state = this.world.block(origin); + + if (BlockUtil.isAir(state)) return true; MovementReaction reaction = getReaction(state, origin); @@ -148,12 +152,13 @@ protected boolean addBlockLine(Vector3i origin, Direction dir) { Vector3i prevPos = pos; pos = pos.sub(offset); - state = this.world.block(pos); - if (BlockUtil.isAir(state)) + if (this.piston.position().equals(pos) || isPositionBlocked(pos)) break; - if (this.piston.position().equals(pos)) + state = this.world.block(pos); + + if (BlockUtil.isAir(state)) break; if (!canStickToEachOther(prevState, prevPos, state, pos)) @@ -198,7 +203,7 @@ protected boolean addBlockLine(Vector3i origin, Direction dir) { if (BlockUtil.isAir(state)) return true; - if (this.piston.position().equals(pos)) + if (this.piston.position().equals(pos) || isPositionBlocked(pos)) return false; reaction = getReaction(state, pos); @@ -242,6 +247,10 @@ protected void reorderListAtCollision(int start, int pivot) { this.toMove.addAll(list); } + protected boolean isPositionBlocked(Vector3i pos) { + return ReactionUtil.isPositionBlocked(this.world, pos, this.movement); + } + public boolean isSticky(BlockState state, Vector3i pos) { return STICKY_BLOCKS.contains(state.type()); } @@ -255,10 +264,10 @@ public boolean canStickToEachOther(BlockState state1, Vector3i pos1, BlockState } public MovementReaction getReaction(BlockState state, Vector3i pos) { - return ReactionUtil.getDefaultReaction(this.world, state, pos, this.movement); + return ReactionUtil.getDefaultReaction(state); } - public static enum MovementReaction { + public enum MovementReaction { NORMAL, DESTROY, BLOCK, PUSH_ONLY } } diff --git a/src/main/java/net/smoofyuniverse/superpiston/impl/BlockUtil.java b/src/main/java/net/smoofyuniverse/superpiston/impl/BlockUtil.java index d2ad6d8..6c086c3 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/impl/BlockUtil.java +++ b/src/main/java/net/smoofyuniverse/superpiston/impl/BlockUtil.java @@ -23,7 +23,6 @@ package net.smoofyuniverse.superpiston.impl; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.material.Material; public class BlockUtil { @@ -32,6 +31,6 @@ public static boolean hasBlockEntity(org.spongepowered.api.block.BlockState stat } public static boolean isAir(org.spongepowered.api.block.BlockState state) { - return ((BlockState) state).getMaterial() == Material.AIR; + return ((BlockState) state).isAir(); } } diff --git a/src/main/java/net/smoofyuniverse/superpiston/impl/ReactionUtil.java b/src/main/java/net/smoofyuniverse/superpiston/impl/ReactionUtil.java index f8b09a4..6c73c1b 100644 --- a/src/main/java/net/smoofyuniverse/superpiston/impl/ReactionUtil.java +++ b/src/main/java/net/smoofyuniverse/superpiston/impl/ReactionUtil.java @@ -28,7 +28,6 @@ import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.piston.PistonBaseBlock; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.material.PushReaction; import net.smoofyuniverse.superpiston.api.structure.calculator.DefaultStructureCalculator.MovementReaction; import org.spongepowered.api.util.Direction; import org.spongepowered.api.world.server.ServerWorld; @@ -37,20 +36,24 @@ public class ReactionUtil { - public static MovementReaction getDefaultReaction(ServerWorld world, org.spongepowered.api.block.BlockState state, Vector3i pos, Direction movement) { + public static boolean isPositionBlocked(ServerWorld world, Vector3i pos, Direction movement) { BlockPos blockPos = VecHelper.toBlockPos(pos); ServerLevel level = (ServerLevel) world; if (!level.getWorldBorder().isWithinBounds(blockPos)) - return MovementReaction.BLOCK; + return true; if (pos.y() < 0 || (movement == Direction.DOWN && pos.y() == 0)) - return MovementReaction.BLOCK; + return true; int h = level.getMaxBuildHeight() - 1; if (pos.y() > h || (movement == Direction.UP && pos.y() == h)) - return MovementReaction.BLOCK; + return true; + return false; + } + + public static MovementReaction getDefaultReaction(org.spongepowered.api.block.BlockState state) { if (BlockUtil.hasBlockEntity(state)) return MovementReaction.BLOCK; @@ -61,26 +64,15 @@ public static MovementReaction getDefaultReaction(ServerWorld world, org.spongep if (block == Blocks.OBSIDIAN || block == Blocks.CRYING_OBSIDIAN || block == Blocks.RESPAWN_ANCHOR || block == Blocks.REINFORCED_DEEPSLATE - || nmsState.getDestroySpeed(level, blockPos) == -1.0f) + || block.defaultDestroyTime() == -1.0f) return MovementReaction.BLOCK; - return fromNMS(nmsState.getPistonPushReaction()); - } - - public static MovementReaction fromNMS(PushReaction reaction) { - switch (reaction) { - case NORMAL: - return MovementReaction.NORMAL; - case DESTROY: - return MovementReaction.DESTROY; - case BLOCK: - return MovementReaction.BLOCK; - case PUSH_ONLY: - return MovementReaction.PUSH_ONLY; - case IGNORE: - throw new IllegalArgumentException("IGNORE"); - default: - throw new IllegalArgumentException(); - } + return switch (nmsState.getPistonPushReaction()) { + case NORMAL -> MovementReaction.NORMAL; + case DESTROY -> MovementReaction.DESTROY; + case BLOCK -> MovementReaction.BLOCK; + case PUSH_ONLY -> MovementReaction.PUSH_ONLY; + case IGNORE -> throw new IllegalArgumentException("IGNORE"); + }; } }