Merge pull request #192 from bm01/master

Repair.addEnchants() messages fix and optimization
This commit is contained in:
nossr50 2012-03-04 13:52:30 -08:00
commit d121358ad2

View File

@ -16,6 +16,9 @@
*/ */
package com.gmail.nossr50.skills; package com.gmail.nossr50.skills;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -62,18 +65,6 @@ public class Repair {
short durabilityBefore = is.getDurability(); short durabilityBefore = is.getDurability();
PlayerInventory inventory = player.getInventory(); PlayerInventory inventory = player.getInventory();
int skillLevel = PP.getSkillLevel(SkillType.REPAIR); int skillLevel = PP.getSkillLevel(SkillType.REPAIR);
//Stuff for keeping enchants
Enchantment[] enchants = new Enchantment[is.getEnchantments().size()];
int[] enchantsLevel = new int[is.getEnchantments().size()];
int pos = 0;
for(Enchantment x : is.getEnchantments().keySet())
{
enchants[pos] = x;
enchantsLevel[pos] = is.getEnchantmentLevel(x);
pos++;
}
if(block != null && mcPermissions.getInstance().repair(player)){ if(block != null && mcPermissions.getInstance().repair(player)){
if(durabilityBefore > 0 && is.getAmount() < 2){ if(durabilityBefore > 0 && is.getAmount() < 2){
@ -86,28 +77,28 @@ public class Repair {
//DIAMOND ARMOR //DIAMOND ARMOR
if(isDiamondArmor(is) && inventory.contains(rDiamond) && skillLevel >= dLevel){ if(isDiamondArmor(is) && inventory.contains(rDiamond) && skillLevel >= dLevel){
inventory.removeItem(new ItemStack(rDiamond, 1)); inventory.removeItem(new ItemStack(rDiamond, 1));
repairItem(player, enchants, enchantsLevel); repairItem(player, is);
xpHandler(player, PP, is, durabilityBefore, 6, true); xpHandler(player, PP, is, durabilityBefore, 6, true);
} }
//IRON ARMOR //IRON ARMOR
else if (isIronArmor(is) && inventory.contains(rIron) && skillLevel >= iLevel){ else if (isIronArmor(is) && inventory.contains(rIron) && skillLevel >= iLevel){
inventory.removeItem(new ItemStack(rIron, 1)); inventory.removeItem(new ItemStack(rIron, 1));
repairItem(player, enchants, enchantsLevel); repairItem(player, is);
xpHandler(player, PP, is, durabilityBefore, 2, true); xpHandler(player, PP, is, durabilityBefore, 2, true);
} }
//GOLD ARMOR //GOLD ARMOR
else if (isGoldArmor(is) && inventory.contains(rGold) && skillLevel >= gLevel){ else if (isGoldArmor(is) && inventory.contains(rGold) && skillLevel >= gLevel){
inventory.removeItem(new ItemStack(rGold, 1)); inventory.removeItem(new ItemStack(rGold, 1));
repairItem(player, enchants, enchantsLevel); repairItem(player, is);
xpHandler(player, PP, is, durabilityBefore, 4, true); xpHandler(player, PP, is, durabilityBefore, 4, true);
} }
//LEATHER ARMOR //LEATHER ARMOR
else if (isLeatherArmor(is) && inventory.contains(rLeather)){ else if (isLeatherArmor(is) && inventory.contains(rLeather)){
inventory.removeItem(new ItemStack(rLeather, 1)); inventory.removeItem(new ItemStack(rLeather, 1));
repairItem(player, enchants, enchantsLevel); repairItem(player, is);
xpHandler(player, PP, is, durabilityBefore, 1, true); xpHandler(player, PP, is, durabilityBefore, 1, true);
} }
@ -125,42 +116,42 @@ public class Repair {
//STONE TOOLS //STONE TOOLS
if(isStoneTools(is) && inventory.contains(rStone) && skillLevel >= sLevel){ if(isStoneTools(is) && inventory.contains(rStone) && skillLevel >= sLevel){
inventory.removeItem(new ItemStack(rStone, 1)); inventory.removeItem(new ItemStack(rStone, 1));
repairItem(player, enchants, enchantsLevel); repairItem(player, is);
xpHandler(player, PP, is, durabilityBefore, 2, false); xpHandler(player, PP, is, durabilityBefore, 2, false);
} }
//WOOD TOOLS //WOOD TOOLS
else if(isWoodTools(is) && inventory.contains(rWood)){ else if(isWoodTools(is) && inventory.contains(rWood)){
inventory.removeItem(new ItemStack(rWood, 1)); inventory.removeItem(new ItemStack(rWood, 1));
repairItem(player, enchants, enchantsLevel); repairItem(player, is);
xpHandler(player, PP, is, durabilityBefore, 2, false); xpHandler(player, PP, is, durabilityBefore, 2, false);
} }
//IRON TOOLS //IRON TOOLS
else if(isIronTools(is) && inventory.contains(rIron) && skillLevel >= iLevel){ else if(isIronTools(is) && inventory.contains(rIron) && skillLevel >= iLevel){
inventory.removeItem(new ItemStack(rIron, 1)); inventory.removeItem(new ItemStack(rIron, 1));
repairItem(player, enchants, enchantsLevel); repairItem(player, is);
xpHandler(player, PP, is, durabilityBefore, 1, true); xpHandler(player, PP, is, durabilityBefore, 1, true);
} }
//DIAMOND TOOLS //DIAMOND TOOLS
else if (isDiamondTools(is) && inventory.contains(rDiamond) && skillLevel >= dLevel){ else if (isDiamondTools(is) && inventory.contains(rDiamond) && skillLevel >= dLevel){
inventory.removeItem(new ItemStack(rDiamond, 1)); inventory.removeItem(new ItemStack(rDiamond, 1));
repairItem(player, enchants, enchantsLevel); repairItem(player, is);
xpHandler(player, PP, is, durabilityBefore, 1, true); xpHandler(player, PP, is, durabilityBefore, 1, true);
} }
//GOLD TOOLS //GOLD TOOLS
else if(isGoldTools(is) && inventory.contains(rGold) && skillLevel >= gLevel){ else if(isGoldTools(is) && inventory.contains(rGold) && skillLevel >= gLevel){
inventory.removeItem(new ItemStack(rGold, 1)); inventory.removeItem(new ItemStack(rGold, 1));
repairItem(player, enchants, enchantsLevel); repairItem(player, is);
xpHandler(player, PP, is, durabilityBefore, 8, true); xpHandler(player, PP, is, durabilityBefore, 8, true);
} }
//BOW //BOW
else if(isBow(is) && inventory.contains(rString)){ else if(isBow(is) && inventory.contains(rString)){
inventory.removeItem(new ItemStack(rString, 1)); inventory.removeItem(new ItemStack(rString, 1));
repairItem(player, enchants, enchantsLevel); repairItem(player, is);
xpHandler(player, PP, is, durabilityBefore, 2, false); xpHandler(player, PP, is, durabilityBefore, 2, false);
} }
@ -224,73 +215,48 @@ public class Repair {
return 0; return 0;
} }
public static void addEnchants(ItemStack is, Enchantment[] enchants, int[] enchantsLvl, PlayerProfile PP, Player player){ public static void addEnchants(Player player, ItemStack is)
if(is.getEnchantments().keySet().size() == 0) {
Map<Enchantment, Integer> enchants = is.getEnchantments();
if(enchants.size() == 0)
return; return;
int pos = 0; int rank = getArcaneForgingRank(Users.getProfile(player).getSkillLevel(SkillType.REPAIR));
int rank = getArcaneForgingRank(PP.getSkillLevel(SkillType.REPAIR));
if(rank == 0) if(rank == 0)
{ {
if(LoadProperties.mayLoseEnchants) for(Enchantment x : enchants.keySet())
{ is.removeEnchantment(x);
player.sendMessage(mcLocale.getString("Repair.LostEnchants")); player.sendMessage(mcLocale.getString("Repair.LostEnchants"));
for(Enchantment x : enchants)
{
is.removeEnchantment(x);
}
}
return; return;
} }
boolean failure = false, downgrade = false; boolean downgraded = false;
for(Entry<Enchantment, Integer> enchant : enchants.entrySet())
if(LoadProperties.mayLoseEnchants)
{ {
for(Enchantment x : enchants) if(Math.random() * 100 <= getEnchantChance(rank))
{ {
//Remove enchant int enchantLevel = enchant.getValue();
is.removeEnchantment(x); if(LoadProperties.mayDowngradeEnchants && enchantLevel > 1)
if(x.canEnchantItem(is))
{ {
if(Math.random() * 100 <= getEnchantChance(rank)) if(Math.random() * 100 <= getDowngradeChance(rank))
{ {
if(enchantsLvl[pos] > 1) is.addEnchantment(enchant.getKey(), enchantLevel--);
{ downgraded = true;
if(LoadProperties.mayDowngradeEnchants)
{
if(Math.random() * 100 <= getDowngradeChance(rank))
{
is.addEnchantment(x, enchantsLvl[pos]-1);
downgrade = true;
} else
{
is.addEnchantment(x, enchantsLvl[pos]);
}
}
}
else {
is.addEnchantment(x, enchantsLvl[pos]);
}
} else {
failure = true;
} }
} }
pos++;
} }
else
is.removeEnchantment(enchant.getKey());
} }
if(failure == false && downgrade == false) Map<Enchantment, Integer> newEnchants = is.getEnchantments();
{ if(newEnchants.isEmpty())
player.sendMessage(mcLocale.getString("Repair.ArcaneFailed"));
else if(downgraded || newEnchants.size() < enchants.size())
player.sendMessage(mcLocale.getString("Repair.Downgraded"));
else
player.sendMessage(mcLocale.getString("Repair.ArcanePerfect")); player.sendMessage(mcLocale.getString("Repair.ArcanePerfect"));
} else {
if(failure == true)
player.sendMessage(mcLocale.getString("Repair.ArcaneFailed"));
if(downgrade == true)
player.sendMessage(mcLocale.getString("Repair.Downgraded"));
}
} }
/** /**
@ -579,12 +545,11 @@ public class Repair {
* @param enchants The enchantments on the item * @param enchants The enchantments on the item
* @param enchantsLevel The level of the enchantments on the item * @param enchantsLevel The level of the enchantments on the item
*/ */
public static void repairItem(Player player, Enchantment[] enchants, int[] enchantsLevel) public static void repairItem(Player player, ItemStack is)
{ {
PlayerProfile PP = Users.getProfile(player);
ItemStack is = player.getItemInHand();
//Handle the enchantments //Handle the enchantments
addEnchants(is, enchants, enchantsLevel, PP, player); if(LoadProperties.mayLoseEnchants)
addEnchants(player, is);
is.setDurability(getRepairAmount(is, player)); is.setDurability(getRepairAmount(is, player));
} }
} }