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