Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ModBlock extends ContainerBlock {
- public static final DirectionProperty PROPERTY_FACING = BlockStateProperties.HORIZONTAL_FACING;
- public static final BooleanProperty PROPERTY_OPEN = BlockStateProperties.OPEN;
- public ModBlock () {
- super(Block.Properties.create(Material.WOOD).hardnessAndResistance(2.5F).sound(SoundType.WOOD));
- setDefaultState(stateContainer.getBaseState().with(PROPERTY_FACING, Direction.NORTH).with(PROPERTY_OPEN, false));
- }
- @Override
- public void tick(BlockState state, ServerWorld worldIn, BlockPos pos, Random rand) {
- TileEntity tileentity = worldIn.getTileEntity(pos);
- if (tileentity instanceof ModTileEntity) {
- ((ModTileEntity) tileentity).modTETick();
- }
- }
- @Override
- public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
- if (!worldIn.isRemote) {
- TileEntity tileentity = worldIn.getTileEntity(pos);
- if(tileentity instanceof ModTileEntity) {
- NetworkHooks.openGui((ServerPlayerEntity) player, ((INamedContainerProvider) tileentity), t -> t.writeBlockPos(pos));
- player.addStat(Stats.OPEN_BARREL);
- }
- }
- return ActionResultType.SUCCESS;
- }
- @Override
- public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
- if (state.getBlock() != newState.getBlock()) {
- TileEntity tileentity = worldIn.getTileEntity(pos);
- if (tileentity instanceof IInventory) {
- InventoryHelper.dropInventoryItems(worldIn, pos, (IInventory) tileentity);
- worldIn.updateComparatorOutputLevel(pos, this);
- }
- super.onReplaced(state, worldIn, pos, newState, isMoving);
- }
- }
- @Override
- public BlockRenderType getRenderType(BlockState state) {
- return BlockRenderType.MODEL;
- }
- @Override
- public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
- if (stack.hasDisplayName()) {
- TileEntity tileentity = worldIn.getTileEntity(pos);
- if (tileentity instanceof ModTileEntity) {
- ((ModTileEntity) tileentity).setCustomName(stack.getDisplayName());
- }
- }
- }
- @Override
- public boolean hasComparatorInputOverride(BlockState state) {
- return true;
- }
- @Override
- public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
- return Container.calcRedstone(worldIn.getTileEntity(pos));
- }
- @Override
- public BlockState rotate(BlockState state, Rotation rot) {
- return state.with(PROPERTY_FACING, rot.rotate(state.get(PROPERTY_FACING)));
- }
- @Override
- public BlockState mirror(BlockState state, Mirror mirrorIn) {
- return state.rotate(mirrorIn.toRotation(state.get(PROPERTY_FACING)));
- }
- @Override
- protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
- builder.add(PROPERTY_FACING, PROPERTY_OPEN);
- }
- @Override
- public BlockState getStateForPlacement(BlockItemUseContext context) {
- return getDefaultState().with(PROPERTY_FACING, context.getPlacementHorizontalFacing().getOpposite());
- }
- @Override
- public boolean hasTileEntity(BlockState state) {
- return true;
- }
- @Nullable
- @Override
- public ModTileEntity createNewTileEntity(IBlockReader worldIn) {
- return new ModTileEntity();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement