2
0

InitialEquipmentData.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.w3c.dom.NamedNodeMap;
  21. import org.w3c.dom.Node;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.engines.DocumentParser;
  24. import com.l2jserver.gameserver.model.StatsSet;
  25. import com.l2jserver.gameserver.model.base.ClassId;
  26. import com.l2jserver.gameserver.model.items.PcItemTemplate;
  27. /**
  28. * This class holds the Initial Equipment information.<br>
  29. * What items get each newly created character and if this item is equipped upon creation (<b>Requires the item to be equippable</b>).
  30. * @author Zoey76
  31. */
  32. public final class InitialEquipmentData extends DocumentParser
  33. {
  34. private static final String filePathNormal = "data/stats/initialEquipment.xml";
  35. private static final String filePathEvent = "data/stats/initialEquipmentEvent.xml";
  36. private final Map<ClassId, List<PcItemTemplate>> _initialEquipmentList = new HashMap<>();
  37. /**
  38. * Instantiates a new initial equipment data.
  39. */
  40. protected InitialEquipmentData()
  41. {
  42. load();
  43. }
  44. @Override
  45. public void load()
  46. {
  47. _initialEquipmentList.clear();
  48. parseDatapackFile(Config.INITIAL_EQUIPMENT_EVENT ? filePathEvent : filePathNormal);
  49. _log.info(getClass().getSimpleName() + ": Loaded " + _initialEquipmentList.size() + " Initial Equipment data.");
  50. }
  51. @Override
  52. protected void parseDocument()
  53. {
  54. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  55. {
  56. if ("list".equalsIgnoreCase(n.getNodeName()))
  57. {
  58. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  59. {
  60. if ("equipment".equalsIgnoreCase(d.getNodeName()))
  61. {
  62. parseEquipment(d);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * Parses the equipment.
  70. * @param d parse an initial equipment and add it to {@link #_initialEquipmentList}
  71. */
  72. private void parseEquipment(Node d)
  73. {
  74. NamedNodeMap attrs = d.getAttributes();
  75. Node attr;
  76. final ClassId classId = ClassId.getClassId(Integer.parseInt(attrs.getNamedItem("classId").getNodeValue()));
  77. final List<PcItemTemplate> equipList = new ArrayList<>();
  78. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  79. {
  80. if ("item".equalsIgnoreCase(c.getNodeName()))
  81. {
  82. final StatsSet set = new StatsSet();
  83. attrs = c.getAttributes();
  84. for (int i = 0; i < attrs.getLength(); i++)
  85. {
  86. attr = attrs.item(i);
  87. set.set(attr.getNodeName(), attr.getNodeValue());
  88. }
  89. equipList.add(new PcItemTemplate(set));
  90. }
  91. }
  92. _initialEquipmentList.put(classId, equipList);
  93. }
  94. /**
  95. * Gets the equipment list.
  96. * @param cId the class Id for the required initial equipment.
  97. * @return the initial equipment for the given class Id.
  98. */
  99. public List<PcItemTemplate> getEquipmentList(ClassId cId)
  100. {
  101. return _initialEquipmentList.get(cId);
  102. }
  103. /**
  104. * Gets the equipment list.
  105. * @param cId the class Id for the required initial equipment.
  106. * @return the initial equipment for the given class Id.
  107. */
  108. public List<PcItemTemplate> getEquipmentList(int cId)
  109. {
  110. return _initialEquipmentList.get(ClassId.getClassId(cId));
  111. }
  112. /**
  113. * Gets the single instance of InitialEquipmentData.
  114. * @return single instance of InitialEquipmentData
  115. */
  116. public static InitialEquipmentData getInstance()
  117. {
  118. return SingletonHolder._instance;
  119. }
  120. private static class SingletonHolder
  121. {
  122. protected static final InitialEquipmentData _instance = new InitialEquipmentData();
  123. }
  124. }