| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.function.Function;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.stats.Stats;
|
| 10 | import net.minecraft.world.Containers;
|
| 11 | import net.minecraft.world.InteractionResult;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 14 | import net.minecraft.world.entity.player.Player;
|
| 15 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 16 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 17 | import net.minecraft.world.level.BlockGetter;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 20 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 21 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 22 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 23 | import net.minecraft.world.level.block.entity.HopperBlockEntity;
|
| 24 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 25 | import net.minecraft.world.level.block.state.BlockState;
|
| 26 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 27 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 28 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 29 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 30 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 31 | import net.minecraft.world.level.redstone.Orientation;
|
| 32 | import net.minecraft.world.phys.BlockHitResult;
|
| 33 | import net.minecraft.world.phys.Vec3;
|
| 34 | import net.minecraft.world.phys.shapes.BooleanOp;
|
| 35 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 36 | import net.minecraft.world.phys.shapes.Shapes;
|
| 37 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 38 | import org.jspecify.annotations.Nullable;
|
| 39 |
|
| 40 | public class HopperBlock extends BaseEntityBlock {
|
| 41 | public static final EnumProperty<Direction> FACING = BlockStateProperties.FACING_HOPPER;
|
| 42 | public static final BooleanProperty ENABLED = BlockStateProperties.ENABLED;
|
| 43 | private final Function<BlockState, VoxelShape> shapes;
|
| 44 | private final Map<Direction, VoxelShape> interactionShapes;
|
| 45 |
|
| 46 | public HopperBlock(final BlockBehaviour.Properties properties) {
|
| 47 | super(properties);
|
| 48 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.DOWN).setValue(ENABLED, true));
|
| 49 | VoxelShape inside = Block.column(12.0, 11.0, 16.0);
|
| 50 | this.shapes = this.makeShapes(inside);
|
| 51 | this.interactionShapes = ImmutableMap.<Direction, VoxelShape>builderWithExpectedSize(5)
|
| 52 | .putAll(Shapes.rotateHorizontal(Shapes.or(inside, Block.boxZ(4.0, 8.0, 10.0, 0.0, 4.0))))
|
| 53 | .put(Direction.DOWN, inside)
|
| 54 | .build();
|
| 55 | }
|
| 56 |
|
| 57 | private Function<BlockState, VoxelShape> makeShapes(final VoxelShape inside) {
|
| 58 | VoxelShape spoutlessHopperOutline = Shapes.or(Block.column(16.0, 10.0, 16.0), Block.column(8.0, 4.0, 10.0));
|
| 59 | VoxelShape spoutlessHopper = Shapes.join(spoutlessHopperOutline, inside, BooleanOp.ONLY_FIRST);
|
| 60 | Map<Direction, VoxelShape> spouts = Shapes.rotateAll(Block.boxZ(4.0, 4.0, 8.0, 0.0, 8.0), new Vec3(8.0, 6.0, 8.0).scale(0.0625));
|
| 61 | return this.getShapeForEachState(state -> Shapes.or(spoutlessHopper, Shapes.join(spouts.get(state.getValue(FACING)), Shapes.block(), BooleanOp.AND)), ENABLED);
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 66 | return this.shapes.apply(state);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected VoxelShape getInteractionShape(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 71 | return this.interactionShapes.get(state.getValue(FACING));
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 76 | Direction direction = context.getClickedFace().getOpposite();
|
| 77 | return this.defaultBlockState().setValue(FACING, direction.getAxis() == Direction.Axis.Y ? Direction.DOWN : direction).setValue(ENABLED, true);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 82 | return new HopperBlockEntity(worldPosition, blockState);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 87 | return level.isClientSide() ? null : createTickerHelper(type, BlockEntityTypes.HOPPER, HopperBlockEntity::pushItemsTick);
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 92 | if (!oldState.is(state.getBlock())) {
|
| 93 | this.checkPoweredState(level, pos, state);
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 99 | if (!level.isClientSide() && level.getBlockEntity(pos) instanceof HopperBlockEntity hopper) {
|
| 100 | player.openMenu(hopper);
|
| 101 | player.awardStat(Stats.INSPECT_HOPPER);
|
| 102 | }
|
| 103 |
|
| 104 | return InteractionResult.SUCCESS;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected void neighborChanged(
|
| 109 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 110 | ) {
|
| 111 | this.checkPoweredState(level, pos, state);
|
| 112 | }
|
| 113 |
|
| 114 | private void checkPoweredState(final Level level, final BlockPos pos, final BlockState state) {
|
| 115 | boolean shouldBeOn = !level.hasNeighborSignal(pos);
|
| 116 | if (shouldBeOn != state.getValue(ENABLED)) {
|
| 117 | level.setBlock(pos, state.setValue(ENABLED, shouldBeOn), 2);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 123 | Containers.updateNeighboursAfterDestroy(state, level, pos);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 128 | return true;
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 133 | return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(level.getBlockEntity(pos));
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 138 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 143 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 148 | builder.add(FACING, ENABLED);
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | protected void entityInside(
|
| 153 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 154 | ) {
|
| 155 | if (level.getBlockEntity(pos) instanceof HopperBlockEntity hopperBlockEntity) {
|
| 156 | HopperBlockEntity.entityInside(level, pos, state, entity, hopperBlockEntity);
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 162 | return false;
|
| 163 | }
|
| 164 | }
|
| 165 |
|