Potions.java 18 KB

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