InitialEquipmentData.java 4.0 KB

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