ItemSkillsTemplate.java 6.3 KB

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