TeleportLocationTable.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.map.hash.TIntObjectHashMap;
  17. import java.sql.Connection;
  18. import java.sql.ResultSet;
  19. import java.sql.Statement;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.model.L2TeleportLocation;
  25. /**
  26. * This class ...
  27. *
  28. * @version $Revision: 1.3.2.2.2.3 $ $Date: 2005/03/27 15:29:18 $
  29. */
  30. public class TeleportLocationTable
  31. {
  32. private static Logger _log = Logger.getLogger(TeleportLocationTable.class.getName());
  33. private TIntObjectHashMap<L2TeleportLocation> _teleports;
  34. public static TeleportLocationTable getInstance()
  35. {
  36. return SingletonHolder._instance;
  37. }
  38. protected TeleportLocationTable()
  39. {
  40. reloadAll();
  41. }
  42. public void reloadAll()
  43. {
  44. _teleports = new TIntObjectHashMap<>();
  45. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  46. Statement s = con.createStatement();
  47. ResultSet rs = s.executeQuery("SELECT id, loc_x, loc_y, loc_z, price, fornoble, itemId FROM teleport"))
  48. {
  49. L2TeleportLocation teleport;
  50. while (rs.next())
  51. {
  52. teleport = new L2TeleportLocation();
  53. teleport.setTeleId(rs.getInt("id"));
  54. teleport.setLocX(rs.getInt("loc_x"));
  55. teleport.setLocY(rs.getInt("loc_y"));
  56. teleport.setLocZ(rs.getInt("loc_z"));
  57. teleport.setPrice(rs.getInt("price"));
  58. teleport.setIsForNoble(rs.getInt("fornoble") == 1);
  59. teleport.setItemId(rs.getInt("itemId"));
  60. _teleports.put(teleport.getTeleId(), teleport);
  61. }
  62. _log.info("TeleportLocationTable: Loaded " + _teleports.size() + " Teleport Location Templates.");
  63. }
  64. catch (Exception e)
  65. {
  66. _log.log(Level.SEVERE, "Error loading Teleport Table.", e);
  67. }
  68. if (Config.CUSTOM_TELEPORT_TABLE)
  69. {
  70. int _cTeleCount = _teleports.size();
  71. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  72. Statement s = con.createStatement();
  73. ResultSet rs = s.executeQuery("SELECT id, loc_x, loc_y, loc_z, price, fornoble, itemId FROM custom_teleport"))
  74. {
  75. L2TeleportLocation teleport;
  76. while (rs.next())
  77. {
  78. teleport = new L2TeleportLocation();
  79. teleport.setTeleId(rs.getInt("id"));
  80. teleport.setLocX(rs.getInt("loc_x"));
  81. teleport.setLocY(rs.getInt("loc_y"));
  82. teleport.setLocZ(rs.getInt("loc_z"));
  83. teleport.setPrice(rs.getInt("price"));
  84. teleport.setIsForNoble(rs.getInt("fornoble") == 1);
  85. teleport.setItemId(rs.getInt("itemId"));
  86. _teleports.put(teleport.getTeleId(), teleport);
  87. }
  88. _cTeleCount = _teleports.size() - _cTeleCount;
  89. if (_cTeleCount > 0)
  90. {
  91. _log.info("TeleportLocationTable: Loaded " + _cTeleCount + " Custom Teleport Location Templates.");
  92. }
  93. }
  94. catch (Exception e)
  95. {
  96. _log.log(Level.WARNING, "Error while creating custom teleport table " + e.getMessage(), e);
  97. }
  98. }
  99. }
  100. /**
  101. * @param id
  102. * @return
  103. */
  104. public L2TeleportLocation getTemplate(int id)
  105. {
  106. return _teleports.get(id);
  107. }
  108. private static class SingletonHolder
  109. {
  110. protected static final TeleportLocationTable _instance = new TeleportLocationTable();
  111. }
  112. }