Files
ThisRocks/src/main/java/eu/midnightdust/motschen/rocks/block/Seashell.java
Martin Prokoph 8e2fc41b99 Official Polymer compatibility
- Based on my PolymerRocks compatibility patch, featuring many improvements over it
- Less hardcoded object instances, allowing for easier addition of new variations
2024-09-16 18:49:17 +02:00

83 lines
3.7 KiB
Java
Executable File

package eu.midnightdust.motschen.rocks.block;
import eu.midnightdust.motschen.rocks.RocksMain;
import eu.midnightdust.motschen.rocks.blockstates.RockVariation;
import eu.midnightdust.motschen.rocks.blockstates.SeashellVariation;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.*;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.WorldView;
import java.util.Objects;
public class Seashell extends Block implements Waterloggable {
private static final VoxelShape SHAPE;
private static final EnumProperty<SeashellVariation> SEASHELL_VARIATION = RocksMain.SEASHELL_VARIATION;
public static final BooleanProperty WATERLOGGED = Properties.WATERLOGGED;
public Seashell() {
super(AbstractBlock.Settings.copy(Blocks.POPPY).nonOpaque().dynamicBounds().sounds(BlockSoundGroup.STONE));
this.setDefaultState(this.stateManager.getDefaultState().with(SEASHELL_VARIATION, SeashellVariation.PINK).with(WATERLOGGED, false));
}
@Override
public FluidState getFluidState(BlockState blockState_1) {
return blockState_1.get(WATERLOGGED) ? Fluids.WATER.getStill(true) : super.getFluidState(blockState_1);
}
@Override
public BlockState getPlacementState(ItemPlacementContext itemPlacementContext) {
FluidState fluidState = itemPlacementContext.getWorld().getFluidState(itemPlacementContext.getBlockPos());
return Objects.requireNonNull(super.getPlacementState(itemPlacementContext))
.with(SEASHELL_VARIATION, SeashellVariation.values()[itemPlacementContext.getWorld().random.nextBetween(0, 2)])
.with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER);
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
if (player.isCreative()) {
world.setBlockState(pos, state.with(SEASHELL_VARIATION, state.get(SEASHELL_VARIATION).next()));
return ActionResult.SUCCESS;
}
else return ActionResult.FAIL;
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(SEASHELL_VARIATION, WATERLOGGED);
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) {
return SHAPE;
}
static {
SHAPE = createCuboidShape(0, 0, 0, 16, 3, 16);
}
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
return world.getBlockState(pos.down()).isSideSolidFullSquare(world,pos,Direction.UP);
}
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
return !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, newState, world, pos, posFrom);
}
protected boolean canReplace(BlockState state, ItemPlacementContext context) {return true;}
}