Skip to content

Instantly share code, notes, and snippets.

@MasterMank
Created June 10, 2023 15:07
Show Gist options
  • Save MasterMank/74cdf8c6b1d4c1b44241b68ab369c465 to your computer and use it in GitHub Desktop.
Save MasterMank/74cdf8c6b1d4c1b44241b68ab369c465 to your computer and use it in GitHub Desktop.
package net.mastermank.corridorsbackrooms.block.custom;
import net.mastermank.corridorsbackrooms.block.entity.WoodenCrateBlockEntity;
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.Container;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
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.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
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.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
public class WoodenCrateBlock extends BaseEntityBlock {
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
public static final BooleanProperty OPEN = BlockStateProperties.OPEN;
public WoodenCrateBlock(Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(OPEN, Boolean.valueOf(false)));
}
private static final VoxelShape SHAPE =
Block.box(0, 0, 0, 16, 16, 16);
@Override
public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pResult) {
if (pLevel.isClientSide) {
return InteractionResult.SUCCESS;
} else {
BlockEntity blockentity = pLevel.getBlockEntity(pPos);
if (blockentity instanceof WoodenCrateBlockEntity) {
pPlayer.openMenu((WoodenCrateBlockEntity) blockentity);
}
return InteractionResult.CONSUME;
}
}
@Override
public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState p_49079_, boolean p_49080_) {
if (!pState.is(p_49079_.getBlock())) {
BlockEntity blockentity = pLevel.getBlockEntity(pPos);
if (blockentity instanceof Container) {
Containers.dropContents(pLevel, pPos, (Container) blockentity);
}
super.onRemove(pState, pLevel, pPos, p_49079_, p_49080_);
}
}
@Override
public void tick(BlockState p_220758_, ServerLevel p_220759_, BlockPos p_220760_, RandomSource p_220761_) {
BlockEntity blockentity = p_220759_.getBlockEntity(p_220760_);
if (blockentity instanceof WoodenCrateBlockEntity) {
((WoodenCrateBlockEntity) blockentity).recheckOpen();
}
}
@Override
public void setPlacedBy(Level p_49052_, BlockPos p_49053_, BlockState p_49054_, @Nullable LivingEntity p_49055_, ItemStack p_49056_) {
if (p_49056_.hasCustomHoverName()) {
BlockEntity blockentity = p_49052_.getBlockEntity(p_49053_);
if (blockentity instanceof WoodenCrateBlockEntity) {
((WoodenCrateBlockEntity) blockentity).setCustomName(p_49056_.getHoverName());
}
}
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new WoodenCrateBlockEntity(pos, state);
}
@Override
public VoxelShape getShape(BlockState p_60555_, BlockGetter p_60556_, BlockPos p_60557_, CollisionContext p_60558_) {
return SHAPE;
}
@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
return this.defaultBlockState().setValue(FACING, pContext.getHorizontalDirection().getOpposite());
}
@Override
public BlockState rotate(BlockState pState, Rotation pRotation) {
return pState.setValue(FACING, pRotation.rotate(pState.getValue(FACING)));
}
@Override
public BlockState mirror(BlockState pState, Mirror pMirror) {
return pState.rotate(pMirror.getRotation(pState.getValue(FACING)));
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING, OPEN);
}
@Override
public RenderShape getRenderShape(BlockState pState) {
return RenderShape.MODEL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment