diff --git a/src/generated/resources/assets/create/lang/en_us.json b/src/generated/resources/assets/create/lang/en_us.json index fb30e864a..b618bbef9 100644 --- a/src/generated/resources/assets/create/lang/en_us.json +++ b/src/generated/resources/assets/create/lang/en_us.json @@ -822,6 +822,12 @@ "create.gui.goggles.kinetic_stats": "Kinetic Stats:", "create.gui.goggles.at_current_speed": "at current speed", "create.gui.goggles.pole_length": "Pole Length:", + "create.gui.assembly.exception": "This Contraption was unable to assemble:", + "create.gui.assembly.exception.unmovableBlock": "Unmovable Block (%4$s) at [%1$s %2$s %3$s]", + "create.gui.assembly.exception.chunkNotLoaded": "The Block at [%1$s %2$s %3$s] was not in a loaded chunk", + "create.gui.assembly.exception.structureTooLarge": "There are too many Blocks included in the contraption.\nThe configured maximum is: %1$s", + "create.gui.assembly.exception.tooManyPistonPoles": "There are too many extension Poles attached to this Piston.\nThe configured maximum is: %1$s", + "create.gui.assembly.exception.noPistonPoles": "The Piston is missing some extension Poles", "create.gui.gauge.info_header": "Gauge Information:", "create.gui.speedometer.title": "Rotation Speed", "create.gui.stressometer.title": "Network Stress", diff --git a/src/generated/resources/assets/create/lang/unfinished/de_de.json b/src/generated/resources/assets/create/lang/unfinished/de_de.json index 10f43ea40..e43663915 100644 --- a/src/generated/resources/assets/create/lang/unfinished/de_de.json +++ b/src/generated/resources/assets/create/lang/unfinished/de_de.json @@ -823,6 +823,12 @@ "create.gui.goggles.kinetic_stats": "UNLOCALIZED: Kinetic Stats:", "create.gui.goggles.at_current_speed": "UNLOCALIZED: at current speed", "create.gui.goggles.pole_length": "UNLOCALIZED: Pole Length:", + "create.gui.assembly.exception": "UNLOCALIZED: This Contraption was unable to assemble:", + "create.gui.assembly.exception.unmovableBlock": "UNLOCALIZED: Unmovable Block (%4$s) at [%1$s %2$s %3$s]", + "create.gui.assembly.exception.chunkNotLoaded": "UNLOCALIZED: The Block at [%1$s %2$s %3$s] was not in a loaded chunk", + "create.gui.assembly.exception.structureTooLarge": "UNLOCALIZED: There are too many Blocks included in the contraption.\nThe configured maximum is: %1$s", + "create.gui.assembly.exception.tooManyPistonPoles": "UNLOCALIZED: There are too many extension Poles attached to this Piston.\nThe configured maximum is: %1$s", + "create.gui.assembly.exception.noPistonPoles": "UNLOCALIZED: The Piston is missing some extension Poles", "create.gui.gauge.info_header": "UNLOCALIZED: Gauge Information:", "create.gui.speedometer.title": "UNLOCALIZED: Rotation Speed", "create.gui.stressometer.title": "UNLOCALIZED: Network Stress", 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/AssemblyException.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/AssemblyException.java new file mode 100644 index 000000000..4beac5ff9 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/AssemblyException.java @@ -0,0 +1,52 @@ +package com.simibubi.create.content.contraptions.components.structureMovement; + +import com.simibubi.create.foundation.config.AllConfigs; +import net.minecraft.block.BlockState; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.TranslationTextComponent; + +public class AssemblyException extends Exception { + public final ITextComponent component; + + public AssemblyException(ITextComponent component) { + this.component = component; + } + + public AssemblyException(String langKey, Object... objects) { + this(new TranslationTextComponent("create.gui.assembly.exception." + langKey, objects)); + } + + public static AssemblyException unmovableBlock(BlockPos pos, BlockState state) { + return new AssemblyException("unmovableBlock", + pos.getX(), + pos.getY(), + pos.getZ(), + new TranslationTextComponent(state.getBlock().getTranslationKey())); + } + + public static AssemblyException unloadedChunk(BlockPos pos) { + return new AssemblyException("chunkNotLoaded", + pos.getX(), + pos.getY(), + pos.getZ()); + } + + public static AssemblyException structureTooLarge() { + return new AssemblyException("structureTooLarge", + AllConfigs.SERVER.kinetics.maxBlocksMoved.get()); + } + + public static AssemblyException tooManyPistonPoles() { + return new AssemblyException("tooManyPistonPoles", + AllConfigs.SERVER.kinetics.maxPistonPoles.get()); + } + + public static AssemblyException noPistonPoles() { + return new AssemblyException("noPistonPoles"); + } + + public String getFormattedText() { + return component.getFormattedText(); + } +} 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 7405bb1ef..6fc93a511 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 @@ -1,26 +1,5 @@ package com.simibubi.create.content.contraptions.components.structureMovement; -import static com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock.isExtensionPole; -import static com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock.isPistonHead; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.UUID; -import java.util.function.BiConsumer; -import java.util.stream.Collectors; - -import javax.annotation.Nullable; - -import org.apache.commons.lang3.tuple.MutablePair; -import org.apache.commons.lang3.tuple.Pair; - import com.simibubi.create.AllBlocks; import com.simibubi.create.AllMovementBehaviours; import com.simibubi.create.content.contraptions.base.IRotate; @@ -55,15 +34,7 @@ import com.simibubi.create.foundation.utility.NBTHelper; import com.simibubi.create.foundation.utility.NBTProcessors; import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.worldWrappers.WrappedWorld; - -import net.minecraft.block.AbstractButtonBlock; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; -import net.minecraft.block.ChestBlock; -import net.minecraft.block.DoorBlock; -import net.minecraft.block.IWaterLoggable; -import net.minecraft.block.PressurePlateBlock; +import net.minecraft.block.*; import net.minecraft.block.material.PushReaction; import net.minecraft.entity.Entity; import net.minecraft.fluid.Fluids; @@ -96,6 +67,16 @@ import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction; import net.minecraftforge.fluids.capability.templates.FluidTank; import net.minecraftforge.items.IItemHandlerModifiable; import net.minecraftforge.items.wrapper.CombinedInvWrapper; +import org.apache.commons.lang3.tuple.MutablePair; +import org.apache.commons.lang3.tuple.Pair; + +import javax.annotation.Nullable; +import java.util.*; +import java.util.function.BiConsumer; +import java.util.stream.Collectors; + +import static com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock.isExtensionPole; +import static com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock.isPistonHead; public abstract class Contraption { @@ -139,7 +120,7 @@ public abstract class Contraption { stabilizedSubContraptions = new HashMap<>(); } - public abstract boolean assemble(World world, BlockPos pos); + public abstract boolean assemble(World world, BlockPos pos) throws AssemblyException; public abstract boolean canBeStabilized(Direction facing, BlockPos localPos); @@ -154,7 +135,7 @@ public abstract class Contraption { } protected boolean addToInitialFrontier(World world, BlockPos pos, Direction forcedDirection, - List frontier) { + Queue frontier) throws AssemblyException { return true; } @@ -165,9 +146,9 @@ public abstract class Contraption { return contraption; } - public boolean searchMovedStructure(World world, BlockPos pos, @Nullable Direction forcedDirection) { + public boolean searchMovedStructure(World world, BlockPos pos, @Nullable Direction forcedDirection) throws AssemblyException { initialPassengers.clear(); - List frontier = new ArrayList<>(); + Queue frontier = new LinkedList<>(); Set visited = new HashSet<>(); anchor = pos; @@ -181,10 +162,10 @@ 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; + throw AssemblyException.structureTooLarge(); } public void onEntityCreated(AbstractContraptionEntity entity) { @@ -196,8 +177,12 @@ public abstract class Contraption { StabilizedContraption subContraption = new StabilizedContraption(face); World world = entity.world; BlockPos pos = blockFace.getPos(); - if (!subContraption.assemble(world, pos)) + try { + if (!subContraption.assemble(world, pos)) + continue; + } catch (AssemblyException e) { continue; + } subContraption.removeBlocksFromWorld(world, BlockPos.ZERO); OrientedContraptionEntity movedContraption = OrientedContraptionEntity.create(world, subContraption, Optional.of(face)); @@ -247,20 +232,25 @@ 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, - Set visited) { - visited.add(pos); - frontier.remove(pos); - - if (!world.isBlockPresent(pos)) + /** move the first block in frontier queue */ + protected boolean moveBlock(World world, @Nullable Direction forcedDirection, Queue frontier, + Set visited) throws AssemblyException { + BlockPos pos = frontier.poll(); + if (pos == null) return false; + visited.add(pos); + + if (World.isOutsideBuildHeight(pos)) + return true; + if (!world.isBlockPresent(pos)) + throw AssemblyException.unloadedChunk(pos); 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)) + throw AssemblyException.unmovableBlock(pos, state); if (state.getBlock() instanceof AbstractChassisBlock && !moveChassis(world, pos, forcedDirection, frontier, visited)) return false; @@ -306,9 +296,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); @@ -318,9 +309,9 @@ 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; + throw AssemblyException.unmovableBlock(pos, state); continue; } @@ -349,10 +340,13 @@ public abstract class Contraption { } addBlock(pos, capture(world, pos)); - return blocks.size() <= AllConfigs.SERVER.kinetics.maxBlocksMoved.get(); + if (blocks.size() <= AllConfigs.SERVER.kinetics.maxBlocksMoved.get()) + return true; + else + throw AssemblyException.structureTooLarge(); } - protected void movePistonHead(World world, BlockPos pos, List frontier, Set visited, + protected void movePistonHead(World world, BlockPos pos, Queue frontier, Set visited, BlockState state) { Direction direction = state.get(MechanicalPistonHeadBlock.FACING); BlockPos offset = pos.offset(direction.getOpposite()); @@ -374,7 +368,7 @@ public abstract class Contraption { } } - protected void movePistonPole(World world, BlockPos pos, List frontier, Set visited, + protected void movePistonPole(World world, BlockPos pos, Queue frontier, Set visited, BlockState state) { for (Direction d : Iterate.directionsInAxis(state.get(PistonExtensionPoleBlock.FACING) .getAxis())) { @@ -397,7 +391,7 @@ public abstract class Contraption { } } - protected void moveGantryPinion(World world, BlockPos pos, List frontier, Set visited, + protected void moveGantryPinion(World world, BlockPos pos, Queue frontier, Set visited, BlockState state) { BlockPos offset = pos.offset(state.get(GantryPinionBlock.FACING)); if (!visited.contains(offset)) @@ -413,7 +407,7 @@ public abstract class Contraption { } } - protected void moveGantryShaft(World world, BlockPos pos, List frontier, Set visited, + protected void moveGantryShaft(World world, BlockPos pos, Queue frontier, Set visited, BlockState state) { for (Direction d : Iterate.directions) { BlockPos offset = pos.offset(d); @@ -429,7 +423,7 @@ public abstract class Contraption { } } - 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 (!canBeStabilized(facing, pos.subtract(anchor))) { BlockPos offset = pos.offset(facing); @@ -440,7 +434,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)) @@ -461,7 +455,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) { @@ -479,8 +473,8 @@ public abstract class Contraption { } } - private boolean moveMechanicalPiston(World world, BlockPos pos, List frontier, Set visited, - BlockState state) { + private boolean moveMechanicalPiston(World world, BlockPos pos, Queue frontier, Set visited, BlockState state) throws AssemblyException { + int limit = AllConfigs.SERVER.kinetics.maxPistonPoles.get(); Direction direction = state.get(MechanicalPistonBlock.FACING); PistonState pistonState = state.get(MechanicalPistonBlock.STATE); if (pistonState == PistonState.MOVING) @@ -503,7 +497,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)) @@ -588,8 +582,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/IDisplayAssemblyExceptions.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/IDisplayAssemblyExceptions.java new file mode 100644 index 000000000..33abfc9fb --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/IDisplayAssemblyExceptions.java @@ -0,0 +1,28 @@ +package com.simibubi.create.content.contraptions.components.structureMovement; + +import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation; +import com.simibubi.create.foundation.utility.Lang; +import net.minecraft.util.text.TextFormatting; + +import java.util.Arrays; +import java.util.List; + +public interface IDisplayAssemblyExceptions { + + default boolean addExceptionToTooltip(List tooltip) { + AssemblyException e = getLastAssemblyException(); + if (e == null) + return false; + + if (!tooltip.isEmpty()) + tooltip.add(""); + + tooltip.add(IHaveGoggleInformation.spacing + TextFormatting.GOLD + Lang.translate("gui.assembly.exception")); + String text = e.getFormattedText(); + Arrays.stream(text.split("\n")).forEach(l -> tooltip.add(IHaveGoggleInformation.spacing + TextFormatting.GRAY + l)); + + return true; + } + + AssemblyException getLastAssemblyException(); +} diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingContraption.java index 8215a87c2..49656f15e 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingContraption.java @@ -4,6 +4,7 @@ import org.apache.commons.lang3.tuple.Pair; import com.simibubi.create.AllTags.AllBlockTags; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionType; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; import net.minecraft.nbt.CompoundNBT; @@ -28,7 +29,7 @@ public class BearingContraption extends Contraption { } @Override - public boolean assemble(World world, BlockPos pos) { + public boolean assemble(World world, BlockPos pos) throws AssemblyException { BlockPos offset = pos.offset(facing); if (!searchMovedStructure(world, offset, null)) return false; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/ClockworkBearingTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/ClockworkBearingTileEntity.java index aa465f4e9..12c65902f 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/ClockworkBearingTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/ClockworkBearingTileEntity.java @@ -1,12 +1,10 @@ package com.simibubi.create.content.contraptions.components.structureMovement.bearing; -import java.util.List; - -import org.apache.commons.lang3.tuple.Pair; - import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.ControlledContraptionEntity; +import com.simibubi.create.content.contraptions.components.structureMovement.IDisplayAssemblyExceptions; import com.simibubi.create.content.contraptions.components.structureMovement.bearing.ClockworkContraption.HandType; import com.simibubi.create.foundation.advancement.AllTriggers; import com.simibubi.create.foundation.gui.AllIcons; @@ -16,7 +14,6 @@ import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollOpt import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.ServerSpeedProvider; - import net.minecraft.block.BlockState; import net.minecraft.nbt.CompoundNBT; import net.minecraft.state.properties.BlockStateProperties; @@ -25,8 +22,12 @@ import net.minecraft.util.Direction; import net.minecraft.util.Direction.Axis; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; +import net.minecraft.util.text.ITextComponent; +import org.apache.commons.lang3.tuple.Pair; -public class ClockworkBearingTileEntity extends KineticTileEntity implements IBearingTileEntity { +import java.util.List; + +public class ClockworkBearingTileEntity extends KineticTileEntity implements IBearingTileEntity, IDisplayAssemblyExceptions { protected ControlledContraptionEntity hourHand; protected ControlledContraptionEntity minuteHand; @@ -37,6 +38,7 @@ public class ClockworkBearingTileEntity extends KineticTileEntity implements IBe protected boolean running; protected boolean assembleNextTick; + protected AssemblyException lastException; protected ScrollOptionBehaviour operationMode; @@ -105,6 +107,11 @@ public class ClockworkBearingTileEntity extends KineticTileEntity implements IBe applyRotations(); } + @Override + public AssemblyException getLastAssemblyException() { + return lastException; + } + protected void applyRotations() { BlockState blockState = getBlockState(); Axis axis = Axis.X; @@ -199,8 +206,15 @@ public class ClockworkBearingTileEntity extends KineticTileEntity implements IBe Direction direction = getBlockState().get(BlockStateProperties.FACING); // Collect Construct - Pair contraption = - ClockworkContraption.assembleClockworkAt(world, pos, direction); + Pair contraption; + try { + contraption = ClockworkContraption.assembleClockworkAt(world, pos, direction); + lastException = null; + } catch (AssemblyException e) { + lastException = e; + sendData(); + return; + } if (contraption == null) return; if (contraption.getLeft() == null) @@ -284,6 +298,8 @@ public class ClockworkBearingTileEntity extends KineticTileEntity implements IBe compound.putBoolean("Running", running); compound.putFloat("HourAngle", hourAngle); compound.putFloat("MinuteAngle", minuteAngle); + if (lastException != null) + compound.putString("LastException", ITextComponent.Serializer.toJson(lastException.component)); super.write(compound, clientPacket); } @@ -295,6 +311,10 @@ public class ClockworkBearingTileEntity extends KineticTileEntity implements IBe running = compound.getBoolean("Running"); hourAngle = compound.getFloat("HourAngle"); minuteAngle = compound.getFloat("MinuteAngle"); + if (compound.contains("LastException")) + lastException = new AssemblyException(ITextComponent.Serializer.fromJson(compound.getString("LastException"))); + else + lastException = null; super.read(compound, clientPacket); if (!clientPacket) 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 4a9c61992..a105ad0d2 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 @@ -7,13 +7,18 @@ import java.util.Set; import org.apache.commons.lang3.tuple.Pair; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionType; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; import com.simibubi.create.foundation.utility.NBTHelper; - import net.minecraft.nbt.CompoundNBT; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import org.apache.commons.lang3.tuple.Pair; + +import java.util.HashSet; +import java.util.Queue; +import java.util.Set; public class ClockworkContraption extends Contraption { @@ -38,7 +43,7 @@ public class ClockworkContraption extends Contraption { } public static Pair assembleClockworkAt(World world, BlockPos pos, - Direction direction) { + Direction direction) throws AssemblyException { int hourArmBlocks = 0; ClockworkContraption hourArm = new ClockworkContraption(); @@ -81,21 +86,23 @@ public class ClockworkContraption extends Contraption { } @Override - public boolean assemble(World world, BlockPos pos) { + public boolean assemble(World world, BlockPos pos) throws AssemblyException { return searchMovedStructure(world, pos, facing); } @Override - public boolean searchMovedStructure(World world, BlockPos pos, Direction direction) { + public boolean searchMovedStructure(World world, BlockPos pos, Direction direction) throws AssemblyException { return super.searchMovedStructure(world, pos.offset(direction, offset + 1), null); } @Override - protected boolean moveBlock(World world, BlockPos pos, Direction direction, List frontier, - Set visited) { - if (ignoreBlocks.contains(pos)) + protected boolean moveBlock(World world, Direction direction, Queue frontier, + Set visited) throws AssemblyException { + 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/bearing/MechanicalBearingTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/MechanicalBearingTileEntity.java index b4dde3bc4..353e58c17 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/MechanicalBearingTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/MechanicalBearingTileEntity.java @@ -1,12 +1,10 @@ package com.simibubi.create.content.contraptions.components.structureMovement.bearing; -import static net.minecraft.state.properties.BlockStateProperties.FACING; - -import java.util.List; - import com.simibubi.create.content.contraptions.base.GeneratingKineticTileEntity; import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.ControlledContraptionEntity; +import com.simibubi.create.content.contraptions.components.structureMovement.IDisplayAssemblyExceptions; import com.simibubi.create.foundation.advancement.AllTriggers; import com.simibubi.create.foundation.item.TooltipHelper; import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour; @@ -14,7 +12,6 @@ import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollOpt import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.ServerSpeedProvider; - import net.minecraft.block.BlockState; import net.minecraft.nbt.CompoundNBT; import net.minecraft.state.properties.BlockStateProperties; @@ -22,8 +19,13 @@ import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; +import net.minecraft.util.text.ITextComponent; -public class MechanicalBearingTileEntity extends GeneratingKineticTileEntity implements IBearingTileEntity { +import java.util.List; + +import static net.minecraft.state.properties.BlockStateProperties.FACING; + +public class MechanicalBearingTileEntity extends GeneratingKineticTileEntity implements IBearingTileEntity, IDisplayAssemblyExceptions { protected ScrollOptionBehaviour movementMode; protected ControlledContraptionEntity movedContraption; @@ -31,6 +33,7 @@ public class MechanicalBearingTileEntity extends GeneratingKineticTileEntity imp protected boolean running; protected boolean assembleNextTick; protected float clientAngleDiff; + protected AssemblyException lastException; public MechanicalBearingTileEntity(TileEntityType type) { super(type); @@ -62,6 +65,8 @@ public class MechanicalBearingTileEntity extends GeneratingKineticTileEntity imp public void write(CompoundNBT compound, boolean clientPacket) { compound.putBoolean("Running", running); compound.putFloat("Angle", angle); + if (lastException != null) + compound.putString("LastException", ITextComponent.Serializer.toJson(lastException.component)); super.write(compound, clientPacket); } @@ -70,6 +75,10 @@ public class MechanicalBearingTileEntity extends GeneratingKineticTileEntity imp float angleBefore = angle; running = compound.getBoolean("Running"); angle = compound.getFloat("Angle"); + if (compound.contains("LastException")) + lastException = new AssemblyException(ITextComponent.Serializer.fromJson(compound.getString("LastException"))); + else + lastException = null; super.read(compound, clientPacket); if (!clientPacket) return; @@ -104,6 +113,11 @@ public class MechanicalBearingTileEntity extends GeneratingKineticTileEntity imp return speed; } + @Override + public AssemblyException getLastAssemblyException() { + return lastException; + } + protected boolean isWindmill() { return false; } @@ -120,8 +134,16 @@ public class MechanicalBearingTileEntity extends GeneratingKineticTileEntity imp Direction direction = getBlockState().get(FACING); BearingContraption contraption = new BearingContraption(isWindmill(), direction); - if (!contraption.assemble(world, pos)) + try { + if (!contraption.assemble(world, pos)) + return; + + lastException = null; + } catch (AssemblyException e) { + lastException = e; + sendData(); return; + } if (isWindmill()) AllTriggers.triggerForNearbyPlayers(AllTriggers.WINDMILL, world, pos, 5); @@ -281,5 +303,4 @@ public class MechanicalBearingTileEntity extends GeneratingKineticTileEntity imp TooltipHelper.addHint(tooltip, "hint.empty_bearing"); return true; } - } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedContraption.java index c8281b7c0..a8b726cd2 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedContraption.java @@ -1,6 +1,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.bearing; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionType; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; import net.minecraft.nbt.CompoundNBT; @@ -20,7 +21,7 @@ public class StabilizedContraption extends Contraption { } @Override - public boolean assemble(World world, BlockPos pos) { + public boolean assemble(World world, BlockPos pos) throws AssemblyException { BlockPos offset = pos.offset(facing); if (!searchMovedStructure(world, offset, null)) return false; 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/CartAssemblerBlock.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java index 49c337697..8a50eb7cf 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerBlock.java @@ -3,6 +3,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.mo import com.simibubi.create.AllBlocks; import com.simibubi.create.AllShapes; import com.simibubi.create.AllTileEntities; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerTileEntity.CartMovementMode; import com.simibubi.create.content.contraptions.components.structureMovement.train.CouplingHandler; @@ -229,13 +230,27 @@ public class CartAssemblerBlock extends AbstractRailBlock .isCoupledThroughContraption()) return; - CartMovementMode mode = - getTileEntityOptional(world, pos).map(te -> CartMovementMode.values()[te.movementMode.value]) + + Optional assembler = getTileEntityOptional(world, pos); + CartMovementMode mode = assembler.map(te -> CartMovementMode.values()[te.movementMode.value]) .orElse(CartMovementMode.ROTATE); MountedContraption contraption = new MountedContraption(mode); - if (!contraption.assemble(world, pos)) + try { + if (!contraption.assemble(world, pos)) + return; + + assembler.ifPresent(te -> { + te.lastException = null; + te.sendData(); + }); + } catch (AssemblyException e) { + assembler.ifPresent(te -> { + te.lastException = e; + te.sendData(); + }); return; + } boolean couplingFound = contraption.connectedCart != null; Optional initialOrientation = cart.getMotion() diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerTileEntity.java index 4e98a97be..4640e7fa9 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/CartAssemblerTileEntity.java @@ -1,7 +1,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.mounted; -import java.util.List; - +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; +import com.simibubi.create.content.contraptions.components.structureMovement.IDisplayAssemblyExceptions; import com.simibubi.create.foundation.gui.AllIcons; import com.simibubi.create.foundation.tileEntity.SmartTileEntity; import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour; @@ -11,17 +11,21 @@ import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.INamedIco import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollOptionBehaviour; import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.VecHelper; - +import net.minecraft.nbt.CompoundNBT; import net.minecraft.state.properties.RailShape; import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.Direction.Axis; import net.minecraft.util.math.Vec3d; +import net.minecraft.util.text.ITextComponent; -public class CartAssemblerTileEntity extends SmartTileEntity { +import java.util.List; + +public class CartAssemblerTileEntity extends SmartTileEntity implements IDisplayAssemblyExceptions { private static final int assemblyCooldown = 8; protected ScrollOptionBehaviour movementMode; private int ticksSinceMinecartUpdate; + protected AssemblyException lastException; public CartAssemblerTileEntity(TileEntityType type) { super(type); @@ -44,6 +48,27 @@ public class CartAssemblerTileEntity extends SmartTileEntity { behaviours.add(movementMode); } + @Override + public void write(CompoundNBT compound, boolean clientPacket) { + if (lastException != null) + compound.putString("LastException", ITextComponent.Serializer.toJson(lastException.component)); + super.write(compound, clientPacket); + } + + @Override + protected void read(CompoundNBT compound, boolean clientPacket) { + if (compound.contains("LastException")) + lastException = new AssemblyException(ITextComponent.Serializer.fromJson(compound.getString("LastException"))); + else + lastException = null; + super.read(compound, clientPacket); + } + + @Override + public AssemblyException getLastAssemblyException() { + return lastException; + } + protected ValueBoxTransform getMovementModeSlot() { return new CartAssemblerValueBoxTransform(); } @@ -103,5 +128,4 @@ public class CartAssemblerTileEntity extends SmartTileEntity { public boolean isMinecartUpdateValid() { return ticksSinceMinecartUpdate >= assemblyCooldown; } - } 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 15e973052..07868c475 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,12 +2,13 @@ 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; import com.simibubi.create.AllBlocks; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionType; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; import com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerTileEntity.CartMovementMode; import com.simibubi.create.foundation.utility.Iterate; @@ -52,7 +53,7 @@ public class MountedContraption extends Contraption { } @Override - public boolean assemble(World world, BlockPos pos) { + public boolean assemble(World world, BlockPos pos) throws AssemblyException { BlockState state = world.getBlockState(pos); if (!state.has(RAIL_SHAPE)) return false; @@ -70,7 +71,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 +105,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/LinearActuatorTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/LinearActuatorTileEntity.java index c60e181a3..1adfdb1cd 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/LinearActuatorTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/LinearActuatorTileEntity.java @@ -1,24 +1,22 @@ package com.simibubi.create.content.contraptions.components.structureMovement.piston; -import java.util.List; - import com.simibubi.create.content.contraptions.base.KineticTileEntity; -import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; -import com.simibubi.create.content.contraptions.components.structureMovement.ControlledContraptionEntity; -import com.simibubi.create.content.contraptions.components.structureMovement.IControlContraption; +import com.simibubi.create.content.contraptions.components.structureMovement.*; import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour; import com.simibubi.create.foundation.tileEntity.behaviour.ValueBoxTransform; import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollOptionBehaviour; import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.ServerSpeedProvider; - import net.minecraft.nbt.CompoundNBT; import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; +import net.minecraft.util.text.ITextComponent; -public abstract class LinearActuatorTileEntity extends KineticTileEntity implements IControlContraption { +import java.util.List; + +public abstract class LinearActuatorTileEntity extends KineticTileEntity implements IControlContraption, IDisplayAssemblyExceptions { public float offset; public boolean running; @@ -27,6 +25,7 @@ public abstract class LinearActuatorTileEntity extends KineticTileEntity impleme protected boolean forceMove; protected ScrollOptionBehaviour movementMode; protected boolean waitingForSpeedChange; + protected AssemblyException lastException; // Custom position sync protected float clientOffsetDiff; @@ -80,7 +79,13 @@ public abstract class LinearActuatorTileEntity extends KineticTileEntity impleme return; } else { if (getSpeed() != 0) - assemble(); + try { + assemble(); + lastException = null; + } catch (AssemblyException e) { + lastException = e; + } + sendData(); } return; } @@ -153,6 +158,8 @@ public abstract class LinearActuatorTileEntity extends KineticTileEntity impleme compound.putBoolean("Running", running); compound.putBoolean("Waiting", waitingForSpeedChange); compound.putFloat("Offset", offset); + if (lastException != null) + compound.putString("LastException", ITextComponent.Serializer.toJson(lastException.component)); super.write(compound, clientPacket); if (clientPacket && forceMove) { @@ -169,6 +176,10 @@ public abstract class LinearActuatorTileEntity extends KineticTileEntity impleme running = compound.getBoolean("Running"); waitingForSpeedChange = compound.getBoolean("Waiting"); offset = compound.getFloat("Offset"); + if (compound.contains("LastException")) + lastException = new AssemblyException(ITextComponent.Serializer.fromJson(compound.getString("LastException"))); + else + lastException = null; super.read(compound, clientPacket); if (!clientPacket) @@ -183,9 +194,14 @@ public abstract class LinearActuatorTileEntity extends KineticTileEntity impleme movedContraption = null; } + @Override + public AssemblyException getLastAssemblyException() { + return lastException; + } + public abstract void disassemble(); - protected abstract void assemble(); + protected abstract void assemble() throws AssemblyException; protected abstract int getExtensionRange(); @@ -288,5 +304,4 @@ public abstract class LinearActuatorTileEntity extends KineticTileEntity impleme public BlockPos getBlockPosition() { return pos; } - } \ No newline at end of file diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/MechanicalPistonTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/MechanicalPistonTileEntity.java index a24d004fe..bb8c2ff7e 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/MechanicalPistonTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/piston/MechanicalPistonTileEntity.java @@ -2,6 +2,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.pi import com.simibubi.create.AllBlocks; import com.simibubi.create.content.contraptions.base.IRotate; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionCollider; import com.simibubi.create.content.contraptions.components.structureMovement.ControlledContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.DirectionalExtenderScrollOptionSlot; @@ -41,7 +42,7 @@ public class MechanicalPistonTileEntity extends LinearActuatorTileEntity { } @Override - public void assemble() { + public void assemble() throws AssemblyException { if (!(world.getBlockState(pos) .getBlock() instanceof MechanicalPistonBlock)) return; 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 965bfe588..09565629a 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 @@ -1,6 +1,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.piston; -import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionType; +import com.simibubi.create.content.contraptions.components.structureMovement.AllContraptionTypes; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.BlockMovementTraits; import com.simibubi.create.content.contraptions.components.structureMovement.TranslatingContraption; import com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock.*; @@ -23,6 +24,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; @@ -51,7 +53,7 @@ public class PistonContraption extends TranslatingContraption { } @Override - public boolean assemble(World world, BlockPos pos) { + public boolean assemble(World world, BlockPos pos) throws AssemblyException { if (!collectExtensions(world, pos, orientation)) return false; int count = blocks.size(); @@ -66,7 +68,7 @@ public class PistonContraption extends TranslatingContraption { return true; } - private boolean collectExtensions(World world, BlockPos pos, Direction direction) { + private boolean collectExtensions(World world, BlockPos pos, Direction direction) throws AssemblyException { List poles = new ArrayList<>(); BlockPos actualStart = pos; BlockState nextBlock = world.getBlockState(actualStart.offset(direction)); @@ -89,7 +91,7 @@ public class PistonContraption extends TranslatingContraption { nextBlock = world.getBlockState(actualStart.offset(direction)); if (extensionsInFront > MechanicalPistonBlock.maxAllowedPistonPoles()) - return false; + throw AssemblyException.tooManyPistonPoles(); } } @@ -112,7 +114,7 @@ public class PistonContraption extends TranslatingContraption { nextBlock = world.getBlockState(end.offset(direction.getOpposite())); if (extensionsInFront + extensionsInBack > MechanicalPistonBlock.maxAllowedPistonPoles()) - return false; + throw AssemblyException.tooManyPistonPoles(); } anchor = pos.offset(direction, initialExtensionProgress + 1); @@ -124,7 +126,7 @@ public class PistonContraption extends TranslatingContraption { 1, 1); if (extensionLength == 0) - return false; + throw AssemblyException.noPistonPoles(); bounds = new AxisAlignedBB(0, 0, 0, 0, 0, 0); @@ -144,7 +146,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) throws AssemblyException { frontier.clear(); boolean sticky = isStickyPiston(world.getBlockState(pos.offset(orientation, -1))); boolean retracting = direction != orientation; @@ -154,17 +156,22 @@ public class PistonContraption extends TranslatingContraption { if (offset == 1 && retracting) return true; BlockPos currentPos = pos.offset(orientation, offset + initialExtensionProgress); - if (!world.isBlockPresent(currentPos)) - return false; - if (!BlockMovementTraits.movementNecessary(world, currentPos)) + if (retracting && World.isOutsideBuildHeight(currentPos)) return true; + if (!world.isBlockPresent(currentPos)) + throw AssemblyException.unloadedChunk(currentPos); 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)) - return retracting; + if (!BlockMovementTraits.movementAllowed(state, world, currentPos)) + if (retracting) + return true; + else + throw AssemblyException.unmovableBlock(currentPos, state); if (retracting && state.getPushReaction() == PushReaction.PUSH_ONLY) return true; frontier.add(currentPos); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/PulleyContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/PulleyContraption.java index d12cecad5..5c444e4da 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/PulleyContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/PulleyContraption.java @@ -1,5 +1,6 @@ package com.simibubi.create.content.contraptions.components.structureMovement.pulley; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionType; import com.simibubi.create.content.contraptions.components.structureMovement.TranslatingContraption; @@ -23,7 +24,7 @@ public class PulleyContraption extends TranslatingContraption { } @Override - public boolean assemble(World world, BlockPos pos) { + public boolean assemble(World world, BlockPos pos) throws AssemblyException { if (!searchMovedStructure(world, pos, null)) return false; startMoving(world); 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..5f0bf3469 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 @@ -1,6 +1,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.pulley; import com.simibubi.create.AllBlocks; +import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.BlockMovementTraits; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionCollider; import com.simibubi.create.content.contraptions.components.structureMovement.ControlledContraptionEntity; @@ -8,7 +9,6 @@ import com.simibubi.create.content.contraptions.components.structureMovement.pis import com.simibubi.create.foundation.config.AllConfigs; import com.simibubi.create.foundation.tileEntity.behaviour.CenteredSideValueBoxTransform; import com.simibubi.create.foundation.tileEntity.behaviour.ValueBoxTransform; - import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.IWaterLoggable; @@ -22,6 +22,7 @@ import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; +import net.minecraft.util.text.ITextComponent; public class PulleyTileEntity extends LinearActuatorTileEntity { @@ -42,7 +43,7 @@ public class PulleyTileEntity extends LinearActuatorTileEntity { } @Override - protected void assemble() { + protected void assemble() throws AssemblyException { if (!(world.getBlockState(pos) .getBlock() instanceof PulleyBlock)) return; @@ -176,9 +177,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(); @@ -188,12 +190,18 @@ public class PulleyTileEntity extends LinearActuatorTileEntity { @Override protected void read(CompoundNBT compound, boolean clientPacket) { initialOffset = compound.getInt("InitialOffset"); + if (compound.contains("LastException")) + lastException = new AssemblyException(ITextComponent.Serializer.fromJson(compound.getString("LastException"))); + else + lastException = null; super.read(compound, clientPacket); } @Override public void write(CompoundNBT compound, boolean clientPacket) { compound.putInt("InitialOffset", initialOffset); + if (lastException != null) + compound.putString("LastException", ITextComponent.Serializer.toJson(lastException.component)); super.write(compound, clientPacket); } diff --git a/src/main/java/com/simibubi/create/content/contraptions/goggles/GoggleOverlayRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/goggles/GoggleOverlayRenderer.java index 07b9dff3a..c61452bb8 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/goggles/GoggleOverlayRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/goggles/GoggleOverlayRenderer.java @@ -4,6 +4,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import com.simibubi.create.AllBlocks; import com.simibubi.create.AllItems; import com.simibubi.create.CreateClient; +import com.simibubi.create.content.contraptions.components.structureMovement.IDisplayAssemblyExceptions; import com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock; import com.simibubi.create.content.contraptions.components.structureMovement.piston.PistonExtensionPoleBlock; import com.simibubi.create.foundation.config.AllConfigs; @@ -92,6 +93,14 @@ public class GoggleOverlayRenderer { tooltip.remove(tooltip.size() - 1); } + if (te instanceof IDisplayAssemblyExceptions) { + boolean exceptionAdded = ((IDisplayAssemblyExceptions) te).addExceptionToTooltip(tooltip); + if (exceptionAdded) { + hasHoveringInformation = true; + hoverAddedInformation = true; + } + } + // break early if goggle or hover returned false when present if ((hasGoggleInformation && !goggleAddedInformation) && (hasHoveringInformation && !hoverAddedInformation)) return; diff --git a/src/main/java/com/simibubi/create/content/contraptions/goggles/IHaveGoggleInformation.java b/src/main/java/com/simibubi/create/content/contraptions/goggles/IHaveGoggleInformation.java index 728f1c631..9a664a92d 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/goggles/IHaveGoggleInformation.java +++ b/src/main/java/com/simibubi/create/content/contraptions/goggles/IHaveGoggleInformation.java @@ -9,13 +9,13 @@ import java.util.List; public interface IHaveGoggleInformation { DecimalFormat decimalFormat = new DecimalFormat("#.##"); - public static String spacing = " "; + String spacing = " "; /** * this method will be called when looking at a TileEntity that implemented this interface * - * @return {{@code true}} if the tooltip creation was successful and should be displayed, - * or {{@code false}} if the overlay should not be displayed + * @return {@code true} if the tooltip creation was successful and should be displayed, + * or {@code false} if the overlay should not be displayed * */ default boolean addToGoggleTooltip(List tooltip, boolean isPlayerSneaking){ return false; diff --git a/src/main/resources/assets/create/lang/default/messages.json b/src/main/resources/assets/create/lang/default/messages.json index 57fbc9b7e..94af8ae4d 100644 --- a/src/main/resources/assets/create/lang/default/messages.json +++ b/src/main/resources/assets/create/lang/default/messages.json @@ -172,6 +172,13 @@ "create.gui.goggles.at_current_speed": "at current speed", "create.gui.goggles.pole_length": "Pole Length:", + "create.gui.assembly.exception": "This Contraption was unable to assemble:", + "create.gui.assembly.exception.unmovableBlock": "Unmovable Block (%4$s) at [%1$s %2$s %3$s]", + "create.gui.assembly.exception.chunkNotLoaded": "The Block at [%1$s %2$s %3$s] was not in a loaded chunk", + "create.gui.assembly.exception.structureTooLarge": "There are too many Blocks included in the contraption.\nThe configured maximum is: %1$s", + "create.gui.assembly.exception.tooManyPistonPoles": "There are too many extension Poles attached to this Piston.\nThe configured maximum is: %1$s", + "create.gui.assembly.exception.noPistonPoles": "The Piston is missing some extension Poles", + "create.gui.gauge.info_header": "Gauge Information:", "create.gui.speedometer.title": "Rotation Speed", "create.gui.stressometer.title": "Network Stress",