| 1 | package net.minecraft.core.dispenser;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.tags.FluidTags;
|
| 7 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 8 | import net.minecraft.world.entity.EntityType;
|
| 9 | import net.minecraft.world.entity.vehicle.boat.AbstractBoat;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.level.block.DispenserBlock;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 |
|
| 14 | public class BoatDispenseItemBehavior extends DefaultDispenseItemBehavior {
|
| 15 | private final DefaultDispenseItemBehavior defaultDispenseItemBehavior = new DefaultDispenseItemBehavior();
|
| 16 | private final EntityType<? extends AbstractBoat> type;
|
| 17 |
|
| 18 | public BoatDispenseItemBehavior(final EntityType<? extends AbstractBoat> type) {
|
| 19 | this.type = type;
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | public ItemStack execute(final BlockSource source, final ItemStack dispensed) {
|
| 24 | Direction direction = source.state().getValue(DispenserBlock.FACING);
|
| 25 | ServerLevel level = source.level();
|
| 26 | Vec3 center = source.center();
|
| 27 | double justOutsideDispenser = 0.5625 + this.type.getWidth() / 2.0;
|
| 28 | double spawnX = center.x() + direction.getStepX() * justOutsideDispenser;
|
| 29 | double spawnY = center.y() + direction.getStepY() * 1.125F;
|
| 30 | double spawnZ = center.z() + direction.getStepZ() * justOutsideDispenser;
|
| 31 | BlockPos frontPos = source.pos().relative(direction);
|
| 32 | double yOffset;
|
| 33 | if (level.getFluidState(frontPos).is(FluidTags.WATER)) {
|
| 34 | yOffset = 1.0;
|
| 35 | } else {
|
| 36 | if (!level.getBlockState(frontPos).isAir() || !level.getFluidState(frontPos.below()).is(FluidTags.WATER)) {
|
| 37 | return this.defaultDispenseItemBehavior.dispense(source, dispensed);
|
| 38 | }
|
| 39 |
|
| 40 | yOffset = 0.0;
|
| 41 | }
|
| 42 |
|
| 43 | AbstractBoat boat = this.type.create(level, EntitySpawnReason.DISPENSER);
|
| 44 | if (boat != null) {
|
| 45 | boat.setInitialPos(spawnX, spawnY + yOffset, spawnZ);
|
| 46 | EntityType.<AbstractBoat>createDefaultStackConfig(level, dispensed, null).apply(boat);
|
| 47 | boat.setYRot(direction.toYRot());
|
| 48 | level.addFreshEntity(boat);
|
| 49 | dispensed.shrink(1);
|
| 50 | }
|
| 51 |
|
| 52 | return dispensed;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected void playSound(final BlockSource source) {
|
| 57 | source.level().levelEvent(1000, source.pos(), 0);
|
| 58 | }
|
| 59 | }
|
| 60 |
|