|
|
|
|
@ -0,0 +1,110 @@
|
|
|
|
|
package dev.micle.wildflowers_backport.block;
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
|
import com.mojang.serialization.MapCodec;
|
|
|
|
|
import dev.micle.wildflowers_backport.util.BlockBehaviourUtils;
|
|
|
|
|
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.ItemStack;
|
|
|
|
|
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.*;
|
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
|
|
|
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
|
|
|
|
import net.minecraft.world.level.block.state.properties.EnumProperty;
|
|
|
|
|
import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
|
|
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
|
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.ParametersAreNonnullByDefault;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
|
|
public class FlowerBedBlock extends BushBlock implements BonemealableBlock, SegmentableBlock {
|
|
|
|
|
public static final MapCodec<FlowerBedBlock> CODEC = BlockBehaviourUtils.simpleCodec(FlowerBedBlock::new);
|
|
|
|
|
public static final EnumProperty<Direction> FACING = BlockStateProperties.HORIZONTAL_FACING;
|
|
|
|
|
public static final IntegerProperty AMOUNT = BlockStateProperties.FLOWER_AMOUNT;
|
|
|
|
|
private final Function<BlockState, VoxelShape> shapes;
|
|
|
|
|
|
|
|
|
|
public FlowerBedBlock(Properties pProperties) {
|
|
|
|
|
super(pProperties);
|
|
|
|
|
this.registerDefaultState(this.getStateDefinition().any().setValue(FACING, Direction.NORTH).setValue(AMOUNT, 1));
|
|
|
|
|
this.shapes = this.makeShapes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Function<BlockState, VoxelShape> makeShapes() {
|
|
|
|
|
return this.stateDefinition.getPossibleStates().stream().collect(ImmutableMap.toImmutableMap(Function.identity(), this.getShapeCalculator(FACING, AMOUNT)))::get;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public BlockState rotate(BlockState blockState, LevelAccessor levelAccessor, BlockPos blockPos, Rotation rotation) {
|
|
|
|
|
return blockState.setValue(FACING, rotation.rotate(blockState.getValue(FACING)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public @NotNull BlockState mirror(BlockState blockState, Mirror mirror) {
|
|
|
|
|
return blockState.rotate(mirror.getRotation(blockState.getValue(FACING)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ParametersAreNonnullByDefault
|
|
|
|
|
public boolean canBeReplaced(BlockState blockState, BlockPlaceContext blockPlaceContext) {
|
|
|
|
|
return this.canBeReplaced(blockState, blockPlaceContext, AMOUNT) ? true : super.canBeReplaced(blockState, blockPlaceContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ParametersAreNonnullByDefault
|
|
|
|
|
public @NotNull VoxelShape getShape(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos, CollisionContext collisionContext) {
|
|
|
|
|
return this.shapes.apply(blockState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public double getShapeHeight() {
|
|
|
|
|
return 3.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IntegerProperty getSegmentAmountProperty() {
|
|
|
|
|
return AMOUNT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ParametersAreNonnullByDefault
|
|
|
|
|
public BlockState getStateForPlacement(BlockPlaceContext blockPlaceContext) {
|
|
|
|
|
return this.getStateForPlacement(blockPlaceContext, this, AMOUNT, FACING);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
|
|
|
builder.add(FACING, AMOUNT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ParametersAreNonnullByDefault
|
|
|
|
|
public boolean isValidBonemealTarget(LevelReader levelReader, BlockPos blockPos, BlockState blockState, boolean isClient) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ParametersAreNonnullByDefault
|
|
|
|
|
public boolean isBonemealSuccess(Level level, RandomSource randomSource, BlockPos blockPos, BlockState blockState) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ParametersAreNonnullByDefault
|
|
|
|
|
public void performBonemeal(ServerLevel serverLevel, RandomSource randomSource, BlockPos blockPos, BlockState blockState) {
|
|
|
|
|
int i = blockState.getValue(AMOUNT);
|
|
|
|
|
if (i < 4) {
|
|
|
|
|
serverLevel.setBlock(blockPos, blockState.setValue(AMOUNT, i + 1), 2);
|
|
|
|
|
} else {
|
|
|
|
|
popResource(serverLevel, blockPos, new ItemStack(this));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|