2
0

EnchantHPBonusData.java 6.1 KB

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