Skip to content

Instantly share code, notes, and snippets.

@MasterMank
Created March 14, 2023 00:49
Show Gist options
  • Save MasterMank/e2ee11315eccb653760095101fdb2a1a to your computer and use it in GitHub Desktop.
Save MasterMank/e2ee11315eccb653760095101fdb2a1a 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.world.Container;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
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 BooleanProperty OPEN = BooleanProperty.create("isopen");
public static final BooleanProperty CLOSED = BooleanProperty.create("isclosed");
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
public WoodenCrateBlock(Properties properties) {
super(properties);
}
private static final VoxelShape SHAPE =
Block.box(0, 0, 0, 16, 16, 16);
@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);
}
@Override
public RenderShape getRenderShape(BlockState pState) {
return RenderShape.MODEL;
}
@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 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.sidedSuccess(pLevel.isClientSide());
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new WoodenCrateBlockEntity(pos, state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment