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 parseDocument(Document doc)
  52. {
  53. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  54. {
  55. if ("list".equalsIgnoreCase(n.getNodeName()))
  56. {
  57. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  58. {
  59. if ("enchantHP".equalsIgnoreCase(d.getNodeName()))
  60. {
  61. final List<Integer> bonuses = new ArrayList<>(12);
  62. for (Node e = d.getFirstChild(); e != null; e = e.getNextSibling())
  63. {
  64. if ("bonus".equalsIgnoreCase(e.getNodeName()))
  65. {
  66. bonuses.add(Integer.parseInt(e.getTextContent()));
  67. }
  68. }
  69. _armorHPBonuses.put(parseEnum(d.getAttributes(), CrystalType.class, "grade"), bonuses);
  70. }
  71. }
  72. }
  73. }
  74. if (!_armorHPBonuses.isEmpty())
  75. {
  76. final ItemTable it = ItemTable.getInstance();
  77. // Armors
  78. final Collection<Integer> armorIds = it.getAllArmorsId();
  79. for (Integer itemId : armorIds)
  80. {
  81. L2Item item = it.getTemplate(itemId);
  82. if ((item != null) && (item.getCrystalType() != CrystalType.NONE))
  83. {
  84. switch (item.getBodyPart())
  85. {
  86. case L2Item.SLOT_CHEST:
  87. case L2Item.SLOT_FEET:
  88. case L2Item.SLOT_GLOVES:
  89. case L2Item.SLOT_HEAD:
  90. case L2Item.SLOT_LEGS:
  91. case L2Item.SLOT_BACK:
  92. case L2Item.SLOT_FULL_ARMOR:
  93. case L2Item.SLOT_UNDERWEAR:
  94. case L2Item.SLOT_L_HAND:
  95. case L2Item.SLOT_BELT:
  96. item.attach(new FuncTemplate(null, null, StatFunction.ENCHANTHP.getName(), -1, Stats.MAX_HP, 0));
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. @Override
  106. public void load()
  107. {
  108. _armorHPBonuses.clear();
  109. parseDatapackFile("data/stats/enchantHPBonus.xml");
  110. LOGGER.info(getClass().getSimpleName() + ": Loaded " + _armorHPBonuses.size() + " Enchant HP Bonuses.");
  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. }