EnchantHPBonusData.java 6.2 KB

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