| 1 | package net.minecraft.core.dispenser;
|
| 2 |
|
| 3 | import net.minecraft.core.Direction;
|
| 4 | import net.minecraft.core.Position;
|
| 5 | import net.minecraft.util.RandomSource;
|
| 6 | import net.minecraft.world.entity.item.ItemEntity;
|
| 7 | import net.minecraft.world.item.ItemStack;
|
| 8 | import net.minecraft.world.level.Level;
|
| 9 | import net.minecraft.world.level.block.DispenserBlock;
|
| 10 |
|
| 11 | public class DefaultDispenseItemBehavior implements DispenseItemBehavior {
|
| 12 | private static final int DEFAULT_ACCURACY = 6;
|
| 13 |
|
| 14 | @Override
|
| 15 | public final ItemStack dispense(final BlockSource source, final ItemStack dispensed) {
|
| 16 | ItemStack result = this.execute(source, dispensed);
|
| 17 | this.playSound(source);
|
| 18 | this.playAnimation(source, source.state().getValue(DispenserBlock.FACING));
|
| 19 | return result;
|
| 20 | }
|
| 21 |
|
| 22 | protected ItemStack execute(final BlockSource source, final ItemStack dispensed) {
|
| 23 | Direction direction = source.state().getValue(DispenserBlock.FACING);
|
| 24 | Position position = DispenserBlock.getDispensePosition(source);
|
| 25 | ItemStack itemStack = dispensed.split(1);
|
| 26 | spawnItem(source.level(), itemStack, 6, direction, position);
|
| 27 | return dispensed;
|
| 28 | }
|
| 29 |
|
| 30 | public static void spawnItem(final Level level, final ItemStack itemStack, final int accuracy, final Direction direction, final Position position) {
|
| 31 | double spawnX = position.x();
|
| 32 | double spawnY = position.y();
|
| 33 | double spawnZ = position.z();
|
| 34 | if (direction.getAxis() == Direction.Axis.Y) {
|
| 35 | spawnY -= 0.125;
|
| 36 | } else {
|
| 37 | spawnY -= 0.15625;
|
| 38 | }
|
| 39 |
|
| 40 | ItemEntity itemEntity = new ItemEntity(level, spawnX, spawnY, spawnZ, itemStack);
|
| 41 | RandomSource random = level.getRandom();
|
| 42 | double pow = random.nextDouble() * 0.1 + 0.2;
|
| 43 | itemEntity.setDeltaMovement(
|
| 44 | random.triangle(direction.getStepX() * pow, 0.0172275 * accuracy),
|
| 45 | random.triangle(0.2, 0.0172275 * accuracy),
|
| 46 | random.triangle(direction.getStepZ() * pow, 0.0172275 * accuracy)
|
| 47 | );
|
| 48 | level.addFreshEntity(itemEntity);
|
| 49 | }
|
| 50 |
|
| 51 | protected void playSound(final BlockSource source) {
|
| 52 | playDefaultSound(source);
|
| 53 | }
|
| 54 |
|
| 55 | protected void playAnimation(final BlockSource source, final Direction direction) {
|
| 56 | playDefaultAnimation(source, direction);
|
| 57 | }
|
| 58 |
|
| 59 | private static void playDefaultSound(final BlockSource source) {
|
| 60 | source.level().levelEvent(1000, source.pos(), 0);
|
| 61 | }
|
| 62 |
|
| 63 | private static void playDefaultAnimation(final BlockSource source, final Direction direction) {
|
| 64 | source.level().levelEvent(2000, source.pos(), direction.get3DDataValue());
|
| 65 | }
|
| 66 |
|
| 67 | protected ItemStack consumeWithRemainder(final BlockSource source, final ItemStack dispensed, final ItemStack remainder) {
|
| 68 | dispensed.shrink(1);
|
| 69 | if (dispensed.isEmpty()) {
|
| 70 | return remainder;
|
| 71 | }
|
| 72 |
|
| 73 | this.addToInventoryOrDispense(source, remainder);
|
| 74 | return dispensed;
|
| 75 | }
|
| 76 |
|
| 77 | private void addToInventoryOrDispense(final BlockSource source, final ItemStack itemStack) {
|
| 78 | ItemStack remainder = source.blockEntity().insertItem(itemStack);
|
| 79 | if (!remainder.isEmpty()) {
|
| 80 | Direction direction = source.state().getValue(DispenserBlock.FACING);
|
| 81 | spawnItem(source.level(), remainder, 6, direction, DispenserBlock.getDispensePosition(source));
|
| 82 | playDefaultSound(source);
|
| 83 | playDefaultAnimation(source, direction);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|