RequestExSetPledgeCrestLarge.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * Format : chdb
  30. * c (id) 0xD0
  31. * h (subid) 0x11
  32. * d data size
  33. * b raw data (picture i think ;) )
  34. * @author -Wooden-
  35. *
  36. */
  37. public final class RequestExSetPledgeCrestLarge extends L2GameClientPacket
  38. {
  39. private static final String _C__D0_11_REQUESTEXSETPLEDGECRESTLARGE = "[C] D0:11 RequestExSetPledgeCrestLarge";
  40. static Logger _log = Logger.getLogger(RequestExSetPledgeCrestLarge.class.getName());
  41. private int _length;
  42. private byte[] _data;
  43. @Override
  44. protected void readImpl()
  45. {
  46. _length = readD();
  47. if (_length > 2176)
  48. return;
  49. _data = new byte[_length];
  50. readB(_data);
  51. }
  52. /* (non-Javadoc)
  53. * @see com.l2jserver.gameserver.clientpackets.ClientBasePacket#runImpl()
  54. */
  55. @Override
  56. protected void runImpl()
  57. {
  58. L2PcInstance activeChar = getClient().getActiveChar();
  59. if (activeChar == null)
  60. return;
  61. L2Clan clan = activeChar.getClan();
  62. if (clan == null)
  63. return;
  64. if (_length < 0)
  65. {
  66. activeChar.sendMessage("File transfer error.");
  67. return;
  68. }
  69. if (_length > 2176)
  70. {
  71. activeChar.sendMessage("The insignia file size is greater than 2176 bytes.");
  72. return;
  73. }
  74. boolean updated = false;
  75. int crestLargeId = -1;
  76. if (_length == 0 || _data == null)
  77. {
  78. CrestCache.getInstance().removePledgeCrestLarge(clan.getCrestLargeId());
  79. clan.setCrestLargeId(crestLargeId = 0);
  80. clan.setHasCrestLarge(false);
  81. activeChar.sendMessage("The insignia has been removed.");
  82. updated = true;
  83. }
  84. else if ((activeChar.getClanPrivileges() & L2Clan.CP_CL_REGISTER_CREST) == L2Clan.CP_CL_REGISTER_CREST)
  85. {
  86. if (clan.getHasCastle() == 0 && clan.getHasHideout() == 0)
  87. {
  88. activeChar.sendMessage("Only a clan that owns a clan hall or a castle can get their emblem displayed on clan related items"); //there is a system message for that but didnt found the id
  89. return;
  90. }
  91. CrestCache crestCache = CrestCache.getInstance();
  92. int newId = IdFactory.getInstance().getNextId();
  93. if (clan.hasCrestLarge())
  94. crestCache.removePledgeCrestLarge(clan.getCrestLargeId());
  95. if (!crestCache.savePledgeCrestLarge(newId, _data))
  96. {
  97. _log.log(Level.INFO, "Error saving large crest for clan " + clan.getName() + " [" + clan.getClanId() + "]");
  98. return;
  99. }
  100. clan.setCrestLargeId(crestLargeId = newId);
  101. clan.setHasCrestLarge(true);
  102. activeChar.sendPacket(new SystemMessage(SystemMessageId.CLAN_EMBLEM_WAS_SUCCESSFULLY_REGISTERED));
  103. updated = true;
  104. }
  105. if (updated && crestLargeId != -1)
  106. {
  107. Connection con = null;
  108. try
  109. {
  110. con = L2DatabaseFactory.getInstance().getConnection();
  111. PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET crest_large_id = ? WHERE clan_id = ?");
  112. statement.setInt(1, crestLargeId);
  113. statement.setInt(2, clan.getClanId());
  114. statement.executeUpdate();
  115. statement.close();
  116. }
  117. catch (SQLException e)
  118. {
  119. _log.warning("Could not update large crest for clan " + clan.getName() + " [" + clan.getClanId() + "] : " + e.getMessage());
  120. }
  121. finally
  122. {
  123. try
  124. {
  125. con.close();
  126. }
  127. catch (Exception e)
  128. {
  129. }
  130. }
  131. for (L2PcInstance member : clan.getOnlineMembers(0))
  132. member.broadcastUserInfo();
  133. }
  134. }
  135. /* (non-Javadoc)
  136. * @see com.l2jserver.gameserver.BasePacket#getType()
  137. */
  138. @Override
  139. public String getType()
  140. {
  141. return _C__D0_11_REQUESTEXSETPLEDGECRESTLARGE;
  142. }
  143. }