--- a/net/minecraft/world/item/MinecartItem.java
+++ b/net/minecraft/world/item/MinecartItem.java
@@ -2,12 +2,14 @@
 
 import net.minecraft.core.BlockPos;
 import net.minecraft.core.Direction;
+import net.minecraft.core.Holder;
 import net.minecraft.core.dispenser.BlockSource;
 import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
 import net.minecraft.core.dispenser.DispenseItemBehavior;
 import net.minecraft.server.level.ServerLevel;
 import net.minecraft.tags.BlockTags;
 import net.minecraft.world.InteractionResult;
+import net.minecraft.world.entity.player.Player;
 import net.minecraft.world.entity.vehicle.AbstractMinecart;
 import net.minecraft.world.item.context.UseOnContext;
 import net.minecraft.world.level.Level;
@@ -17,50 +19,84 @@
 import net.minecraft.world.level.block.state.properties.RailShape;
 import net.minecraft.world.level.gameevent.GameEvent;
 import net.minecraft.world.phys.Vec3;
+// CraftBukkit start
+import org.bukkit.craftbukkit.block.CraftBlock;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.event.block.BlockDispenseEvent;
+// CraftBukkit end
 
 public class MinecartItem extends Item {
+
     private static final DispenseItemBehavior DISPENSE_ITEM_BEHAVIOR = new DefaultDispenseItemBehavior() {
         private final DefaultDispenseItemBehavior defaultDispenseItemBehavior = new DefaultDispenseItemBehavior();
 
         @Override
         public ItemStack execute(BlockSource pointer, ItemStack stack) {
-            Direction direction = pointer.state().getValue(DispenserBlock.FACING);
-            ServerLevel serverLevel = pointer.level();
-            Vec3 vec3 = pointer.center();
-            double d = vec3.x() + (double)direction.getStepX() * 1.125;
-            double e = Math.floor(vec3.y()) + (double)direction.getStepY();
-            double f = vec3.z() + (double)direction.getStepZ() * 1.125;
-            BlockPos blockPos = pointer.pos().relative(direction);
-            BlockState blockState = serverLevel.getBlockState(blockPos);
-            RailShape railShape = blockState.getBlock() instanceof BaseRailBlock
-                ? blockState.getValue(((BaseRailBlock)blockState.getBlock()).getShapeProperty())
-                : RailShape.NORTH_SOUTH;
-            double g;
-            if (blockState.is(BlockTags.RAILS)) {
-                if (railShape.isAscending()) {
-                    g = 0.6;
+            Direction enumdirection = (Direction) pointer.state().getValue(DispenserBlock.FACING);
+            ServerLevel worldserver = pointer.level();
+            Vec3 vec3d = pointer.center();
+            double d0 = vec3d.x() + (double) enumdirection.getStepX() * 1.125D;
+            double d1 = Math.floor(vec3d.y()) + (double) enumdirection.getStepY();
+            double d2 = vec3d.z() + (double) enumdirection.getStepZ() * 1.125D;
+            BlockPos blockposition = pointer.pos().relative(enumdirection);
+            BlockState iblockdata = worldserver.getBlockState(blockposition);
+            RailShape blockpropertytrackposition = iblockdata.getBlock() instanceof BaseRailBlock ? (RailShape) iblockdata.getValue(((BaseRailBlock) iblockdata.getBlock()).getShapeProperty()) : RailShape.NORTH_SOUTH;
+            double d3;
+
+            if (iblockdata.is(BlockTags.RAILS)) {
+                if (blockpropertytrackposition.isAscending()) {
+                    d3 = 0.6D;
                 } else {
-                    g = 0.1;
+                    d3 = 0.1D;
                 }
             } else {
-                if (!blockState.isAir() || !serverLevel.getBlockState(blockPos.below()).is(BlockTags.RAILS)) {
+                if (!iblockdata.isAir() || !worldserver.getBlockState(blockposition.below()).is(BlockTags.RAILS)) {
                     return this.defaultDispenseItemBehavior.dispense(pointer, stack);
                 }
 
-                BlockState blockState2 = serverLevel.getBlockState(blockPos.below());
-                RailShape railShape2 = blockState2.getBlock() instanceof BaseRailBlock
-                    ? blockState2.getValue(((BaseRailBlock)blockState2.getBlock()).getShapeProperty())
-                    : RailShape.NORTH_SOUTH;
-                if (direction != Direction.DOWN && railShape2.isAscending()) {
-                    g = -0.4;
+                BlockState iblockdata1 = worldserver.getBlockState(blockposition.below());
+                RailShape blockpropertytrackposition1 = iblockdata1.getBlock() instanceof BaseRailBlock ? (RailShape) iblockdata1.getValue(((BaseRailBlock) iblockdata1.getBlock()).getShapeProperty()) : RailShape.NORTH_SOUTH;
+
+                if (enumdirection != Direction.DOWN && blockpropertytrackposition1.isAscending()) {
+                    d3 = -0.4D;
                 } else {
-                    g = -0.9;
+                    d3 = -0.9D;
                 }
             }
 
-            AbstractMinecart abstractMinecart = AbstractMinecart.createMinecart(serverLevel, d, e + g, f, ((MinecartItem)stack.getItem()).type, stack, null);
-            serverLevel.addFreshEntity(abstractMinecart);
-            stack.shrink(1);
+            // CraftBukkit start
+            // EntityMinecartAbstract entityminecartabstract = EntityMinecartAbstract.createMinecart(worldserver, d0, d1 + d3, d2, ((ItemMinecart) itemstack.getItem()).type);
+            ItemStack itemstack1 = stack.copyWithCount(1); // Paper - shrink below and single item in event
+            org.bukkit.block.Block block2 = CraftBlock.at(worldserver, pointer.pos());
+            CraftItemStack craftItem = CraftItemStack.asCraftMirror(itemstack1);
+
+            BlockDispenseEvent event = new BlockDispenseEvent(block2, craftItem.clone(), new org.bukkit.util.Vector(d0, d1 + d3, d2));
+            if (!DispenserBlock.eventFired) {
+                worldserver.getCraftServer().getPluginManager().callEvent(event);
+            }
+
+            if (event.isCancelled()) {
+                // stack.grow(1); // Paper - shrink below
+                return stack;
+            }
+
+            boolean shrink = true; // Paper
+            if (!event.getItem().equals(craftItem)) {
+                shrink = false; // Paper - shrink below
+                // Chain to handler for new item
+                ItemStack eventStack = CraftItemStack.asNMSCopy(event.getItem());
+                DispenseItemBehavior idispensebehavior = (DispenseItemBehavior) DispenserBlock.DISPENSER_REGISTRY.get(eventStack.getItem());
+                if (idispensebehavior != DispenseItemBehavior.NOOP && idispensebehavior != this) {
+                    idispensebehavior.dispense(pointer, eventStack);
+                    return stack;
+                }
+            }
+
+            itemstack1 = CraftItemStack.asNMSCopy(event.getItem());
+            AbstractMinecart entityminecartabstract = AbstractMinecart.createMinecart(worldserver, event.getVelocity().getX(), event.getVelocity().getY(), event.getVelocity().getZ(), ((MinecartItem) itemstack1.getItem()).type, itemstack1, (Player) null);
+
+            if (worldserver.addFreshEntity(entityminecartabstract) && shrink) stack.shrink(1); // Paper - actually handle here
+            // CraftBukkit end
             return stack;
         }
 
@@ -74,42 +110,43 @@
     public MinecartItem(AbstractMinecart.Type type, Item.Properties settings) {
         super(settings);
         this.type = type;
-        DispenserBlock.registerBehavior(this, DISPENSE_ITEM_BEHAVIOR);
+        DispenserBlock.registerBehavior(this, MinecartItem.DISPENSE_ITEM_BEHAVIOR);
     }
 
     @Override
     public InteractionResult useOn(UseOnContext context) {
-        Level level = context.getLevel();
-        BlockPos blockPos = context.getClickedPos();
-        BlockState blockState = level.getBlockState(blockPos);
-        if (!blockState.is(BlockTags.RAILS)) {
+        Level world = context.getLevel();
+        BlockPos blockposition = context.getClickedPos();
+        BlockState iblockdata = world.getBlockState(blockposition);
+
+        if (!iblockdata.is(BlockTags.RAILS)) {
             return InteractionResult.FAIL;
         } else {
-            ItemStack itemStack = context.getItemInHand();
-            if (level instanceof ServerLevel serverLevel) {
-                RailShape railShape = blockState.getBlock() instanceof BaseRailBlock
-                    ? blockState.getValue(((BaseRailBlock)blockState.getBlock()).getShapeProperty())
-                    : RailShape.NORTH_SOUTH;
-                double d = 0.0;
-                if (railShape.isAscending()) {
-                    d = 0.5;
+            ItemStack itemstack = context.getItemInHand();
+
+            if (world instanceof ServerLevel) {
+                ServerLevel worldserver = (ServerLevel) world;
+                RailShape blockpropertytrackposition = iblockdata.getBlock() instanceof BaseRailBlock ? (RailShape) iblockdata.getValue(((BaseRailBlock) iblockdata.getBlock()).getShapeProperty()) : RailShape.NORTH_SOUTH;
+                double d0 = 0.0D;
+
+                if (blockpropertytrackposition.isAscending()) {
+                    d0 = 0.5D;
                 }
 
-                AbstractMinecart abstractMinecart = AbstractMinecart.createMinecart(
-                    serverLevel,
-                    (double)blockPos.getX() + 0.5,
-                    (double)blockPos.getY() + 0.0625 + d,
-                    (double)blockPos.getZ() + 0.5,
-                    this.type,
-                    itemStack,
-                    context.getPlayer()
-                );
-                serverLevel.addFreshEntity(abstractMinecart);
-                serverLevel.gameEvent(GameEvent.ENTITY_PLACE, blockPos, GameEvent.Context.of(context.getPlayer(), serverLevel.getBlockState(blockPos.below())));
+                AbstractMinecart entityminecartabstract = AbstractMinecart.createMinecart(worldserver, (double) blockposition.getX() + 0.5D, (double) blockposition.getY() + 0.0625D + d0, (double) blockposition.getZ() + 0.5D, this.type, itemstack, context.getPlayer());
+
+                // CraftBukkit start
+                if (org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPlaceEvent(context, entityminecartabstract).isCancelled()) {
+                    if (context.getPlayer() != null) context.getPlayer().containerMenu.sendAllDataToRemote(); // Paper - Fix inventory desync
+                    return InteractionResult.FAIL;
+                }
+                // CraftBukkit end
+                if (!worldserver.addFreshEntity(entityminecartabstract)) return InteractionResult.PASS; // CraftBukkit
+                worldserver.gameEvent((Holder) GameEvent.ENTITY_PLACE, blockposition, GameEvent.Context.of(context.getPlayer(), worldserver.getBlockState(blockposition.below())));
             }
 
-            itemStack.shrink(1);
-            return InteractionResult.sidedSuccess(level.isClientSide);
+            itemstack.shrink(1);
+            return InteractionResult.sidedSuccess(world.isClientSide);
         }
     }
 }
