CoupleManager.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. private FastList<Couple> _couples;
  43. public void reload()
  44. {
  45. getCouples().clear();
  46. load();
  47. }
  48. private final void load()
  49. {
  50. Connection con = null;
  51. try
  52. {
  53. con = L2DatabaseFactory.getInstance().getConnection();
  54. PreparedStatement statement = con.prepareStatement("Select id from mods_wedding order by id");
  55. ResultSet rs = statement.executeQuery();
  56. while (rs.next())
  57. {
  58. getCouples().add(new Couple(rs.getInt("id")));
  59. }
  60. rs.close();
  61. statement.close();
  62. _log.info("Loaded: " + getCouples().size() + " couples(s)");
  63. }
  64. catch (Exception e)
  65. {
  66. _log.log(Level.SEVERE, "Exception: CoupleManager.load(): " + e.getMessage(), e);
  67. }
  68. finally
  69. {
  70. L2DatabaseFactory.close(con);
  71. }
  72. }
  73. public final Couple getCouple(int coupleId)
  74. {
  75. int index = getCoupleIndex(coupleId);
  76. if (index >= 0)
  77. return getCouples().get(index);
  78. return null;
  79. }
  80. public void createCouple(L2PcInstance player1, L2PcInstance player2)
  81. {
  82. if (player1 != null && player2 != null)
  83. {
  84. if (player1.getPartnerId() == 0 && player2.getPartnerId() == 0)
  85. {
  86. int _player1id = player1.getObjectId();
  87. int _player2id = player2.getObjectId();
  88. Couple _new = new Couple(player1, player2);
  89. getCouples().add(_new);
  90. player1.setPartnerId(_player2id);
  91. player2.setPartnerId(_player1id);
  92. player1.setCoupleId(_new.getId());
  93. player2.setCoupleId(_new.getId());
  94. }
  95. }
  96. }
  97. public void deleteCouple(int coupleId)
  98. {
  99. int index = getCoupleIndex(coupleId);
  100. Couple couple = getCouples().get(index);
  101. if (couple != null)
  102. {
  103. L2PcInstance player1 = L2World.getInstance().getPlayer(couple.getPlayer1Id());
  104. L2PcInstance player2 = L2World.getInstance().getPlayer(couple.getPlayer2Id());
  105. if (player1 != null)
  106. {
  107. player1.setPartnerId(0);
  108. player1.setMarried(false);
  109. player1.setCoupleId(0);
  110. }
  111. if (player2 != null)
  112. {
  113. player2.setPartnerId(0);
  114. player2.setMarried(false);
  115. player2.setCoupleId(0);
  116. }
  117. couple.divorce();
  118. getCouples().remove(index);
  119. }
  120. }
  121. public final int getCoupleIndex(int coupleId)
  122. {
  123. int i = 0;
  124. for (Couple temp : getCouples())
  125. {
  126. if (temp != null && temp.getId() == coupleId)
  127. return i;
  128. i++;
  129. }
  130. return -1;
  131. }
  132. public final FastList<Couple> getCouples()
  133. {
  134. if (_couples == null)
  135. _couples = new FastList<Couple>();
  136. return _couples;
  137. }
  138. @SuppressWarnings("synthetic-access")
  139. private static class SingletonHolder
  140. {
  141. protected static final CoupleManager _instance = new CoupleManager();
  142. }
  143. }