RequestSetPledgeCrest.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.network.clientpackets;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.SQLException;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import com.l2jserver.L2DatabaseFactory;
  22. import com.l2jserver.gameserver.cache.CrestCache;
  23. import com.l2jserver.gameserver.idfactory.IdFactory;
  24. import com.l2jserver.gameserver.model.L2Clan;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  28. /**
  29. * This class ...
  30. *
  31. * @version $Revision: 1.2.2.1.2.4 $ $Date: 2005/03/27 15:29:30 $
  32. */
  33. public final class RequestSetPledgeCrest extends L2GameClientPacket
  34. {
  35. private static final String _C__53_REQUESTSETPLEDGECREST = "[C] 53 RequestSetPledgeCrest";
  36. static Logger _log = Logger.getLogger(RequestSetPledgeCrest.class.getName());
  37. private int _length;
  38. private byte[] _data;
  39. @Override
  40. protected void readImpl()
  41. {
  42. _length = readD();
  43. if (_length < 0 || _length > 256)
  44. return;
  45. _data = new byte[_length];
  46. readB(_data);
  47. }
  48. @Override
  49. protected void runImpl()
  50. {
  51. L2PcInstance activeChar = getClient().getActiveChar();
  52. if (activeChar == null)
  53. return;
  54. L2Clan clan = activeChar.getClan();
  55. if (clan == null)
  56. return;
  57. if (clan.getDissolvingExpiryTime() > System.currentTimeMillis())
  58. {
  59. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_SET_CREST_WHILE_DISSOLUTION_IN_PROGRESS));
  60. return;
  61. }
  62. if (_length < 0)
  63. {
  64. activeChar.sendMessage("File transfer error.");
  65. return;
  66. }
  67. if (_length > 256)
  68. {
  69. activeChar.sendMessage("The clan crest file size was too big (max 256 bytes).");
  70. return;
  71. }
  72. if (_length == 0 || _data.length == 0)
  73. {
  74. CrestCache.getInstance().removePledgeCrest(clan.getCrestId());
  75. clan.setCrestId(0);
  76. clan.setHasCrest(false);
  77. activeChar.sendPacket(new SystemMessage(SystemMessageId.CLAN_CREST_HAS_BEEN_DELETED));
  78. for (L2PcInstance member : clan.getOnlineMembers(0))
  79. member.broadcastUserInfo();
  80. return;
  81. }
  82. if ((activeChar.getClanPrivileges() & L2Clan.CP_CL_REGISTER_CREST) == L2Clan.CP_CL_REGISTER_CREST)
  83. {
  84. if (clan.getLevel() < 3)
  85. {
  86. activeChar.sendPacket(new SystemMessage(SystemMessageId.CLAN_LVL_3_NEEDED_TO_SET_CREST));
  87. return;
  88. }
  89. CrestCache crestCache = CrestCache.getInstance();
  90. int newId = IdFactory.getInstance().getNextId();
  91. if(clan.hasCrest())
  92. {
  93. crestCache.removePledgeCrest(clan.getCrestId());
  94. }
  95. if (!crestCache.savePledgeCrest(newId,_data))
  96. {
  97. _log.log(Level.INFO, "Error loading crest of clan:" + clan.getName());
  98. return;
  99. }
  100. Connection con = null;
  101. try
  102. {
  103. con = L2DatabaseFactory.getInstance().getConnection();
  104. PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET crest_id = ? WHERE clan_id = ?");
  105. statement.setInt(1, newId);
  106. statement.setInt(2, clan.getClanId());
  107. statement.executeUpdate();
  108. statement.close();
  109. }
  110. catch (SQLException e)
  111. {
  112. _log.warning("could not update the crest id:"+e.getMessage());
  113. }
  114. finally
  115. {
  116. try { con.close(); } catch (Exception e) {}
  117. }
  118. clan.setCrestId(newId);
  119. clan.setHasCrest(true);
  120. for (L2PcInstance member : clan.getOnlineMembers(0))
  121. member.broadcastUserInfo();
  122. }
  123. }
  124. /* (non-Javadoc)
  125. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  126. */
  127. @Override
  128. public String getType()
  129. {
  130. return _C__53_REQUESTSETPLEDGECREST;
  131. }
  132. }