EnchantHPBonusData.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 gnu.trove.TIntObjectHashMap;
  17. import java.io.File;
  18. import java.util.Collection;
  19. import java.util.StringTokenizer;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.gameserver.model.L2ItemInstance;
  28. import com.l2jserver.gameserver.skills.Stats;
  29. import com.l2jserver.gameserver.skills.funcs.FuncTemplate;
  30. import com.l2jserver.gameserver.skills.funcs.LambdaConst;
  31. import com.l2jserver.gameserver.templates.item.L2Item;
  32. /**
  33. *
  34. * @author MrPoke
  35. */
  36. public class EnchantHPBonusData
  37. {
  38. protected static final Logger _log = Logger.getLogger(EnchantHPBonusData.class.getName());
  39. private final TIntObjectHashMap<Integer[]> _armorHPBonus = new TIntObjectHashMap<Integer[]>();
  40. private static final float fullArmorModifier = 1.5f;
  41. public static final EnchantHPBonusData getInstance()
  42. {
  43. return SingletonHolder._instance;
  44. }
  45. private EnchantHPBonusData()
  46. {
  47. load();
  48. }
  49. public void reload()
  50. {
  51. load();
  52. }
  53. private void load()
  54. {
  55. _armorHPBonus.clear();
  56. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  57. factory.setValidating(false);
  58. factory.setIgnoringComments(true);
  59. File file = new File(Config.DATAPACK_ROOT, "data/enchantHPBonus.xml");
  60. Document doc = null;
  61. if (file.exists())
  62. {
  63. try
  64. {
  65. doc = factory.newDocumentBuilder().parse(file);
  66. }
  67. catch (Exception e)
  68. {
  69. _log.log(Level.WARNING, "Could not parse enchantHPBonus.xml file: " + e.getMessage(), e);
  70. }
  71. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  72. {
  73. if ("list".equalsIgnoreCase(n.getNodeName()))
  74. {
  75. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  76. {
  77. if ("enchantHP".equalsIgnoreCase(d.getNodeName()))
  78. {
  79. NamedNodeMap attrs = d.getAttributes();
  80. Node att;
  81. Integer grade;
  82. att = attrs.getNamedItem("grade");
  83. if (att == null)
  84. {
  85. _log.severe("[EnchantHPBonusData] Missing grade, skipping");
  86. continue;
  87. }
  88. grade = Integer.parseInt(att.getNodeValue());
  89. att = attrs.getNamedItem("values");
  90. if (att == null)
  91. {
  92. _log.severe("[EnchantHPBonusData] Missing bonus id: " + grade + ", skipping");
  93. continue;
  94. }
  95. StringTokenizer st = new StringTokenizer(att.getNodeValue(), ",");
  96. int tokenCount = st.countTokens();
  97. Integer[] bonus = new Integer[tokenCount];
  98. for (int i = 0; i < tokenCount; i++)
  99. {
  100. Integer value = Integer.decode(st.nextToken().trim());
  101. if (value == null)
  102. {
  103. _log.severe("[EnchantHPBonusData] Bad Hp value!! grade: " + grade + " token: " + i);
  104. value = 0;
  105. }
  106. bonus[i] = value;
  107. }
  108. _armorHPBonus.put(grade, bonus);
  109. }
  110. }
  111. }
  112. }
  113. if (_armorHPBonus.isEmpty())
  114. return;
  115. Collection<Integer> itemIds = ItemTable.getInstance().getAllArmorsId();
  116. int count = 0;
  117. for (Integer itemId : itemIds)
  118. {
  119. L2Item item = ItemTable.getInstance().getTemplate(itemId);
  120. if (item != null && item.getCrystalType() != L2Item.CRYSTAL_NONE)
  121. {
  122. switch (item.getBodyPart())
  123. {
  124. case L2Item.SLOT_CHEST:
  125. case L2Item.SLOT_FEET:
  126. case L2Item.SLOT_GLOVES:
  127. case L2Item.SLOT_HEAD:
  128. case L2Item.SLOT_LEGS:
  129. case L2Item.SLOT_BACK:
  130. case L2Item.SLOT_FULL_ARMOR:
  131. case L2Item.SLOT_UNDERWEAR:
  132. case L2Item.SLOT_L_HAND:
  133. count++;
  134. FuncTemplate ft = new FuncTemplate(null, null, "EnchantHp", Stats.MAX_HP, 0x60, new LambdaConst(0));
  135. item.attach(ft);
  136. break;
  137. }
  138. }
  139. }
  140. // shields in the weapons table
  141. itemIds = ItemTable.getInstance().getAllWeaponsId();
  142. for (Integer itemId : itemIds)
  143. {
  144. L2Item item = ItemTable.getInstance().getTemplate(itemId);
  145. if (item != null && item.getCrystalType() != L2Item.CRYSTAL_NONE)
  146. {
  147. switch (item.getBodyPart())
  148. {
  149. case L2Item.SLOT_L_HAND:
  150. count++;
  151. FuncTemplate ft = new FuncTemplate(null, null, "EnchantHp", Stats.MAX_HP, 0x60, new LambdaConst(0));
  152. item.attach(ft);
  153. break;
  154. }
  155. }
  156. }
  157. _log.info("Enchant HP Bonus registered for " + count + " items!");
  158. }
  159. }
  160. public final int getHPBonus(L2ItemInstance item)
  161. {
  162. final Integer[] values = _armorHPBonus.get(item.getItem().getItemGradeSPlus());
  163. if (values == null || values.length == 0)
  164. return 0;
  165. if (item.getItem().getBodyPart() == L2Item.SLOT_FULL_ARMOR)
  166. return (int) (values[Math.min(item.getOlyEnchantLevel(), values.length) - 1] * fullArmorModifier);
  167. else
  168. return values[Math.min(item.getOlyEnchantLevel(), values.length) - 1];
  169. }
  170. @SuppressWarnings("synthetic-access")
  171. private static class SingletonHolder
  172. {
  173. protected static final EnchantHPBonusData _instance = new EnchantHPBonusData();
  174. }
  175. }