2
0

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