Harvest.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.L2Skill.SkillType;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2MonsterInstance;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  27. import net.sf.l2j.gameserver.network.SystemMessageId;
  28. import net.sf.l2j.gameserver.network.serverpackets.InventoryUpdate;
  29. import net.sf.l2j.gameserver.network.serverpackets.ItemList;
  30. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  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 SkillType[] SKILL_IDS = {SkillType.HARVEST};
  39. private L2PcInstance _activeChar;
  40. private L2MonsterInstance _target;
  41. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  42. {
  43. if (!(activeChar instanceof L2PcInstance))
  44. return;
  45. _activeChar = (L2PcInstance) activeChar;
  46. L2Object[] targetList = skill.getTargetList(activeChar);
  47. InventoryUpdate iu = Config.FORCE_INVENTORY_UPDATE ? null : new InventoryUpdate();
  48. if (targetList == null) {
  49. return;
  50. }
  51. if (Config.DEBUG)
  52. {
  53. _log.info("Casting harvest");
  54. }
  55. for (int index = 0; index < targetList.length; index++) {
  56. if (!(targetList[index] instanceof L2MonsterInstance))
  57. continue;
  58. _target = (L2MonsterInstance) targetList[index];
  59. if (_activeChar != _target.getSeeder()) {
  60. SystemMessage sm = new SystemMessage(SystemMessageId.YOU_ARE_NOT_AUTHORIZED_TO_HARVEST);
  61. _activeChar.sendPacket(sm);
  62. continue;
  63. }
  64. boolean send = false;
  65. int total = 0;
  66. int cropId = 0;
  67. // TODO: check items and amount of items player harvest
  68. if (_target.isSeeded()) {
  69. if (calcSuccess()) {
  70. L2Attackable.RewardItem[] items = _target.takeHarvest();
  71. if (items != null && items.length > 0) {
  72. for (L2Attackable.RewardItem ritem : items) {
  73. cropId = ritem.getItemId(); // always got 1 type of crop as reward
  74. if (_activeChar.isInParty())
  75. _activeChar.getParty().distributeItem(_activeChar, ritem, true, _target);
  76. else {
  77. L2ItemInstance item = _activeChar.getInventory().addItem("Manor", ritem.getItemId(), ritem.getCount(), _activeChar, _target);
  78. if (iu != null) iu.addItem(item);
  79. send = true;
  80. total += ritem.getCount();
  81. }
  82. }
  83. if (send) {
  84. SystemMessage smsg = new SystemMessage(SystemMessageId.YOU_PICKED_UP_S1_S2);
  85. smsg.addNumber(total);
  86. smsg.addItemName(cropId);
  87. _activeChar.sendPacket(smsg);
  88. if (_activeChar.getParty() != null) {
  89. smsg = new SystemMessage(SystemMessageId.S1_HARVESTED_S3_S2S);
  90. smsg.addString(_activeChar.getName());
  91. smsg.addNumber(total);
  92. smsg.addItemName(cropId);
  93. _activeChar.getParty().broadcastToPartyMembers(_activeChar, smsg);
  94. }
  95. if (iu != null) _activeChar.sendPacket(iu);
  96. else _activeChar.sendPacket(new ItemList(_activeChar, false));
  97. }
  98. }
  99. } else {
  100. _activeChar.sendPacket(new SystemMessage(SystemMessageId.THE_HARVEST_HAS_FAILED));
  101. }
  102. } else {
  103. _activeChar.sendPacket(new SystemMessage(SystemMessageId.THE_HARVEST_FAILED_BECAUSE_THE_SEED_WAS_NOT_SOWN));
  104. }
  105. }
  106. }
  107. private boolean calcSuccess() {
  108. int basicSuccess = 100;
  109. int levelPlayer = _activeChar.getLevel();
  110. int levelTarget = _target.getLevel();
  111. int diff = (levelPlayer - levelTarget);
  112. if(diff < 0)
  113. diff = -diff;
  114. // apply penalty, target <=> player levels
  115. // 5% penalty for each level
  116. if(diff > 5) {
  117. basicSuccess -= (diff-5) * 5;
  118. }
  119. // success rate cant be less than 1%
  120. if(basicSuccess < 1)
  121. basicSuccess = 1;
  122. int rate = Rnd.nextInt(99);
  123. if(rate < basicSuccess)
  124. return true;
  125. return false;
  126. }
  127. public SkillType[] getSkillIds() {
  128. return SKILL_IDS;
  129. }
  130. }