| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.dispenser.BlockSource;
|
| 7 | import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
|
| 8 | import net.minecraft.core.dispenser.DispenseItemBehavior;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.world.Container;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 14 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 15 | import net.minecraft.world.level.block.entity.DispenserBlockEntity;
|
| 16 | import net.minecraft.world.level.block.entity.DropperBlockEntity;
|
| 17 | import net.minecraft.world.level.block.entity.HopperBlockEntity;
|
| 18 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import org.slf4j.Logger;
|
| 21 |
|
| 22 | public class DropperBlock extends DispenserBlock {
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | private static final DispenseItemBehavior DISPENSE_BEHAVIOUR = new DefaultDispenseItemBehavior();
|
| 25 |
|
| 26 | public DropperBlock(final BlockBehaviour.Properties properties) {
|
| 27 | super(properties);
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | protected DispenseItemBehavior getDispenseMethod(final Level level, final ItemStack itemStack) {
|
| 32 | return DISPENSE_BEHAVIOUR;
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 37 | return new DropperBlockEntity(worldPosition, blockState);
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected void dispenseFrom(final ServerLevel level, final BlockState state, final BlockPos pos) {
|
| 42 | DispenserBlockEntity blockEntity = level.getBlockEntity(pos, BlockEntityTypes.DROPPER).orElse(null);
|
| 43 | if (blockEntity == null) {
|
| 44 | LOGGER.warn("Ignoring dispensing attempt for Dropper without matching block entity at {}", pos);
|
| 45 | } else {
|
| 46 | BlockSource source = new BlockSource(level, pos, state, blockEntity);
|
| 47 | int slot = blockEntity.getRandomSlot(level.getRandom());
|
| 48 | if (slot < 0) {
|
| 49 | level.levelEvent(1001, pos, 0);
|
| 50 | } else {
|
| 51 | ItemStack itemStack = blockEntity.getItem(slot);
|
| 52 | if (!itemStack.isEmpty()) {
|
| 53 | Direction direction = level.getBlockState(pos).getValue(FACING);
|
| 54 | Container into = HopperBlockEntity.getContainerAt(level, pos.relative(direction));
|
| 55 | ItemStack remaining;
|
| 56 | if (into == null) {
|
| 57 | remaining = DISPENSE_BEHAVIOUR.dispense(source, itemStack);
|
| 58 | } else {
|
| 59 | remaining = HopperBlockEntity.addItem(blockEntity, into, itemStack.copyWithCount(1), direction.getOpposite());
|
| 60 | if (remaining.isEmpty()) {
|
| 61 | remaining = itemStack.copy();
|
| 62 | remaining.shrink(1);
|
| 63 | } else {
|
| 64 | remaining = itemStack.copy();
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | blockEntity.setItem(slot, remaining);
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|