TeleportLocationTable.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.model.L2TeleportLocation;
  30. public class TeleportLocationTable
  31. {
  32. private static Logger _log = Logger.getLogger(TeleportLocationTable.class.getName());
  33. private final Map<Integer, L2TeleportLocation> _teleports = new HashMap<>();
  34. protected TeleportLocationTable()
  35. {
  36. reloadAll();
  37. }
  38. public void reloadAll()
  39. {
  40. _teleports.clear();
  41. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  42. Statement s = con.createStatement();
  43. ResultSet rs = s.executeQuery("SELECT id, loc_x, loc_y, loc_z, price, fornoble, itemId FROM teleport"))
  44. {
  45. L2TeleportLocation teleport;
  46. while (rs.next())
  47. {
  48. teleport = new L2TeleportLocation();
  49. teleport.setTeleId(rs.getInt("id"));
  50. teleport.setLocX(rs.getInt("loc_x"));
  51. teleport.setLocY(rs.getInt("loc_y"));
  52. teleport.setLocZ(rs.getInt("loc_z"));
  53. teleport.setPrice(rs.getInt("price"));
  54. teleport.setIsForNoble(rs.getInt("fornoble") == 1);
  55. teleport.setItemId(rs.getInt("itemId"));
  56. _teleports.put(teleport.getTeleId(), teleport);
  57. }
  58. _log.info(getClass().getSimpleName() + ": Loaded " + _teleports.size() + " Teleport Location Templates.");
  59. }
  60. catch (Exception e)
  61. {
  62. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error loading Teleport Table.", e);
  63. }
  64. if (Config.CUSTOM_TELEPORT_TABLE)
  65. {
  66. int _cTeleCount = _teleports.size();
  67. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  68. Statement s = con.createStatement();
  69. ResultSet rs = s.executeQuery("SELECT id, loc_x, loc_y, loc_z, price, fornoble, itemId FROM custom_teleport"))
  70. {
  71. L2TeleportLocation teleport;
  72. while (rs.next())
  73. {
  74. teleport = new L2TeleportLocation();
  75. teleport.setTeleId(rs.getInt("id"));
  76. teleport.setLocX(rs.getInt("loc_x"));
  77. teleport.setLocY(rs.getInt("loc_y"));
  78. teleport.setLocZ(rs.getInt("loc_z"));
  79. teleport.setPrice(rs.getInt("price"));
  80. teleport.setIsForNoble(rs.getInt("fornoble") == 1);
  81. teleport.setItemId(rs.getInt("itemId"));
  82. _teleports.put(teleport.getTeleId(), teleport);
  83. }
  84. _cTeleCount = _teleports.size() - _cTeleCount;
  85. if (_cTeleCount > 0)
  86. {
  87. _log.info(getClass().getSimpleName() + ": Loaded " + _cTeleCount + " Custom Teleport Location Templates.");
  88. }
  89. }
  90. catch (Exception e)
  91. {
  92. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error while creating custom teleport table " + e.getMessage(), e);
  93. }
  94. }
  95. }
  96. /**
  97. * @param id
  98. * @return
  99. */
  100. public L2TeleportLocation getTemplate(int id)
  101. {
  102. return _teleports.get(id);
  103. }
  104. public static TeleportLocationTable getInstance()
  105. {
  106. return SingletonHolder._instance;
  107. }
  108. private static class SingletonHolder
  109. {
  110. protected static final TeleportLocationTable _instance = new TeleportLocationTable();
  111. }
  112. }