CoupleManager.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 com.l2jserver.L2DatabaseFactory;
  22. import com.l2jserver.gameserver.model.L2World;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.model.entity.Couple;
  25. import javolution.util.FastList;
  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. PreparedStatement statement;
  61. ResultSet rs;
  62. con = L2DatabaseFactory.getInstance().getConnection();
  63. statement = con.prepareStatement("Select id from mods_wedding order by id");
  64. rs = statement.executeQuery();
  65. while (rs.next())
  66. {
  67. getCouples().add(new Couple(rs.getInt("id")));
  68. }
  69. statement.close();
  70. _log.info("Loaded: " + getCouples().size() + " couples(s)");
  71. }
  72. catch (Exception e)
  73. {
  74. _log.log(Level.SEVERE, "Exception: CoupleManager.load(): " + e.getMessage(), e);
  75. }
  76. finally
  77. {
  78. L2DatabaseFactory.close(con);
  79. }
  80. }
  81. // =========================================================
  82. // Property - Public
  83. public final Couple getCouple(int coupleId)
  84. {
  85. int index = getCoupleIndex(coupleId);
  86. if (index >= 0)
  87. return getCouples().get(index);
  88. return null;
  89. }
  90. public void createCouple(L2PcInstance player1, L2PcInstance player2)
  91. {
  92. if (player1 != null && player2 != null)
  93. {
  94. if (player1.getPartnerId() == 0 && player2.getPartnerId() == 0)
  95. {
  96. int _player1id = player1.getObjectId();
  97. int _player2id = player2.getObjectId();
  98. Couple _new = new Couple(player1, player2);
  99. getCouples().add(_new);
  100. player1.setPartnerId(_player2id);
  101. player2.setPartnerId(_player1id);
  102. player1.setCoupleId(_new.getId());
  103. player2.setCoupleId(_new.getId());
  104. }
  105. }
  106. }
  107. public void deleteCouple(int coupleId)
  108. {
  109. int index = getCoupleIndex(coupleId);
  110. Couple couple = getCouples().get(index);
  111. if (couple != null)
  112. {
  113. L2PcInstance player1 = L2World.getInstance().getPlayer(couple.getPlayer1Id());
  114. L2PcInstance player2 = L2World.getInstance().getPlayer(couple.getPlayer2Id());
  115. if (player1 != null)
  116. {
  117. player1.setPartnerId(0);
  118. player1.setMarried(false);
  119. player1.setCoupleId(0);
  120. }
  121. if (player2 != null)
  122. {
  123. player2.setPartnerId(0);
  124. player2.setMarried(false);
  125. player2.setCoupleId(0);
  126. }
  127. couple.divorce();
  128. getCouples().remove(index);
  129. }
  130. }
  131. public final int getCoupleIndex(int coupleId)
  132. {
  133. int i = 0;
  134. for (Couple temp : getCouples())
  135. {
  136. if (temp != null && temp.getId() == coupleId)
  137. return i;
  138. i++;
  139. }
  140. return -1;
  141. }
  142. public final FastList<Couple> getCouples()
  143. {
  144. if (_couples == null)
  145. _couples = new FastList<Couple>();
  146. return _couples;
  147. }
  148. @SuppressWarnings("synthetic-access")
  149. private static class SingletonHolder
  150. {
  151. protected static final CoupleManager _instance = new CoupleManager();
  152. }
  153. }