CoupleManager.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javolution.util.FastList;
  26. import com.l2jserver.L2DatabaseFactory;
  27. import com.l2jserver.gameserver.model.L2World;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.model.entity.Couple;
  30. /**
  31. * @author evill33t
  32. */
  33. public final class CoupleManager
  34. {
  35. private static final Logger _log = Logger.getLogger(CoupleManager.class.getName());
  36. private FastList<Couple> _couples;
  37. protected CoupleManager()
  38. {
  39. load();
  40. }
  41. public void reload()
  42. {
  43. getCouples().clear();
  44. load();
  45. }
  46. private final void load()
  47. {
  48. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  49. Statement ps = con.createStatement();
  50. ResultSet rs = ps.executeQuery("SELECT id FROM mods_wedding ORDER BY id"))
  51. {
  52. while (rs.next())
  53. {
  54. getCouples().add(new Couple(rs.getInt("id")));
  55. }
  56. _log.info(getClass().getSimpleName() + ": Loaded: " + getCouples().size() + " couples(s)");
  57. }
  58. catch (Exception e)
  59. {
  60. _log.log(Level.SEVERE, "Exception: CoupleManager.load(): " + e.getMessage(), e);
  61. }
  62. }
  63. public final Couple getCouple(int coupleId)
  64. {
  65. int index = getCoupleIndex(coupleId);
  66. if (index >= 0)
  67. {
  68. return getCouples().get(index);
  69. }
  70. return null;
  71. }
  72. public void createCouple(L2PcInstance player1, L2PcInstance player2)
  73. {
  74. if ((player1 != null) && (player2 != null))
  75. {
  76. if ((player1.getPartnerId() == 0) && (player2.getPartnerId() == 0))
  77. {
  78. int _player1id = player1.getObjectId();
  79. int _player2id = player2.getObjectId();
  80. Couple _new = new Couple(player1, player2);
  81. getCouples().add(_new);
  82. player1.setPartnerId(_player2id);
  83. player2.setPartnerId(_player1id);
  84. player1.setCoupleId(_new.getId());
  85. player2.setCoupleId(_new.getId());
  86. }
  87. }
  88. }
  89. public void deleteCouple(int coupleId)
  90. {
  91. int index = getCoupleIndex(coupleId);
  92. Couple couple = getCouples().get(index);
  93. if (couple != null)
  94. {
  95. L2PcInstance player1 = L2World.getInstance().getPlayer(couple.getPlayer1Id());
  96. L2PcInstance player2 = L2World.getInstance().getPlayer(couple.getPlayer2Id());
  97. if (player1 != null)
  98. {
  99. player1.setPartnerId(0);
  100. player1.setMarried(false);
  101. player1.setCoupleId(0);
  102. }
  103. if (player2 != null)
  104. {
  105. player2.setPartnerId(0);
  106. player2.setMarried(false);
  107. player2.setCoupleId(0);
  108. }
  109. couple.divorce();
  110. getCouples().remove(index);
  111. }
  112. }
  113. public final int getCoupleIndex(int coupleId)
  114. {
  115. int i = 0;
  116. for (Couple temp : getCouples())
  117. {
  118. if ((temp != null) && (temp.getId() == coupleId))
  119. {
  120. return i;
  121. }
  122. i++;
  123. }
  124. return -1;
  125. }
  126. public final FastList<Couple> getCouples()
  127. {
  128. if (_couples == null)
  129. {
  130. _couples = new FastList<>();
  131. }
  132. return _couples;
  133. }
  134. public static final CoupleManager getInstance()
  135. {
  136. return SingletonHolder._instance;
  137. }
  138. private static class SingletonHolder
  139. {
  140. protected static final CoupleManager _instance = new CoupleManager();
  141. }
  142. }