EnchantItemHPBonusData.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2004-2015 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.data.xml.impl;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.EnumMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.w3c.dom.Document;
  26. import org.w3c.dom.Node;
  27. import com.l2jserver.gameserver.datatables.ItemTable;
  28. import com.l2jserver.gameserver.enums.StatFunction;
  29. import com.l2jserver.gameserver.model.items.L2Item;
  30. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  31. import com.l2jserver.gameserver.model.items.type.CrystalType;
  32. import com.l2jserver.gameserver.model.stats.Stats;
  33. import com.l2jserver.gameserver.model.stats.functions.FuncTemplate;
  34. import com.l2jserver.util.data.xml.IXmlReader;
  35. /**
  36. * This class holds the Enchant HP Bonus Data.
  37. * @author MrPoke, Zoey76
  38. */
  39. public class EnchantItemHPBonusData implements IXmlReader
  40. {
  41. private final Map<CrystalType, List<Integer>> _armorHPBonuses = new EnumMap<>(CrystalType.class);
  42. private static final float FULL_ARMOR_MODIFIER = 1.5f; // TODO: Move it to config!
  43. /**
  44. * Instantiates a new enchant hp bonus data.
  45. */
  46. protected EnchantItemHPBonusData()
  47. {
  48. load();
  49. }
  50. @Override
  51. public void load()
  52. {
  53. _armorHPBonuses.clear();
  54. parseDatapackFile("data/stats/enchantHPBonus.xml");
  55. LOGGER.info("{}: Loaded {} Enchant HP Bonuses.", getClass().getSimpleName(), _armorHPBonuses.size());
  56. }
  57. @Override
  58. public void parseDocument(Document doc)
  59. {
  60. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  61. {
  62. if ("list".equalsIgnoreCase(n.getNodeName()))
  63. {
  64. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  65. {
  66. if ("enchantHP".equalsIgnoreCase(d.getNodeName()))
  67. {
  68. final List<Integer> bonuses = new ArrayList<>(12);
  69. for (Node e = d.getFirstChild(); e != null; e = e.getNextSibling())
  70. {
  71. if ("bonus".equalsIgnoreCase(e.getNodeName()))
  72. {
  73. bonuses.add(Integer.parseInt(e.getTextContent()));
  74. }
  75. }
  76. _armorHPBonuses.put(parseEnum(d.getAttributes(), CrystalType.class, "grade"), bonuses);
  77. }
  78. }
  79. }
  80. }
  81. if (!_armorHPBonuses.isEmpty())
  82. {
  83. final ItemTable it = ItemTable.getInstance();
  84. // Armors
  85. final Collection<Integer> armorIds = it.getAllArmorsId();
  86. for (Integer itemId : armorIds)
  87. {
  88. L2Item item = it.getTemplate(itemId);
  89. if ((item != null) && (item.getCrystalType() != CrystalType.NONE))
  90. {
  91. switch (item.getBodyPart())
  92. {
  93. case L2Item.SLOT_CHEST:
  94. case L2Item.SLOT_FEET:
  95. case L2Item.SLOT_GLOVES:
  96. case L2Item.SLOT_HEAD:
  97. case L2Item.SLOT_LEGS:
  98. case L2Item.SLOT_BACK:
  99. case L2Item.SLOT_FULL_ARMOR:
  100. case L2Item.SLOT_UNDERWEAR:
  101. case L2Item.SLOT_L_HAND:
  102. case L2Item.SLOT_BELT:
  103. item.attach(new FuncTemplate(null, null, StatFunction.ENCHANTHP.getName(), -1, Stats.MAX_HP, 0));
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * Gets the HP bonus.
  114. * @param item the item
  115. * @return the HP bonus
  116. */
  117. public final int getHPBonus(L2ItemInstance item)
  118. {
  119. final List<Integer> values = _armorHPBonuses.get(item.getItem().getItemGradeSPlus());
  120. if ((values == null) || values.isEmpty() || (item.getOlyEnchantLevel() <= 0))
  121. {
  122. return 0;
  123. }
  124. final int bonus = values.get(Math.min(item.getOlyEnchantLevel(), values.size()) - 1);
  125. if (item.getItem().getBodyPart() == L2Item.SLOT_FULL_ARMOR)
  126. {
  127. return (int) (bonus * FULL_ARMOR_MODIFIER);
  128. }
  129. return bonus;
  130. }
  131. /**
  132. * Gets the single instance of EnchantHPBonusData.
  133. * @return single instance of EnchantHPBonusData
  134. */
  135. public static final EnchantItemHPBonusData getInstance()
  136. {
  137. return SingletonHolder._instance;
  138. }
  139. private static class SingletonHolder
  140. {
  141. protected static final EnchantItemHPBonusData _instance = new EnchantItemHPBonusData();
  142. }
  143. }