ItemSkillsTemplate.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. boolean hasConsumeSkill = false;
  66. for (SkillHolder skillInfo : skills)
  67. {
  68. if (skillInfo == null)
  69. {
  70. continue;
  71. }
  72. Skill itemSkill = skillInfo.getSkill();
  73. if (itemSkill != null)
  74. {
  75. if (itemSkill.getItemConsumeId() > 0)
  76. {
  77. hasConsumeSkill = true;
  78. }
  79. if (!itemSkill.checkCondition(playable, playable.getTarget(), false))
  80. {
  81. return false;
  82. }
  83. if (playable.isSkillDisabled(itemSkill))
  84. {
  85. return false;
  86. }
  87. // Verify that skill is not under reuse.
  88. if (!checkReuse(playable, itemSkill, item))
  89. {
  90. return false;
  91. }
  92. if (!item.isPotion() && !item.isElixir() && !item.isScroll() && playable.isCastingNow())
  93. {
  94. return false;
  95. }
  96. // Send message to the master.
  97. if (playable.isPet())
  98. {
  99. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PET_USES_S1);
  100. sm.addSkillName(itemSkill);
  101. playable.sendPacket(sm);
  102. }
  103. if (itemSkill.isSimultaneousCast() || ((item.getItem().hasImmediateEffect() || item.getItem().hasExImmediateEffect()) && itemSkill.isStatic()))
  104. {
  105. playable.doSimultaneousCast(itemSkill);
  106. }
  107. else
  108. {
  109. playable.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  110. if (!playable.useMagic(itemSkill, forceUse, false))
  111. {
  112. return false;
  113. }
  114. }
  115. if (itemSkill.getReuseDelay() > 0)
  116. {
  117. playable.addTimeStamp(itemSkill, itemSkill.getReuseDelay());
  118. }
  119. }
  120. }
  121. if (checkConsume(item, hasConsumeSkill))
  122. {
  123. if (!playable.destroyItem("Consume", item.getObjectId(), 1, playable, false))
  124. {
  125. playable.sendPacket(SystemMessageId.NOT_ENOUGH_ITEMS);
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. /**
  132. * @param item the item being used
  133. * @param hasConsumeSkill
  134. * @return {@code true} check if item use consume item, {@code false} otherwise
  135. */
  136. private boolean checkConsume(L2ItemInstance item, boolean hasConsumeSkill)
  137. {
  138. switch (item.getItem().getDefaultAction())
  139. {
  140. case CAPSULE:
  141. case SKILL_REDUCE:
  142. {
  143. if (!hasConsumeSkill && item.getItem().hasImmediateEffect())
  144. {
  145. return true;
  146. }
  147. }
  148. }
  149. return false;
  150. }
  151. /**
  152. * @param playable the character using the item or skill
  153. * @param skill the skill being used, can be null
  154. * @param item the item being used
  155. * @return {@code true} if the the item or skill to check is available, {@code false} otherwise
  156. */
  157. private boolean checkReuse(L2Playable playable, Skill skill, L2ItemInstance item)
  158. {
  159. final long remainingTime = (skill != null) ? playable.getSkillRemainingReuseTime(skill.getReuseHashCode()) : playable.getItemRemainingReuseTime(item.getObjectId());
  160. final boolean isAvailable = remainingTime <= 0;
  161. if (playable.isPlayer())
  162. {
  163. if (!isAvailable)
  164. {
  165. final int hours = (int) (remainingTime / 3600000L);
  166. final int minutes = (int) (remainingTime % 3600000L) / 60000;
  167. final int seconds = (int) ((remainingTime / 1000) % 60);
  168. SystemMessage sm = null;
  169. if (hours > 0)
  170. {
  171. sm = SystemMessage.getSystemMessage(SystemMessageId.S2_HOURS_S3_MINUTES_S4_SECONDS_REMAINING_FOR_REUSE_S1);
  172. if ((skill == null) || skill.isStatic())
  173. {
  174. sm.addItemName(item);
  175. }
  176. else
  177. {
  178. sm.addSkillName(skill);
  179. }
  180. sm.addInt(hours);
  181. sm.addInt(minutes);
  182. }
  183. else if (minutes > 0)
  184. {
  185. sm = SystemMessage.getSystemMessage(SystemMessageId.S2_MINUTES_S3_SECONDS_REMAINING_FOR_REUSE_S1);
  186. if ((skill == null) || skill.isStatic())
  187. {
  188. sm.addItemName(item);
  189. }
  190. else
  191. {
  192. sm.addSkillName(skill);
  193. }
  194. sm.addInt(minutes);
  195. }
  196. else
  197. {
  198. sm = SystemMessage.getSystemMessage(SystemMessageId.S2_SECONDS_REMAINING_FOR_REUSE_S1);
  199. if ((skill == null) || skill.isStatic())
  200. {
  201. sm.addItemName(item);
  202. }
  203. else
  204. {
  205. sm.addSkillName(skill);
  206. }
  207. }
  208. sm.addInt(seconds);
  209. playable.sendPacket(sm);
  210. }
  211. }
  212. return isAvailable;
  213. }
  214. }