Logout.java 4.7 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 net.sf.l2j.gameserver.clientpackets;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.logging.Logger;
  19. import net.sf.l2j.Config;
  20. import net.sf.l2j.L2DatabaseFactory;
  21. import net.sf.l2j.gameserver.Olympiad;
  22. import net.sf.l2j.gameserver.SevenSignsFestival;
  23. import net.sf.l2j.gameserver.communitybbs.Manager.RegionBBSManager;
  24. import net.sf.l2j.gameserver.datatables.SkillTable;
  25. import net.sf.l2j.gameserver.model.L2Party;
  26. import net.sf.l2j.gameserver.model.L2World;
  27. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  28. import net.sf.l2j.gameserver.model.entity.TvTEvent;
  29. import net.sf.l2j.gameserver.network.SystemMessageId;
  30. import net.sf.l2j.gameserver.serverpackets.ActionFailed;
  31. import net.sf.l2j.gameserver.serverpackets.FriendList;
  32. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  33. import net.sf.l2j.gameserver.taskmanager.AttackStanceTaskManager;
  34. /**
  35. * This class ...
  36. *
  37. * @version $Revision: 1.9.4.3 $ $Date: 2005/03/27 15:29:30 $
  38. */
  39. public final class Logout extends L2GameClientPacket
  40. {
  41. private static final String _C__09_LOGOUT = "[C] 09 Logout";
  42. private static Logger _log = Logger.getLogger(Logout.class.getName());
  43. @Override
  44. protected void readImpl()
  45. {
  46. }
  47. @Override
  48. protected void runImpl()
  49. {
  50. // Dont allow leaving if player is fighting
  51. L2PcInstance player = getClient().getActiveChar();
  52. if (player == null)
  53. return;
  54. player.getInventory().updateDatabase();
  55. if(AttackStanceTaskManager.getInstance().getAttackStanceTask(player))
  56. {
  57. if (Config.DEBUG) _log.fine("Player " + player.getName() + " tried to logout while fighting");
  58. player.sendPacket(new SystemMessage(SystemMessageId.CANT_LOGOUT_WHILE_FIGHTING));
  59. player.sendPacket(new ActionFailed());
  60. return;
  61. }
  62. if(player.atEvent) {
  63. player.sendPacket(SystemMessage.sendString("A superior power doesn't allow you to leave the event"));
  64. return;
  65. }
  66. if (player.isInOlympiadMode() || Olympiad.getInstance().isRegistered(player))
  67. {
  68. player.sendMessage("You cant logout in olympiad mode");
  69. return;
  70. }
  71. // Prevent player from logging out if they are a festival participant
  72. // and it is in progress, otherwise notify party members that the player
  73. // is not longer a participant.
  74. if (player.isFestivalParticipant()) {
  75. if (SevenSignsFestival.getInstance().isFestivalInitialized())
  76. {
  77. player.sendMessage("You cannot log out while you are a participant in a festival.");
  78. return;
  79. }
  80. L2Party playerParty = player.getParty();
  81. if (playerParty != null)
  82. player.getParty().broadcastToPartyMembers(SystemMessage.sendString(player.getName() + " has been removed from the upcoming festival."));
  83. }
  84. if (player.isFlying())
  85. {
  86. player.removeSkill(SkillTable.getInstance().getInfo(4289, 1));
  87. }
  88. TvTEvent.onLogout(player);
  89. RegionBBSManager.getInstance().changeCommunityBoard();
  90. player.deleteMe();
  91. notifyFriends(player);
  92. }
  93. private void notifyFriends(L2PcInstance cha)
  94. {
  95. java.sql.Connection con = null;
  96. try {
  97. con = L2DatabaseFactory.getInstance().getConnection();
  98. PreparedStatement statement;
  99. statement = con.prepareStatement("SELECT friend_name FROM character_friends WHERE char_id=?");
  100. statement.setInt(1, cha.getObjectId());
  101. ResultSet rset = statement.executeQuery();
  102. L2PcInstance friend;
  103. String friendName;
  104. while (rset.next())
  105. {
  106. friendName = rset.getString("friend_name");
  107. friend = L2World.getInstance().getPlayer(friendName);
  108. if (friend != null) //friend logged in.
  109. {
  110. friend.sendPacket(new FriendList(friend));
  111. }
  112. }
  113. rset.close();
  114. statement.close();
  115. }
  116. catch (Exception e) {
  117. _log.warning("could not restore friend data:"+e);
  118. }
  119. finally {
  120. try {con.close();} catch (Exception e){}
  121. }
  122. }
  123. /* (non-Javadoc)
  124. * @see net.sf.l2j.gameserver.clientpackets.ClientBasePacket#getType()
  125. */
  126. @Override
  127. public String getType()
  128. {
  129. return _C__09_LOGOUT;
  130. }
  131. }