Skip to content

Commit

Permalink
[BUG] Fix AdvancedTorches breaking when block underneath them changes -
Browse files Browse the repository at this point in the history
  • Loading branch information
hypherionmc committed Apr 2, 2023
1 parent 006ee6d commit cde200f
Showing 1 changed file with 51 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.AttachFace;
Expand Down Expand Up @@ -81,14 +80,62 @@ public AdvancedTorchBlock(String name, DyeColor color, CraterCreativeModeTab tab
}

@Override
public VoxelShape getShape(BlockState blockState, BlockGetter level, BlockPos pos, CollisionContext context) {
public @NotNull VoxelShape getShape(BlockState blockState, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull CollisionContext context) {
return switch (blockState.getValue(ATTACH_FACE)) {
case FLOOR -> SHAPES.get(Direction.UP);
case WALL -> SHAPES.get(blockState.getValue(FACING));
case CEILING -> null;
};
}

@Override
public @NotNull BlockState updateShape(@NotNull BlockState stateIn, @NotNull Direction facing, @NotNull BlockState neighbourState, @NotNull LevelAccessor levelIn, @NotNull BlockPos currentPos, @NotNull BlockPos newPos) {
AttachFace attachFace = stateIn.getValue(ATTACH_FACE);

if (attachFace == AttachFace.FLOOR) {
return facing == Direction.DOWN && !this.canSurvive(stateIn, levelIn, currentPos) ? Blocks.AIR.defaultBlockState() : super.updateShape(stateIn, facing, neighbourState, levelIn, currentPos, newPos);
}

return facing.getOpposite() == stateIn.getValue(FACING) && !stateIn.canSurvive(levelIn, currentPos) ? Blocks.AIR.defaultBlockState() : stateIn;
}

@Override
public boolean canSurvive(BlockState stateIn, @NotNull LevelReader levelIn, @NotNull BlockPos currentPos) {
AttachFace attachFace = stateIn.getValue(ATTACH_FACE);

if (attachFace == AttachFace.FLOOR) {
return canSupportCenter(levelIn, currentPos.below(), Direction.UP);
}

Direction direction = stateIn.getValue(FACING);
BlockPos neighbourPos = currentPos.relative(direction.getOpposite());
BlockState currentState = levelIn.getBlockState(neighbourPos);
return currentState.isFaceSturdy(levelIn, neighbourPos, direction);
}

@Override
public void animateTick(BlockState stateIn, @NotNull Level levelIn, @NotNull BlockPos pos, @NotNull RandomSource random) {
if (stateIn.getValue(LIT)) {
DyeColor color = stateIn.getValue(COLOR);

if (stateIn.getValue(ATTACH_FACE) == AttachFace.FLOOR) {
double d0 = (double) pos.getX() + 0.5D;
double d1 = (double) pos.getY() + 0.7D;
double d2 = (double) pos.getZ() + 0.5D;
levelIn.addParticle(ParticleTypes.SMOKE, d0, d1, d2, 0.0D, 0.0D, 0.0D);
levelIn.addParticle(FlameParticles.getParticleByColor(color).get(), d0, d1, d2, 0D, 0D, 0D);
} else {
Direction direction = stateIn.getValue(FACING);
double d0 = (double) pos.getX() + 0.5D;
double d1 = (double) pos.getY() + 0.7D;
double d2 = (double) pos.getZ() + 0.5D;
Direction direction1 = direction.getOpposite();
levelIn.addParticle(ParticleTypes.SMOKE, d0 + 0.37D * (double) direction1.getStepX(), d1 + 0.15D, d2 + 0.37D * (double) direction1.getStepZ(), 0.0D, 0.0D, 0.0D);
levelIn.addParticle(FlameParticles.getParticleByColor(color).get(), d0 + 0.37D * (double) direction1.getStepX(), d1 + 0.15D, d2 + 0.37D * (double) direction1.getStepZ(), 0D, 0D, 0D);
}
}
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(LIT, ATTACH_FACE, FACING, COLOR);
Expand All @@ -107,17 +154,6 @@ public BlockState getStateForPlacement(BlockPlaceContext context) {
return state.setValue(LIT, CommonRegistration.config.torchConfig.litByDefault);
}

@Override
public BlockState updateShape(BlockState stateIn, Direction facing, BlockState neighbourState, LevelAccessor levelIn, BlockPos currentPos, BlockPos newPos) {
if (facing == Direction.DOWN && !this.isValidPosition(stateIn, levelIn, currentPos, facing)) {
return Blocks.AIR.defaultBlockState();
}
return super.updateShape(stateIn, facing, neighbourState, levelIn, currentPos, newPos);
}

public boolean isValidPosition(BlockState state, LevelAccessor levelAccessor, BlockPos pos, Direction direction) {
return canSupportCenter(levelAccessor, pos, direction);
}

@Override
public void toggleLight(Level worldIn, BlockState state, BlockPos pos) {
Expand Down Expand Up @@ -185,29 +221,6 @@ public void appendHoverText(ItemStack stack, BlockGetter level, List<Component>
super.appendHoverText(stack, level, tooltip, options);
}

@Override
public void animateTick(BlockState stateIn, Level levelIn, BlockPos pos, RandomSource random) {
if (stateIn.getValue(LIT)) {
DyeColor color = stateIn.getValue(COLOR);

if (stateIn.getValue(ATTACH_FACE) == AttachFace.FLOOR) {
double d0 = (double) pos.getX() + 0.5D;
double d1 = (double) pos.getY() + 0.7D;
double d2 = (double) pos.getZ() + 0.5D;
levelIn.addParticle(ParticleTypes.SMOKE, d0, d1, d2, 0.0D, 0.0D, 0.0D);
levelIn.addParticle(FlameParticles.getParticleByColor(color).get(), d0, d1, d2, 0D, 0D, 0D);
} else {
Direction direction = stateIn.getValue(FACING);
double d0 = (double) pos.getX() + 0.5D;
double d1 = (double) pos.getY() + 0.7D;
double d2 = (double) pos.getZ() + 0.5D;
Direction direction1 = direction.getOpposite();
levelIn.addParticle(ParticleTypes.SMOKE, d0 + 0.37D * (double) direction1.getStepX(), d1 + 0.15D, d2 + 0.37D * (double) direction1.getStepZ(), 0.0D, 0.0D, 0.0D);
levelIn.addParticle(FlameParticles.getParticleByColor(color).get(), d0 + 0.37D * (double) direction1.getStepX(), d1 + 0.15D, d2 + 0.37D * (double) direction1.getStepZ(), 0D, 0D, 0D);
}
}
}

@Override
public @NotNull ItemStack getCloneItemStack(@NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull BlockState state) {
return StackUtil.getColorStack(this, state.getValue(COLOR));
Expand Down

0 comments on commit cde200f

Please sign in to comment.