EnchantItemHPBonusData.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.EnumMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.gameserver.engines.DocumentParser;
  27. import com.l2jserver.gameserver.model.items.L2Item;
  28. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  29. import com.l2jserver.gameserver.model.items.type.CrystalType;
  30. import com.l2jserver.gameserver.model.skills.funcs.FuncTemplate;
  31. import com.l2jserver.gameserver.model.skills.funcs.LambdaConst;
  32. import com.l2jserver.gameserver.model.stats.Stats;
  33. /**
  34. * This class holds the Enchant HP Bonus Data.
  35. * @author MrPoke, Zoey76
  36. */
  37. public class EnchantItemHPBonusData extends DocumentParser
  38. {
  39. private final Map<CrystalType, List<Integer>> _armorHPBonuses = new EnumMap<>(CrystalType.class);
  40. private static final float fullArmorModifier = 1.5f; // TODO: Move it to config!
  41. /**
  42. * Instantiates a new enchant hp bonus data.
  43. */
  44. protected EnchantItemHPBonusData()
  45. {
  46. load();
  47. }
  48. @Override
  49. protected void parseDocument()
  50. {
  51. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  52. {
  53. if ("list".equalsIgnoreCase(n.getNodeName()))
  54. {
  55. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  56. {
  57. if ("enchantHP".equalsIgnoreCase(d.getNodeName()))
  58. {
  59. List<Integer> bonuses = new ArrayList<>();
  60. for (Node e = d.getFirstChild(); e != null; e = e.getNextSibling())
  61. {
  62. if ("bonus".equalsIgnoreCase(e.getNodeName()))
  63. {
  64. bonuses.add(Integer.valueOf(e.getTextContent()));
  65. }
  66. }
  67. _armorHPBonuses.put(parseEnum(d.getAttributes(), CrystalType.class, "grade"), bonuses);
  68. }
  69. }
  70. }
  71. }
  72. if (!_armorHPBonuses.isEmpty())
  73. {
  74. final ItemTable it = ItemTable.getInstance();
  75. L2Item item;
  76. // Armors
  77. final Collection<Integer> armorIds = it.getAllArmorsId();
  78. for (Integer itemId : armorIds)
  79. {
  80. item = it.getTemplate(itemId);
  81. if ((item != null) && (item.getCrystalType() != CrystalType.NONE))
  82. {
  83. switch (item.getBodyPart())
  84. {
  85. case L2Item.SLOT_CHEST:
  86. case L2Item.SLOT_FEET:
  87. case L2Item.SLOT_GLOVES:
  88. case L2Item.SLOT_HEAD:
  89. case L2Item.SLOT_LEGS:
  90. case L2Item.SLOT_BACK:
  91. case L2Item.SLOT_FULL_ARMOR:
  92. case L2Item.SLOT_UNDERWEAR:
  93. case L2Item.SLOT_L_HAND:
  94. item.attach(new FuncTemplate(null, null, "EnchantHp", Stats.MAX_HP, 0x60, new LambdaConst(0)));
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. }
  101. // Shields
  102. final Collection<Integer> shieldIds = it.getAllWeaponsId();
  103. for (Integer itemId : shieldIds)
  104. {
  105. item = it.getTemplate(itemId);
  106. if ((item != null) && (item.getCrystalType() != CrystalType.NONE))
  107. {
  108. switch (item.getBodyPart())
  109. {
  110. case L2Item.SLOT_L_HAND:
  111. item.attach(new FuncTemplate(null, null, "EnchantHp", Stats.MAX_HP, 0x60, new LambdaConst(0)));
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. @Override
  121. public void load()
  122. {
  123. _armorHPBonuses.clear();
  124. parseDatapackFile("data/stats/enchantHPBonus.xml");
  125. _log.info(getClass().getSimpleName() + ": Loaded " + _armorHPBonuses.size() + " Enchant HP Bonuses.");
  126. }
  127. /**
  128. * Gets the HP bonus.
  129. * @param item the item
  130. * @return the HP bonus
  131. */
  132. public final int getHPBonus(L2ItemInstance item)
  133. {
  134. final List<Integer> values = _armorHPBonuses.get(item.getItem().getItemGradeSPlus());
  135. if ((values == null) || values.isEmpty() || (item.getOlyEnchantLevel() <= 0))
  136. {
  137. return 0;
  138. }
  139. final int bonus = values.get(Math.min(item.getOlyEnchantLevel(), values.size()) - 1);
  140. if (item.getItem().getBodyPart() == L2Item.SLOT_FULL_ARMOR)
  141. {
  142. return (int) (bonus * fullArmorModifier);
  143. }
  144. return bonus;
  145. }
  146. /**
  147. * Gets the single instance of EnchantHPBonusData.
  148. * @return single instance of EnchantHPBonusData
  149. */
  150. public static final EnchantItemHPBonusData getInstance()
  151. {
  152. return SingletonHolder._instance;
  153. }
  154. private static class SingletonHolder
  155. {
  156. protected static final EnchantItemHPBonusData _instance = new EnchantItemHPBonusData();
  157. }
  158. }