InitialEquipmentData.java 3.9 KB

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