| 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.BlockTags;
|
| 7 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 8 | import net.minecraft.world.entity.EntityType;
|
| 9 | import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.level.block.BaseRailBlock;
|
| 12 | import net.minecraft.world.level.block.DispenserBlock;
|
| 13 | import net.minecraft.world.level.block.state.BlockState;
|
| 14 | import net.minecraft.world.level.block.state.properties.RailShape;
|
| 15 | import net.minecraft.world.phys.Vec3;
|
| 16 |
|
| 17 | public class MinecartDispenseItemBehavior extends DefaultDispenseItemBehavior {
|
| 18 | private final DefaultDispenseItemBehavior defaultDispenseItemBehavior = new DefaultDispenseItemBehavior();
|
| 19 | private final EntityType<? extends AbstractMinecart> entityType;
|
| 20 |
|
| 21 | public MinecartDispenseItemBehavior(final EntityType<? extends AbstractMinecart> entityType) {
|
| 22 | this.entityType = entityType;
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public ItemStack execute(final BlockSource source, final ItemStack dispensed) {
|
| 27 | Direction direction = source.state().getValue(DispenserBlock.FACING);
|
| 28 | ServerLevel level = source.level();
|
| 29 | Vec3 center = source.center();
|
| 30 | double spawnX = center.x() + direction.getStepX() * 1.125;
|
| 31 | double spawnY = Math.floor(center.y()) + direction.getStepY();
|
| 32 | double spawnZ = center.z() + direction.getStepZ() * 1.125;
|
| 33 | BlockPos front = source.pos().relative(direction);
|
| 34 | BlockState blockFront = level.getBlockState(front);
|
| 35 | double yOffset;
|
| 36 | if (blockFront.is(BlockTags.RAILS)) {
|
| 37 | if (getRailShape(blockFront).isSlope()) {
|
| 38 | yOffset = 0.6;
|
| 39 | } else {
|
| 40 | yOffset = 0.1;
|
| 41 | }
|
| 42 | } else {
|
| 43 | if (!blockFront.isAir()) {
|
| 44 | return this.defaultDispenseItemBehavior.dispense(source, dispensed);
|
| 45 | }
|
| 46 |
|
| 47 | BlockState blockBelow = level.getBlockState(front.below());
|
| 48 | if (!blockBelow.is(BlockTags.RAILS)) {
|
| 49 | return this.defaultDispenseItemBehavior.dispense(source, dispensed);
|
| 50 | }
|
| 51 |
|
| 52 | if (direction != Direction.DOWN && getRailShape(blockBelow).isSlope()) {
|
| 53 | yOffset = -0.4;
|
| 54 | } else {
|
| 55 | yOffset = -0.9;
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | Vec3 spawnPos = new Vec3(spawnX, spawnY + yOffset, spawnZ);
|
| 60 | AbstractMinecart minecart = AbstractMinecart.createMinecart(
|
| 61 | level, spawnPos.x, spawnPos.y, spawnPos.z, this.entityType, EntitySpawnReason.DISPENSER, dispensed, null
|
| 62 | );
|
| 63 | if (minecart != null) {
|
| 64 | level.addFreshEntity(minecart);
|
| 65 | dispensed.shrink(1);
|
| 66 | }
|
| 67 |
|
| 68 | return dispensed;
|
| 69 | }
|
| 70 |
|
| 71 | private static RailShape getRailShape(final BlockState blockFront) {
|
| 72 | return blockFront.getBlock() instanceof BaseRailBlock railBlock ? blockFront.getValue(railBlock.getShapeProperty()) : RailShape.NORTH_SOUTH;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected void playSound(final BlockSource source) {
|
| 77 | source.level().levelEvent(1000, source.pos(), 0);
|
| 78 | }
|
| 79 | }
|
| 80 |
|