InitialEquipmentData.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. parseFile(new File(Config.DATAPACK_ROOT, Config.INITIAL_EQUIPMENT_EVENT ? filePathEvent : filePathNormal));
  43. _log.info(getClass().getSimpleName() + ": Loaded " + _initialEquipmentList.size() + " Initial Equipment data.");
  44. }
  45. @Override
  46. protected void parseDocument(Document doc)
  47. {
  48. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  49. {
  50. if ("list".equalsIgnoreCase(n.getNodeName()))
  51. {
  52. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  53. {
  54. if ("equipment".equalsIgnoreCase(d.getNodeName()))
  55. {
  56. parseEquipment(d);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. * @param d parse an initial equipment and add it to {@link #_initialEquipmentList}
  64. */
  65. private void parseEquipment(Node d)
  66. {
  67. NamedNodeMap attrs = d.getAttributes();
  68. Node attr;
  69. final ClassId classId = ClassId.getClassId(Integer.parseInt(attrs.getNamedItem("classId").getNodeValue()));
  70. final List<PcItemTemplate> equipList = new ArrayList<>();
  71. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  72. {
  73. if ("item".equalsIgnoreCase(c.getNodeName()))
  74. {
  75. final StatsSet set = new StatsSet();
  76. attrs = c.getAttributes();
  77. for (int i = 0; i < attrs.getLength(); i++)
  78. {
  79. attr = attrs.item(i);
  80. set.set(attr.getNodeName(), attr.getNodeValue());
  81. }
  82. equipList.add(new PcItemTemplate(set));
  83. }
  84. }
  85. _initialEquipmentList.put(classId, equipList);
  86. }
  87. /**
  88. * @param cId the class Id for the required initial equipment.
  89. * @return the initial equipment for the given class Id.
  90. */
  91. public List<PcItemTemplate> getEquipmentList(ClassId cId)
  92. {
  93. return _initialEquipmentList.get(cId);
  94. }
  95. /**
  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(int cId)
  100. {
  101. return _initialEquipmentList.get(ClassId.getClassId(cId));
  102. }
  103. public static InitialEquipmentData getInstance()
  104. {
  105. return SingletonHolder._instance;
  106. }
  107. @SuppressWarnings("synthetic-access")
  108. private static class SingletonHolder
  109. {
  110. protected static final InitialEquipmentData _instance = new InitialEquipmentData();
  111. }
  112. }