TeleportLocationTable.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.PreparedStatement;
  19. import java.sql.ResultSet;
  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. Connection con = null;
  46. try
  47. {
  48. con = L2DatabaseFactory.getInstance().getConnection();
  49. PreparedStatement statement = con.prepareStatement("SELECT id, loc_x, loc_y, loc_z, price, fornoble, itemId FROM teleport");
  50. ResultSet rset = statement.executeQuery();
  51. L2TeleportLocation teleport;
  52. while (rset.next())
  53. {
  54. teleport = new L2TeleportLocation();
  55. teleport.setTeleId(rset.getInt("id"));
  56. teleport.setLocX(rset.getInt("loc_x"));
  57. teleport.setLocY(rset.getInt("loc_y"));
  58. teleport.setLocZ(rset.getInt("loc_z"));
  59. teleport.setPrice(rset.getInt("price"));
  60. teleport.setIsForNoble(rset.getInt("fornoble") == 1);
  61. teleport.setItemId(rset.getInt("itemId"));
  62. _teleports.put(teleport.getTeleId(), teleport);
  63. }
  64. rset.close();
  65. statement.close();
  66. _log.info("TeleportLocationTable: Loaded " + _teleports.size() + " Teleport Location Templates.");
  67. }
  68. catch (Exception e)
  69. {
  70. _log.log(Level.SEVERE, "Error loading Teleport Table.", e);
  71. }
  72. finally
  73. {
  74. L2DatabaseFactory.close(con);
  75. }
  76. if (Config.CUSTOM_TELEPORT_TABLE)
  77. {
  78. try
  79. {
  80. con = L2DatabaseFactory.getInstance().getConnection();
  81. PreparedStatement statement = con.prepareStatement("SELECT id, loc_x, loc_y, loc_z, price, fornoble, itemId FROM custom_teleport");
  82. ResultSet rset = statement.executeQuery();
  83. L2TeleportLocation teleport;
  84. int _cTeleCount = _teleports.size();
  85. while (rset.next())
  86. {
  87. teleport = new L2TeleportLocation();
  88. teleport.setTeleId(rset.getInt("id"));
  89. teleport.setLocX(rset.getInt("loc_x"));
  90. teleport.setLocY(rset.getInt("loc_y"));
  91. teleport.setLocZ(rset.getInt("loc_z"));
  92. teleport.setPrice(rset.getInt("price"));
  93. teleport.setIsForNoble(rset.getInt("fornoble") == 1);
  94. teleport.setItemId(rset.getInt("itemId"));
  95. _teleports.put(teleport.getTeleId(), teleport);
  96. }
  97. rset.close();
  98. statement.close();
  99. _cTeleCount = _teleports.size() - _cTeleCount;
  100. if (_cTeleCount > 0)
  101. _log.info("TeleportLocationTable: Loaded " + _cTeleCount + " Custom Teleport Location Templates.");
  102. }
  103. catch (Exception e)
  104. {
  105. _log.log(Level.WARNING, "Error while creating custom teleport table " + e.getMessage(), e);
  106. }
  107. finally
  108. {
  109. L2DatabaseFactory.close(con);
  110. }
  111. }
  112. }
  113. /**
  114. * @param id
  115. * @return
  116. */
  117. public L2TeleportLocation getTemplate(int id)
  118. {
  119. return _teleports.get(id);
  120. }
  121. private static class SingletonHolder
  122. {
  123. protected static final TeleportLocationTable _instance = new TeleportLocationTable();
  124. }
  125. }