InitialEquipmentData.java 4.2 KB

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