Potions.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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.itemhandlers;
  16. import java.util.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.datatables.SkillTable;
  19. import net.sf.l2j.gameserver.handler.IItemHandler;
  20. import net.sf.l2j.gameserver.model.L2Effect;
  21. import net.sf.l2j.gameserver.model.L2ItemInstance;
  22. import net.sf.l2j.gameserver.model.L2Skill;
  23. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  24. import net.sf.l2j.gameserver.model.actor.instance.L2PetInstance;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2SummonInstance;
  27. import net.sf.l2j.gameserver.model.entity.TvTEvent;
  28. import net.sf.l2j.gameserver.network.SystemMessageId;
  29. import net.sf.l2j.gameserver.network.serverpackets.ActionFailed;
  30. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  31. import net.sf.l2j.gameserver.templates.skills.L2EffectType;
  32. /**
  33. * This class ...
  34. *
  35. * @version $Revision: 1.2.4.4 $ $Date: 2005/03/27 15:30:07 $
  36. */
  37. public class Potions implements IItemHandler
  38. {
  39. protected static final Logger _log = Logger.getLogger(Potions.class.getName());
  40. private static final int[] ITEM_IDS =
  41. {
  42. 65, 725, 726, 727, 728, 734, 735, 1060, 1061, 1073,
  43. 1374, 1375, 1539, 1540, 5591, 5592, 6035, 6036,
  44. 6652, 6553, 6554, 6555, 8193, 8194, 8195, 8196,
  45. 8197, 8198, 8199, 8200, 8201, 8202, 8600, 8601,
  46. 8602, 8603, 8604, 8605, 8606, 8607,
  47. 8608, 8609, 8610, 8611, 8612, 8613, 8614, 10155, 10157,
  48. //Attribute Potion
  49. 9997, 9998, 9999, 10000, 10001, 10002,
  50. //elixir of life
  51. 8622, 8623, 8624, 8625, 8626, 8627,
  52. //elixir of Strength
  53. 8628, 8629, 8630, 8631, 8632, 8633,
  54. //elixir of cp
  55. 8634, 8635, 8636, 8637, 8638, 8639,
  56. // Endeavor Potion
  57. 733,
  58. // Juices
  59. 10260, 10261, 10262, 10263, 10264, 10265, 10266, 10267, 10268, 10269, 10270,
  60. // CT2 herbs
  61. 10655, 10656, 10657
  62. };
  63. /**
  64. *
  65. * @see net.sf.l2j.gameserver.handler.IItemHandler#useItem(net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance, net.sf.l2j.gameserver.model.L2ItemInstance)
  66. */
  67. public synchronized void useItem(L2PlayableInstance playable, L2ItemInstance item)
  68. {
  69. L2PcInstance activeChar;
  70. boolean res = false;
  71. if (playable instanceof L2PcInstance)
  72. activeChar = (L2PcInstance) playable;
  73. else if (playable instanceof L2PetInstance)
  74. activeChar = ((L2PetInstance) playable).getOwner();
  75. else
  76. return;
  77. if (!TvTEvent.onPotionUse(playable.getObjectId()))
  78. {
  79. playable.sendPacket(ActionFailed.STATIC_PACKET);
  80. return;
  81. }
  82. if (activeChar.isInOlympiadMode())
  83. {
  84. activeChar.sendPacket(new SystemMessage(SystemMessageId.THIS_ITEM_IS_NOT_AVAILABLE_FOR_THE_OLYMPIAD_EVENT));
  85. return;
  86. }
  87. if (activeChar.isAllSkillsDisabled())
  88. {
  89. ActionFailed af = ActionFailed.STATIC_PACKET;
  90. activeChar.sendPacket(af);
  91. return;
  92. }
  93. int itemId = item.getItemId();
  94. switch (itemId)
  95. {
  96. // HEALING AND SPEED POTIONS
  97. case 65: // red_potion, xml: 2001
  98. res = usePotion(activeChar, 2001, 1);
  99. break;
  100. case 725: // healing_drug, xml: 2002
  101. if (!isEffectReplaceable(activeChar, L2EffectType.HEAL_OVER_TIME, itemId))
  102. return;
  103. res = usePotion(activeChar, 2002, 1);
  104. break;
  105. case 727: // _healing_potion, xml: 2032
  106. if (!isEffectReplaceable(activeChar, L2EffectType.HEAL_OVER_TIME, itemId))
  107. return;
  108. res = usePotion(activeChar, 2032, 1);
  109. break;
  110. case 728: // custom mana potion, xml: 2005
  111. if (Config.L2JMOD_ENABLE_MANA_POTIONS_SUPPORT)
  112. res = usePotion(activeChar, 2005, 1);
  113. break;
  114. case 733: // Endeavor Potion, xml: 2010
  115. res = usePotion(activeChar, 2010, 1);
  116. break;
  117. case 734: // quick_step_potion, xml: 2011
  118. res = usePotion(activeChar, 2011, 1);
  119. break;
  120. case 735: // swift_attack_potion, xml: 2012
  121. res = usePotion(activeChar, 2012, 1);
  122. break;
  123. case 1060: // lesser_healing_potion,
  124. case 1073: // beginner's potion, xml: 2031
  125. if (!isEffectReplaceable(activeChar, L2EffectType.HEAL_OVER_TIME, itemId))
  126. return;
  127. res = usePotion(activeChar, 2031, 1);
  128. break;
  129. case 1061: // healing_potion, xml: 2032
  130. if (!isEffectReplaceable(activeChar, L2EffectType.HEAL_OVER_TIME, itemId))
  131. return;
  132. res = usePotion(activeChar, 2032, 1);
  133. break;
  134. case 10157: // instant haste_potion, xml: 2398
  135. res = usePotion(activeChar, 2398, 1);
  136. break;
  137. case 1374: // adv_quick_step_potion, xml: 2034
  138. res = usePotion(activeChar, 2034, 1);
  139. break;
  140. case 1375: // adv_swift_attack_potion, xml: 2035
  141. res = usePotion(activeChar, 2035, 1);
  142. break;
  143. case 1539: // greater_healing_potion, xml: 2037
  144. if (!isEffectReplaceable(activeChar, L2EffectType.HEAL_OVER_TIME, itemId))
  145. return;
  146. res = usePotion(activeChar, 2037, 1);
  147. break;
  148. case 1540: // quick_healing_potion, xml: 2038
  149. if (!isEffectReplaceable(activeChar, L2EffectType.HEAL_OVER_TIME, itemId))
  150. return;
  151. res = usePotion(activeChar, 2038, 1);
  152. break;
  153. case 5591:
  154. case 5592: // CP and Greater CP
  155. if (!isEffectReplaceable(activeChar, L2EffectType.COMBAT_POINT_HEAL_OVER_TIME, itemId))
  156. return;
  157. res = usePotion(activeChar, 2166, (itemId == 5591) ? 1 : 2);
  158. break;
  159. case 6035: // Magic Haste Potion, xml: 2169
  160. res = usePotion(activeChar, 2169, 1);
  161. break;
  162. case 6036: // Greater Magic Haste Potion, xml: 2169
  163. res = usePotion(activeChar, 2169, 2);
  164. break;
  165. case 10155: //Mental Potion XML:2396
  166. res = usePotion(activeChar, 2396, 1);
  167. break;
  168. // ATTRIBUTE POTION
  169. case 9997: // Fire Resist Potion, xml: 2335
  170. res = usePotion(activeChar, 2335, 1);
  171. break;
  172. case 9998: // Water Resist Potion, xml: 2336
  173. res = usePotion(activeChar, 2336, 1);
  174. break;
  175. case 9999: // Earth Resist Potion, xml: 2338
  176. res = usePotion(activeChar, 2338, 1);
  177. break;
  178. case 10000: // Wind Resist Potion, xml: 2337
  179. res = usePotion(activeChar, 2337, 1);
  180. break;
  181. case 10001: // Dark Resist Potion, xml: 2340
  182. res = usePotion(activeChar, 2340, 1);
  183. break;
  184. case 10002: // Divine Resist Potion, xml: 2339
  185. res = usePotion(activeChar, 2339, 1);
  186. break;
  187. // ELIXIR
  188. case 8622:
  189. case 8623:
  190. case 8624:
  191. case 8625:
  192. case 8626:
  193. case 8627:
  194. {
  195. // elixir of Life
  196. byte expIndex = (byte) activeChar.getExpertiseIndex();
  197. res = usePotion(activeChar, 2287, (expIndex > 5 ? expIndex : expIndex + 1));
  198. break;
  199. }
  200. case 8628:
  201. case 8629:
  202. case 8630:
  203. case 8631:
  204. case 8632:
  205. case 8633:
  206. {
  207. byte expIndex = (byte) activeChar.getExpertiseIndex();
  208. // elixir of Strength
  209. res = usePotion(activeChar, 2288, (expIndex > 5 ? expIndex : expIndex + 1));
  210. break;
  211. }
  212. case 8634:
  213. case 8635:
  214. case 8636:
  215. case 8637:
  216. case 8638:
  217. case 8639:
  218. {
  219. byte expIndex = (byte) activeChar.getExpertiseIndex();
  220. // elixir of cp
  221. res = usePotion(activeChar, 2289, (expIndex > 5 ? expIndex : expIndex + 1));
  222. break;
  223. }
  224. // VALAKAS AMULETS
  225. case 6652: // Amulet Protection of Valakas
  226. res = usePotion(activeChar, 2231, 1);
  227. break;
  228. case 6653: // Amulet Flames of Valakas
  229. res = usePotion(activeChar, 2223, 1);
  230. break;
  231. case 6654: // Amulet Flames of Valakas
  232. res = usePotion(activeChar, 2233, 1);
  233. break;
  234. case 6655: // Amulet Slay Valakas
  235. res = usePotion(activeChar, 2232, 1);
  236. break;
  237. // HERBS
  238. case 8600: // Herb of Life
  239. res = usePotion(playable, 2278, 1);
  240. break;
  241. case 8601: // Greater Herb of Life
  242. res = usePotion(playable, 2278, 2);
  243. break;
  244. case 8602: // Superior Herb of Life
  245. res = usePotion(playable, 2278, 3);
  246. break;
  247. case 8603: // Herb of Mana
  248. res = usePotion(playable, 2279, 1);
  249. break;
  250. case 8604: // Greater Herb of Mane
  251. res = usePotion(playable, 2279, 2);
  252. break;
  253. case 8605: // Superior Herb of Mane
  254. res = usePotion(playable, 2279, 3);
  255. break;
  256. case 8606: // Herb of Strength
  257. res = usePotion(playable, 2280, 1);
  258. break;
  259. case 8607: // Herb of Magic
  260. res = usePotion(playable, 2281, 1);
  261. break;
  262. case 8608: // Herb of Atk. Spd.
  263. res = usePotion(playable, 2282, 1);
  264. break;
  265. case 8609: // Herb of Casting Spd.
  266. res = usePotion(playable, 2283, 1);
  267. break;
  268. case 8610: // Herb of Critical Attack
  269. res = usePotion(playable, 2284, 1);
  270. break;
  271. case 8611: // Herb of Speed
  272. res = usePotion(playable, 2285, 1);
  273. break;
  274. case 8612: // Herb of Warrior
  275. res = usePotion(playable, 2280, 1);// Herb of Strength
  276. res = usePotion(playable, 2282, 1);// Herb of Atk. Spd
  277. res = usePotion(playable, 2284, 1);// Herb of Critical Attack
  278. break;
  279. case 8613: // Herb of Mystic
  280. res = usePotion(playable, 2281, 1);// Herb of Magic
  281. res = usePotion(playable, 2283, 1);// Herb of Casting Spd.
  282. break;
  283. case 8614: // Herb of Warrior
  284. res = usePotion(playable, 2278, 3);// Superior Herb of Life
  285. res = usePotion(playable, 2279, 3);// Superior Herb of Mana
  286. break;
  287. case 10655:
  288. res = usePotion(playable, 2512, 1);
  289. break;
  290. case 10656:
  291. res = usePotion(playable, 2514, 1);
  292. break;
  293. case 10657:
  294. res = usePotion(playable, 2513, 1);
  295. break;
  296. // FISHERMAN POTIONS
  297. case 8193: // Fisherman's Potion - Green
  298. if (activeChar.getSkillLevel(1315) <= 3)
  299. {
  300. playable.destroyItem("Consume", item.getObjectId(), 1, null, false);
  301. playable.sendPacket(new SystemMessage(SystemMessageId.NOTHING_HAPPENED));
  302. return;
  303. }
  304. res = usePotion(activeChar, 2274, 1);
  305. break;
  306. case 8194: // Fisherman's Potion - Jade
  307. if (activeChar.getSkillLevel(1315) <= 6)
  308. {
  309. playable.destroyItem("Consume", item.getObjectId(), 1, null, false);
  310. playable.sendPacket(new SystemMessage(SystemMessageId.NOTHING_HAPPENED));
  311. return;
  312. }
  313. res = usePotion(activeChar, 2274, 2);
  314. break;
  315. case 8195: // Fisherman's Potion - Blue
  316. if (activeChar.getSkillLevel(1315) <= 9)
  317. {
  318. playable.destroyItem("Consume", item.getObjectId(), 1, null, false);
  319. playable.sendPacket(new SystemMessage(SystemMessageId.NOTHING_HAPPENED));
  320. return;
  321. }
  322. res = usePotion(activeChar, 2274, 3);
  323. break;
  324. case 8196: // Fisherman's Potion - Yellow
  325. if (activeChar.getSkillLevel(1315) <= 12)
  326. {
  327. playable.destroyItem("Consume", item.getObjectId(), 1, null, false);
  328. playable.sendPacket(new SystemMessage(SystemMessageId.NOTHING_HAPPENED));
  329. return;
  330. }
  331. res = usePotion(activeChar, 2274, 4);
  332. break;
  333. case 8197: // Fisherman's Potion - Orange
  334. if (activeChar.getSkillLevel(1315) <= 15)
  335. {
  336. playable.destroyItem("Consume", item.getObjectId(), 1, null, false);
  337. playable.sendPacket(new SystemMessage(SystemMessageId.NOTHING_HAPPENED));
  338. return;
  339. }
  340. res = usePotion(activeChar, 2274, 5);
  341. break;
  342. case 8198: // Fisherman's Potion - Purple
  343. if (activeChar.getSkillLevel(1315) <= 18)
  344. {
  345. playable.destroyItem("Consume", item.getObjectId(), 1, null, false);
  346. playable.sendPacket(new SystemMessage(SystemMessageId.NOTHING_HAPPENED));
  347. return;
  348. }
  349. res = usePotion(activeChar, 2274, 6);
  350. break;
  351. case 8199: // Fisherman's Potion - Red
  352. if (activeChar.getSkillLevel(1315) <= 21)
  353. {
  354. playable.destroyItem("Consume", item.getObjectId(), 1, null, false);
  355. playable.sendPacket(new SystemMessage(SystemMessageId.NOTHING_HAPPENED));
  356. return;
  357. }
  358. res = usePotion(activeChar, 2274, 7);
  359. break;
  360. case 8200: // Fisherman's Potion - White
  361. if (activeChar.getSkillLevel(1315) <= 24)
  362. {
  363. playable.destroyItem("Consume", item.getObjectId(), 1, null, false);
  364. playable.sendPacket(new SystemMessage(SystemMessageId.NOTHING_HAPPENED));
  365. return;
  366. }
  367. res = usePotion(activeChar, 2274, 8);
  368. break;
  369. case 8201: // Fisherman's Potion - Black
  370. res = usePotion(activeChar, 2274, 9);
  371. break;
  372. case 8202: // Fishing Potion
  373. res = usePotion(activeChar, 2275, 1);
  374. break;
  375. // Juices
  376. // added by Z0mbie!
  377. case 10260: // Haste Juice,xml:2429
  378. res = usePotion(activeChar, 2429, 1);
  379. break;
  380. case 10261: // Accuracy Juice,xml:2430
  381. res = usePotion(activeChar, 2430, 1);
  382. break;
  383. case 10262: // Critical Power Juice,xml:2431
  384. res = usePotion(activeChar, 2431, 1);
  385. break;
  386. case 10263: // Critical Attack Juice,xml:2432
  387. res = usePotion(activeChar, 2432, 1);
  388. break;
  389. case 10264: // Casting Speed Juice,xml:2433
  390. res = usePotion(activeChar, 2433, 1);
  391. break;
  392. case 10265: // Evasion Juice,xml:2434
  393. res = usePotion(activeChar, 2434, 1);
  394. break;
  395. case 10266: // Magic Power Juice,xml:2435
  396. res = usePotion(activeChar, 2435, 1);
  397. break;
  398. case 10267: // Power Juice,xml:2436
  399. res = usePotion(activeChar, 2436, 1);
  400. break;
  401. case 10268: // Speed Juice,xml:2437
  402. res = usePotion(activeChar, 2437, 1);
  403. break;
  404. case 10269: // Defense Juice,xml:2438
  405. res = usePotion(activeChar, 2438, 1);
  406. break;
  407. case 10270: // MP Consumption Juice,xml: 2439
  408. res = usePotion(activeChar, 2439, 1);
  409. break;
  410. default:
  411. }
  412. if (res)
  413. playable.destroyItem("Consume", item.getObjectId(), 1, null, false);
  414. }
  415. /**
  416. *
  417. * @param activeChar
  418. * @param effectType
  419. * @param itemId
  420. * @return
  421. */
  422. @SuppressWarnings("unchecked")
  423. private boolean isEffectReplaceable(L2PcInstance activeChar, Enum effectType, int itemId)
  424. {
  425. L2Effect[] effects = activeChar.getAllEffects();
  426. if (effects == null)
  427. return true;
  428. for (L2Effect e : effects)
  429. {
  430. if (e.getEffectType() == effectType)
  431. {
  432. // One can reuse pots after 2/3 of their duration is over.
  433. // It would be faster to check if its > 10 but that would screw custom pot durations...
  434. if (e.getTaskTime() > (e.getSkill().getBuffDuration() * 67) / 100000)
  435. return true;
  436. SystemMessage sm = new SystemMessage(SystemMessageId.S1_PREPARED_FOR_REUSE);
  437. sm.addItemName(itemId);
  438. activeChar.sendPacket(sm);
  439. return false;
  440. }
  441. }
  442. return true;
  443. }
  444. /**
  445. *
  446. * @param activeChar
  447. * @param magicId
  448. * @param level
  449. * @return
  450. */
  451. public boolean usePotion(L2PlayableInstance activeChar, int magicId, int level)
  452. {
  453. L2Skill skill = SkillTable.getInstance().getInfo(magicId, level);
  454. if (skill != null)
  455. {
  456. // Return false if potion is in reuse
  457. // so is not destroyed from inventory
  458. if (activeChar.isSkillDisabled(skill.getId()))
  459. {
  460. SystemMessage sm = new SystemMessage(SystemMessageId.S1_PREPARED_FOR_REUSE);
  461. sm.addSkillName(skill);
  462. activeChar.sendPacket(sm);
  463. return false;
  464. }
  465. activeChar.doSimultaneousCast(skill);
  466. if (activeChar instanceof L2PcInstance)
  467. {
  468. L2PcInstance player = (L2PcInstance)activeChar;
  469. //only for Heal potions
  470. if (magicId == 2031 || magicId == 2032 || magicId == 2037)
  471. {
  472. player.shortBuffStatusUpdate(magicId, level, 15);
  473. }
  474. // Summons should be affected by herbs too, self time effect is handled at L2Effect constructor
  475. else if (((magicId > 2277 && magicId < 2286) || (magicId >= 2512 && magicId <= 2514))
  476. && (player.getPet() != null && player.getPet() instanceof L2SummonInstance))
  477. {
  478. player.getPet().doSimultaneousCast(skill);
  479. }
  480. if (!(player.isSitting() && !skill.isPotion()))
  481. return true;
  482. }
  483. else if (activeChar instanceof L2PetInstance)
  484. {
  485. SystemMessage sm = new SystemMessage(SystemMessageId.PET_USES_S1);
  486. sm.addString(skill.getName());
  487. ((L2PetInstance)(activeChar)).getOwner().sendPacket(sm);
  488. }
  489. }
  490. return false;
  491. }
  492. /**
  493. *
  494. * @see net.sf.l2j.gameserver.handler.IItemHandler#getItemIds()
  495. */
  496. public int[] getItemIds()
  497. {
  498. return ITEM_IDS;
  499. }
  500. }