Skip to content

Commit

Permalink
Merge pull request #22 from The-Fireplace-Minecraft-Mods/fix/improve-…
Browse files Browse the repository at this point in the history
…blocks-placement

Fix: Improve blocks placement
  • Loading branch information
Daniel-Mendes authored Jan 3, 2024
2 parents 677118f + 82d23aa commit db9ecd7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.the_fireplace.caterpillar.block;

import dev.the_fireplace.caterpillar.block.entity.CollectorBlockEntity;
import dev.the_fireplace.caterpillar.block.util.CaterpillarBlockUtil;
import dev.the_fireplace.caterpillar.block.util.*;
import dev.the_fireplace.caterpillar.registry.BlockEntityRegistry;
import dev.the_fireplace.caterpillar.registry.BlockRegistry;
import net.minecraft.world.level.block.state.StateDefinition;
Expand Down Expand Up @@ -97,10 +97,10 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {
Direction direction = context.getHorizontalDirection();

if (
pos.getY() < level.getMaxBuildHeight() - 1 &&
level.getBlockState(pos.above()).canBeReplaced(context)
pos.getY() < level.getMaxBuildHeight() - 1 &&
level.getBlockState(pos.above()).canBeReplaced(context)
) {
BlockPos caterpillarHeadPos = CaterpillarBlockUtil.getCaterpillarHeadPos(level, pos.above().relative(direction), direction);
BlockPos caterpillarHeadPos = CaterpillarBlockUtil.getCaterpillarHeadPos(level, pos.relative(direction), direction);

if (CaterpillarBlockUtil.getConnectedCaterpillarBlockEntities(level, caterpillarHeadPos, new ArrayList<>()).stream().noneMatch(blockEntity -> blockEntity instanceof CollectorBlockEntity)) {
if (CaterpillarBlockUtil.isConnectedCaterpillarSameDirection(level, pos.above(), direction)) {
Expand All @@ -124,9 +124,17 @@ public BlockPos getBasePos(BlockState state, BlockPos pos) {

@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity livingEntity, @NotNull ItemStack stack) {
level.setBlockAndUpdate(pos.above(), state.setValue(CollectorBlock.HALF, DoubleBlockHalf.UPPER).setValue(WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
Direction direction = state.getValue(FACING);
BlockState blockStateInFront = level.getBlockState(pos.relative(direction));
Block blockInFront = blockStateInFront.getBlock();

super.setPlacedBy(level, pos, state, livingEntity, stack);
if (!CaterpillarBlockUtil.isCaterpillarBlock(blockInFront)) {
level.setBlockAndUpdate(pos.above(), state.setValue(CollectorBlock.HALF, DoubleBlockHalf.UPPER).setValue(WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos, state.setValue(CollectorBlock.HALF, DoubleBlockHalf.LOWER).setValue(WATERLOGGED, level.getFluidState(pos).getType() == Fluids.WATER));
} else {
level.setBlockAndUpdate(pos, state.setValue(CollectorBlock.HALF, DoubleBlockHalf.UPPER).setValue(WATERLOGGED, level.getFluidState(pos).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.below(), state.setValue(CollectorBlock.HALF, DoubleBlockHalf.LOWER).setValue(WATERLOGGED, level.getFluidState(pos.below()).getType() == Fluids.WATER));
}
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,18 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {
FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos());

if (
blockPos.getY() < level.getMaxBuildHeight() - 1 &&
level.getBlockState(blockPos.relative(direction.getClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.relative(direction.getCounterClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above().relative(direction.getClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above().relative(direction.getCounterClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above()).canBeReplaced(context) &&
level.getBlockState(blockPos.above(2)).canBeReplaced(context) &&
level.getBlockState(blockPos.above(2).relative(direction.getClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above(2).relative(direction.getCounterClockWise())).canBeReplaced(context)
blockPos.getY() < level.getMaxBuildHeight() - 1 &&
level.getBlockState(blockPos.relative(direction.getClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.relative(direction.getCounterClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above().relative(direction.getClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above().relative(direction.getCounterClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above()).canBeReplaced(context) &&
level.getBlockState(blockPos.above().relative(direction.getOpposite())).canBeReplaced(context) &&
level.getBlockState(blockPos.above(2)).canBeReplaced(context) &&
level.getBlockState(blockPos.above(2).relative(direction.getClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above(2).relative(direction.getCounterClockWise())).canBeReplaced(context)
) {
BlockPos caterpillarHeadPos = CaterpillarBlockUtil.getCaterpillarHeadPos(level, blockPos.above().relative(direction), direction);
BlockPos caterpillarHeadPos = CaterpillarBlockUtil.getCaterpillarHeadPos(level, blockPos.relative(direction), direction);

if (CaterpillarBlockUtil.getConnectedCaterpillarBlockEntities(level, caterpillarHeadPos, new ArrayList<>()).stream().noneMatch(blockEntity -> blockEntity instanceof DrillHeadBlockEntity)) {
if (CaterpillarBlockUtil.isConnectedCaterpillarSameDirection(level, blockPos.above(), direction)) {
Expand All @@ -218,8 +219,6 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(@NotNull Level lev
@Override
public void setPlacedBy(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state, @Nullable LivingEntity livingEntity, @NotNull ItemStack stack) {
buildStructure(level, pos, state);

super.setPlacedBy(level, pos, state, livingEntity, stack);
}

@Override
Expand Down Expand Up @@ -263,16 +262,32 @@ public static void removeStructure(Level level, BlockPos pos, BlockState state)

public static void buildStructure(Level level, BlockPos pos, BlockState state) {
Direction direction = state.getValue(FACING);

level.setBlockAndUpdate(pos.relative(direction.getCounterClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_BOTTOM_LEFT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.relative(direction.getClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_BOTTOM_RIGHT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above().relative(direction.getCounterClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_LEFT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above().relative(direction.getClockWise()), state.setValue(PART, DrillHeadPart.BIT_RIGHT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(2), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_TOP).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above(2)).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_MIDDLE).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(2).relative(direction.getCounterClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_TOP_LEFT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above(2).relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(2).relative(direction.getClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_TOP_RIGHT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above(2).relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above().relative(direction.getOpposite()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BASE).setValue(WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
BlockState blockStateInFront = level.getBlockState(pos.relative(direction));
Block blockInFront = blockStateInFront.getBlock();

if (!CaterpillarBlockUtil.isCaterpillarBlock(blockInFront)) {
level.setBlockAndUpdate(pos.relative(direction.getCounterClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_BOTTOM_LEFT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.relative(direction.getClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_BOTTOM_RIGHT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos, state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_BOTTOM).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above().relative(direction.getCounterClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_LEFT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above().relative(direction.getClockWise()), state.setValue(PART, DrillHeadPart.BIT_RIGHT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(2), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_TOP).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above(2)).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_MIDDLE).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(2).relative(direction.getCounterClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_TOP_LEFT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above(2).relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(2).relative(direction.getClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_TOP_RIGHT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above(2).relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above().relative(direction.getOpposite()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BASE).setValue(WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
} else {
level.setBlockAndUpdate(pos.below().relative(direction.getCounterClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_BOTTOM_LEFT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.below().relative(direction.getClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_BOTTOM_RIGHT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.below(), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_BOTTOM).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.relative(direction.getCounterClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_LEFT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.relative(direction.getClockWise()), state.setValue(PART, DrillHeadPart.BIT_RIGHT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_TOP).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above(2)).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos, state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_MIDDLE).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above().relative(direction.getCounterClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_TOP_LEFT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above(2).relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above().relative(direction.getClockWise()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BIT_TOP_RIGHT).setValue(DrillHeadBlock.WATERLOGGED, level.getFluidState(pos.above(2).relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.relative(direction.getOpposite()), state.setValue(DrillHeadBlock.PART, DrillHeadPart.BASE).setValue(WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
}
}

public static void moveStructure(Level level, BlockPos pos, BlockState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {
FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos());

if (
blockPos.getY() < level.getMaxBuildHeight() - 1 &&
level.getBlockState(blockPos.above().relative(direction.getClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above().relative(direction.getCounterClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above()).canBeReplaced(context) &&
level.getBlockState(blockPos.above(2)).canBeReplaced(context)
blockPos.getY() < level.getMaxBuildHeight() - 1 &&
level.getBlockState(blockPos.above().relative(direction.getClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above().relative(direction.getCounterClockWise())).canBeReplaced(context) &&
level.getBlockState(blockPos.above()).canBeReplaced(context) &&
level.getBlockState(blockPos.above(2)).canBeReplaced(context)
) {
BlockPos caterpillarHeadPos = CaterpillarBlockUtil.getCaterpillarHeadPos(level, blockPos.above().relative(direction), direction);
BlockPos caterpillarHeadPos = CaterpillarBlockUtil.getCaterpillarHeadPos(level, blockPos.relative(direction), direction);

if (CaterpillarBlockUtil.getConnectedCaterpillarBlockEntities(level, caterpillarHeadPos, new ArrayList<>()).stream().noneMatch(blockEntity -> blockEntity instanceof ReinforcementBlockEntity)) {
if (CaterpillarBlockUtil.isConnectedCaterpillarSameDirection(level, blockPos.above(), direction)) {
Expand All @@ -151,13 +151,22 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity livingEntity, @NotNull ItemStack stack) {
Direction direction = state.getValue(FACING);

level.setBlockAndUpdate(pos.above().relative(direction.getCounterClockWise()), state.setValue(PART, ReinforcementPart.LEFT).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above().relative(direction.getClockWise()), state.setValue(ReinforcementBlock.PART, ReinforcementPart.RIGHT).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(), state.setValue(ReinforcementBlock.PART, ReinforcementPart.BASE).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(2), state.setValue(ReinforcementBlock.PART, ReinforcementPart.TOP).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above(2)).getType() == Fluids.WATER));

super.setPlacedBy(level, pos, state, livingEntity, stack);
BlockState blockStateInFront = level.getBlockState(pos.relative(direction));
Block blockInFront = blockStateInFront.getBlock();

if (!CaterpillarBlockUtil.isCaterpillarBlock(blockInFront)) {
level.setBlockAndUpdate(pos.above().relative(direction.getCounterClockWise()), state.setValue(PART, ReinforcementPart.LEFT).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above().relative(direction.getClockWise()), state.setValue(ReinforcementBlock.PART, ReinforcementPart.RIGHT).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(), state.setValue(ReinforcementBlock.PART, ReinforcementPart.BASE).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(2), state.setValue(ReinforcementBlock.PART, ReinforcementPart.TOP).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above(2)).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos, state.setValue(ReinforcementBlock.PART, ReinforcementPart.BOTTOM).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos).getType() == Fluids.WATER));
} else {
level.setBlockAndUpdate(pos.relative(direction.getCounterClockWise()), state.setValue(PART, ReinforcementPart.LEFT).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getCounterClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.relative(direction.getClockWise()), state.setValue(ReinforcementBlock.PART, ReinforcementPart.RIGHT).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above().relative(direction.getClockWise())).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos, state.setValue(ReinforcementBlock.PART, ReinforcementPart.BASE).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above()).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.above(), state.setValue(ReinforcementBlock.PART, ReinforcementPart.TOP).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos.above(2)).getType() == Fluids.WATER));
level.setBlockAndUpdate(pos.below(), state.setValue(ReinforcementBlock.PART, ReinforcementPart.BOTTOM).setValue(ReinforcementBlock.WATERLOGGED, level.getFluidState(pos).getType() == Fluids.WATER));
}
}

@Override
Expand Down
Loading

0 comments on commit db9ecd7

Please sign in to comment.