NpcWalkerRoutesData.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.logging.Level;
  21. import org.w3c.dom.NamedNodeMap;
  22. import org.w3c.dom.Node;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.engines.DocumentParser;
  25. import com.l2jserver.gameserver.model.L2NpcWalkerNode;
  26. import com.l2jserver.gameserver.network.NpcStringId;
  27. /**
  28. * Main Table to Load Npc Walkers Routes and Chat.
  29. * @author Rayan, JIV
  30. */
  31. public class NpcWalkerRoutesData extends DocumentParser
  32. {
  33. private static final Map<Integer, List<L2NpcWalkerNode>> _routes = new HashMap<>();
  34. protected NpcWalkerRoutesData()
  35. {
  36. if (Config.ALLOW_NPC_WALKERS)
  37. {
  38. load();
  39. }
  40. }
  41. @Override
  42. public void load()
  43. {
  44. _routes.clear();
  45. parseDatapackFile("data/WalkerRoutes.xml");
  46. _log.info(getClass().getSimpleName() + ": Loaded " + _routes.size() + " Npc Walker Routes.");
  47. }
  48. @Override
  49. protected void parseDocument()
  50. {
  51. final Node n = getCurrentDocument().getFirstChild();
  52. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  53. {
  54. if (d.getNodeName().equals("walker"))
  55. {
  56. List<L2NpcWalkerNode> list = new ArrayList<>(5);
  57. final Integer npcId = parseInteger(d.getAttributes(), "npcId");
  58. for (Node r = d.getFirstChild(); r != null; r = r.getNextSibling())
  59. {
  60. if (r.getNodeName().equals("route"))
  61. {
  62. NamedNodeMap attrs = r.getAttributes();
  63. int id = parseInt(attrs, "id");
  64. int x = parseInt(attrs, "X");
  65. int y = parseInt(attrs, "Y");
  66. int z = parseInt(attrs, "Z");
  67. int delay = parseInt(attrs, "delay");
  68. String chatString = null;
  69. NpcStringId npcString = null;
  70. Node node = attrs.getNamedItem("string");
  71. if (node != null)
  72. {
  73. chatString = node.getNodeValue();
  74. }
  75. else
  76. {
  77. node = attrs.getNamedItem("npcString");
  78. if (node != null)
  79. {
  80. npcString = NpcStringId.getNpcStringId(node.getNodeValue());
  81. if (npcString == null)
  82. {
  83. _log.log(Level.WARNING, "NpcWalkerRoutersTable: Unknown npcstring '" + node.getNodeValue() + ".");
  84. continue;
  85. }
  86. }
  87. else
  88. {
  89. node = attrs.getNamedItem("npcStringId");
  90. if (node != null)
  91. {
  92. npcString = NpcStringId.getNpcStringId(parseInt(node));
  93. if (npcString == null)
  94. {
  95. _log.log(Level.WARNING, "NpcWalkerRoutersTable: Unknown npcstring '" + node.getNodeValue() + ".");
  96. continue;
  97. }
  98. }
  99. }
  100. }
  101. list.add(new L2NpcWalkerNode(id, npcString, chatString, x, y, z, delay, parseBoolean(attrs, "run")));
  102. }
  103. }
  104. _routes.put(npcId, list);
  105. }
  106. }
  107. }
  108. public List<L2NpcWalkerNode> getRouteForNpc(int id)
  109. {
  110. return _routes.get(id);
  111. }
  112. public static NpcWalkerRoutesData getInstance()
  113. {
  114. return SingletonHolder._instance;
  115. }
  116. private static class SingletonHolder
  117. {
  118. protected static final NpcWalkerRoutesData _instance = new NpcWalkerRoutesData();
  119. }
  120. }