CoupleManager.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.instancemanager;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastList;
  22. import com.l2jserver.L2DatabaseFactory;
  23. import com.l2jserver.gameserver.model.L2World;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.model.entity.Couple;
  26. /**
  27. * @author evill33t
  28. *
  29. */
  30. public class CoupleManager
  31. {
  32. private static final Logger _log = Logger.getLogger(CoupleManager.class.getName());
  33. private CoupleManager()
  34. {
  35. _log.info("L2JMOD: Initializing CoupleManager");
  36. load();
  37. }
  38. public static final CoupleManager getInstance()
  39. {
  40. return SingletonHolder._instance;
  41. }
  42. // =========================================================
  43. // =========================================================
  44. // Data Field
  45. private FastList<Couple> _couples;
  46. // =========================================================
  47. // Method - Public
  48. public void reload()
  49. {
  50. getCouples().clear();
  51. load();
  52. }
  53. // =========================================================
  54. // Method - Private
  55. private final void load()
  56. {
  57. Connection con = null;
  58. try
  59. {
  60. con = L2DatabaseFactory.getInstance().getConnection();
  61. PreparedStatement statement = con.prepareStatement("Select id from mods_wedding order by id");
  62. ResultSet rs = statement.executeQuery();
  63. while (rs.next())
  64. {
  65. getCouples().add(new Couple(rs.getInt("id")));
  66. }
  67. rs.close();
  68. statement.close();
  69. _log.info("Loaded: " + getCouples().size() + " couples(s)");
  70. }
  71. catch (Exception e)
  72. {
  73. _log.log(Level.SEVERE, "Exception: CoupleManager.load(): " + e.getMessage(), e);
  74. }
  75. finally
  76. {
  77. L2DatabaseFactory.close(con);
  78. }
  79. }
  80. // =========================================================
  81. // Property - Public
  82. public final Couple getCouple(int coupleId)
  83. {
  84. int index = getCoupleIndex(coupleId);
  85. if (index >= 0)
  86. return getCouples().get(index);
  87. return null;
  88. }
  89. public void createCouple(L2PcInstance player1, L2PcInstance player2)
  90. {
  91. if (player1 != null && player2 != null)
  92. {
  93. if (player1.getPartnerId() == 0 && player2.getPartnerId() == 0)
  94. {
  95. int _player1id = player1.getObjectId();
  96. int _player2id = player2.getObjectId();
  97. Couple _new = new Couple(player1, player2);
  98. getCouples().add(_new);
  99. player1.setPartnerId(_player2id);
  100. player2.setPartnerId(_player1id);
  101. player1.setCoupleId(_new.getId());
  102. player2.setCoupleId(_new.getId());
  103. }
  104. }
  105. }
  106. public void deleteCouple(int coupleId)
  107. {
  108. int index = getCoupleIndex(coupleId);
  109. Couple couple = getCouples().get(index);
  110. if (couple != null)
  111. {
  112. L2PcInstance player1 = L2World.getInstance().getPlayer(couple.getPlayer1Id());
  113. L2PcInstance player2 = L2World.getInstance().getPlayer(couple.getPlayer2Id());
  114. if (player1 != null)
  115. {
  116. player1.setPartnerId(0);
  117. player1.setMarried(false);
  118. player1.setCoupleId(0);
  119. }
  120. if (player2 != null)
  121. {
  122. player2.setPartnerId(0);
  123. player2.setMarried(false);
  124. player2.setCoupleId(0);
  125. }
  126. couple.divorce();
  127. getCouples().remove(index);
  128. }
  129. }
  130. public final int getCoupleIndex(int coupleId)
  131. {
  132. int i = 0;
  133. for (Couple temp : getCouples())
  134. {
  135. if (temp != null && temp.getId() == coupleId)
  136. return i;
  137. i++;
  138. }
  139. return -1;
  140. }
  141. public final FastList<Couple> getCouples()
  142. {
  143. if (_couples == null)
  144. _couples = new FastList<Couple>();
  145. return _couples;
  146. }
  147. @SuppressWarnings("synthetic-access")
  148. private static class SingletonHolder
  149. {
  150. protected static final CoupleManager _instance = new CoupleManager();
  151. }
  152. }