CoupleManager.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. try
  79. {
  80. con.close();
  81. }
  82. catch (Exception e)
  83. {
  84. }
  85. }
  86. }
  87. // =========================================================
  88. // Property - Public
  89. public final Couple getCouple(int coupleId)
  90. {
  91. int index = getCoupleIndex(coupleId);
  92. if (index >= 0)
  93. return getCouples().get(index);
  94. return null;
  95. }
  96. public void createCouple(L2PcInstance player1, L2PcInstance player2)
  97. {
  98. if (player1 != null && player2 != null)
  99. {
  100. if (player1.getPartnerId() == 0 && player2.getPartnerId() == 0)
  101. {
  102. int _player1id = player1.getObjectId();
  103. int _player2id = player2.getObjectId();
  104. Couple _new = new Couple(player1, player2);
  105. getCouples().add(_new);
  106. player1.setPartnerId(_player2id);
  107. player2.setPartnerId(_player1id);
  108. player1.setCoupleId(_new.getId());
  109. player2.setCoupleId(_new.getId());
  110. }
  111. }
  112. }
  113. public void deleteCouple(int coupleId)
  114. {
  115. int index = getCoupleIndex(coupleId);
  116. Couple couple = getCouples().get(index);
  117. if (couple != null)
  118. {
  119. L2PcInstance player1 = (L2PcInstance) L2World.getInstance().findObject(couple.getPlayer1Id());
  120. L2PcInstance player2 = (L2PcInstance) L2World.getInstance().findObject(couple.getPlayer2Id());
  121. if (player1 != null)
  122. {
  123. player1.setPartnerId(0);
  124. player1.setMarried(false);
  125. player1.setCoupleId(0);
  126. }
  127. if (player2 != null)
  128. {
  129. player2.setPartnerId(0);
  130. player2.setMarried(false);
  131. player2.setCoupleId(0);
  132. }
  133. couple.divorce();
  134. getCouples().remove(index);
  135. }
  136. }
  137. public final int getCoupleIndex(int coupleId)
  138. {
  139. int i = 0;
  140. for (Couple temp : getCouples())
  141. {
  142. if (temp != null && temp.getId() == coupleId)
  143. return i;
  144. i++;
  145. }
  146. return -1;
  147. }
  148. public final FastList<Couple> getCouples()
  149. {
  150. if (_couples == null)
  151. _couples = new FastList<Couple>();
  152. return _couples;
  153. }
  154. @SuppressWarnings("synthetic-access")
  155. private static class SingletonHolder
  156. {
  157. protected static final CoupleManager _instance = new CoupleManager();
  158. }
  159. }