EnchantHPBonusData.java 4.8 KB

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