PlayerTemplateData.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.gameserver.model.Location;
  28. import com.l2jserver.gameserver.model.StatsSet;
  29. import com.l2jserver.gameserver.model.actor.templates.L2PcTemplate;
  30. import com.l2jserver.gameserver.model.base.ClassId;
  31. import com.l2jserver.util.data.xml.IXmlReader;
  32. /**
  33. * Loads player's base stats.
  34. * @author Forsaiken, Zoey76, GKR
  35. */
  36. public final class PlayerTemplateData implements IXmlReader
  37. {
  38. private final Map<ClassId, L2PcTemplate> _playerTemplates = new HashMap<>();
  39. private int _dataCount = 0;
  40. protected PlayerTemplateData()
  41. {
  42. load();
  43. }
  44. @Override
  45. public void load()
  46. {
  47. _playerTemplates.clear();
  48. parseDatapackDirectory("data/stats/chars/baseStats", false);
  49. LOGGER.info("{}: Loaded {} character templates.", getClass().getSimpleName(), _playerTemplates.size());
  50. LOGGER.info("{}: Loaded {} level up gain records.", getClass().getSimpleName(), _dataCount);
  51. }
  52. @Override
  53. public void parseDocument(Document doc)
  54. {
  55. NamedNodeMap attrs;
  56. int classId = 0;
  57. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  58. {
  59. if ("list".equalsIgnoreCase(n.getNodeName()))
  60. {
  61. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  62. {
  63. if ("classId".equalsIgnoreCase(d.getNodeName()))
  64. {
  65. classId = Integer.parseInt(d.getTextContent());
  66. }
  67. else if ("staticData".equalsIgnoreCase(d.getNodeName()))
  68. {
  69. StatsSet set = new StatsSet();
  70. set.set("classId", classId);
  71. List<Location> creationPoints = new ArrayList<>();
  72. for (Node nd = d.getFirstChild(); nd != null; nd = nd.getNextSibling())
  73. {
  74. // Skip odd nodes
  75. if (nd.getNodeName().equals("#text"))
  76. {
  77. continue;
  78. }
  79. if (nd.getChildNodes().getLength() > 1)
  80. {
  81. for (Node cnd = nd.getFirstChild(); cnd != null; cnd = cnd.getNextSibling())
  82. {
  83. // use L2CharTemplate(superclass) fields for male collision height and collision radius
  84. if (nd.getNodeName().equalsIgnoreCase("collisionMale"))
  85. {
  86. if (cnd.getNodeName().equalsIgnoreCase("radius"))
  87. {
  88. set.set("collision_radius", cnd.getTextContent());
  89. }
  90. else if (cnd.getNodeName().equalsIgnoreCase("height"))
  91. {
  92. set.set("collision_height", cnd.getTextContent());
  93. }
  94. }
  95. if ("node".equalsIgnoreCase(cnd.getNodeName()))
  96. {
  97. attrs = cnd.getAttributes();
  98. creationPoints.add(new Location(parseInteger(attrs, "x"), parseInteger(attrs, "y"), parseInteger(attrs, "z")));
  99. }
  100. else if ("walk".equalsIgnoreCase(cnd.getNodeName()))
  101. {
  102. set.set("baseWalkSpd", cnd.getTextContent());
  103. }
  104. else if ("run".equalsIgnoreCase(cnd.getNodeName()))
  105. {
  106. set.set("baseRunSpd", cnd.getTextContent());
  107. }
  108. else if ("slowSwim".equals(cnd.getNodeName()))
  109. {
  110. set.set("baseSwimWalkSpd", cnd.getTextContent());
  111. }
  112. else if ("fastSwim".equals(cnd.getNodeName()))
  113. {
  114. set.set("baseSwimRunSpd", cnd.getTextContent());
  115. }
  116. else if (!cnd.getNodeName().equals("#text"))
  117. {
  118. set.set((nd.getNodeName() + cnd.getNodeName()), cnd.getTextContent());
  119. }
  120. }
  121. }
  122. else
  123. {
  124. set.set(nd.getNodeName(), nd.getTextContent());
  125. }
  126. }
  127. // calculate total pdef and mdef from parts
  128. set.set("basePDef", (set.getInt("basePDefchest", 0) + set.getInt("basePDeflegs", 0) + set.getInt("basePDefhead", 0) + set.getInt("basePDeffeet", 0) + set.getInt("basePDefgloves", 0) + set.getInt("basePDefunderwear", 0) + set.getInt("basePDefcloak", 0)));
  129. set.set("baseMDef", (set.getInt("baseMDefrear", 0) + set.getInt("baseMDeflear", 0) + set.getInt("baseMDefrfinger", 0) + set.getInt("baseMDefrfinger", 0) + set.getInt("baseMDefneck", 0)));
  130. _playerTemplates.put(ClassId.getClassId(classId), new L2PcTemplate(set, creationPoints));
  131. }
  132. else if ("lvlUpgainData".equalsIgnoreCase(d.getNodeName()))
  133. {
  134. for (Node lvlNode = d.getFirstChild(); lvlNode != null; lvlNode = lvlNode.getNextSibling())
  135. {
  136. if ("level".equalsIgnoreCase(lvlNode.getNodeName()))
  137. {
  138. attrs = lvlNode.getAttributes();
  139. int level = parseInteger(attrs, "val");
  140. for (Node valNode = lvlNode.getFirstChild(); valNode != null; valNode = valNode.getNextSibling())
  141. {
  142. String nodeName = valNode.getNodeName();
  143. if ((nodeName.startsWith("hp") || nodeName.startsWith("mp") || nodeName.startsWith("cp")) && _playerTemplates.containsKey(ClassId.getClassId(classId)))
  144. {
  145. _playerTemplates.get(ClassId.getClassId(classId)).setUpgainValue(nodeName, level, Double.parseDouble(valNode.getTextContent()));
  146. _dataCount++;
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. public L2PcTemplate getTemplate(ClassId classId)
  157. {
  158. return _playerTemplates.get(classId);
  159. }
  160. public L2PcTemplate getTemplate(int classId)
  161. {
  162. return _playerTemplates.get(ClassId.getClassId(classId));
  163. }
  164. public static final PlayerTemplateData getInstance()
  165. {
  166. return SingletonHolder._instance;
  167. }
  168. private static class SingletonHolder
  169. {
  170. protected static final PlayerTemplateData _instance = new PlayerTemplateData();
  171. }
  172. }