HellboundSpawns.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2004-2014 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack 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 DataPack 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 hellbound;
  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.gameserver.datatables.NpcData;
  27. import com.l2jserver.gameserver.datatables.SpawnTable;
  28. import com.l2jserver.gameserver.engines.DocumentParser;
  29. import com.l2jserver.gameserver.model.L2Spawn;
  30. import com.l2jserver.gameserver.model.Location;
  31. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  32. /**
  33. * Hellbound Spawns parser.
  34. * @author Zoey76
  35. */
  36. public final class HellboundSpawns extends DocumentParser
  37. {
  38. private final List<L2Spawn> _spawns = new ArrayList<>();
  39. private final Map<Integer, int[]> _spawnLevels = new HashMap<>();
  40. public HellboundSpawns()
  41. {
  42. load();
  43. }
  44. @Override
  45. public void load()
  46. {
  47. _spawns.clear();
  48. _spawnLevels.clear();
  49. parseDatapackFile("data/scripts/hellbound/hellboundSpawns.xml");
  50. _log.info(getClass().getSimpleName() + ": Loaded " + _spawns.size() + " Hellbound spawns.");
  51. }
  52. @Override
  53. protected void parseDocument()
  54. {
  55. for (Node node = getCurrentDocument().getFirstChild(); node != null; node = node.getNextSibling())
  56. {
  57. if ("list".equals(node.getNodeName()))
  58. {
  59. for (Node npc = node.getFirstChild(); npc != null; npc = npc.getNextSibling())
  60. {
  61. parseSpawn(npc);
  62. }
  63. }
  64. }
  65. }
  66. /**
  67. * Parses the spawn.
  68. * @param npc the NPC to parse
  69. */
  70. private void parseSpawn(Node npc)
  71. {
  72. if ("npc".equals(npc.getNodeName()))
  73. {
  74. final Node id = npc.getAttributes().getNamedItem("id");
  75. if (id == null)
  76. {
  77. _log.severe(getClass().getSimpleName() + ": Missing NPC ID, skipping record!");
  78. return;
  79. }
  80. final int npcId = Integer.parseInt(id.getNodeValue());
  81. final L2NpcTemplate template = NpcData.getInstance().getTemplate(npcId);
  82. if (template == null)
  83. {
  84. _log.warning(getClass().getSimpleName() + ": Missing NPC template for ID: " + npcId + "!");
  85. return;
  86. }
  87. Location loc = null;
  88. int delay = 0;
  89. int randomInterval = 0;
  90. int minLevel = 1;
  91. int maxLevel = 100;
  92. for (Node element = npc.getFirstChild(); element != null; element = element.getNextSibling())
  93. {
  94. final NamedNodeMap attrs = element.getAttributes();
  95. minLevel = 1;
  96. maxLevel = 100;
  97. switch (element.getNodeName())
  98. {
  99. case "location":
  100. {
  101. loc = new Location(parseInteger(attrs, "x"), parseInteger(attrs, "y"), parseInteger(attrs, "z"), parseInteger(attrs, "heading", 0));
  102. break;
  103. }
  104. case "respawn":
  105. {
  106. delay = parseInteger(attrs, "delay");
  107. randomInterval = attrs.getNamedItem("randomInterval") != null ? parseInteger(attrs, "randomInterval") : 1;
  108. break;
  109. }
  110. case "hellboundLevel":
  111. {
  112. minLevel = parseInteger(attrs, "min", 1);
  113. maxLevel = parseInteger(attrs, "max", 100);
  114. break;
  115. }
  116. }
  117. }
  118. try
  119. {
  120. final L2Spawn spawn = new L2Spawn(template);
  121. spawn.setAmount(1);
  122. if (loc == null)
  123. {
  124. _log.warning("location is null");
  125. }
  126. spawn.setLocation(loc);
  127. spawn.setRespawnDelay(delay, randomInterval);
  128. _spawnLevels.put(npcId, new int[]
  129. {
  130. minLevel,
  131. maxLevel
  132. });
  133. SpawnTable.getInstance().addNewSpawn(spawn, false);
  134. _spawns.add(spawn);
  135. }
  136. catch (SecurityException | ClassNotFoundException | NoSuchMethodException e)
  137. {
  138. _log.warning(getClass().getSimpleName() + ": Couldn't load spawns: " + e.getMessage());
  139. }
  140. }
  141. }
  142. /**
  143. * Gets all Hellbound spawns.
  144. * @return the list of Hellbound spawns.
  145. */
  146. public List<L2Spawn> getSpawns()
  147. {
  148. return _spawns;
  149. }
  150. /**
  151. * Gets the spawn minimum level.
  152. * @param npcId the NPC ID
  153. * @return the spawn minimum level
  154. */
  155. public int getSpawnMinLevel(int npcId)
  156. {
  157. return _spawnLevels.containsKey(npcId) ? _spawnLevels.get(npcId)[0] : 1;
  158. }
  159. /**
  160. * Gets the spawn maximum level.
  161. * @param npcId the NPC ID
  162. * @return the spawn maximum level
  163. */
  164. public int getSpawnMaxLevel(int npcId)
  165. {
  166. return _spawnLevels.containsKey(npcId) ? _spawnLevels.get(npcId)[1] : 1;
  167. }
  168. public static HellboundSpawns getInstance()
  169. {
  170. return SingletonHolder.INSTANCE;
  171. }
  172. private static class SingletonHolder
  173. {
  174. protected static final HellboundSpawns INSTANCE = new HellboundSpawns();
  175. }
  176. }