--- a/net/minecraft/world/inventory/ContainerAnvil.java
+++ b/net/minecraft/world/inventory/ContainerAnvil.java
@@ -21,6 +21,10 @@
 import net.minecraft.world.level.block.state.IBlockData;
 import org.slf4j.Logger;
 
+// CraftBukkit start
+import org.bukkit.craftbukkit.inventory.view.CraftAnvilView;
+// CraftBukkit end
+
 public class ContainerAnvil extends ContainerAnvilAbstract {
 
     public static final int INPUT_SLOT = 0;
@@ -45,6 +49,11 @@
     private static final int ADDITIONAL_SLOT_X_PLACEMENT = 76;
     private static final int RESULT_SLOT_X_PLACEMENT = 134;
     private static final int SLOT_Y_PLACEMENT = 47;
+    // CraftBukkit start
+    public static final int DEFAULT_DENIED_COST = -1;
+    public int maximumRepairCost = 40;
+    private CraftAnvilView bukkitEntity;
+    // CraftBukkit end
 
     public ContainerAnvil(int i, PlayerInventory playerinventory) {
         this(i, playerinventory, ContainerAccess.NULL);
@@ -72,7 +81,7 @@
 
     @Override
     protected boolean mayPickup(EntityHuman entityhuman, boolean flag) {
-        return (entityhuman.hasInfiniteMaterials() || entityhuman.experienceLevel >= this.cost.get()) && this.cost.get() > 0;
+        return (entityhuman.hasInfiniteMaterials() || entityhuman.experienceLevel >= this.cost.get()) && this.cost.get() > ContainerAnvil.DEFAULT_DENIED_COST && flag; // CraftBukkit - allow cost 0 like a free item
     }
 
     @Override
@@ -94,7 +103,7 @@
             this.inputSlots.setItem(1, ItemStack.EMPTY);
         }
 
-        this.cost.set(0);
+        this.cost.set(DEFAULT_DENIED_COST); // CraftBukkit - use a variable for set a cost for denied item
         this.inputSlots.setItem(0, ItemStack.EMPTY);
         this.access.execute((world, blockposition) -> {
             IBlockData iblockdata = world.getBlockState(blockposition);
@@ -143,8 +152,8 @@
                 if (itemstack1.isDamageableItem() && itemstack.isValidRepairItem(itemstack2)) {
                     k = Math.min(itemstack1.getDamageValue(), itemstack1.getMaxDamage() / 4);
                     if (k <= 0) {
-                        this.resultSlots.setItem(0, ItemStack.EMPTY);
-                        this.cost.set(0);
+                        org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.EMPTY); // CraftBukkit
+                        this.cost.set(DEFAULT_DENIED_COST); // CraftBukkit - use a variable for set a cost for denied item
                         return;
                     }
 
@@ -158,8 +167,8 @@
                     this.repairItemCountCost = i1;
                 } else {
                     if (!flag && (!itemstack1.is(itemstack2.getItem()) || !itemstack1.isDamageableItem())) {
-                        this.resultSlots.setItem(0, ItemStack.EMPTY);
-                        this.cost.set(0);
+                        org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.EMPTY); // CraftBukkit
+                        this.cost.set(DEFAULT_DENIED_COST); // CraftBukkit - use a variable for set a cost for denied item
                         return;
                     }
 
@@ -233,8 +242,8 @@
                     }
 
                     if (flag2 && !flag1) {
-                        this.resultSlots.setItem(0, ItemStack.EMPTY);
-                        this.cost.set(0);
+                        org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.EMPTY); // CraftBukkit
+                        this.cost.set(DEFAULT_DENIED_COST); // CraftBukkit - use a variable for set a cost for denied item
                         return;
                     }
                 }
@@ -260,14 +269,14 @@
             }
 
             if (b0 == i && b0 > 0) {
-                if (this.cost.get() >= 40) {
-                    this.cost.set(39);
+                if (this.cost.get() >= maximumRepairCost) { // CraftBukkit
+                    this.cost.set(maximumRepairCost - 1); // CraftBukkit
                 }
 
                 this.onlyRenaming = true;
             }
 
-            if (this.cost.get() >= 40 && !this.player.getAbilities().instabuild) {
+            if (this.cost.get() >= maximumRepairCost && !this.player.getAbilities().instabuild) { // CraftBukkit
                 itemstack1 = ItemStack.EMPTY;
             }
 
@@ -285,12 +294,13 @@
                 EnchantmentManager.setEnchantments(itemstack1, itemenchantments_a.toImmutable());
             }
 
-            this.resultSlots.setItem(0, itemstack1);
+            org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), itemstack1); // CraftBukkit
             this.broadcastChanges();
         } else {
-            this.resultSlots.setItem(0, ItemStack.EMPTY);
-            this.cost.set(0);
+            org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.EMPTY); // CraftBukkit
+            this.cost.set(DEFAULT_DENIED_COST); // CraftBukkit - use a variable for set a cost for denied item
         }
+        sendAllDataToRemote(); // CraftBukkit - SPIGOT-6686, SPIGOT-7931: Always send completed inventory to stay in sync with client
     }
 
     public static int calculateIncreasedRepairCost(int i) {
@@ -329,4 +339,19 @@
     public int getCost() {
         return this.cost.get();
     }
+
+    // CraftBukkit start
+    @Override
+    public CraftAnvilView getBukkitView() {
+        if (bukkitEntity != null) {
+            return bukkitEntity;
+        }
+
+        org.bukkit.craftbukkit.inventory.CraftInventoryAnvil inventory = new org.bukkit.craftbukkit.inventory.CraftInventoryAnvil(
+                access.getLocation(), this.inputSlots, this.resultSlots);
+        bukkitEntity = new CraftAnvilView(this.player.getBukkitEntity(), inventory, this);
+        bukkitEntity.updateFromLegacy(inventory);
+        return bukkitEntity;
+    }
+    // CraftBukkit end
 }
