RequestSetCrop.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.network.clientpackets;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.instancemanager.CastleManorManager;
  24. import com.l2jserver.gameserver.model.ClanPrivilege;
  25. import com.l2jserver.gameserver.model.CropProcure;
  26. import com.l2jserver.gameserver.model.L2Seed;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. /**
  29. * @author l3x
  30. */
  31. public final class RequestSetCrop extends L2GameClientPacket
  32. {
  33. private static final int BATCH_LENGTH = 21; // length of the one item
  34. private int _manorId;
  35. private List<CropProcure> _items;
  36. @Override
  37. protected void readImpl()
  38. {
  39. _manorId = readD();
  40. final int count = readD();
  41. if ((count <= 0) || (count > Config.MAX_ITEM_IN_PACKET) || ((count * BATCH_LENGTH) != _buf.remaining()))
  42. {
  43. return;
  44. }
  45. _items = new ArrayList<>(count);
  46. for (int i = 0; i < count; i++)
  47. {
  48. final int itemId = readD();
  49. final long sales = readQ();
  50. final long price = readQ();
  51. final int type = readC();
  52. if ((itemId < 1) || (sales < 0) || (price < 0))
  53. {
  54. _items.clear();
  55. return;
  56. }
  57. if (sales > 0)
  58. {
  59. _items.add(new CropProcure(itemId, sales, type, sales, price));
  60. }
  61. }
  62. }
  63. @Override
  64. protected void runImpl()
  65. {
  66. if (_items.isEmpty())
  67. {
  68. return;
  69. }
  70. final CastleManorManager manor = CastleManorManager.getInstance();
  71. if (!manor.isModifiablePeriod())
  72. {
  73. sendActionFailed();
  74. return;
  75. }
  76. // Check player privileges
  77. final L2PcInstance player = getActiveChar();
  78. if ((player == null) || (player.getClan() == null) || (player.getClan().getCastleId() != _manorId) || !player.hasClanPrivilege(ClanPrivilege.CS_MANOR_ADMIN) || !player.getLastFolkNPC().canInteract(player))
  79. {
  80. sendActionFailed();
  81. return;
  82. }
  83. // Filter crops with start amount lower than 0 and incorrect price
  84. final List<CropProcure> list = new ArrayList<>(_items.size());
  85. for (CropProcure cp : _items)
  86. {
  87. final L2Seed s = manor.getSeedByCrop(cp.getId(), _manorId);
  88. if ((s != null) && (cp.getStartAmount() <= s.getCropLimit()) && (cp.getPrice() >= s.getCropMinPrice()) && (cp.getPrice() <= s.getCropMaxPrice()))
  89. {
  90. list.add(cp);
  91. }
  92. }
  93. // Save crop list
  94. manor.setNextCropProcure(list, _manorId);
  95. }
  96. @Override
  97. public String getType()
  98. {
  99. return "[C] D0:04 RequestSetCrop";
  100. }
  101. }