--- a/net/minecraft/server/ContainerAnvil.java
+++ b/net/minecraft/server/ContainerAnvil.java
@@ -6,12 +6,20 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+// CraftBukkit start
+import org.bukkit.craftbukkit.inventory.CraftInventoryView;
+// CraftBukkit end
+
 public class ContainerAnvil extends ContainerAnvilAbstract {
 
     private static final Logger LOGGER = LogManager.getLogger();
     private int h;
     public String renameText;
     public final ContainerProperty levelCost;
+    // CraftBukkit start
+    public int maximumRepairCost = 40;
+    private CraftInventoryView bukkitEntity;
+    // CraftBukkit end
 
     public ContainerAnvil(int i, PlayerInventory playerinventory) {
         this(i, playerinventory, ContainerAccess.a);
@@ -85,7 +93,7 @@
         byte b1 = 0;
 
         if (itemstack.isEmpty()) {
-            this.resultInventory.setItem(0, ItemStack.b);
+            org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.b); // CraftBukkit
             this.levelCost.set(0);
         } else {
             ItemStack itemstack1 = itemstack.cloneItemStack();
@@ -103,7 +111,7 @@
                 if (itemstack1.e() && itemstack1.getItem().a(itemstack, itemstack2)) {
                     k = Math.min(itemstack1.getDamage(), itemstack1.h() / 4);
                     if (k <= 0) {
-                        this.resultInventory.setItem(0, ItemStack.b);
+                        org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.b); // CraftBukkit
                         this.levelCost.set(0);
                         return;
                     }
@@ -118,7 +126,7 @@
                     this.h = i1;
                 } else {
                     if (!flag && (itemstack1.getItem() != itemstack2.getItem() || !itemstack1.e())) {
-                        this.resultInventory.setItem(0, ItemStack.b);
+                        org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.b); // CraftBukkit
                         this.levelCost.set(0);
                         return;
                     }
@@ -208,7 +216,7 @@
                     }
 
                     if (flag2 && !flag1) {
-                        this.resultInventory.setItem(0, ItemStack.b);
+                        org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), ItemStack.b); // CraftBukkit
                         this.levelCost.set(0);
                         return;
                     }
@@ -232,11 +240,11 @@
                 itemstack1 = ItemStack.b;
             }
 
-            if (b1 == i && b1 > 0 && this.levelCost.get() >= 40) {
-                this.levelCost.set(39);
+            if (b1 == i && b1 > 0 && this.levelCost.get() >= maximumRepairCost) { // CraftBukkit
+                this.levelCost.set(maximumRepairCost - 1); // CraftBukkit
             }
 
-            if (this.levelCost.get() >= 40 && !this.player.abilities.canInstantlyBuild) {
+            if (this.levelCost.get() >= maximumRepairCost && !this.player.abilities.canInstantlyBuild) { // CraftBukkit
                 itemstack1 = ItemStack.b;
             }
 
@@ -255,7 +263,7 @@
                 EnchantmentManager.a(map, itemstack1);
             }
 
-            this.resultInventory.setItem(0, itemstack1);
+            org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(getBukkitView(), itemstack1); // CraftBukkit
             this.c();
         }
     }
@@ -278,4 +286,18 @@
 
         this.e();
     }
+
+    // CraftBukkit start
+    @Override
+    public CraftInventoryView getBukkitView() {
+        if (bukkitEntity != null) {
+            return bukkitEntity;
+        }
+
+        org.bukkit.craftbukkit.inventory.CraftInventory inventory = new org.bukkit.craftbukkit.inventory.CraftInventoryAnvil(
+                containerAccess.getLocation(), this.repairInventory, this.resultInventory, this);
+        bukkitEntity = new CraftInventoryView(this.player.getBukkitEntity(), inventory, this);
+        return bukkitEntity;
+    }
+    // CraftBukkit end
 }