CoupleManager.java 3.9 KB

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