HellboundSpawns.java 4.8 KB

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