RequestSellItem.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 net.sf.l2j.Config;
  17. import net.sf.l2j.gameserver.cache.HtmCache;
  18. import net.sf.l2j.gameserver.model.L2ItemInstance;
  19. import net.sf.l2j.gameserver.model.L2Object;
  20. import net.sf.l2j.gameserver.model.actor.instance.L2FishermanInstance;
  21. import net.sf.l2j.gameserver.model.actor.instance.L2MercManagerInstance;
  22. import net.sf.l2j.gameserver.model.actor.instance.L2MerchantInstance;
  23. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  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.ActionFailed;
  27. import net.sf.l2j.gameserver.serverpackets.ItemList;
  28. import net.sf.l2j.gameserver.serverpackets.NpcHtmlMessage;
  29. import net.sf.l2j.gameserver.serverpackets.StatusUpdate;
  30. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  31. import net.sf.l2j.gameserver.util.Util;
  32. /**
  33. * This class ...
  34. *
  35. * @version $Revision: 1.3.2.1.2.4 $ $Date: 2005/03/27 15:29:30 $
  36. */
  37. public final class RequestSellItem extends L2GameClientPacket
  38. {
  39. private static final String _C__1E_REQUESTSELLITEM = "[C] 1E RequestSellItem";
  40. //private static Logger _log = Logger.getLogger(RequestSellItem.class.getName());
  41. private int _listId;
  42. private int _count;
  43. private int[] _items; // count*3
  44. public RequestSellItem()
  45. {
  46. }
  47. /**
  48. * packet type id 0x1e
  49. *
  50. * sample
  51. *
  52. * 1e
  53. * 00 00 00 00 // list id
  54. * 02 00 00 00 // number of items
  55. *
  56. * 71 72 00 10 // object id
  57. * ea 05 00 00 // item id
  58. * 01 00 00 00 // item count
  59. *
  60. * 76 4b 00 10 // object id
  61. * 2e 0a 00 00 // item id
  62. * 01 00 00 00 // item count
  63. *
  64. * format: cdd (ddd)
  65. * @param decrypt
  66. */
  67. @Override
  68. protected void readImpl()
  69. {
  70. _listId = readD();
  71. _count = readD();
  72. if (_count <= 0 || _count * 12 > _buf.remaining() || _count > Config.MAX_ITEM_IN_PACKET)
  73. {
  74. _count = 0; _items = null;
  75. return;
  76. }
  77. _items = new int[_count * 3];
  78. for (int i = 0; i < _count; i++)
  79. {
  80. int objectId = readD(); _items[i * 3 + 0] = objectId;
  81. int itemId = readD(); _items[i * 3 + 1] = itemId;
  82. long cnt = readD();
  83. if (cnt > Integer.MAX_VALUE || cnt <= 0)
  84. {
  85. _count = 0; _items = null;
  86. return;
  87. }
  88. _items[i * 3 + 2] = (int)cnt;
  89. }
  90. }
  91. @Override
  92. protected void runImpl()
  93. {
  94. this.processSell();
  95. }
  96. protected void processSell()
  97. {
  98. L2PcInstance player = getClient().getActiveChar();
  99. if (player == null) return;
  100. // Alt game - Karma punishment
  101. if (!Config.ALT_GAME_KARMA_PLAYER_CAN_SHOP && player.getKarma() > 0) return;
  102. L2Object target = player.getTarget();
  103. if (!player.isGM() && (target == null // No target (ie GM Shop)
  104. || !(target instanceof L2MerchantInstance || target instanceof L2MercManagerInstance) // Target not a merchant and not mercmanager
  105. || !player.isInsideRadius(target, L2NpcInstance.INTERACTION_DISTANCE, false, false) // Distance is too far
  106. )) return;
  107. boolean ok = true;
  108. String htmlFolder = "";
  109. if (target != null)
  110. {
  111. if (target instanceof L2MerchantInstance)
  112. htmlFolder = "merchant";
  113. else if (target instanceof L2FishermanInstance)
  114. htmlFolder = "fisherman";
  115. else
  116. ok = false;
  117. }
  118. else
  119. ok = false;
  120. L2NpcInstance merchant = null;
  121. if (ok)
  122. merchant = (L2NpcInstance)target;
  123. if (_listId > 1000000) // lease
  124. {
  125. if (merchant.getTemplate().npcId != _listId-1000000)
  126. {
  127. sendPacket(ActionFailed.STATIC_PACKET);
  128. return;
  129. }
  130. }
  131. long totalPrice = 0;
  132. // Proceed the sell
  133. for (int i = 0; i < _count; i++)
  134. {
  135. int objectId = _items[i * 3 + 0];
  136. @SuppressWarnings("unused")
  137. int itemId = _items[i * 3 + 1];
  138. int count = _items[i * 3 + 2];
  139. if (count < 0 || count > Integer.MAX_VALUE)
  140. {
  141. Util.handleIllegalPlayerAction(player,"Warning!! Character "+player.getName()+" of account "+player.getAccountName()+" tried to purchase over "+Integer.MAX_VALUE+" items at the same time.", Config.DEFAULT_PUNISH);
  142. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED);
  143. sendPacket(sm);
  144. sm = null;
  145. return;
  146. }
  147. L2ItemInstance item = player.checkItemManipulation(objectId, count, "sell");
  148. if (item == null || (!item.getItem().isSellable())) continue;
  149. totalPrice += item.getReferencePrice() * count /2;
  150. if (totalPrice > Integer.MAX_VALUE)
  151. {
  152. Util.handleIllegalPlayerAction(player,"Warning!! Character "+player.getName()+" of account "+player.getAccountName()+" tried to purchase over "+Integer.MAX_VALUE+" adena worth of goods.", Config.DEFAULT_PUNISH);
  153. return;
  154. }
  155. item = player.getInventory().destroyItem("Sell", objectId, count, player, null);
  156. /* TODO: Disabled until Leaseholders are rewritten ;-)
  157. int price = item.getReferencePrice()*(int)count/2;
  158. L2ItemInstance li = null;
  159. L2ItemInstance la = null;
  160. if (_listId > 1000000) {
  161. li = merchant.findLeaseItem(item.getItemId(),item.getEnchantLevel());
  162. la = merchant.getLeaseAdena();
  163. if (li == null || la == null) continue;
  164. price = li.getPriceToBuy()*(int)count; // player sells, thus merchant buys.
  165. if (price > la.getCount()) continue;
  166. }
  167. */
  168. /* TODO: Disabled until Leaseholders are rewritten ;-)
  169. if (item != null && _listId > 1000000) {
  170. li.setCount(li.getCount()+(int)count);
  171. li.updateDatabase();
  172. la.setCount(la.getCount()-price);
  173. la.updateDatabase();
  174. }
  175. */
  176. }
  177. player.addAdena("Sell", (int)totalPrice, merchant, false);
  178. String html = HtmCache.getInstance().getHtm("data/html/"+ htmlFolder +"/" + merchant.getNpcId() + "-sold.htm");
  179. if (html != null)
  180. {
  181. NpcHtmlMessage soldMsg = new NpcHtmlMessage(merchant.getObjectId());
  182. soldMsg.setHtml(html.replaceAll("%objectId%", String.valueOf(merchant.getObjectId())));
  183. player.sendPacket(soldMsg);
  184. }
  185. // Update current load as well
  186. StatusUpdate su = new StatusUpdate(player.getObjectId());
  187. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  188. player.sendPacket(su);
  189. player.sendPacket(new ItemList(player, true));
  190. }
  191. /* (non-Javadoc)
  192. * @see net.sf.l2j.gameserver.clientpackets.ClientBasePacket#getType()
  193. */
  194. @Override
  195. public String getType()
  196. {
  197. return _C__1E_REQUESTSELLITEM;
  198. }
  199. }