|
| 1 | +package net.roboxgamer.modernutils.block.custom; |
| 2 | + |
| 3 | +import net.minecraft.core.BlockPos; |
| 4 | +import net.minecraft.core.Direction; |
| 5 | +import net.minecraft.server.level.ServerPlayer; |
| 6 | +import net.minecraft.world.InteractionHand; |
| 7 | +import net.minecraft.world.InteractionResult; |
| 8 | +import net.minecraft.world.entity.player.Player; |
| 9 | +import net.minecraft.world.item.context.BlockPlaceContext; |
| 10 | +import net.minecraft.world.level.Level; |
| 11 | +import net.minecraft.world.level.block.Block; |
| 12 | +import net.minecraft.world.level.block.EntityBlock; |
| 13 | +import net.minecraft.world.level.block.RenderShape; |
| 14 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 15 | +import net.minecraft.world.level.block.entity.BlockEntityTicker; |
| 16 | +import net.minecraft.world.level.block.entity.BlockEntityType; |
| 17 | +import net.minecraft.world.level.block.state.BlockState; |
| 18 | +import net.minecraft.world.level.block.state.StateDefinition; |
| 19 | +import net.minecraft.world.level.block.state.properties.BlockStateProperties; |
| 20 | +import net.minecraft.world.level.block.state.properties.BooleanProperty; |
| 21 | +import net.minecraft.world.level.block.state.properties.DirectionProperty; |
| 22 | +import net.minecraft.world.phys.BlockHitResult; |
| 23 | +import org.jetbrains.annotations.NotNull; |
| 24 | +import org.jetbrains.annotations.Nullable; |
| 25 | + |
| 26 | +import net.roboxgamer.modernutils.block.entity.custom.MechanicalFurnaceBlockEntity; |
| 27 | + |
| 28 | +public class MechanicalFurnaceBlock extends Block implements EntityBlock { |
| 29 | + public static final BooleanProperty LIT = BlockStateProperties.LIT; |
| 30 | + public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING; |
| 31 | + |
| 32 | + public MechanicalFurnaceBlock(Properties properties) { |
| 33 | + super(properties); |
| 34 | + this.registerDefaultState(this.stateDefinition.any() |
| 35 | + .setValue(LIT, Boolean.FALSE) |
| 36 | + .setValue(FACING, Direction.NORTH)); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { |
| 41 | + builder.add(LIT); |
| 42 | + builder.add(FACING); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public @Nullable BlockState getStateForPlacement(BlockPlaceContext context) { |
| 47 | + return this.defaultBlockState() |
| 48 | + .setValue(FACING, context.getHorizontalDirection().getOpposite()); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public RenderShape getRenderShape(BlockState state) { |
| 53 | + return RenderShape.MODEL; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean isMoving) { |
| 58 | + if (state.getBlock() != newState.getBlock()) { |
| 59 | + BlockEntity blockEntity = level.getBlockEntity(pos); |
| 60 | + if (blockEntity instanceof MechanicalFurnaceBlockEntity) { |
| 61 | + ((MechanicalFurnaceBlockEntity) blockEntity).drops(); |
| 62 | + } |
| 63 | + } |
| 64 | + super.onRemove(state, level, pos, newState, isMoving); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected @NotNull InteractionResult useWithoutItem(@NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos, |
| 69 | + @NotNull Player player, @NotNull BlockHitResult hitResult) { |
| 70 | + if (level.isClientSide) { |
| 71 | + return InteractionResult.SUCCESS; |
| 72 | + } |
| 73 | + var hand = player.getUsedItemHand(); |
| 74 | + if (hand != InteractionHand.MAIN_HAND) return InteractionResult.PASS; |
| 75 | + |
| 76 | + BlockEntity be = level.getBlockEntity(pos); |
| 77 | + if (!(be instanceof MechanicalFurnaceBlockEntity blockEntity)) return InteractionResult.PASS; |
| 78 | + |
| 79 | + if (!level.isClientSide() && player instanceof ServerPlayer serverPlayer) { |
| 80 | + serverPlayer.openMenu(blockEntity, pos); |
| 81 | + } |
| 82 | + return InteractionResult.CONSUME; |
| 83 | + } |
| 84 | + |
| 85 | + @Nullable |
| 86 | + @Override |
| 87 | + public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { |
| 88 | + return new MechanicalFurnaceBlockEntity(pos, state); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public @Nullable <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, @NotNull BlockState blockState, @NotNull BlockEntityType<T> blockEntityType) { |
| 93 | + return level.isClientSide ? null : ((level1, pos, state, blockEntity) -> ((MechanicalFurnaceBlockEntity) blockEntity).tick(level1, pos, state,((MechanicalFurnaceBlockEntity) blockEntity))); |
| 94 | + } |
| 95 | +} |
0 commit comments