NpcWalkerRoutesTable.java 3.4 KB

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