CoupleManager.java 5.0 KB

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