CoupleManager.java 4.5 KB

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