Cogwheel refactor Part II

- Refactored getStateForPlacement to reduce doubled code
This commit is contained in:
grimmauld 2021-03-27 23:05:59 +01:00
parent 4ff20b4217
commit 7b022cd302

View file

@ -5,18 +5,14 @@ import com.simibubi.create.AllShapes;
import com.simibubi.create.content.contraptions.base.IRotate; import com.simibubi.create.content.contraptions.base.IRotate;
import com.simibubi.create.content.contraptions.relays.advanced.SpeedControllerBlock; import com.simibubi.create.content.contraptions.relays.advanced.SpeedControllerBlock;
import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.Iterate;
import mcp.MethodsReturnNonnullByDefault;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.fluid.Fluids; import net.minecraft.fluid.Fluids;
import net.minecraft.fluid.IFluidState;
import net.minecraft.item.BlockItemUseContext; import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.state.properties.BlockStateProperties; import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.Direction.Axis; import net.minecraft.util.Direction.Axis;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.shapes.ISelectionContext; import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.util.math.shapes.VoxelShape;
@ -24,6 +20,11 @@ import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorldReader; import net.minecraft.world.IWorldReader;
import net.minecraft.world.World; import net.minecraft.world.World;
import javax.annotation.ParametersAreNonnullByDefault;
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@SuppressWarnings("deprecation")
public class CogWheelBlock extends AbstractShaftBlock implements ICogWheel { public class CogWheelBlock extends AbstractShaftBlock implements ICogWheel {
boolean isLarge; boolean isLarge;
@ -67,65 +68,48 @@ public class CogWheelBlock extends AbstractShaftBlock implements ICogWheel{
if (blockState.has(AXIS) && facing.getAxis() == blockState.get(AXIS)) if (blockState.has(AXIS) && facing.getAxis() == blockState.get(AXIS))
continue; continue;
boolean smallCog = ICogWheel.isSmallCog(blockState); if (ICogWheel.isLargeCog(blockState) || isLargeCog() && ICogWheel.isSmallCog(blockState))
if (ICogWheel.isLargeCog(blockState) || isLarge && smallCog)
return false; return false;
} }
return true; return true;
} }
protected Axis getAxisForPlacement(BlockItemUseContext context) {
if (context.getPlayer() != null && context.getPlayer().isSneaking())
return context.getFace().getAxis();
World world = context.getWorld();
BlockState stateBelow = world.getBlockState(context.getPos().down());
if (AllBlocks.ROTATION_SPEED_CONTROLLER.has(stateBelow) && isLargeCog())
return stateBelow.get(SpeedControllerBlock.HORIZONTAL_AXIS) == Axis.X ? Axis.Z : Axis.X;
BlockPos placedOnPos = context.getPos().offset(context.getFace().getOpposite());
BlockState placedAgainst = world.getBlockState(placedOnPos);
Block block = placedAgainst.getBlock();
if (ICogWheel.isSmallCog(placedAgainst))
return ((IRotate) block).getRotationAxis(placedAgainst);
Axis preferredAxis = getPreferredAxis(context);
return preferredAxis != null ? preferredAxis : context.getFace().getAxis();
}
@Override @Override
public BlockState getStateForPlacement(BlockItemUseContext context) { public BlockState getStateForPlacement(BlockItemUseContext context) {
BlockPos placedOnPos = context.getPos() boolean shouldWaterlog = context.getWorld().getFluidState(context.getPos()).getFluid() == Fluids.WATER;
.offset(context.getFace()
.getOpposite());
World world = context.getWorld();
BlockState placedAgainst = world.getBlockState(placedOnPos);
Block block = placedAgainst.getBlock();
if (context.getPlayer() != null && context.getPlayer()
.isSneaking())
return this.getDefaultState() return this.getDefaultState()
.with(AXIS, context.getFace() .with(AXIS, getAxisForPlacement(context))
.getAxis()); .with(BlockStateProperties.WATERLOGGED, shouldWaterlog);
BlockState stateBelow = world.getBlockState(context.getPos()
.down());
IFluidState ifluidstate = context.getWorld()
.getFluidState(context.getPos());
if (AllBlocks.ROTATION_SPEED_CONTROLLER.has(stateBelow) && isLarge) {
return this.getDefaultState()
.with(BlockStateProperties.WATERLOGGED, ifluidstate.getFluid() == Fluids.WATER)
.with(AXIS, stateBelow.get(SpeedControllerBlock.HORIZONTAL_AXIS) == Axis.X ? Axis.Z : Axis.X);
}
if (!ICogWheel.isSmallCog(placedAgainst)) {
Axis preferredAxis = getPreferredAxis(context);
if (preferredAxis != null)
return this.getDefaultState()
.with(AXIS, preferredAxis)
.with(BlockStateProperties.WATERLOGGED, ifluidstate.getFluid() == Fluids.WATER);
return this.getDefaultState()
.with(AXIS, context.getFace()
.getAxis())
.with(BlockStateProperties.WATERLOGGED, ifluidstate.getFluid() == Fluids.WATER);
}
return getDefaultState().with(AXIS, ((IRotate) block).getRotationAxis(placedAgainst));
} }
@Override @Override
public float getParticleTargetRadius() { public float getParticleTargetRadius() {
return isLarge ? 1.125f : .65f; return isLargeCog() ? 1.125f : .65f;
} }
@Override @Override
public float getParticleInitialRadius() { public float getParticleInitialRadius() {
return isLarge ? 1f : .75f; return isLargeCog() ? 1f : .75f;
}
public void fillItemGroup(ItemGroup group, NonNullList<ItemStack> items) {
items.add(new ItemStack(this));
} }
} }