minecraft-src/net/minecraft/world/level/block/HopperBlock.java
2025-07-04 03:45:38 +03:00

171 lines
6.9 KiB
Java

package net.minecraft.world.level.block;
import com.google.common.collect.ImmutableMap;
import com.mojang.serialization.MapCodec;
import java.util.Map;
import java.util.function.Function;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.stats.Stats;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.InsideBlockEffectApplier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
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.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.entity.HopperBlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition.Builder;
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.EnumProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.redstone.Orientation;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
public class HopperBlock extends BaseEntityBlock {
public static final MapCodec<HopperBlock> CODEC = simpleCodec(HopperBlock::new);
public static final EnumProperty<Direction> FACING = BlockStateProperties.FACING_HOPPER;
public static final BooleanProperty ENABLED = BlockStateProperties.ENABLED;
private final Function<BlockState, VoxelShape> shapes;
private final Map<Direction, VoxelShape> interactionShapes;
@Override
public MapCodec<HopperBlock> codec() {
return CODEC;
}
public HopperBlock(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.DOWN).setValue(ENABLED, true));
VoxelShape voxelShape = Block.column(12.0, 11.0, 16.0);
this.shapes = this.makeShapes(voxelShape);
this.interactionShapes = ImmutableMap.<Direction, VoxelShape>builderWithExpectedSize(5)
.putAll(Shapes.rotateHorizontal(Shapes.or(voxelShape, Block.boxZ(4.0, 8.0, 10.0, 0.0, 4.0))))
.put(Direction.DOWN, voxelShape)
.build();
}
private Function<BlockState, VoxelShape> makeShapes(VoxelShape shape) {
VoxelShape voxelShape = Shapes.or(Block.column(16.0, 10.0, 16.0), Block.column(8.0, 4.0, 10.0));
VoxelShape voxelShape2 = Shapes.join(voxelShape, shape, BooleanOp.ONLY_FIRST);
Map<Direction, VoxelShape> map = 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));
return this.getShapeForEachState(
blockState -> Shapes.or(voxelShape2, Shapes.join((VoxelShape)map.get(blockState.getValue(FACING)), Shapes.block(), BooleanOp.AND)), new Property[]{ENABLED}
);
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return (VoxelShape)this.shapes.apply(state);
}
@Override
protected VoxelShape getInteractionShape(BlockState state, BlockGetter level, BlockPos pos) {
return (VoxelShape)this.interactionShapes.get(state.getValue(FACING));
}
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
Direction direction = context.getClickedFace().getOpposite();
return this.defaultBlockState().setValue(FACING, direction.getAxis() == Direction.Axis.Y ? Direction.DOWN : direction).setValue(ENABLED, true);
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new HopperBlockEntity(pos, state);
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> blockEntityType) {
return level.isClientSide ? null : createTickerHelper(blockEntityType, BlockEntityType.HOPPER, HopperBlockEntity::pushItemsTick);
}
@Override
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
if (!oldState.is(state.getBlock())) {
this.checkPoweredState(level, pos, state);
}
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
if (!level.isClientSide && level.getBlockEntity(pos) instanceof HopperBlockEntity hopperBlockEntity) {
player.openMenu(hopperBlockEntity);
player.awardStat(Stats.INSPECT_HOPPER);
}
return InteractionResult.SUCCESS;
}
@Override
protected void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, @Nullable Orientation orientation, boolean movedByPiston) {
this.checkPoweredState(level, pos, state);
}
private void checkPoweredState(Level level, BlockPos pos, BlockState state) {
boolean bl = !level.hasNeighborSignal(pos);
if (bl != (Boolean)state.getValue(ENABLED)) {
level.setBlock(pos, state.setValue(ENABLED, bl), 2);
}
}
@Override
protected void affectNeighborsAfterRemoval(BlockState state, ServerLevel level, BlockPos pos, boolean movedByPiston) {
Containers.updateNeighboursAfterDestroy(state, level, pos);
}
@Override
protected boolean hasAnalogOutputSignal(BlockState state) {
return true;
}
@Override
protected int getAnalogOutputSignal(BlockState state, Level level, BlockPos pos) {
return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(level.getBlockEntity(pos));
}
@Override
protected BlockState rotate(BlockState state, Rotation rotation) {
return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
}
@Override
protected BlockState mirror(BlockState state, Mirror mirror) {
return state.rotate(mirror.getRotation(state.getValue(FACING)));
}
@Override
protected void createBlockStateDefinition(Builder<Block, BlockState> builder) {
builder.add(FACING, ENABLED);
}
@Override
protected void entityInside(BlockState state, Level level, BlockPos pos, Entity entity, InsideBlockEffectApplier effectApplier) {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof HopperBlockEntity) {
HopperBlockEntity.entityInside(level, pos, state, entity, (HopperBlockEntity)blockEntity);
}
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}
}