|
| 1 | +package su.terrafirmagreg.core.common.data.blocks; |
| 2 | + |
| 3 | +import net.dries007.tfc.common.blocks.ExtendedBlock; |
| 4 | +import net.dries007.tfc.common.blocks.ExtendedProperties; |
| 5 | +import net.dries007.tfc.common.fluids.FluidProperty; |
| 6 | +import net.dries007.tfc.common.fluids.IFluidLoggable; |
| 7 | +import net.minecraft.core.BlockPos; |
| 8 | +import net.minecraft.core.Direction; |
| 9 | +import net.minecraft.world.entity.player.Player; |
| 10 | +import net.minecraft.world.item.context.BlockPlaceContext; |
| 11 | +import net.minecraft.world.level.BlockGetter; |
| 12 | +import net.minecraft.world.level.Level; |
| 13 | +import net.minecraft.world.level.LevelReader; |
| 14 | +import net.minecraft.world.level.block.Block; |
| 15 | +import net.minecraft.world.level.block.Blocks; |
| 16 | +import net.minecraft.world.level.block.state.BlockState; |
| 17 | +import net.minecraft.world.level.block.state.StateDefinition; |
| 18 | +import net.minecraft.world.level.block.state.properties.BlockStateProperties; |
| 19 | +import net.minecraft.world.level.block.state.properties.IntegerProperty; |
| 20 | +import net.minecraft.world.level.material.FluidState; |
| 21 | +import net.minecraft.world.level.material.Fluids; |
| 22 | +import net.minecraft.world.phys.shapes.CollisionContext; |
| 23 | +import net.minecraft.world.phys.shapes.VoxelShape; |
| 24 | +import su.terrafirmagreg.core.compat.kjs.TFGBlockProperties; |
| 25 | + |
| 26 | +public class TallDecorativePlantBlock extends ExtendedBlock implements IFluidLoggable { |
| 27 | + |
| 28 | + public static final FluidProperty FLUID = TFGBlockProperties.SPACE_WATER; |
| 29 | + public static final VoxelShape DEFAULT_SHAPE = Block.box(2.0F, 0.0F, 2.0F, 14.0F, 16.0F, 14.0F); |
| 30 | + public static final IntegerProperty HEIGHT = TFGBlockProperties.HEIGHT; |
| 31 | + private final VoxelShape shape; |
| 32 | + private final int maxHeight; |
| 33 | + |
| 34 | + public TallDecorativePlantBlock(ExtendedProperties properties, VoxelShape shape, int maxHeight) { |
| 35 | + super(properties); |
| 36 | + this.shape = shape; |
| 37 | + this.maxHeight = maxHeight; |
| 38 | + |
| 39 | + this.registerDefaultState(this.stateDefinition.any() |
| 40 | + .setValue(TFGBlockProperties.HEIGHT, 0) |
| 41 | + .setValue(getFluidProperty(), getFluidProperty().keyFor(Fluids.EMPTY))); |
| 42 | + |
| 43 | + getExtendedProperties().offsetType(OffsetType.XZ); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { |
| 48 | + super.createBlockStateDefinition(builder); |
| 49 | + builder.add(TFGBlockProperties.HEIGHT); |
| 50 | + builder.add(getFluidProperty()); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public BlockState getStateForPlacement(BlockPlaceContext context) { |
| 55 | + BlockPos pos = context.getClickedPos(); |
| 56 | + FluidState fluidState = context.getLevel().getFluidState(pos); |
| 57 | + |
| 58 | + BlockState state = super.getStateForPlacement(context); |
| 59 | + if (getFluidProperty().canContain(fluidState.getType())) |
| 60 | + { |
| 61 | + state = state.setValue(getFluidProperty(), getFluidProperty().keyForOrEmpty(fluidState.getType())); |
| 62 | + } |
| 63 | + return state; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public FluidProperty getFluidProperty() { |
| 68 | + return FLUID; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { |
| 73 | + return shape != null ? shape : super.getShape(state, level, pos, context); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) { |
| 78 | + int h = state.getValue(TFGBlockProperties.HEIGHT); |
| 79 | + |
| 80 | + if (h == 0) { |
| 81 | + BlockPos below = pos.below(); |
| 82 | + BlockState belowState = level.getBlockState(below); |
| 83 | + return belowState.isFaceSturdy(level, below, Direction.UP); |
| 84 | + } else { |
| 85 | + BlockState belowState = level.getBlockState(pos.below()); |
| 86 | + return belowState.is(this) && belowState.getValue(TFGBlockProperties.HEIGHT) - 1 == h; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean isMoving) { |
| 92 | + if (state.getValue(TFGBlockProperties.HEIGHT) != 0) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + BlockPos test = pos.above(); |
| 97 | + for (int i = 0; i < maxHeight; i++) { |
| 98 | + if (!level.getBlockState(test).canBeReplaced()) { |
| 99 | + return; |
| 100 | + } |
| 101 | + test = test.above(); |
| 102 | + } |
| 103 | + |
| 104 | + test = pos.above(); |
| 105 | + for (int i = 1; i < maxHeight; i++) { |
| 106 | + level.setBlock(test, this.defaultBlockState().setValue(TFGBlockProperties.HEIGHT, i), 3); |
| 107 | + test = test.above(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public boolean onDestroyedByPlayer(BlockState state, Level level, BlockPos pos, Player player, boolean willHarvest, FluidState fluid) { |
| 113 | + playerWillDestroy(level, pos, state, player); |
| 114 | + |
| 115 | + BlockPos toDestroyPos = pos.below(); |
| 116 | + BlockState nextState = level.getBlockState(toDestroyPos); |
| 117 | + |
| 118 | + while (nextState.is(this)) { |
| 119 | + level.destroyBlock(toDestroyPos, false, player); |
| 120 | + toDestroyPos = toDestroyPos.below(); |
| 121 | + nextState = level.getBlockState(toDestroyPos); |
| 122 | + } |
| 123 | + |
| 124 | + toDestroyPos = pos.above(); |
| 125 | + nextState = level.getBlockState(toDestroyPos); |
| 126 | + |
| 127 | + while (nextState.is(this)) { |
| 128 | + level.destroyBlock(toDestroyPos, false, player); |
| 129 | + toDestroyPos = toDestroyPos.above(); |
| 130 | + nextState = level.getBlockState(toDestroyPos); |
| 131 | + } |
| 132 | + |
| 133 | + return level.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState()); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + @SuppressWarnings("deprecation") |
| 138 | + public FluidState getFluidState(BlockState state) { |
| 139 | + return IFluidLoggable.super.getFluidLoggedState(state); |
| 140 | + } |
| 141 | +} |
0 commit comments