RequestSetAllyCrest.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.datatables.ClanTable;
  24. import com.l2jserver.gameserver.idfactory.IdFactory;
  25. import com.l2jserver.gameserver.model.L2Clan;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. /**
  28. * Client packet for setting ally crest.
  29. *
  30. */
  31. public final class RequestSetAllyCrest extends L2GameClientPacket
  32. {
  33. private static final String _C__87_REQUESTSETALLYCREST = "[C] 87 RequestSetAllyCrest";
  34. static Logger _log = Logger.getLogger(RequestSetAllyCrest.class.getName());
  35. private int _length;
  36. private byte[] _data;
  37. @Override
  38. protected void readImpl()
  39. {
  40. _length = readD();
  41. if (_length > 192)
  42. return;
  43. _data = new byte[_length];
  44. readB(_data);
  45. }
  46. @Override
  47. protected void runImpl()
  48. {
  49. L2PcInstance activeChar = getClient().getActiveChar();
  50. if (activeChar == null)
  51. return;
  52. if (_length < 0)
  53. {
  54. activeChar.sendMessage("File transfer error.");
  55. return;
  56. }
  57. if (_length > 192)
  58. {
  59. activeChar.sendMessage("The ally crest file size was too big (max 192 bytes).");
  60. return;
  61. }
  62. if (activeChar.getAllyId() != 0)
  63. {
  64. L2Clan leaderclan = ClanTable.getInstance().getClan(activeChar.getAllyId());
  65. if (activeChar.getClanId() != leaderclan.getClanId() || !activeChar.isClanLeader())
  66. {
  67. return;
  68. }
  69. CrestCache crestCache = CrestCache.getInstance();
  70. boolean remove = false;
  71. if (_length == 0 || _data.length == 0)
  72. remove = true;
  73. int newId = 0;
  74. if (!remove)
  75. newId = IdFactory.getInstance().getNextId();
  76. if (leaderclan.getAllyCrestId() != 0)
  77. {
  78. crestCache.removeAllyCrest(leaderclan.getAllyCrestId());
  79. }
  80. if (!remove && !crestCache.saveAllyCrest(newId, _data))
  81. {
  82. _log.log(Level.INFO, "Error saving crest for ally " + leaderclan.getAllyName() + " [" + leaderclan.getAllyId() + "]");
  83. return;
  84. }
  85. Connection con = null;
  86. try
  87. {
  88. con = L2DatabaseFactory.getInstance().getConnection();
  89. PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET ally_crest_id = ? WHERE ally_id = ?");
  90. statement.setInt(1, newId);
  91. statement.setInt(2, leaderclan.getAllyId());
  92. statement.executeUpdate();
  93. statement.close();
  94. }
  95. catch (SQLException e)
  96. {
  97. _log.warning("Could not update ally crest for ally " + leaderclan.getAllyName() + " [" + leaderclan.getAllyId() + "] : " + e.getMessage());
  98. }
  99. finally
  100. {
  101. try
  102. {
  103. con.close();
  104. }
  105. catch (Exception e)
  106. {
  107. }
  108. }
  109. for (L2Clan clan : ClanTable.getInstance().getClans())
  110. {
  111. if (clan.getAllyId() == activeChar.getAllyId())
  112. {
  113. clan.setAllyCrestId(newId);
  114. for (L2PcInstance member : clan.getOnlineMembers(0))
  115. member.broadcastUserInfo();
  116. }
  117. }
  118. }
  119. }
  120. /* (non-Javadoc)
  121. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#getType()
  122. */
  123. @Override
  124. public String getType()
  125. {
  126. return _C__87_REQUESTSETALLYCREST;
  127. }
  128. }