--- a/net/minecraft/world/item/MinecartItem.java
+++ b/net/minecraft/world/item/MinecartItem.java
@@ -5,6 +5,7 @@
 import net.minecraft.core.Direction;
 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.vehicle.AbstractMinecart;
@@ -16,48 +17,86 @@
 import net.minecraft.world.level.block.state.properties.RailShape;
 import net.minecraft.world.level.gameevent.GameEvent;
 
+// CraftBukkit start
+import org.bukkit.craftbukkit.v1_19_R1.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.getBlockState().getValue(DispenserBlock.FACING);
-            Level level = pointer.getLevel();
-            double d = pointer.x() + (double)direction.getStepX() * 1.125D;
-            double e = Math.floor(pointer.y()) + (double)direction.getStepY();
-            double f = pointer.z() + (double)direction.getStepZ() * 1.125D;
-            BlockPos blockPos = pointer.getPos().relative(direction);
-            BlockState blockState = level.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.6D;
+            Direction enumdirection = (Direction) pointer.getBlockState().getValue(DispenserBlock.FACING);
+            ServerLevel worldserver = pointer.getLevel();
+            double d0 = pointer.x() + (double) enumdirection.getStepX() * 1.125D;
+            double d1 = Math.floor(pointer.y()) + (double) enumdirection.getStepY();
+            double d2 = pointer.z() + (double) enumdirection.getStepZ() * 1.125D;
+            BlockPos blockposition = pointer.getPos().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.1D;
+                    d3 = 0.1D;
                 }
             } else {
-                if (!blockState.isAir() || !level.getBlockState(blockPos.below()).is(BlockTags.RAILS)) {
+                if (!iblockdata.isAir() || !worldserver.getBlockState(blockposition.below()).is(BlockTags.RAILS)) {
                     return this.defaultDispenseItemBehavior.dispense(pointer, stack);
                 }
 
-                BlockState blockState2 = level.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.4D;
+                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.9D;
+                    d3 = -0.9D;
                 }
             }
 
-            AbstractMinecart abstractMinecart = AbstractMinecart.createMinecart(level, d, e + g, f, ((MinecartItem)stack.getItem()).type);
+            // CraftBukkit start
+            // EntityMinecartAbstract entityminecartabstract = EntityMinecartAbstract.createMinecart(worldserver, d0, d1 + d3, d2, ((ItemMinecart) itemstack.getItem()).type);
+            ItemStack itemstack1 = stack.split(1);
+            org.bukkit.block.Block block2 = worldserver.getWorld().getBlockAt(pointer.getPos().getX(), pointer.getPos().getY(), pointer.getPos().getZ());
+            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);
+                return stack;
+            }
+
+            if (!event.getItem().equals(craftItem)) {
+                stack.grow(1);
+                // 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);
+
             if (stack.hasCustomHoverName()) {
-                abstractMinecart.setCustomName(stack.getHoverName());
+                entityminecartabstract.setCustomName(stack.getHoverName());
             }
 
-            level.addFreshEntity(abstractMinecart);
-            stack.shrink(1);
+            if (!worldserver.addFreshEntity(entityminecartabstract)) stack.grow(1);
+            // itemstack.shrink(1); // CraftBukkit - handled during event processing
+            // CraftBukkit end
             return stack;
         }
 
@@ -71,36 +110,45 @@
     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.isClientSide) {
-                RailShape railShape = blockState.getBlock() instanceof BaseRailBlock ? blockState.getValue(((BaseRailBlock)blockState.getBlock()).getShapeProperty()) : RailShape.NORTH_SOUTH;
-                double d = 0.0D;
-                if (railShape.isAscending()) {
-                    d = 0.5D;
+            ItemStack itemstack = context.getItemInHand();
+
+            if (!world.isClientSide) {
+                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(level, (double)blockPos.getX() + 0.5D, (double)blockPos.getY() + 0.0625D + d, (double)blockPos.getZ() + 0.5D, this.type);
-                if (itemStack.hasCustomHoverName()) {
-                    abstractMinecart.setCustomName(itemStack.getHoverName());
+                AbstractMinecart entityminecartabstract = AbstractMinecart.createMinecart(world, (double) blockposition.getX() + 0.5D, (double) blockposition.getY() + 0.0625D + d0, (double) blockposition.getZ() + 0.5D, this.type);
+
+                if (itemstack.hasCustomHoverName()) {
+                    entityminecartabstract.setCustomName(itemstack.getHoverName());
                 }
 
-                level.addFreshEntity(abstractMinecart);
-                level.gameEvent(GameEvent.ENTITY_PLACE, blockPos, GameEvent.Context.of(context.getPlayer(), level.getBlockState(blockPos.below())));
+                // CraftBukkit start
+                if (org.bukkit.craftbukkit.v1_19_R1.event.CraftEventFactory.callEntityPlaceEvent(context, entityminecartabstract).isCancelled()) {
+                    return InteractionResult.FAIL;
+                }
+                // CraftBukkit end
+                if (!world.addFreshEntity(entityminecartabstract)) return InteractionResult.PASS; // CraftBukkit
+                world.gameEvent(GameEvent.ENTITY_PLACE, blockposition, GameEvent.Context.of(context.getPlayer(), world.getBlockState(blockposition.below())));
             }
 
-            itemStack.shrink(1);
-            return InteractionResult.sidedSuccess(level.isClientSide);
+            itemstack.shrink(1);
+            return InteractionResult.sidedSuccess(world.isClientSide);
         }
     }
 }
