NpcWalkerRoutesTable.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.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. /**
  29. * Main Table to Load Npc Walkers Routes and Chat SQL Table.<br>
  30. *
  31. * @author Rayan RPG for L2Emu Project, JIV
  32. *
  33. * @since 927
  34. *
  35. */
  36. public class NpcWalkerRoutesTable
  37. {
  38. private final static Logger _log = Logger.getLogger(SpawnTable.class.getName());
  39. private TIntObjectHashMap<List<L2NpcWalkerNode>> _routes = new TIntObjectHashMap<List<L2NpcWalkerNode>>();
  40. public static NpcWalkerRoutesTable getInstance()
  41. {
  42. return SingletonHolder._instance;
  43. }
  44. private NpcWalkerRoutesTable()
  45. {
  46. if (Config.ALLOW_NPC_WALKERS)
  47. {
  48. _log.info("Initializing Walkers Routes Table.");
  49. load();
  50. }
  51. }
  52. public void load()
  53. {
  54. _routes.clear();
  55. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  56. factory.setValidating(false);
  57. factory.setIgnoringComments(true);
  58. File file = new File(Config.DATAPACK_ROOT, "data/WalkerRoutes.xml");
  59. Document doc = null;
  60. if (file.exists())
  61. {
  62. try
  63. {
  64. doc = factory.newDocumentBuilder().parse(file);
  65. }
  66. catch (Exception e)
  67. {
  68. _log.log(Level.WARNING, "Could not parse WalkerRoutes.xml file: " + e.getMessage(), e);
  69. }
  70. Node n = doc.getFirstChild();
  71. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  72. {
  73. if (d.getNodeName().equals("walker"))
  74. {
  75. List<L2NpcWalkerNode> list = new ArrayList<L2NpcWalkerNode>();
  76. int npcId = Integer.parseInt(d.getAttributes().getNamedItem("npcId").getNodeValue());
  77. for (Node r = d.getFirstChild(); r != null; r = r.getNextSibling())
  78. {
  79. if (r.getNodeName().equals("route"))
  80. {
  81. NamedNodeMap attrs = r.getAttributes();
  82. int id = Integer.parseInt(attrs.getNamedItem("id").getNodeValue());
  83. int x = Integer.parseInt(attrs.getNamedItem("X").getNodeValue());
  84. int y = Integer.parseInt(attrs.getNamedItem("Y").getNodeValue());
  85. int z = Integer.parseInt(attrs.getNamedItem("Z").getNodeValue());
  86. int delay = Integer.parseInt(attrs.getNamedItem("delay").getNodeValue());
  87. String chatString = null;
  88. int npcString = -1;
  89. Node node = attrs.getNamedItem("string");
  90. if (node != null)
  91. chatString = node.getNodeValue();
  92. else
  93. {
  94. node = attrs.getNamedItem("npcString");
  95. if (node != null)
  96. npcString = Integer.parseInt(node.getNodeValue());
  97. }
  98. boolean running = Boolean.parseBoolean(attrs.getNamedItem("run").getNodeValue());
  99. list.add(new L2NpcWalkerNode(id, npcString, chatString, x, y, z, delay, running));
  100. }
  101. }
  102. _routes.put(npcId, list);
  103. }
  104. }
  105. }
  106. for (Object list : _routes.getValues())
  107. ((ArrayList<?>)list).trimToSize();
  108. _log.info("WalkerRoutesTable: Loaded " + _routes.size() + " Npc Walker Routes.");
  109. }
  110. public List<L2NpcWalkerNode> getRouteForNpc(int id)
  111. {
  112. return _routes.get(id);
  113. }
  114. @SuppressWarnings("synthetic-access")
  115. private static class SingletonHolder
  116. {
  117. protected static final NpcWalkerRoutesTable _instance = new NpcWalkerRoutesTable();
  118. }
  119. public static void main(String... arg)
  120. {
  121. getInstance().load();
  122. }
  123. }