EnchantHPBonusData.java 5.8 KB

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