RequestSetSeed.java 4.6 KB

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