NpcWalkerRoutesTable.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.<br>
  31. * @author Rayan, JIV
  32. */
  33. public class NpcWalkerRoutesTable
  34. {
  35. private final static Logger _log = Logger.getLogger(NpcWalkerRoutesTable.class.getName());
  36. private final TIntObjectHashMap<List<L2NpcWalkerNode>> _routes = new TIntObjectHashMap<List<L2NpcWalkerNode>>();
  37. public static NpcWalkerRoutesTable getInstance()
  38. {
  39. return SingletonHolder._instance;
  40. }
  41. protected NpcWalkerRoutesTable()
  42. {
  43. if (Config.ALLOW_NPC_WALKERS)
  44. {
  45. _log.info("Initializing Walkers Routes Table.");
  46. load();
  47. }
  48. }
  49. public void load()
  50. {
  51. _routes.clear();
  52. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  53. factory.setValidating(false);
  54. factory.setIgnoringComments(true);
  55. File file = new File(Config.DATAPACK_ROOT, "data/WalkerRoutes.xml");
  56. Document doc = null;
  57. if (file.exists())
  58. {
  59. try
  60. {
  61. doc = factory.newDocumentBuilder().parse(file);
  62. }
  63. catch (Exception e)
  64. {
  65. _log.log(Level.WARNING, "Could not parse WalkerRoutes.xml file: " + e.getMessage(), e);
  66. return;
  67. }
  68. Node n = doc.getFirstChild();
  69. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  70. {
  71. if (d.getNodeName().equals("walker"))
  72. {
  73. List<L2NpcWalkerNode> list = new ArrayList<L2NpcWalkerNode>();
  74. int npcId = Integer.parseInt(d.getAttributes().getNamedItem("npcId").getNodeValue());
  75. for (Node r = d.getFirstChild(); r != null; r = r.getNextSibling())
  76. {
  77. if (r.getNodeName().equals("route"))
  78. {
  79. NamedNodeMap attrs = r.getAttributes();
  80. int id = Integer.parseInt(attrs.getNamedItem("id").getNodeValue());
  81. int x = Integer.parseInt(attrs.getNamedItem("X").getNodeValue());
  82. int y = Integer.parseInt(attrs.getNamedItem("Y").getNodeValue());
  83. int z = Integer.parseInt(attrs.getNamedItem("Z").getNodeValue());
  84. int delay = Integer.parseInt(attrs.getNamedItem("delay").getNodeValue());
  85. String chatString = null;
  86. NpcStringId npcString = null;
  87. Node node = attrs.getNamedItem("string");
  88. if (node != null)
  89. {
  90. chatString = node.getNodeValue();
  91. }
  92. else
  93. {
  94. node = attrs.getNamedItem("npcString");
  95. if (node != null)
  96. {
  97. npcString = NpcStringId.getNpcStringId(node.getNodeValue());
  98. if (npcString == null)
  99. {
  100. _log.log(Level.WARNING, "NpcWalkerRoutersTable: Unknown npcstring '" + node.getNodeValue() + ".");
  101. continue;
  102. }
  103. }
  104. else
  105. {
  106. node = attrs.getNamedItem("npcStringId");
  107. if (node != null)
  108. {
  109. npcString = NpcStringId.getNpcStringId(Integer.parseInt(node.getNodeValue()));
  110. if (npcString == null)
  111. {
  112. _log.log(Level.WARNING, "NpcWalkerRoutersTable: Unknown npcstring '" + node.getNodeValue() + ".");
  113. continue;
  114. }
  115. }
  116. }
  117. }
  118. boolean running = Boolean.parseBoolean(attrs.getNamedItem("run").getNodeValue());
  119. list.add(new L2NpcWalkerNode(id, npcString, chatString, x, y, z, delay, running));
  120. }
  121. }
  122. // ArrayList has initial capacity of 10, let's trim them to size before putting it into the map.
  123. ((ArrayList<L2NpcWalkerNode>) list).trimToSize();
  124. _routes.put(npcId, list);
  125. }
  126. }
  127. }
  128. _log.info("WalkerRoutesTable: Loaded " + _routes.size() + " Npc Walker Routes.");
  129. }
  130. public List<L2NpcWalkerNode> getRouteForNpc(int id)
  131. {
  132. return _routes.get(id);
  133. }
  134. private static class SingletonHolder
  135. {
  136. protected static final NpcWalkerRoutesTable _instance = new NpcWalkerRoutesTable();
  137. }
  138. }