NpcWalkerRoutesTable.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.datatables;
  16. import gnu.trove.map.hash.TIntObjectHashMap;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.gameserver.model.L2NpcWalkerNode;
  28. import com.l2jserver.gameserver.network.NpcStringId;
  29. /**
  30. * Main Table to Load Npc Walkers Routes and Chat SQL Table.<br>
  31. *
  32. * @author Rayan RPG for L2Emu Project, JIV
  33. *
  34. * @since 927
  35. *
  36. */
  37. public class NpcWalkerRoutesTable
  38. {
  39. private final static Logger _log = Logger.getLogger(SpawnTable.class.getName());
  40. private TIntObjectHashMap<List<L2NpcWalkerNode>> _routes = new TIntObjectHashMap<List<L2NpcWalkerNode>>();
  41. public static NpcWalkerRoutesTable getInstance()
  42. {
  43. return SingletonHolder._instance;
  44. }
  45. private NpcWalkerRoutesTable()
  46. {
  47. if (Config.ALLOW_NPC_WALKERS)
  48. {
  49. _log.info("Initializing Walkers Routes Table.");
  50. load();
  51. }
  52. }
  53. public void load()
  54. {
  55. _routes.clear();
  56. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  57. factory.setValidating(false);
  58. factory.setIgnoringComments(true);
  59. File file = new File(Config.DATAPACK_ROOT, "data/WalkerRoutes.xml");
  60. Document doc = null;
  61. if (file.exists())
  62. {
  63. try
  64. {
  65. doc = factory.newDocumentBuilder().parse(file);
  66. }
  67. catch (Exception e)
  68. {
  69. _log.log(Level.WARNING, "Could not parse WalkerRoutes.xml file: " + e.getMessage(), e);
  70. }
  71. Node n = doc.getFirstChild();
  72. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  73. {
  74. if (d.getNodeName().equals("walker"))
  75. {
  76. List<L2NpcWalkerNode> list = new ArrayList<L2NpcWalkerNode>();
  77. int npcId = Integer.parseInt(d.getAttributes().getNamedItem("npcId").getNodeValue());
  78. for (Node r = d.getFirstChild(); r != null; r = r.getNextSibling())
  79. {
  80. if (r.getNodeName().equals("route"))
  81. {
  82. NamedNodeMap attrs = r.getAttributes();
  83. int id = Integer.parseInt(attrs.getNamedItem("id").getNodeValue());
  84. int x = Integer.parseInt(attrs.getNamedItem("X").getNodeValue());
  85. int y = Integer.parseInt(attrs.getNamedItem("Y").getNodeValue());
  86. int z = Integer.parseInt(attrs.getNamedItem("Z").getNodeValue());
  87. int delay = Integer.parseInt(attrs.getNamedItem("delay").getNodeValue());
  88. String chatString = null;
  89. NpcStringId npcString = null;
  90. Node node = attrs.getNamedItem("string");
  91. if (node != null)
  92. chatString = node.getNodeValue();
  93. else
  94. {
  95. node = attrs.getNamedItem("npcString");
  96. if (node != null)
  97. {
  98. npcString = NpcStringId.getNpcStringId(node.getNodeValue());
  99. if (npcString == null)
  100. {
  101. _log.log(Level.WARNING, "NpcWalkerRoutersTable: Unknown npcstring '" + node.getNodeValue() + ".");
  102. continue;
  103. }
  104. }
  105. else
  106. {
  107. node = attrs.getNamedItem("npcStringId");
  108. if (node != null)
  109. {
  110. npcString = NpcStringId.getNpcStringId(Integer.parseInt(node.getNodeValue()));
  111. if (npcString == null)
  112. {
  113. _log.log(Level.WARNING, "NpcWalkerRoutersTable: Unknown npcstring '" + node.getNodeValue() + ".");
  114. continue;
  115. }
  116. }
  117. }
  118. }
  119. boolean running = Boolean.parseBoolean(attrs.getNamedItem("run").getNodeValue());
  120. list.add(new L2NpcWalkerNode(id, npcString, chatString, x, y, z, delay, running));
  121. }
  122. }
  123. _routes.put(npcId, list);
  124. }
  125. }
  126. }
  127. for (Object list : _routes.values())
  128. ((ArrayList<?>)list).trimToSize();
  129. _log.info("WalkerRoutesTable: Loaded " + _routes.size() + " Npc Walker Routes.");
  130. }
  131. public List<L2NpcWalkerNode> getRouteForNpc(int id)
  132. {
  133. return _routes.get(id);
  134. }
  135. @SuppressWarnings("synthetic-access")
  136. private static class SingletonHolder
  137. {
  138. protected static final NpcWalkerRoutesTable _instance = new NpcWalkerRoutesTable();
  139. }
  140. public static void main(String... arg)
  141. {
  142. getInstance().load();
  143. }
  144. }