EnchantHPBonusData.java 4.8 KB

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