RequestBuySeed.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.datatables.ItemTable;
  18. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  19. import net.sf.l2j.gameserver.instancemanager.CastleManorManager;
  20. import net.sf.l2j.gameserver.instancemanager.CastleManorManager.SeedProduction;
  21. import net.sf.l2j.gameserver.model.L2ItemInstance;
  22. import net.sf.l2j.gameserver.model.L2Object;
  23. import net.sf.l2j.gameserver.model.actor.instance.L2ManorManagerInstance;
  24. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  25. import net.sf.l2j.gameserver.model.entity.Castle;
  26. import net.sf.l2j.gameserver.network.SystemMessageId;
  27. import net.sf.l2j.gameserver.serverpackets.ActionFailed;
  28. import net.sf.l2j.gameserver.serverpackets.InventoryUpdate;
  29. import net.sf.l2j.gameserver.serverpackets.StatusUpdate;
  30. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  31. import net.sf.l2j.gameserver.templates.L2Item;
  32. import net.sf.l2j.gameserver.util.Util;
  33. /**
  34. * Format: cdd[dd]
  35. * c // id (0xC4)
  36. *
  37. * d // manor id
  38. * d // seeds to buy
  39. * [
  40. * d // seed id
  41. * d // count
  42. * ]
  43. * @param decrypt
  44. * @author l3x
  45. */
  46. public class RequestBuySeed extends L2GameClientPacket
  47. {
  48. private static final String _C__C4_REQUESTBUYSEED = "[C] C4 RequestBuySeed";
  49. private int _count;
  50. private int _manorId;
  51. private int[] _items; // size _count * 2
  52. @Override
  53. protected void readImpl()
  54. {
  55. _manorId = readD();
  56. _count = readD();
  57. if (_count > 500 || _count * 8 < _buf.remaining()) // check values
  58. {
  59. _count = 0;
  60. return;
  61. }
  62. _items = new int[_count * 2];
  63. for (int i = 0; i < _count; i++)
  64. {
  65. int itemId = readD();
  66. _items[i * 2 + 0] = itemId;
  67. long cnt = readD();
  68. if (cnt > Integer.MAX_VALUE || cnt < 1)
  69. {
  70. _count = 0;
  71. _items = null;
  72. return;
  73. }
  74. _items[i * 2 + 1] = (int) cnt;
  75. }
  76. }
  77. @Override
  78. protected void runImpl()
  79. {
  80. long totalPrice = 0;
  81. int slots = 0;
  82. int totalWeight = 0;
  83. L2PcInstance player = getClient().getActiveChar();
  84. if (player == null)
  85. return;
  86. if (_count < 1)
  87. {
  88. sendPacket(ActionFailed.STATIC_PACKET);
  89. return;
  90. }
  91. L2Object target = player.getTarget();
  92. if (!(target instanceof L2ManorManagerInstance))
  93. target = player.getLastFolkNPC();
  94. if (!(target instanceof L2ManorManagerInstance))
  95. return;
  96. Castle castle = CastleManager.getInstance().getCastleById(_manorId);
  97. for (int i = 0; i < _count; i++)
  98. {
  99. int seedId = _items[i * 2 + 0];
  100. int count = _items[i * 2 + 1];
  101. int price = 0;
  102. int residual = 0;
  103. SeedProduction seed = castle.getSeed(seedId,CastleManorManager.PERIOD_CURRENT);
  104. price = seed.getPrice();
  105. residual = seed.getCanProduce();
  106. if (price <= 0)
  107. return;
  108. if (residual < count)
  109. return;
  110. totalPrice += count * price;
  111. L2Item template = ItemTable.getInstance().getTemplate(seedId);
  112. totalWeight += count * template.getWeight();
  113. if (!template.isStackable())
  114. slots += count;
  115. else if (player.getInventory().getItemByItemId(seedId) == null)
  116. slots++;
  117. }
  118. if (totalPrice > Integer.MAX_VALUE)
  119. {
  120. Util.handleIllegalPlayerAction(player, "Warning!! Character "
  121. + player.getName() + " of account "
  122. + player.getAccountName() + " tried to purchase over "
  123. + Integer.MAX_VALUE + " adena worth of goods.",
  124. Config.DEFAULT_PUNISH);
  125. return;
  126. }
  127. if (!player.getInventory().validateWeight(totalWeight))
  128. {
  129. sendPacket(new SystemMessage(SystemMessageId.WEIGHT_LIMIT_EXCEEDED));
  130. return;
  131. }
  132. if (!player.getInventory().validateCapacity(slots))
  133. {
  134. sendPacket(new SystemMessage(SystemMessageId.SLOTS_FULL));
  135. return;
  136. }
  137. // Charge buyer
  138. if ((totalPrice < 0) || !player.reduceAdena("Buy", (int) totalPrice, target, false))
  139. {
  140. sendPacket(new SystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA));
  141. return;
  142. }
  143. // Adding to treasury for Manor Castle
  144. castle.addToTreasuryNoTax((int) totalPrice);
  145. // Proceed the purchase
  146. InventoryUpdate playerIU = new InventoryUpdate();
  147. for (int i = 0; i < _count; i++)
  148. {
  149. int seedId = _items[i * 2 + 0];
  150. int count = _items[i * 2 + 1];
  151. if (count < 0)
  152. count = 0;
  153. // Update Castle Seeds Amount
  154. SeedProduction seed = castle.getSeed(seedId,
  155. CastleManorManager.PERIOD_CURRENT);
  156. seed.setCanProduce(seed.getCanProduce() - count);
  157. if (Config.ALT_MANOR_SAVE_ALL_ACTIONS)
  158. CastleManager.getInstance().getCastleById(_manorId).updateSeed(
  159. seed.getId(), seed.getCanProduce(),
  160. CastleManorManager.PERIOD_CURRENT);
  161. // Add item to Inventory and adjust update packet
  162. L2ItemInstance item = player.getInventory().addItem("Buy", seedId,
  163. count, player, target);
  164. if (item.getCount() > count)
  165. playerIU.addModifiedItem(item);
  166. else
  167. playerIU.addNewItem(item);
  168. // Send Char Buy Messages
  169. SystemMessage sm = null;
  170. sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  171. sm.addItemName(seedId);
  172. sm.addNumber(count);
  173. player.sendPacket(sm);
  174. }
  175. // Send update packets
  176. player.sendPacket(playerIU);
  177. StatusUpdate su = new StatusUpdate(player.getObjectId());
  178. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  179. player.sendPacket(su);
  180. }
  181. @Override
  182. public String getType()
  183. {
  184. return _C__C4_REQUESTBUYSEED;
  185. }
  186. }