ItemSkillsTemplate.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2004-2014 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack 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 DataPack 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 handlers.itemhandlers;
  20. import com.l2jserver.gameserver.ai.CtrlIntention;
  21. import com.l2jserver.gameserver.handler.IItemHandler;
  22. import com.l2jserver.gameserver.model.actor.L2Playable;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.model.entity.TvTEvent;
  25. import com.l2jserver.gameserver.model.holders.SkillHolder;
  26. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  27. import com.l2jserver.gameserver.model.skills.L2Skill;
  28. import com.l2jserver.gameserver.network.SystemMessageId;
  29. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  30. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  31. /**
  32. * Template for item skills handler.
  33. * @author Zoey76
  34. */
  35. public class ItemSkillsTemplate implements IItemHandler
  36. {
  37. @Override
  38. public boolean useItem(L2Playable playable, L2ItemInstance item, boolean forceUse)
  39. {
  40. if (!playable.isPlayer() && !playable.isPet())
  41. {
  42. return false;
  43. }
  44. if (!TvTEvent.onScrollUse(playable.getObjectId()))
  45. {
  46. playable.sendPacket(ActionFailed.STATIC_PACKET);
  47. return false;
  48. }
  49. // Pets can use items only when they are tradable.
  50. if (playable.isPet() && !item.isTradeable())
  51. {
  52. playable.sendPacket(SystemMessageId.ITEM_NOT_FOR_PETS);
  53. return false;
  54. }
  55. // Verify that item is not under reuse.
  56. if (!checkReuse(playable, null, item))
  57. {
  58. return false;
  59. }
  60. final SkillHolder[] skills = item.getEtcItem().getSkills();
  61. if (skills == null)
  62. {
  63. _log.info("Item " + item + " does not have registered any skill for handler.");
  64. return false;
  65. }
  66. final L2PcInstance activeChar = playable.getActingPlayer();
  67. for (SkillHolder skillInfo : skills)
  68. {
  69. if (skillInfo == null)
  70. {
  71. continue;
  72. }
  73. L2Skill itemSkill = skillInfo.getSkill();
  74. if (itemSkill != null)
  75. {
  76. if (!itemSkill.checkCondition(playable, playable.getTarget(), false))
  77. {
  78. return false;
  79. }
  80. if (playable.isSkillDisabled(itemSkill))
  81. {
  82. return false;
  83. }
  84. // Verify that skill is not under reuse.
  85. if (!checkReuse(playable, itemSkill, item))
  86. {
  87. return false;
  88. }
  89. if (!item.isPotion() && !item.isElixir() && playable.isCastingNow())
  90. {
  91. return false;
  92. }
  93. if ((itemSkill.getItemConsumeId() == 0) && (itemSkill.getItemConsume() > 0) && (item.isPotion() || item.isElixir() || itemSkill.isSimultaneousCast()))
  94. {
  95. if (!playable.destroyItem("Consume", item.getObjectId(), itemSkill.getItemConsume(), playable, false))
  96. {
  97. playable.sendPacket(SystemMessageId.NOT_ENOUGH_ITEMS);
  98. return false;
  99. }
  100. }
  101. // Send message to the master.
  102. if (playable.isPet())
  103. {
  104. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PET_USES_S1);
  105. sm.addSkillName(itemSkill);
  106. playable.sendPacket(sm);
  107. }
  108. if (itemSkill.isSimultaneousCast() || ((item.getItem().hasImmediateEffect() || item.getItem().hasExImmediateEffect()) && itemSkill.isStatic()))
  109. {
  110. playable.doSimultaneousCast(itemSkill);
  111. // Summons should be affected by herbs too, self time effect is handled at L2Effect constructor
  112. if (!playable.isSummon() && activeChar.hasSummon())
  113. {
  114. activeChar.getSummon().doSimultaneousCast(itemSkill);
  115. }
  116. }
  117. else
  118. {
  119. playable.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  120. if (!playable.useMagic(itemSkill, forceUse, false))
  121. {
  122. return false;
  123. }
  124. // Consume.
  125. if ((itemSkill.getItemConsumeId() == 0) && (itemSkill.getItemConsume() > 0))
  126. {
  127. if (!playable.destroyItem("Consume", item.getObjectId(), itemSkill.getItemConsume(), null, false))
  128. {
  129. playable.sendPacket(SystemMessageId.NOT_ENOUGH_ITEMS);
  130. return false;
  131. }
  132. }
  133. }
  134. if (itemSkill.getReuseDelay() > 0)
  135. {
  136. playable.addTimeStamp(itemSkill, itemSkill.getReuseDelay());
  137. }
  138. }
  139. }
  140. return true;
  141. }
  142. /**
  143. * @param playable the character using the item or skill
  144. * @param skill the skill being used, can be null
  145. * @param item the item being used
  146. * @return {@code true} if the the item or skill to check is available, {@code false} otherwise
  147. */
  148. private boolean checkReuse(L2Playable playable, L2Skill skill, L2ItemInstance item)
  149. {
  150. final long remainingTime = (skill != null) ? playable.getSkillRemainingReuseTime(skill.getReuseHashCode()) : playable.getItemRemainingReuseTime(item.getObjectId());
  151. final boolean isAvailable = remainingTime <= 0;
  152. if (playable.isPlayer())
  153. {
  154. if (!isAvailable)
  155. {
  156. final int hours = (int) (remainingTime / 3600000L);
  157. final int minutes = (int) (remainingTime % 3600000L) / 60000;
  158. final int seconds = (int) ((remainingTime / 1000) % 60);
  159. SystemMessage sm = null;
  160. if (hours > 0)
  161. {
  162. sm = SystemMessage.getSystemMessage(SystemMessageId.S2_HOURS_S3_MINUTES_S4_SECONDS_REMAINING_FOR_REUSE_S1);
  163. if ((skill == null) || skill.isStatic())
  164. {
  165. sm.addItemName(item);
  166. }
  167. else
  168. {
  169. sm.addSkillName(skill);
  170. }
  171. sm.addInt(hours);
  172. sm.addInt(minutes);
  173. }
  174. else if (minutes > 0)
  175. {
  176. sm = SystemMessage.getSystemMessage(SystemMessageId.S2_MINUTES_S3_SECONDS_REMAINING_FOR_REUSE_S1);
  177. if ((skill == null) || skill.isStatic())
  178. {
  179. sm.addItemName(item);
  180. }
  181. else
  182. {
  183. sm.addSkillName(skill);
  184. }
  185. sm.addInt(minutes);
  186. }
  187. else
  188. {
  189. sm = SystemMessage.getSystemMessage(SystemMessageId.S2_SECONDS_REMAINING_FOR_REUSE_S1);
  190. if ((skill == null) || skill.isStatic())
  191. {
  192. sm.addItemName(item);
  193. }
  194. else
  195. {
  196. sm.addSkillName(skill);
  197. }
  198. }
  199. sm.addInt(seconds);
  200. playable.sendPacket(sm);
  201. }
  202. }
  203. return isAvailable;
  204. }
  205. }