Harvest.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.handler.skillhandlers;
  16. import java.util.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.handler.ISkillHandler;
  19. import net.sf.l2j.gameserver.model.L2Attackable;
  20. import net.sf.l2j.gameserver.model.L2Character;
  21. import net.sf.l2j.gameserver.model.L2ItemInstance;
  22. import net.sf.l2j.gameserver.model.L2Object;
  23. import net.sf.l2j.gameserver.model.L2Skill;
  24. import net.sf.l2j.gameserver.model.actor.instance.L2MonsterInstance;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  26. import net.sf.l2j.gameserver.network.SystemMessageId;
  27. import net.sf.l2j.gameserver.network.serverpackets.InventoryUpdate;
  28. import net.sf.l2j.gameserver.network.serverpackets.ItemList;
  29. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  30. import net.sf.l2j.gameserver.templates.L2SkillType;
  31. import net.sf.l2j.util.Rnd;
  32. /**
  33. * @author l3x
  34. */
  35. public class Harvest implements ISkillHandler
  36. {
  37. private static Logger _log = Logger.getLogger(Harvest.class.getName());
  38. private static final L2SkillType[] SKILL_IDS =
  39. {
  40. L2SkillType.HARVEST
  41. };
  42. private L2PcInstance _activeChar;
  43. private L2MonsterInstance _target;
  44. /**
  45. *
  46. * @see net.sf.l2j.gameserver.handler.ISkillHandler#useSkill(net.sf.l2j.gameserver.model.L2Character, net.sf.l2j.gameserver.model.L2Skill, net.sf.l2j.gameserver.model.L2Object[])
  47. */
  48. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  49. {
  50. if (!(activeChar instanceof L2PcInstance))
  51. return;
  52. _activeChar = (L2PcInstance) activeChar;
  53. L2Object[] targetList = skill.getTargetList(activeChar);
  54. InventoryUpdate iu = Config.FORCE_INVENTORY_UPDATE ? null : new InventoryUpdate();
  55. if (targetList == null)
  56. {
  57. return;
  58. }
  59. if (Config.DEBUG)
  60. {
  61. _log.info("Casting harvest");
  62. }
  63. for (int index = 0; index < targetList.length; index++)
  64. {
  65. if (!(targetList[index] instanceof L2MonsterInstance))
  66. continue;
  67. _target = (L2MonsterInstance) targetList[index];
  68. if (_activeChar != _target.getSeeder())
  69. {
  70. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_ARE_NOT_AUTHORIZED_TO_HARVEST);
  71. _activeChar.sendPacket(sm);
  72. continue;
  73. }
  74. boolean send = false;
  75. int total = 0;
  76. int cropId = 0;
  77. // TODO: check items and amount of items player harvest
  78. if (_target.isSeeded())
  79. {
  80. if (calcSuccess())
  81. {
  82. L2Attackable.RewardItem[] items = _target.takeHarvest();
  83. if (items != null && items.length > 0)
  84. {
  85. for (L2Attackable.RewardItem ritem : items)
  86. {
  87. cropId = ritem.getItemId(); // always got 1 type of crop as reward
  88. if (_activeChar.isInParty())
  89. _activeChar.getParty().distributeItem(_activeChar, ritem, true, _target);
  90. else
  91. {
  92. L2ItemInstance item = _activeChar.getInventory().addItem("Manor", ritem.getItemId(), ritem.getCount(), _activeChar, _target);
  93. if (iu != null)
  94. iu.addItem(item);
  95. send = true;
  96. total += ritem.getCount();
  97. }
  98. }
  99. if (send)
  100. {
  101. SystemMessage smsg = new SystemMessage(SystemMessageId.YOU_PICKED_UP_S1_S2);
  102. smsg.addNumber(total);
  103. smsg.addItemName(cropId);
  104. _activeChar.sendPacket(smsg);
  105. if (_activeChar.getParty() != null)
  106. {
  107. smsg = new SystemMessage(SystemMessageId.S1_HARVESTED_S3_S2S);
  108. smsg.addString(_activeChar.getName());
  109. smsg.addNumber(total);
  110. smsg.addItemName(cropId);
  111. _activeChar.getParty().broadcastToPartyMembers(_activeChar, smsg);
  112. }
  113. if (iu != null)
  114. _activeChar.sendPacket(iu);
  115. else
  116. _activeChar.sendPacket(new ItemList(_activeChar, false));
  117. }
  118. }
  119. }
  120. else
  121. {
  122. _activeChar.sendPacket(new SystemMessage(SystemMessageId.THE_HARVEST_HAS_FAILED));
  123. }
  124. }
  125. else
  126. {
  127. _activeChar.sendPacket(new SystemMessage(SystemMessageId.THE_HARVEST_FAILED_BECAUSE_THE_SEED_WAS_NOT_SOWN));
  128. }
  129. }
  130. }
  131. /**
  132. *
  133. * @return
  134. */
  135. private boolean calcSuccess()
  136. {
  137. int basicSuccess = 100;
  138. int levelPlayer = _activeChar.getLevel();
  139. int levelTarget = _target.getLevel();
  140. int diff = (levelPlayer - levelTarget);
  141. if (diff < 0)
  142. diff = -diff;
  143. // apply penalty, target <=> player levels
  144. // 5% penalty for each level
  145. if (diff > 5)
  146. {
  147. basicSuccess -= (diff - 5) * 5;
  148. }
  149. // success rate cant be less than 1%
  150. if (basicSuccess < 1)
  151. basicSuccess = 1;
  152. int rate = Rnd.nextInt(99);
  153. if (rate < basicSuccess)
  154. return true;
  155. return false;
  156. }
  157. /**
  158. *
  159. * @see net.sf.l2j.gameserver.handler.ISkillHandler#getSkillIds()
  160. */
  161. public L2SkillType[] getSkillIds()
  162. {
  163. return SKILL_IDS;
  164. }
  165. }