RequestBuySeed.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 static com.l2jserver.gameserver.model.actor.L2Npc.INTERACTION_DISTANCE;
  17. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.MAX_ADENA;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.datatables.ItemTable;
  20. import com.l2jserver.gameserver.instancemanager.CastleManager;
  21. import com.l2jserver.gameserver.instancemanager.CastleManorManager;
  22. import com.l2jserver.gameserver.instancemanager.CastleManorManager.SeedProduction;
  23. import com.l2jserver.gameserver.model.L2Object;
  24. import com.l2jserver.gameserver.model.actor.instance.L2ManorManagerInstance;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.model.entity.Castle;
  27. import com.l2jserver.gameserver.network.SystemMessageId;
  28. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  29. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  30. import com.l2jserver.gameserver.templates.item.L2Item;
  31. import com.l2jserver.gameserver.util.Util;
  32. /**
  33. * Format: cdd[dd]
  34. * c // id (0xC4)
  35. *
  36. * d // manor id
  37. * d // seeds to buy
  38. * [
  39. * d // seed id
  40. * q // count
  41. * ]
  42. * @param decrypt
  43. * @author l3x
  44. */
  45. public class RequestBuySeed extends L2GameClientPacket
  46. {
  47. private static final String _C__C4_REQUESTBUYSEED = "[C] C4 RequestBuySeed";
  48. private static final int BATCH_LENGTH = 12; // length of the one item
  49. private int _manorId;
  50. private Seed[] _seeds = null;
  51. @Override
  52. protected void readImpl()
  53. {
  54. _manorId = readD();
  55. int count = readD();
  56. if (count <= 0
  57. || count > Config.MAX_ITEM_IN_PACKET
  58. || count * BATCH_LENGTH != _buf.remaining())
  59. {
  60. return;
  61. }
  62. _seeds = new Seed[count];
  63. for (int i = 0; i < count; i++)
  64. {
  65. int itemId = readD();
  66. long cnt = readQ();
  67. if (cnt < 1)
  68. {
  69. _seeds = null;
  70. return;
  71. }
  72. _seeds[i] = new Seed(itemId, cnt);
  73. }
  74. }
  75. @Override
  76. protected void runImpl()
  77. {
  78. L2PcInstance player = getClient().getActiveChar();
  79. if (player == null)
  80. return;
  81. if (!getClient().getFloodProtectors().getManor().tryPerformAction("BuySeed"))
  82. return;
  83. if (_seeds == null)
  84. {
  85. sendPacket(ActionFailed.STATIC_PACKET);
  86. return;
  87. }
  88. L2Object manager = player.getTarget();
  89. if (!(manager instanceof L2ManorManagerInstance))
  90. manager = player.getLastFolkNPC();
  91. if (!(manager instanceof L2ManorManagerInstance))
  92. return;
  93. if (!player.isInsideRadius(manager, INTERACTION_DISTANCE, true, false))
  94. return;
  95. Castle castle = CastleManager.getInstance().getCastleById(_manorId);
  96. long totalPrice = 0;
  97. int slots = 0;
  98. int totalWeight = 0;
  99. for (Seed i : _seeds)
  100. {
  101. if (!i.setProduction(castle))
  102. return;
  103. totalPrice += i.getPrice();
  104. if (totalPrice > MAX_ADENA)
  105. {
  106. Util.handleIllegalPlayerAction(player, "Warning!! Character "
  107. + player.getName() + " of account "
  108. + player.getAccountName() + " tried to purchase over "
  109. + MAX_ADENA + " adena worth of goods.",
  110. Config.DEFAULT_PUNISH);
  111. return;
  112. }
  113. L2Item template = ItemTable.getInstance().getTemplate(i.getSeedId());
  114. totalWeight += i.getCount() * template.getWeight();
  115. if (!template.isStackable())
  116. slots += i.getCount();
  117. else if (player.getInventory().getItemByItemId(i.getSeedId()) == null)
  118. slots++;
  119. }
  120. if (!player.getInventory().validateWeight(totalWeight))
  121. {
  122. sendPacket(SystemMessage.getSystemMessage(SystemMessageId.WEIGHT_LIMIT_EXCEEDED));
  123. return;
  124. }
  125. if (!player.getInventory().validateCapacity(slots))
  126. {
  127. sendPacket(SystemMessage.getSystemMessage(SystemMessageId.SLOTS_FULL));
  128. return;
  129. }
  130. // test adena
  131. if (totalPrice < 0 || player.getAdena() < totalPrice)
  132. {
  133. sendPacket(SystemMessage.getSystemMessage(SystemMessageId.YOU_NOT_ENOUGH_ADENA));
  134. return;
  135. }
  136. // Proceed the purchase
  137. for (Seed i : _seeds)
  138. {
  139. // take adena and check seed amount once again
  140. if (!player.reduceAdena("Buy", i.getPrice(), player, false) || !i.updateProduction(castle))
  141. {
  142. // failed buy, reduce total price
  143. totalPrice -= i.getPrice();
  144. continue;
  145. }
  146. // Add item to Inventory and adjust update packet
  147. player.addItem("Buy", i.getSeedId(), i.getCount(), manager, true);
  148. }
  149. // Adding to treasury for Manor Castle
  150. if (totalPrice > 0)
  151. {
  152. castle.addToTreasuryNoTax(totalPrice);
  153. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_DISAPPEARED_ADENA);
  154. sm.addItemNumber(totalPrice);
  155. player.sendPacket(sm);
  156. }
  157. }
  158. private static class Seed
  159. {
  160. private final int _seedId;
  161. private final long _count;
  162. SeedProduction _seed;
  163. public Seed(int id, long num)
  164. {
  165. _seedId = id;
  166. _count = num;
  167. }
  168. public int getSeedId()
  169. {
  170. return _seedId;
  171. }
  172. public long getCount()
  173. {
  174. return _count;
  175. }
  176. public long getPrice()
  177. {
  178. return _seed.getPrice() * _count;
  179. }
  180. public boolean setProduction(Castle c)
  181. {
  182. _seed = c.getSeed(_seedId, CastleManorManager.PERIOD_CURRENT);
  183. // invalid price - seed disabled
  184. if (_seed.getPrice() <= 0)
  185. return false;
  186. // try to buy more than castle can produce
  187. if (_seed.getCanProduce() < _count)
  188. return false;
  189. // check for overflow
  190. if ((MAX_ADENA / _count) < _seed.getPrice())
  191. return false;
  192. return true;
  193. }
  194. public boolean updateProduction(Castle c)
  195. {
  196. synchronized(_seed)
  197. {
  198. long amount = _seed.getCanProduce();
  199. if (_count > amount)
  200. return false; // not enough seeds
  201. _seed.setCanProduce(amount - _count);
  202. }
  203. // Update Castle Seeds Amount
  204. if (Config.ALT_MANOR_SAVE_ALL_ACTIONS)
  205. c.updateSeed(_seedId, _seed.getCanProduce(), CastleManorManager.PERIOD_CURRENT);
  206. return true;
  207. }
  208. }
  209. @Override
  210. public String getType()
  211. {
  212. return _C__C4_REQUESTBUYSEED;
  213. }
  214. }