content: package com.accbdd.complicated_bees.block;
  
  import com.accbdd.complicated_bees.block.entity.BeeSorterBlockEntity;
  import com.accbdd.complicated_bees.screen.BeeSorterMenu;
  import net.minecraft.core.BlockPos;
  import net.minecraft.network.chat.Component;
  import net.minecraft.server.level.ServerPlayer;
  import net.minecraft.world.Containers;
  import net.minecraft.world.InteractionHand;
  import net.minecraft.world.InteractionResult;
  import net.minecraft.world.SimpleMenuProvider;
  import net.minecraft.world.entity.player.Player;
  import net.minecraft.world.item.context.BlockPlaceContext;
  import net.minecraft.world.level.Level;
  import net.minecraft.world.level.block.BaseEntityBlock;
  import net.minecraft.world.level.block.Block;
  import net.minecraft.world.level.block.RenderShape;
  import net.minecraft.world.level.block.SoundType;
  import net.minecraft.world.level.block.entity.BlockEntity;
  import net.minecraft.world.level.block.entity.BlockEntityTicker;
  import net.minecraft.world.level.block.entity.BlockEntityType;
  import net.minecraft.world.level.block.state.BlockBehaviour;
  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.phys.BlockHitResult;
  import net.minecraftforge.items.IItemHandler;
  import net.minecraftforge.network.NetworkHooks;
  import org.jetbrains.annotations.Nullable;
  
  public class BeeSorterBlock extends BaseEntityBlock {
      public static final BooleanProperty ENABLED = BlockStateProperties.ENABLED;
      public static final String SCREEN_BEE_SORTER = "gui.complicated_bees.bee_sorter";
  
      public BeeSorterBlock() {
          super(BlockBehaviour.Properties.of()
                  .strength(3.5F)
                  .sound(SoundType.WOOD));
      }
  
      @Nullable
      @Override
      public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
          return new BeeSorterBlockEntity(pPos, pState);
      }
  
      @Override
      public RenderShape getRenderShape(BlockState pState) {
          return RenderShape.MODEL;
      }
  
      @Override
      public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
          if (!pLevel.isClientSide) {
              BlockEntity be = pLevel.getBlockEntity(pPos);
              if (be instanceof BeeSorterBlockEntity) {
                  NetworkHooks.openScreen((ServerPlayer) pPlayer,
                          new SimpleMenuProvider((id, inv, player) -> new BeeSorterMenu(id, inv, pPos), Component.translatable(SCREEN_BEE_SORTER)),
                          buf -> {
                              buf.writeBlockPos(pPos);
                              buf.writeByteArray(((BeeSorterBlockEntity) be).getTypeFilters());
                          });
              } else {
                  throw new IllegalStateException("Our named container provider is missing!");
              }
          }
          return InteractionResult.SUCCESS;
      }
  
      @Override
      public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pMovedByPiston) {
          BlockEntity blockentity = pLevel.getBlockEntity(pPos);
          if (blockentity instanceof BeeSorterBlockEntity sorter && !pNewState.is(this)) {
              IItemHandler handler = sorter.getItem();
              for (int i = 0; i < handler.getSlots(); i++) {
                  Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY(), pPos.getZ(), handler.getStackInSlot(i));
              }
          }
          super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston);
      }
  
      @Nullable
      @Override
      public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
          if (!pLevel.isClientSide) {
              return (lvl, pos, st, be) -> {
                  if (be instanceof BeeSorterBlockEntity sorter) {
                      sorter.serverTick();
                  }
              };
          }
          return null;
      }
  
      @Override
      public void neighborChanged(BlockState pState, Level pLevel, BlockPos pPos, Block pBlock, BlockPos pFromPos, boolean pIsMoving) {
          this.checkPoweredState(pLevel, pPos, pState, 4);
      }
  
      private void checkPoweredState(Level pLevel, BlockPos pPos, BlockState pState, int pFlags) {
          boolean flag = !pLevel.hasNeighborSignal(pPos);
          if (flag != pState.getValue(ENABLED)) {
              pLevel.setBlock(pPos, pState.setValue(ENABLED, flag), pFlags);
          }
      }
  
      @Override
      protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
          super.createBlockStateDefinition(pBuilder);
          pBuilder.add(ENABLED);
      }
  
      @Nullable
      @Override
      public BlockState getStateForPlacement(BlockPlaceContext pContext) {
          return this.defaultBlockState().setValue(ENABLED, true);
      }
  }
  