HellboundSpawns.java 5.2 KB

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