2
0

RequestSetCrop.java 4.6 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.util.ArrayList;
  17. import java.util.List;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.instancemanager.CastleManager;
  20. import com.l2jserver.gameserver.instancemanager.CastleManorManager;
  21. import com.l2jserver.gameserver.instancemanager.CastleManorManager.CropProcure;
  22. import com.l2jserver.gameserver.model.L2Clan;
  23. import com.l2jserver.gameserver.model.L2Object;
  24. import com.l2jserver.gameserver.model.actor.instance.L2CastleChamberlainInstance;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.model.entity.Castle;
  27. import com.l2jserver.gameserver.util.Util;
  28. import static com.l2jserver.gameserver.model.actor.L2Npc.INTERACTION_DISTANCE;
  29. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.MAX_ADENA;
  30. /**
  31. * Format: (ch) dd [dddc]
  32. * d - manor id
  33. * d - size
  34. * [
  35. * d - crop id
  36. * d - sales
  37. * d - price
  38. * c - reward type
  39. * ]
  40. * @author l3x
  41. *
  42. */
  43. public class RequestSetCrop extends L2GameClientPacket {
  44. private static final String _C__D0_0B_REQUESTSETCROP = "[C] D0:0B RequestSetCrop";
  45. //private static Logger _log = Logger.getLogger(RequestSetCrop.class.getName());
  46. private static final int BATCH_LENGTH = 21; // length of the one item
  47. private int _manorId;
  48. private Crop[] _items = null;
  49. @Override
  50. protected void readImpl()
  51. {
  52. _manorId = readD();
  53. int count = readD();
  54. if (count <= 0
  55. || count > Config.MAX_ITEM_IN_PACKET
  56. || count * BATCH_LENGTH != _buf.remaining())
  57. {
  58. return;
  59. }
  60. _items = new Crop[count];
  61. for (int i = 0; i < count; i++)
  62. {
  63. int itemId = readD();
  64. long sales = readQ();
  65. long price = readQ();
  66. int type = readC();
  67. if (itemId < 1 || sales < 0 || price < 0)
  68. {
  69. _items = null;
  70. return;
  71. }
  72. _items[i] = new Crop(itemId, sales, price, type);
  73. }
  74. }
  75. @Override
  76. protected void runImpl()
  77. {
  78. if (_items == null)
  79. return;
  80. L2PcInstance player = getClient().getActiveChar();
  81. // check player privileges
  82. if (player == null
  83. || player.getClan() == null
  84. || (player.getClanPrivileges() & L2Clan.CP_CS_MANOR_ADMIN) == 0)
  85. return;
  86. // check castle owner
  87. Castle currentCastle = CastleManager.getInstance().getCastleById(_manorId);
  88. if (currentCastle.getOwnerId() != player.getClanId())
  89. return;
  90. L2Object manager = player.getTarget();
  91. if (!(manager instanceof L2CastleChamberlainInstance))
  92. manager = player.getLastFolkNPC();
  93. if (!(manager instanceof L2CastleChamberlainInstance))
  94. return;
  95. if (((L2CastleChamberlainInstance)manager).getCastle() != currentCastle)
  96. return;
  97. if (!player.isInsideRadius(manager, INTERACTION_DISTANCE, true, false))
  98. return;
  99. List<CropProcure> crops = new ArrayList<CropProcure>(_items.length);
  100. for (Crop i : _items)
  101. {
  102. CropProcure s = i.getCrop();
  103. if (s == null)
  104. {
  105. Util.handleIllegalPlayerAction(player, "Warning!! Character "
  106. + player.getName() + " of account "
  107. + player.getAccountName()
  108. + " tried to overflow while setting manor.",
  109. Config.DEFAULT_PUNISH);
  110. return;
  111. }
  112. crops.add(s);
  113. }
  114. currentCastle.setCropProcure(crops, CastleManorManager.PERIOD_NEXT);
  115. if (Config.ALT_MANOR_SAVE_ALL_ACTIONS)
  116. currentCastle.saveCropData(CastleManorManager.PERIOD_NEXT);
  117. }
  118. private class Crop
  119. {
  120. private final int _itemId;
  121. private final long _sales;
  122. private final long _price;
  123. private final int _type;
  124. public Crop(int id, long s, long p, int t)
  125. {
  126. _itemId = id;
  127. _sales = s;
  128. _price = p;
  129. _type = t;
  130. }
  131. public CropProcure getCrop()
  132. {
  133. if (_sales != 0 && (MAX_ADENA / _sales) < _price)
  134. return null;
  135. return CastleManorManager.getInstance().getNewCropProcure(_itemId, _sales, _type, _price, _sales);
  136. }
  137. }
  138. @Override
  139. public String getType()
  140. {
  141. return _C__D0_0B_REQUESTSETCROP;
  142. }
  143. }