diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/GeneratingKineticTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/base/GeneratingKineticTileEntity.java index 279f177cc..fa0d6f017 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/GeneratingKineticTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/GeneratingKineticTileEntity.java @@ -38,7 +38,7 @@ public abstract class GeneratingKineticTileEntity extends KineticTileEntity { if (!(tileEntity instanceof KineticTileEntity)) return; KineticTileEntity sourceTe = (KineticTileEntity) tileEntity; - if (reActivateSource && sourceTe != null && Math.abs(sourceTe.getSpeed()) >= Math.abs(getGeneratedSpeed())) + if (reActivateSource && Math.abs(sourceTe.getSpeed()) >= Math.abs(getGeneratedSpeed())) reActivateSource = false; } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/BlockMovementTraits.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/BlockMovementTraits.java index ad25a1b97..aaaef728b 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/BlockMovementTraits.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/BlockMovementTraits.java @@ -53,8 +53,7 @@ import net.minecraft.world.World; public class BlockMovementTraits { - public static boolean movementNecessary(World world, BlockPos pos) { - BlockState state = world.getBlockState(pos); + public static boolean movementNecessary(BlockState state, World world, BlockPos pos) { if (isBrittle(state)) return true; if (state.getBlock() instanceof FenceGateBlock) @@ -68,18 +67,17 @@ public class BlockMovementTraits { return true; } - public static boolean movementAllowed(World world, BlockPos pos) { - BlockState blockState = world.getBlockState(pos); - Block block = blockState.getBlock(); + public static boolean movementAllowed(BlockState state, World world, BlockPos pos) { + Block block = state.getBlock(); if (block instanceof AbstractChassisBlock) return true; - if (blockState.getBlockHardness(world, pos) == -1) + if (state.getBlockHardness(world, pos) == -1) return false; - if (AllBlockTags.NON_MOVABLE.matches(blockState)) + if (AllBlockTags.NON_MOVABLE.matches(state)) return false; // Move controllers only when they aren't moving - if (block instanceof MechanicalPistonBlock && blockState.get(MechanicalPistonBlock.STATE) != PistonState.MOVING) + if (block instanceof MechanicalPistonBlock && state.get(MechanicalPistonBlock.STATE) != PistonState.MOVING) return true; if (block instanceof MechanicalBearingBlock) { TileEntity te = world.getTileEntity(pos); @@ -97,11 +95,11 @@ public class BlockMovementTraits { return !((PulleyTileEntity) te).running; } - if (AllBlocks.BELT.has(blockState)) + if (AllBlocks.BELT.has(state)) return true; - if (blockState.getBlock() instanceof GrindstoneBlock) + if (state.getBlock() instanceof GrindstoneBlock) return true; - return blockState.getPushReaction() != PushReaction.BLOCK; + return state.getPushReaction() != PushReaction.BLOCK; } /** diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java index bb4dadb7f..8ed8f16fe 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java @@ -8,9 +8,11 @@ import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Queue; import java.util.Set; import java.util.UUID; import java.util.function.BiConsumer; @@ -148,7 +150,7 @@ public abstract class Contraption { } protected boolean addToInitialFrontier(World world, BlockPos pos, Direction forcedDirection, - List frontier) { + Queue frontier) { return true; } @@ -161,7 +163,7 @@ public abstract class Contraption { public boolean searchMovedStructure(World world, BlockPos pos, @Nullable Direction forcedDirection) { initialPassengers.clear(); - List frontier = new ArrayList<>(); + Queue frontier = new LinkedList<>(); Set visited = new HashSet<>(); anchor = pos; @@ -175,7 +177,7 @@ public abstract class Contraption { for (int limit = 100000; limit > 0; limit--) { if (frontier.isEmpty()) return true; - if (!moveBlock(world, frontier.remove(0), forcedDirection, frontier, visited)) + if (!moveBlock(world, forcedDirection, frontier, visited)) return false; } return false; @@ -241,20 +243,22 @@ public abstract class Contraption { fluidStorage.forEach((pos, mfs) -> mfs.tick(entity, pos, world.isRemote)); } - protected boolean moveBlock(World world, BlockPos pos, @Nullable Direction forcedDirection, List frontier, + protected boolean moveBlock(World world, @Nullable Direction forcedDirection, Queue frontier, Set visited) { + BlockPos pos = frontier.poll(); + if (pos == null) + return false; visited.add(pos); - frontier.remove(pos); if (!world.isBlockPresent(pos)) return false; if (isAnchoringBlockAt(pos)) return true; - if (!BlockMovementTraits.movementNecessary(world, pos)) - return true; - if (!movementAllowed(world, pos)) - return false; BlockState state = world.getBlockState(pos); + if (!BlockMovementTraits.movementNecessary(state, world, pos)) + return true; + if (!movementAllowed(state, world, pos)) + return false; if (state.getBlock() instanceof AbstractChassisBlock && !moveChassis(world, pos, forcedDirection, frontier, visited)) return false; @@ -290,9 +294,10 @@ public abstract class Contraption { } // Cart assemblers attach themselves - BlockState stateBelow = world.getBlockState(pos.down()); - if (!visited.contains(pos.down()) && AllBlocks.CART_ASSEMBLER.has(stateBelow)) - frontier.add(pos.down()); + BlockPos posDown = pos.down(); + BlockState stateBelow = world.getBlockState(posDown); + if (!visited.contains(posDown) && AllBlocks.CART_ASSEMBLER.has(stateBelow)) + frontier.add(posDown); Map superglue = SuperGlueHandler.gatherGlue(world, pos); @@ -302,7 +307,7 @@ public abstract class Contraption { BlockState blockState = world.getBlockState(offsetPos); if (isAnchoringBlockAt(offsetPos)) continue; - if (!movementAllowed(world, offsetPos)) { + if (!movementAllowed(blockState, world, offsetPos)) { if (offset == forcedDirection) return false; continue; @@ -336,7 +341,7 @@ public abstract class Contraption { return blocks.size() <= AllConfigs.SERVER.kinetics.maxBlocksMoved.get(); } - private void moveBearing(BlockPos pos, List frontier, Set visited, BlockState state) { + private void moveBearing(BlockPos pos, Queue frontier, Set visited, BlockState state) { Direction facing = state.get(MechanicalBearingBlock.FACING); if (!canAxisBeStabilized(facing.getAxis())) { BlockPos offset = pos.offset(facing); @@ -347,7 +352,7 @@ public abstract class Contraption { pendingSubContraptions.add(new BlockFace(pos, facing)); } - private void moveBelt(BlockPos pos, List frontier, Set visited, BlockState state) { + private void moveBelt(BlockPos pos, Queue frontier, Set visited, BlockState state) { BlockPos nextPos = BeltBlock.nextSegmentPosition(state, pos, true); BlockPos prevPos = BeltBlock.nextSegmentPosition(state, pos, false); if (nextPos != null && !visited.contains(nextPos)) @@ -368,7 +373,7 @@ public abstract class Contraption { } } - private void movePulley(World world, BlockPos pos, List frontier, Set visited) { + private void movePulley(World world, BlockPos pos, Queue frontier, Set visited) { int limit = AllConfigs.SERVER.kinetics.maxRopeLength.get(); BlockPos ropePos = pos; while (limit-- >= 0) { @@ -386,7 +391,7 @@ public abstract class Contraption { } } - private boolean moveMechanicalPiston(World world, BlockPos pos, List frontier, Set visited, + private boolean moveMechanicalPiston(World world, BlockPos pos, Queue frontier, Set visited, BlockState state) { int limit = AllConfigs.SERVER.kinetics.maxPistonPoles.get(); Direction direction = state.get(MechanicalPistonBlock.FACING); @@ -432,7 +437,7 @@ public abstract class Contraption { return true; } - private boolean moveChassis(World world, BlockPos pos, Direction movementDirection, List frontier, + private boolean moveChassis(World world, BlockPos pos, Direction movementDirection, Queue frontier, Set visited) { TileEntity te = world.getTileEntity(pos); if (!(te instanceof ChassisTileEntity)) @@ -517,8 +522,8 @@ public abstract class Contraption { return globalPos.subtract(anchor); } - protected boolean movementAllowed(World world, BlockPos pos) { - return BlockMovementTraits.movementAllowed(world, pos); + protected boolean movementAllowed(BlockState state, World world, BlockPos pos) { + return BlockMovementTraits.movementAllowed(state, world, pos); } protected boolean isAnchoringBlockAt(BlockPos pos) { diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/ClockworkContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/ClockworkContraption.java index 31b7709f3..785a4c2ad 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/ClockworkContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/ClockworkContraption.java @@ -1,7 +1,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.bearing; import java.util.HashSet; -import java.util.List; +import java.util.Queue; import java.util.Set; import org.apache.commons.lang3.tuple.Pair; @@ -92,11 +92,13 @@ public class ClockworkContraption extends Contraption { } @Override - protected boolean moveBlock(World world, BlockPos pos, Direction direction, List frontier, + protected boolean moveBlock(World world, Direction direction, Queue frontier, Set visited) { - if (ignoreBlocks.contains(pos)) + if (ignoreBlocks.contains(frontier.peek())) { + frontier.poll(); return true; - return super.moveBlock(world, pos, direction, frontier, visited); + } + return super.moveBlock(world, direction, frontier, visited); } @Override diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/ChassisTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/ChassisTileEntity.java index 832b656da..fcb4f1c1b 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/ChassisTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/ChassisTileEntity.java @@ -7,6 +7,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Queue; import java.util.Set; import com.simibubi.create.AllBlocks; @@ -76,12 +77,12 @@ public class ChassisTileEntity extends SmartTileEntity { } public List collectChassisGroup() { - List frontier = new ArrayList<>(); + Queue frontier = new LinkedList<>(); List collected = new ArrayList<>(); Set visited = new HashSet<>(); frontier.add(pos); while (!frontier.isEmpty()) { - BlockPos current = frontier.remove(0); + BlockPos current = frontier.poll(); if (visited.contains(current)) continue; visited.add(current); @@ -96,7 +97,7 @@ public class ChassisTileEntity extends SmartTileEntity { return collected; } - public boolean addAttachedChasses(List frontier, Set visited) { + public boolean addAttachedChasses(Queue frontier, Set visited) { BlockState state = getBlockState(); if (!(state.getBlock() instanceof AbstractChassisBlock)) return false; @@ -166,7 +167,7 @@ public class ChassisTileEntity extends SmartTileEntity { break; // Ignore replaceable Blocks and Air-like - if (!BlockMovementTraits.movementNecessary(world, current)) + if (!BlockMovementTraits.movementNecessary(currentState, world, current)) break; if (BlockMovementTraits.isBrittle(currentState)) break; @@ -207,7 +208,7 @@ public class ChassisTileEntity extends SmartTileEntity { continue; if (!searchPos.withinDistance(pos, chassisRange + .5f)) continue; - if (!BlockMovementTraits.movementNecessary(world, searchPos)) + if (!BlockMovementTraits.movementNecessary(searchedState, world, searchPos)) continue; if (BlockMovementTraits.isBrittle(searchedState)) continue; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/SuperGlueEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/SuperGlueEntity.java index f677487af..eba72ea54 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/SuperGlueEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/SuperGlueEntity.java @@ -180,7 +180,7 @@ public class SuperGlueEntity extends Entity implements IEntityAdditionalSpawnDat BlockState state = world.getBlockState(pos); if (BlockMovementTraits.isBlockAttachedTowards(world, pos, state, direction)) return true; - if (!BlockMovementTraits.movementNecessary(world, pos)) + if (!BlockMovementTraits.movementNecessary(state, world, pos)) return false; if (BlockMovementTraits.notSupportive(state, direction)) return false; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java index 3d6a28dd8..b8b219a5b 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MountedContraption.java @@ -2,7 +2,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.mo import static com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerBlock.RAIL_SHAPE; -import java.util.List; +import java.util.Queue; import org.apache.commons.lang3.tuple.Pair; @@ -70,7 +70,7 @@ public class MountedContraption extends Contraption { } @Override - protected boolean addToInitialFrontier(World world, BlockPos pos, Direction direction, List frontier) { + protected boolean addToInitialFrontier(World world, BlockPos pos, Direction direction, Queue frontier) { frontier.clear(); frontier.add(pos.up()); return true; @@ -104,11 +104,10 @@ public class MountedContraption extends Contraption { } @Override - protected boolean movementAllowed(World world, BlockPos pos) { - BlockState blockState = world.getBlockState(pos); - if (!pos.equals(anchor) && AllBlocks.CART_ASSEMBLER.has(blockState)) - return testSecondaryCartAssembler(world, blockState, pos); - return super.movementAllowed(world, pos); + protected boolean movementAllowed(BlockState state, World world, BlockPos pos) { + if (!pos.equals(anchor) && AllBlocks.CART_ASSEMBLER.has(state)) + return testSecondaryCartAssembler(world, state, pos); + return super.movementAllowed(state, world, pos); } protected boolean testSecondaryCartAssembler(World world, BlockState state, BlockPos pos) { diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/PistonContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/PistonContraption.java index 1e101fd94..0eada3483 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/PistonContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/PistonContraption.java @@ -23,6 +23,7 @@ import org.apache.commons.lang3.tuple.Pair; import java.util.ArrayList; import java.util.List; +import java.util.Queue; import static com.simibubi.create.AllBlocks.MECHANICAL_PISTON_HEAD; import static com.simibubi.create.AllBlocks.PISTON_EXTENSION_POLE; @@ -144,7 +145,7 @@ public class PistonContraption extends TranslatingContraption { } @Override - protected boolean addToInitialFrontier(World world, BlockPos pos, Direction direction, List frontier) { + protected boolean addToInitialFrontier(World world, BlockPos pos, Direction direction, Queue frontier) { frontier.clear(); boolean sticky = isStickyPiston(world.getBlockState(pos.offset(orientation, -1))); boolean retracting = direction != orientation; @@ -156,14 +157,14 @@ public class PistonContraption extends TranslatingContraption { BlockPos currentPos = pos.offset(orientation, offset + initialExtensionProgress); if (!world.isBlockPresent(currentPos)) return false; - if (!BlockMovementTraits.movementNecessary(world, currentPos)) - return true; BlockState state = world.getBlockState(currentPos); + if (!BlockMovementTraits.movementNecessary(state, world, currentPos)) + return true; if (BlockMovementTraits.isBrittle(state) && !(state.getBlock() instanceof CarpetBlock)) return true; if (isPistonHead(state) && state.get(FACING) == direction.getOpposite()) return true; - if (!BlockMovementTraits.movementAllowed(world, currentPos)) + if (!BlockMovementTraits.movementAllowed(state, world, currentPos)) return retracting; if (retracting && state.getPushReaction() == PushReaction.PUSH_ONLY) return true; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/PulleyTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/PulleyTileEntity.java index e2d4b7c0d..54cee9d6a 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/PulleyTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/PulleyTileEntity.java @@ -176,9 +176,10 @@ public class PulleyTileEntity extends LinearActuatorTileEntity { return; BlockPos posBelow = pos.down((int) (offset + getMovementSpeed()) + 1); - if (!BlockMovementTraits.movementNecessary(world, posBelow)) + BlockState state = world.getBlockState(posBelow); + if (!BlockMovementTraits.movementNecessary(state, world, posBelow)) return; - if (BlockMovementTraits.isBrittle(world.getBlockState(posBelow))) + if (BlockMovementTraits.isBrittle(state)) return; disassemble();