2
0

InitialEquipmentData.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2004-2013 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.datatables;
  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.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.gameserver.engines.DocumentParser;
  28. import com.l2jserver.gameserver.model.StatsSet;
  29. import com.l2jserver.gameserver.model.base.ClassId;
  30. import com.l2jserver.gameserver.model.items.PcItemTemplate;
  31. /**
  32. * This class holds the Initial Equipment information.<br>
  33. * What items get each newly created character and if this item is equipped upon creation (<b>Requires the item to be equippable</b>).
  34. * @author Zoey76
  35. */
  36. public final class InitialEquipmentData extends DocumentParser
  37. {
  38. private static final String NORMAL = "data/stats/initialEquipment.xml";
  39. private static final String EVENT = "data/stats/initialEquipmentEvent.xml";
  40. private final Map<ClassId, List<PcItemTemplate>> _initialEquipmentList = new HashMap<>();
  41. /**
  42. * Instantiates a new initial equipment data.
  43. */
  44. protected InitialEquipmentData()
  45. {
  46. load();
  47. }
  48. @Override
  49. public void load()
  50. {
  51. _initialEquipmentList.clear();
  52. parseDatapackFile(Config.INITIAL_EQUIPMENT_EVENT ? EVENT : NORMAL);
  53. _log.info(getClass().getSimpleName() + ": Loaded " + _initialEquipmentList.size() + " Initial Equipment data.");
  54. }
  55. @Override
  56. protected void parseDocument()
  57. {
  58. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  59. {
  60. if ("list".equalsIgnoreCase(n.getNodeName()))
  61. {
  62. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  63. {
  64. if ("equipment".equalsIgnoreCase(d.getNodeName()))
  65. {
  66. parseEquipment(d);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * Parses the equipment.
  74. * @param d parse an initial equipment and add it to {@link #_initialEquipmentList}
  75. */
  76. private void parseEquipment(Node d)
  77. {
  78. NamedNodeMap attrs = d.getAttributes();
  79. Node attr;
  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. 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. }