TerritoryTable.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2004-2013 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.SQLException;
  23. import java.sql.Statement;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.model.L2Territory;
  30. /**
  31. * @author Balancer, Mr
  32. */
  33. public class TerritoryTable
  34. {
  35. private static final Logger _log = Logger.getLogger(TerritoryTable.class.getName());
  36. private static final Map<Integer, L2Territory> _territory = new HashMap<>();
  37. /**
  38. * Instantiates a new territory.
  39. */
  40. protected TerritoryTable()
  41. {
  42. load();
  43. }
  44. /**
  45. * Gets the random point.
  46. * @param terr the territory Id?
  47. * @return the random point
  48. */
  49. public int[] getRandomPoint(int terr)
  50. {
  51. return _territory.get(terr).getRandomPoint();
  52. }
  53. /**
  54. * Gets the proc max.
  55. * @param terr the territory Id?
  56. * @return the proc max
  57. */
  58. public int getProcMax(int terr)
  59. {
  60. return _territory.get(terr).getProcMax();
  61. }
  62. /**
  63. * Load the data from database.
  64. */
  65. public void load()
  66. {
  67. _territory.clear();
  68. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  69. Statement stmt = con.createStatement();
  70. ResultSet rset = stmt.executeQuery("SELECT * FROM locations WHERE loc_id>0"))
  71. {
  72. while (rset.next())
  73. {
  74. int terrId = rset.getInt("loc_id");
  75. L2Territory terr = _territory.get(terrId);
  76. if (terr == null)
  77. {
  78. terr = new L2Territory(terrId);
  79. _territory.put(terrId, terr);
  80. }
  81. terr.add(rset.getInt("loc_x"), rset.getInt("loc_y"), rset.getInt("loc_zmin"), rset.getInt("loc_zmax"), rset.getInt("proc"));
  82. }
  83. _log.info("TerritoryTable: Loaded " + _territory.size() + " territories from database.");
  84. }
  85. catch (SQLException e)
  86. {
  87. _log.log(Level.SEVERE, "TerritoryTable: Failed to load territories from database!", e);
  88. }
  89. }
  90. /**
  91. * Gets the single instance of Territory.
  92. * @return single instance of Territory
  93. */
  94. public static TerritoryTable getInstance()
  95. {
  96. return SingletonHolder._instance;
  97. }
  98. private static class SingletonHolder
  99. {
  100. protected static final TerritoryTable _instance = new TerritoryTable();
  101. }
  102. }