From 4ff20b42179b6fbdfc09ec088c681b373282ab04 Mon Sep 17 00:00:00 2001 From: grimmauld Date: Sat, 27 Mar 2021 22:42:27 +0100 Subject: [PATCH] Cogwheel refactor Part I - Moved hasIntegratedCogWheel and isSmallCog to ICogWheel --- .../contraptions/RotationPropagator.java | 16 +++++----- .../content/contraptions/base/IRotate.java | 2 -- .../contraptions/base/KineticBlock.java | 5 ---- .../contraptions/base/KineticTileEntity.java | 3 +- .../base/KineticTileEntityRenderer.java | 3 +- .../base/KineticTileInstance.java | 3 +- .../crafter/MechanicalCrafterBlock.java | 8 ++--- .../components/millstone/MillstoneBlock.java | 8 ++--- .../mixer/MechanicalMixerBlock.java | 8 ++--- .../contraptions/fluids/PumpBlock.java | 8 ++--- .../relays/advanced/SpeedControllerBlock.java | 3 +- .../relays/elementary/CogWheelBlock.java | 30 +++++++------------ .../relays/elementary/CogwheelBlockItem.java | 8 ++--- .../relays/elementary/ICogWheel.java | 22 ++++++++++++++ .../block/mechanicalArm/ArmBlock.java | 8 ++--- 15 files changed, 62 insertions(+), 73 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/relays/elementary/ICogWheel.java diff --git a/src/main/java/com/simibubi/create/content/contraptions/RotationPropagator.java b/src/main/java/com/simibubi/create/content/contraptions/RotationPropagator.java index dd29f947e..faa8c2046 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/RotationPropagator.java +++ b/src/main/java/com/simibubi/create/content/contraptions/RotationPropagator.java @@ -1,6 +1,5 @@ package com.simibubi.create.content.contraptions; -import static com.simibubi.create.content.contraptions.relays.elementary.CogWheelBlock.isLargeCog; import static net.minecraft.state.properties.BlockStateProperties.AXIS; import java.util.LinkedList; @@ -12,6 +11,7 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.relays.advanced.SpeedControllerBlock; import com.simibubi.create.content.contraptions.relays.advanced.SpeedControllerTileEntity; import com.simibubi.create.content.contraptions.relays.elementary.CogWheelBlock; +import com.simibubi.create.content.contraptions.relays.elementary.ICogWheel; import com.simibubi.create.content.contraptions.relays.encased.DirectionalShaftHalvesTileEntity; import com.simibubi.create.content.contraptions.relays.encased.EncasedBeltBlock; import com.simibubi.create.content.contraptions.relays.encased.SplitShaftTileEntity; @@ -66,8 +66,8 @@ public class RotationPropagator { alignedAxes && definitionFrom.hasShaftTowards(world, from.getPos(), stateFrom, direction) && definitionTo.hasShaftTowards(world, to.getPos(), stateTo, direction.getOpposite()); - boolean connectedByGears = definitionFrom.hasIntegratedCogwheel(world, from.getPos(), stateFrom) - && definitionTo.hasIntegratedCogwheel(world, to.getPos(), stateTo); + boolean connectedByGears = ICogWheel.isSmallCog(stateFrom) + && ICogWheel.isSmallCog(stateTo); float custom = from.propagateRotationTo(to, stateFrom, stateTo, diff, connectedByAxis, connectedByGears); if (custom != 0) @@ -98,10 +98,10 @@ public class RotationPropagator { } // Gear <-> Large Gear - if (isLargeCog(stateFrom) && definitionTo.hasIntegratedCogwheel(world, to.getPos(), stateTo)) + if (ICogWheel.isLargeCog(stateFrom) && ICogWheel.isSmallCog(stateTo)) if (isLargeToSmallCog(stateFrom, stateTo, definitionTo, diff)) return -2f; - if (isLargeCog(stateTo) && definitionFrom.hasIntegratedCogwheel(world, from.getPos(), stateFrom)) + if (ICogWheel.isLargeCog(stateTo) && ICogWheel.isSmallCog(stateFrom)) if (isLargeToSmallCog(stateTo, stateFrom, definitionFrom, diff)) return -.5f; @@ -109,7 +109,7 @@ public class RotationPropagator { if (connectedByGears) { if (diff.manhattanDistance(BlockPos.ZERO) != 1) return 0; - if (isLargeCog(stateTo)) + if (ICogWheel.isLargeCog(stateTo)) return 0; if (direction.getAxis() == definitionFrom.getRotationAxis(stateFrom)) return 0; @@ -137,7 +137,7 @@ public class RotationPropagator { } private static boolean isLargeToLargeGear(BlockState from, BlockState to, BlockPos diff) { - if (!isLargeCog(from) || !isLargeCog(to)) + if (!ICogWheel.isLargeCog(from) || !ICogWheel.isLargeCog(to)) return false; Axis fromAxis = from.get(AXIS); Axis toAxis = to.get(AXIS); @@ -186,7 +186,7 @@ public class RotationPropagator { } private static boolean isLargeCogToSpeedController(BlockState from, BlockState to, BlockPos diff) { - if (!isLargeCog(from) || !AllBlocks.ROTATION_SPEED_CONTROLLER.has(to)) + if (!ICogWheel.isLargeCog(from) || !AllBlocks.ROTATION_SPEED_CONTROLLER.has(to)) return false; if (!diff.equals(BlockPos.ZERO.down())) return false; diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/IRotate.java b/src/main/java/com/simibubi/create/content/contraptions/base/IRotate.java index 225532912..fa025e01a 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/IRotate.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/IRotate.java @@ -118,8 +118,6 @@ public interface IRotate extends IWrenchable { } public boolean hasShaftTowards(IWorldReader world, BlockPos pos, BlockState state, Direction face); - - public boolean hasIntegratedCogwheel(IWorldReader world, BlockPos pos, BlockState state); public Axis getRotationAxis(BlockState state); diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/KineticBlock.java b/src/main/java/com/simibubi/create/content/contraptions/base/KineticBlock.java index ee0734ae3..4836a2d72 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/KineticBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/KineticBlock.java @@ -72,11 +72,6 @@ public abstract class KineticBlock extends Block implements IRotate { return false; } - @Override - public boolean hasIntegratedCogwheel(IWorldReader world, BlockPos pos, BlockState state) { - return false; - } - @Override public boolean hasTileEntity(BlockState state) { return true; diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntity.java index f38efb6fa..ae701fb85 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntity.java @@ -14,6 +14,7 @@ import com.simibubi.create.content.contraptions.base.IRotate.SpeedLevel; import com.simibubi.create.content.contraptions.base.IRotate.StressImpact; import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation; import com.simibubi.create.content.contraptions.goggles.IHaveHoveringInformation; +import com.simibubi.create.content.contraptions.relays.elementary.ICogWheel; import com.simibubi.create.foundation.config.AllConfigs; import com.simibubi.create.foundation.item.TooltipHelper; import com.simibubi.create.foundation.render.backend.FastRenderDispatcher; @@ -529,7 +530,7 @@ public abstract class KineticTileEntity extends SmartTileEntity } protected boolean canPropagateDiagonally(IRotate block, BlockState state) { - return block.hasIntegratedCogwheel(world, pos, state); + return ICogWheel.isSmallCog(state); } @Override diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java index 6ff654402..fe297f718 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java @@ -6,6 +6,7 @@ import com.simibubi.create.AllBlocks; import com.simibubi.create.CreateClient; import com.simibubi.create.content.contraptions.KineticDebugger; import com.simibubi.create.content.contraptions.relays.elementary.CogWheelBlock; +import com.simibubi.create.content.contraptions.relays.elementary.ICogWheel; import com.simibubi.create.foundation.render.Compartment; import com.simibubi.create.foundation.render.SuperByteBuffer; import com.simibubi.create.foundation.render.backend.FastRenderDispatcher; @@ -96,7 +97,7 @@ public class KineticTileEntityRenderer extends SafeTileEntityRenderer extends T } protected float getRotationOffset(final Direction.Axis axis) { - float offset = CogWheelBlock.isLargeCog(blockState) ? 11.25f : 0; + float offset = ICogWheel.isLargeCog(blockState) ? 11.25f : 0; double d = (((axis == Direction.Axis.X) ? 0 : pos.getX()) + ((axis == Direction.Axis.Y) ? 0 : pos.getY()) + ((axis == Direction.Axis.Z) ? 0 : pos.getZ())) % 2; if (d == 0) { diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCrafterBlock.java b/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCrafterBlock.java index 7f4c9d899..5d4d79ce6 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCrafterBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCrafterBlock.java @@ -7,6 +7,7 @@ import com.simibubi.create.content.contraptions.base.HorizontalKineticBlock; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.components.crafter.ConnectedInputHandler.ConnectedInput; import com.simibubi.create.content.contraptions.components.crafter.MechanicalCrafterTileEntity.Phase; +import com.simibubi.create.content.contraptions.relays.elementary.ICogWheel; import com.simibubi.create.foundation.block.ITE; import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour; import com.simibubi.create.foundation.tileEntity.behaviour.inventory.InvManipulationBehaviour; @@ -41,7 +42,7 @@ import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemStackHandler; -public class MechanicalCrafterBlock extends HorizontalKineticBlock implements ITE { +public class MechanicalCrafterBlock extends HorizontalKineticBlock implements ITE, ICogWheel { public static final EnumProperty POINTING = EnumProperty.create("pointing", Pointing.class); @@ -60,11 +61,6 @@ public class MechanicalCrafterBlock extends HorizontalKineticBlock implements IT return AllTileEntities.MECHANICAL_CRAFTER.create(); } - @Override - public boolean hasIntegratedCogwheel(IWorldReader world, BlockPos pos, BlockState state) { - return true; - } - @Override public Axis getRotationAxis(BlockState state) { return state.get(HORIZONTAL_FACING) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/millstone/MillstoneBlock.java b/src/main/java/com/simibubi/create/content/contraptions/components/millstone/MillstoneBlock.java index 9696e6b11..7c7ecc989 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/millstone/MillstoneBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/millstone/MillstoneBlock.java @@ -3,6 +3,7 @@ package com.simibubi.create.content.contraptions.components.millstone; import com.simibubi.create.AllShapes; import com.simibubi.create.AllTileEntities; import com.simibubi.create.content.contraptions.base.KineticBlock; +import com.simibubi.create.content.contraptions.relays.elementary.ICogWheel; import com.simibubi.create.foundation.block.ITE; import com.simibubi.create.foundation.item.ItemHelper; import com.simibubi.create.foundation.utility.Iterate; @@ -30,7 +31,7 @@ import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandlerModifiable; import net.minecraftforge.items.ItemStackHandler; -public class MillstoneBlock extends KineticBlock implements ITE { +public class MillstoneBlock extends KineticBlock implements ITE, ICogWheel { public MillstoneBlock(Properties properties) { super(properties); @@ -131,11 +132,6 @@ public class MillstoneBlock extends KineticBlock implements ITE { +public class MechanicalMixerBlock extends KineticBlock implements ITE, ICogWheel { public MechanicalMixerBlock(Properties properties) { super(properties); @@ -51,11 +52,6 @@ public class MechanicalMixerBlock extends KineticBlock implements ITE items) { items.add(new ItemStack(this)); } - - // IRotate - - @Override - public boolean hasIntegratedCogwheel(IWorldReader world, BlockPos pos, BlockState state) { - return !isLarge; - } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/CogwheelBlockItem.java b/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/CogwheelBlockItem.java index 6d8018e30..9b5c2af90 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/CogwheelBlockItem.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/CogwheelBlockItem.java @@ -21,7 +21,6 @@ import com.simibubi.create.AllBlocks; import com.simibubi.create.AllShapes; import com.simibubi.create.content.contraptions.base.DirectionalKineticBlock; import com.simibubi.create.content.contraptions.base.HorizontalKineticBlock; -import com.simibubi.create.content.contraptions.base.IRotate; import com.simibubi.create.foundation.advancement.AllTriggers; import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.placement.IPlacementHelper; @@ -241,8 +240,7 @@ public class CogwheelBlockItem extends BlockItem { @Override public Predicate getStatePredicate() { - return s -> !AllBlocks.COGWHEEL.has(s) && s.getBlock() instanceof IRotate - && ((IRotate) s.getBlock()).hasIntegratedCogwheel(null, null, null); + return s -> !AllBlocks.COGWHEEL.has(s) && ICogWheel.isSmallCog(s); } @Override @@ -285,7 +283,7 @@ public class CogwheelBlockItem extends BlockItem { } } - static public boolean hasLargeCogwheelNeighbor(World world, BlockPos pos, Axis axis) { + public static boolean hasLargeCogwheelNeighbor(World world, BlockPos pos, Axis axis) { for (Direction dir : Iterate.directions) { if (dir.getAxis() == axis) continue; @@ -297,7 +295,7 @@ public class CogwheelBlockItem extends BlockItem { return false; } - static public boolean hasSmallCogwheelNeighbor(World world, BlockPos pos, Axis axis) { + public static boolean hasSmallCogwheelNeighbor(World world, BlockPos pos, Axis axis) { for (Direction dir : Iterate.directions) { if (dir.getAxis() == axis) continue; diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/ICogWheel.java b/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/ICogWheel.java new file mode 100644 index 000000000..c8b4cab28 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/elementary/ICogWheel.java @@ -0,0 +1,22 @@ +package com.simibubi.create.content.contraptions.relays.elementary; + +import com.simibubi.create.content.contraptions.base.IRotate; +import net.minecraft.block.BlockState; + +public interface ICogWheel extends IRotate { + static boolean isSmallCog(BlockState state) { + return state.getBlock() instanceof ICogWheel && ((ICogWheel) state.getBlock()).isSmallCog(); + } + + static boolean isLargeCog(BlockState state) { + return state.getBlock() instanceof ICogWheel && ((ICogWheel) state.getBlock()).isLargeCog(); + } + + default boolean isLargeCog() { + return false; + } + + default boolean isSmallCog() { + return !isLargeCog(); + } +} diff --git a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmBlock.java b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmBlock.java index 13354fc17..cbf44210c 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmBlock.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmBlock.java @@ -1,5 +1,6 @@ package com.simibubi.create.content.logistics.block.mechanicalArm; +import com.simibubi.create.content.contraptions.relays.elementary.ICogWheel; import org.apache.commons.lang3.mutable.MutableBoolean; import com.simibubi.create.AllShapes; @@ -29,7 +30,7 @@ import net.minecraft.world.IBlockReader; import net.minecraft.world.IWorldReader; import net.minecraft.world.World; -public class ArmBlock extends KineticBlock implements ITE { +public class ArmBlock extends KineticBlock implements ITE, ICogWheel { public static final BooleanProperty CEILING = BooleanProperty.create("ceiling"); @@ -48,11 +49,6 @@ public class ArmBlock extends KineticBlock implements ITE { return getDefaultState().with(CEILING, ctx.getFace() == Direction.DOWN); } - @Override - public boolean hasIntegratedCogwheel(IWorldReader world, BlockPos pos, BlockState state) { - return true; - } - @Override public VoxelShape getShape(BlockState state, IBlockReader p_220053_2_, BlockPos p_220053_3_, ISelectionContext p_220053_4_) {