NpcWalkerRoutesTable.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver.datatables;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import javolution.util.FastList;
  23. import javolution.util.FastMap;
  24. import net.sf.l2j.L2DatabaseFactory;
  25. import net.sf.l2j.gameserver.model.L2NpcWalkerNode;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. /**
  29. * Main Table to Load Npc Walkers Routes and Chat SQL Table.<br>
  30. *
  31. * @author Rayan RPG for L2Emu Project
  32. *
  33. * @since 927
  34. *
  35. */
  36. public class NpcWalkerRoutesTable
  37. {
  38. private final static Log _log = LogFactory.getLog(SpawnTable.class.getName());
  39. private static NpcWalkerRoutesTable _instance;
  40. private FastList<L2NpcWalkerNode> _routes;
  41. public static NpcWalkerRoutesTable getInstance()
  42. {
  43. if(_instance == null)
  44. {
  45. _instance = new NpcWalkerRoutesTable();
  46. _log.info("Initializing Walkers Routes Table.");
  47. }
  48. return _instance;
  49. }
  50. private NpcWalkerRoutesTable()
  51. {
  52. }
  53. //FIXME: NPE while loading. :S
  54. public void load()
  55. {
  56. _routes = new FastList<L2NpcWalkerNode>();
  57. java.sql.Connection con = null;
  58. try
  59. {
  60. con = L2DatabaseFactory.getInstance().getConnection();
  61. PreparedStatement statement = con.prepareStatement("SELECT route_id, npc_id, move_point, chatText, move_x, move_y, move_z, delay, running FROM walker_routes");
  62. ResultSet rset = statement.executeQuery();
  63. L2NpcWalkerNode route;
  64. while (rset.next())
  65. {
  66. route = new L2NpcWalkerNode();
  67. route.setRouteId(rset.getInt("route_id"));
  68. route.setNpcId(rset.getInt("npc_id"));
  69. route.setMovePoint(rset.getString("move_point"));
  70. route.setChatText(rset.getString("chatText"));
  71. route.setMoveX(rset.getInt("move_x"));
  72. route.setMoveY(rset.getInt("move_y"));
  73. route.setMoveZ(rset.getInt("move_z"));
  74. route.setDelay(rset.getInt("delay"));
  75. route.setRunning(rset.getBoolean("running"));
  76. _routes.add(route);
  77. }
  78. rset.close();
  79. statement.close();
  80. _log.info("WalkerRoutesTable: Loaded "+_routes.size()+" Npc Walker Routes.");
  81. rset.close();
  82. statement.close();
  83. }
  84. catch (Exception e)
  85. {
  86. _log.fatal("WalkerRoutesTable: Error while loading Npc Walkers Routes: "+e.getMessage());
  87. }
  88. finally
  89. {
  90. try
  91. {
  92. con.close();
  93. }
  94. catch (Exception e) {}
  95. }
  96. }
  97. public FastList<L2NpcWalkerNode> getRouteForNpc(int id)
  98. {
  99. FastList<L2NpcWalkerNode> _return = new FastList<L2NpcWalkerNode>();
  100. for (FastList.Node<L2NpcWalkerNode> n = _routes.head(), end = _routes.tail(); (n = n.getNext()) != end;) {
  101. if(n.getValue().getNpcId() == id)
  102. {
  103. _return.add(n.getValue());
  104. }
  105. }
  106. return _return;
  107. }
  108. }