NpcWalkerRoutesTable.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import com.l2jserver.L2DatabaseFactory;
  22. import com.l2jserver.gameserver.model.L2NpcWalkerNode;
  23. import javolution.util.FastList;
  24. /**
  25. * Main Table to Load Npc Walkers Routes and Chat SQL Table.<br>
  26. *
  27. * @author Rayan RPG for L2Emu Project
  28. *
  29. * @since 927
  30. *
  31. */
  32. public class NpcWalkerRoutesTable
  33. {
  34. private final static Logger _log = Logger.getLogger(SpawnTable.class.getName());
  35. private FastList<L2NpcWalkerNode> _routes = new FastList<L2NpcWalkerNode>();
  36. public static NpcWalkerRoutesTable getInstance()
  37. {
  38. return SingletonHolder._instance;
  39. }
  40. private NpcWalkerRoutesTable()
  41. {
  42. _log.info("Initializing Walkers Routes Table.");
  43. }
  44. public void load()
  45. {
  46. _routes.clear();
  47. Connection con = null;
  48. try
  49. {
  50. con = L2DatabaseFactory.getInstance().getConnection();
  51. PreparedStatement statement = con.prepareStatement("SELECT route_id, npc_id, move_point, chatText, move_x, move_y, move_z, delay, running FROM walker_routes ORDER By move_point ASC");
  52. ResultSet rset = statement.executeQuery();
  53. L2NpcWalkerNode route;
  54. while (rset.next())
  55. {
  56. route = new L2NpcWalkerNode();
  57. route.setRouteId(rset.getInt("route_id"));
  58. route.setNpcId(rset.getInt("npc_id"));
  59. route.setMovePoint(rset.getString("move_point"));
  60. route.setChatText(rset.getString("chatText"));
  61. route.setMoveX(rset.getInt("move_x"));
  62. route.setMoveY(rset.getInt("move_y"));
  63. route.setMoveZ(rset.getInt("move_z"));
  64. route.setDelay(rset.getInt("delay"));
  65. route.setRunning(rset.getBoolean("running"));
  66. _routes.add(route);
  67. }
  68. rset.close();
  69. statement.close();
  70. _log.info("WalkerRoutesTable: Loaded " + _routes.size() + " Npc Walker Routes.");
  71. }
  72. catch (Exception e)
  73. {
  74. _log.log(Level.SEVERE, "WalkerRoutesTable: Error while loading Npc Walkers Routes: " + e.getMessage(), e);
  75. }
  76. finally
  77. {
  78. L2DatabaseFactory.close(con);
  79. }
  80. }
  81. public FastList<L2NpcWalkerNode> getRouteForNpc(int id)
  82. {
  83. FastList<L2NpcWalkerNode> _return = new FastList<L2NpcWalkerNode>();
  84. for (FastList.Node<L2NpcWalkerNode> n = _routes.head(), end = _routes.tail(); (n = n.getNext()) != end;)
  85. {
  86. if (n.getValue().getNpcId() == id)
  87. {
  88. _return.add(n.getValue());
  89. }
  90. }
  91. return _return;
  92. }
  93. @SuppressWarnings("synthetic-access")
  94. private static class SingletonHolder
  95. {
  96. protected static final NpcWalkerRoutesTable _instance = new NpcWalkerRoutesTable();
  97. }
  98. }