CoupleManager.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2004-2015 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.List;
  24. import java.util.concurrent.CopyOnWriteArrayList;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  28. import com.l2jserver.gameserver.model.L2World;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.model.entity.Couple;
  31. /**
  32. * @author evill33t
  33. */
  34. public final class CoupleManager
  35. {
  36. private static final Logger _log = Logger.getLogger(CoupleManager.class.getName());
  37. private final List<Couple> _couples = new CopyOnWriteArrayList<>();
  38. protected CoupleManager()
  39. {
  40. load();
  41. }
  42. public void reload()
  43. {
  44. _couples.clear();
  45. load();
  46. }
  47. private final void load()
  48. {
  49. try (Connection con = ConnectionFactory.getInstance().getConnection();
  50. Statement ps = con.createStatement();
  51. ResultSet rs = ps.executeQuery("SELECT id FROM mods_wedding ORDER BY id"))
  52. {
  53. while (rs.next())
  54. {
  55. getCouples().add(new Couple(rs.getInt("id")));
  56. }
  57. _log.info(getClass().getSimpleName() + ": Loaded: " + getCouples().size() + " couples(s)");
  58. }
  59. catch (Exception e)
  60. {
  61. _log.log(Level.SEVERE, "Exception: CoupleManager.load(): " + e.getMessage(), e);
  62. }
  63. }
  64. public final Couple getCouple(int coupleId)
  65. {
  66. int index = getCoupleIndex(coupleId);
  67. if (index >= 0)
  68. {
  69. return getCouples().get(index);
  70. }
  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 couple = new Couple(player1, player2);
  82. getCouples().add(couple);
  83. player1.setPartnerId(player2id);
  84. player2.setPartnerId(player1id);
  85. player1.setCoupleId(couple.getId());
  86. player2.setCoupleId(couple.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. {
  121. return i;
  122. }
  123. i++;
  124. }
  125. return -1;
  126. }
  127. public final List<Couple> getCouples()
  128. {
  129. return _couples;
  130. }
  131. public static final CoupleManager getInstance()
  132. {
  133. return SingletonHolder._instance;
  134. }
  135. private static class SingletonHolder
  136. {
  137. protected static final CoupleManager _instance = new CoupleManager();
  138. }
  139. }