Skip to content

Commit

Permalink
make papyrus stem+umbel destruct nicely if stem is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
bazke committed Nov 6, 2024
1 parent c4cc997 commit e93ca4c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/main/java/com/lovetropics/extras/block/PapyrusStemBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.lovetropics.extras.ExtraBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.RandomSource;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
Expand All @@ -28,8 +31,6 @@ public final class PapyrusStemBlock extends Block implements BonemealableBlock {


public static final EnumProperty<Type> TYPE = EnumProperty.create("type", Type.class);
public static final BooleanProperty GROWING = BooleanProperty.create("growing");
private static final Set<Block> GROWS_ON = Set.of(Blocks.GRASS_BLOCK, Blocks.DIRT);
private static final VoxelShape SHAPE = Block.box(7.0, 0.0, 7.0, 9.0, 16.0, 9.0);
protected static final int MAX_HEIGHT = 4;
private static final int MIN_HEIGHT_FOR_FLOWERS = 2;
Expand Down Expand Up @@ -116,17 +117,27 @@ public void performBonemeal(final ServerLevel serverLevel, final RandomSource ra
protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
final BlockState stateBelow = level.getBlockState(pos.below());

return stateBelow.is(BlockTags.DIRT) || state.is(this);
}

return canGrowOn(stateBelow);
@Override
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
if (!state.canSurvive(level, pos)) {
level.scheduleTick(pos, this, 1);
}
return super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(TYPE);
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (!state.canSurvive(level, pos)) {
level.destroyBlock(pos, true);
}
}

private boolean canGrowOn(BlockState state) {
return GROWS_ON.contains(state.getBlock()) || state.is(ExtraBlocks.PAPYRUS_STEM);
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(TYPE);
}

public enum Type implements StringRepresentable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.lovetropics.extras.ExtraBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
Expand Down Expand Up @@ -38,7 +41,7 @@ protected void randomTick(final BlockState state, final ServerLevel level, final
}

//10% chance to age
if (state.getValue(AGEING) && random.nextInt(10) == 0) {
if (random.nextInt(10) == 0) {
PapyrusStemBlock.Type type = state.getValue(TYPE);
if (type == PapyrusStemBlock.Type.PLAIN) {
ageSelfAndStem(PapyrusStemBlock.Type.DRY, level, pos);
Expand Down Expand Up @@ -69,6 +72,28 @@ public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, Co
return SHAPE;
}

@Override
protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
final BlockState stateBelow = level.getBlockState(pos.below());

return stateBelow.is(ExtraBlocks.PAPYRUS_STEM.get());
}

@Override
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
if (!state.canSurvive(level, pos)) {
level.scheduleTick(pos, this, 1);
}
return super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}

@Override
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (!state.canSurvive(level, pos)) {
level.destroyBlock(pos, true);
}
}

@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockState blockstate = context.getLevel().getBlockState(context.getClickedPos().below());
Expand Down

0 comments on commit e93ca4c

Please sign in to comment.