int i1;
int j1;
int k1;
int l1;
Iterator iterator;
Enchantment enchantment;
if (inputB != null) {
if (!ForgeHooks.onAnvilChange(this, inputA, inputB, outputSlot, repairedItemName, baseCost)) return;
isBook = inputB.getItem() == Items.enchanted_book && Items.enchanted_book.func_92110_g(inputB).tagCount() > 0;
if (inputACopy.isItemStackDamageable() && inputACopy.getItem().getIsRepairable(inputA, inputB)) {
l = Math.min(inputACopy.getItemDamageForDisplay(), inputACopy.getMaxDamage() / 4);
if (l <= 0) {
this.outputSlot.setInventorySlotContents(0, (ItemStack) null);
this.maximumCost = 0;
return;
}
for (i1 = 0; l > 0 && i1 < inputB.stackSize; ++i1) {
j1 = inputACopy.getItemDamageForDisplay() - l;
inputACopy.setItemDamage(j1);
i += Math.max(1, l / 100) + enchantments.size();
l = Math.min(inputACopy.getItemDamageForDisplay(), inputACopy.getMaxDamage() / 4);
}
this.stackSizeToBeUsedInRepair = i1;
} else {
if (!isBook && (inputACopy.getItem() != inputB.getItem() || !inputACopy.isItemStackDamageable())) {
this.outputSlot.setInventorySlotContents(0, (ItemStack) null);
this.maximumCost = 0;
return;
}
if (inputACopy.isItemStackDamageable() && !isBook) {
l = inputA.getMaxDamage() - inputA.getItemDamageForDisplay();
i1 = inputB.getMaxDamage() - inputB.getItemDamageForDisplay();
j1 = i1 + inputACopy.getMaxDamage() * 12 / 100;
int i2 = l + j1;
k1 = inputACopy.getMaxDamage() - i2;
if (k1 < 0)
k1 = 0;
if (k1 < inputACopy.getItemDamage()) {
inputACopy.setItemDamage(k1);
i += Math.max(1, j1 / 100);
}
}
Map map1 = EnchantmentHelper.getEnchantments(inputB);
iterator = map1.keySet().iterator();
while (iterator.hasNext()) {
j1 = ((Integer) iterator.next()).intValue();
enchantment = Enchantment.enchantmentsList[j1];
k1 = enchantments.containsKey(Integer.valueOf(j1)) ? ((Integer) enchantments.get(Integer.valueOf(j1))).intValue() : 0;
l1 = ((Integer) map1.get(Integer.valueOf(j1))).intValue();
int j2;
if (k1 == l1) {
++l1;
j2 = l1;
} else
j2 = Math.max(l1, k1);
l1 = j2;
int k2 = l1 - k1;
boolean isEnchantmentValid = enchantment.canApply(inputA);
if (player.capabilities.isCreativeMode || inputA.getItem() == Items.enchanted_book)
isEnchantmentValid = true;
Iterator iterator1 = enchantments.keySet().iterator();
while (iterator1.hasNext()) {
int l2 = ((Integer) iterator1.next()).intValue();
if (l2 != j1 && !enchantment.canApplyTogether(Enchantment.enchantmentsList[l2])) {
isEnchantmentValid = false;
i += k2;
}
}
if (isEnchantmentValid) {
if (l1 > enchantment.getMaxLevel())
l1 = enchantment.getMaxLevel();
enchantments.put(Integer.valueOf(j1), Integer.valueOf(l1));
int i3 = 0;
switch (enchantment.getWeight()) {
case 1:
i3 = 8;
break;
case 2:
i3 = 4;
case 3:
case 4:
case 6:
case 7:
case 8:
case 9:
default:
break;
case 5:
i3 = 2;
break;
case 10:
i3 = 1;
}
if (isBook)
i3 = Math.max(1, i3 / 2);
i += i3 * k2;
}
}
}
}
if (StringUtils.isBlank(this.repairedItemName)) {
if (inputA.hasDisplayName()) {
j = inputA.isItemStackDamageable() ? 7 : inputA.stackSize * 5;
i += j;
inputACopy.func_135074_t();
}
} else if (!this.repairedItemName.equals(inputA.getDisplayName())) {
j = inputA.isItemStackDamageable() ? 7 : inputA.stackSize * 5;
i += j;
if (inputA.hasDisplayName())
baseCost += j / 2;
inputACopy.setStackDisplayName(this.repairedItemName);
}
l = 0;
for (iterator = enchantments.keySet().iterator(); iterator.hasNext(); baseCost += l + k1 * l1) {
j1 = ((Integer) iterator.next()).intValue();
enchantment = Enchantment.enchantmentsList[j1];
k1 = ((Integer) enchantments.get(Integer.valueOf(j1))).intValue();
l1 = 0;
++l;
switch (enchantment.getWeight()) {
case 1:
l1 = 8;
break;
case 2:
l1 = 4;