@SuppressWarnings("unchecked")
Map<Integer, Integer> addedEnchantments = EnchantmentHelper.getEnchantments(addedItem);
for(Integer addedEnchId : addedEnchantments.keySet())
{
Enchantment enchantment = Enchantment.enchantmentsList[addedEnchId];
int existingEnchLevel = existingEnchantments.containsKey(addedEnchId) ? existingEnchantments.get(addedEnchId) : 0;
int addedEnchLevel = addedEnchantments.get(addedEnchId);
int newEnchLevel;
if(existingEnchLevel == addedEnchLevel)
{
++addedEnchLevel;
newEnchLevel = addedEnchLevel;
}
else
{
newEnchLevel = Math.max(addedEnchLevel, existingEnchLevel);
}
addedEnchLevel = newEnchLevel;
int levelDifference = addedEnchLevel - existingEnchLevel;
boolean canEnchantmentBeAdded = enchantment.canApply(outputItem);
if(outputItem.itemID == ItemEnchantedBook.enchantedBook.itemID)
{
canEnchantmentBeAdded = true;
}
for(Integer existingEnchId : existingEnchantments.keySet())
{
if(existingEnchId != addedEnchId && !enchantment.canApplyTogether(Enchantment.enchantmentsList[existingEnchId]))
{
canEnchantmentBeAdded = false;
totalEnchCost += levelDifference;
}
}
if(canEnchantmentBeAdded)
{
if(newEnchLevel > enchantment.getMaxLevel())
{
newEnchLevel = enchantment.getMaxLevel();
}
existingEnchantments.put(Integer.valueOf(addedEnchId), Integer.valueOf(newEnchLevel));
int enchCost = 0;
switch(enchantment.getWeight())
{
case 1:
enchCost = 8;
break;
case 2:
enchCost = 4;
case 3:
case 4:
case 6:
case 7:
case 8:
case 9:
default:
break;
case 5:
enchCost = 2;
break;
case 10:
enchCost = 1;
}
if(enchantingWithBook)
{
enchCost = Math.max(1, enchCost / 2);
}
totalEnchCost += enchCost * levelDifference;
}
}
}
}
int enchCount = 0;
for(Integer existingEnchId : existingEnchantments.keySet())
{
Enchantment enchantment = Enchantment.enchantmentsList[existingEnchId];
int existingEnchLevel = existingEnchantments.get(existingEnchId);
int enchCost = 0;
++enchCount;
switch(enchantment.getWeight())
{
case 1:
enchCost = 8;
break;
case 2: