L2Potion.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver.model;
  20. import java.util.concurrent.Future;
  21. import java.util.logging.Logger;
  22. import net.sf.l2j.Config;
  23. import net.sf.l2j.gameserver.ThreadPoolManager;
  24. /**
  25. *
  26. * This class ...
  27. *
  28. * @version $Revision: 1.2.2.1.2.5 $ $Date: 2005/03/27 15:29:30 $
  29. */
  30. public class L2Potion extends L2Object
  31. {
  32. protected static final Logger _log = Logger.getLogger(L2Character.class.getName());
  33. @SuppressWarnings("unused")
  34. private L2Character _target;
  35. private Future _potionhpRegTask;
  36. private Future _potionmpRegTask;
  37. protected int _milliseconds;
  38. protected double _effect;
  39. protected int _duration;
  40. private int _potion;
  41. protected Object _mpLock = new Object();
  42. protected Object _hpLock = new Object();
  43. class PotionHpHealing implements Runnable
  44. {
  45. L2Character _instance;
  46. public PotionHpHealing(L2Character instance)
  47. {
  48. _instance = instance;
  49. }
  50. public void run()
  51. {
  52. try
  53. {
  54. synchronized(_hpLock)
  55. {
  56. double nowHp = _instance.getCurrentHp();
  57. if(_duration == 0)
  58. {
  59. stopPotionHpRegeneration();
  60. }
  61. if (_duration != 0)
  62. {
  63. nowHp += _effect;
  64. _instance.setCurrentHp(nowHp);
  65. _duration = _duration - (_milliseconds / 1000);
  66. setCurrentHpPotion2();
  67. }
  68. }
  69. }
  70. catch (Exception e)
  71. {
  72. _log.warning("Error in hp potion task:"+e);
  73. }
  74. }
  75. }
  76. public L2Potion(int objectId)
  77. {
  78. super(objectId);
  79. }
  80. public void stopPotionHpRegeneration()
  81. {
  82. if (_potionhpRegTask != null)
  83. {
  84. _potionhpRegTask.cancel(false);
  85. }
  86. _potionhpRegTask = null;
  87. if (Config.DEBUG) _log.fine("Potion HP regen stop");
  88. }
  89. public void setCurrentHpPotion2()
  90. {
  91. if (_duration == 0)
  92. {
  93. stopPotionHpRegeneration();
  94. }
  95. }
  96. public void setCurrentHpPotion1(L2Character activeChar, int item)
  97. {
  98. _potion = item;
  99. _target = activeChar;
  100. switch (_potion)
  101. {
  102. case (1540):
  103. double nowHp = activeChar.getCurrentHp();
  104. nowHp+=435;
  105. if (nowHp>= activeChar.getMaxHp())
  106. {
  107. nowHp = activeChar.getMaxHp();
  108. }
  109. activeChar.setCurrentHp(nowHp);
  110. break;
  111. case (728):
  112. double nowMp = activeChar.getMaxMp();
  113. nowMp+=435;
  114. if (nowMp>= activeChar.getMaxMp())
  115. {
  116. nowMp = activeChar.getMaxMp();
  117. }
  118. activeChar.setCurrentMp(nowMp);
  119. break;
  120. case (726):
  121. _milliseconds = 500;
  122. _duration = 15;
  123. _effect = 1.5;
  124. startPotionMpRegeneration(activeChar);
  125. break;
  126. }
  127. }
  128. class PotionMpHealing implements Runnable
  129. {
  130. L2Character _instance;
  131. public PotionMpHealing(L2Character instance)
  132. {
  133. _instance = instance;
  134. }
  135. public void run()
  136. {
  137. try
  138. {
  139. synchronized(_mpLock)
  140. {
  141. double nowMp = _instance.getCurrentMp();
  142. if(_duration == 0)
  143. {
  144. stopPotionMpRegeneration();
  145. }
  146. if (_duration != 0)
  147. {
  148. nowMp+=_effect;
  149. _instance.setCurrentMp(nowMp);
  150. _duration=(_duration-(_milliseconds/1000));
  151. setCurrentMpPotion2();
  152. }
  153. }
  154. }
  155. catch (Exception e)
  156. {
  157. _log.warning("error in mp potion task:"+e);
  158. }
  159. }
  160. }
  161. private void startPotionMpRegeneration(L2Character activeChar)
  162. {
  163. _potionmpRegTask = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(
  164. new PotionMpHealing(activeChar), 1000, _milliseconds);
  165. if (Config.DEBUG) _log.fine("Potion MP regen Started");
  166. }
  167. public void stopPotionMpRegeneration()
  168. {
  169. if (_potionmpRegTask != null)
  170. {
  171. _potionmpRegTask.cancel(false);
  172. }
  173. _potionmpRegTask = null;
  174. if (Config.DEBUG) _log.fine("Potion MP regen stop");
  175. }
  176. public void setCurrentMpPotion2()
  177. {
  178. if (_duration == 0)
  179. {
  180. stopPotionMpRegeneration();
  181. }
  182. }
  183. public void setCurrentMpPotion1(L2Character activeChar, int item)
  184. {
  185. _potion = item;
  186. _target = activeChar;
  187. switch (_potion)
  188. {
  189. }
  190. }
  191. /* (non-Javadoc)
  192. * @see net.sf.l2j.gameserver.model.L2Object#isAttackable()
  193. */
  194. @Override
  195. public boolean isAutoAttackable(@SuppressWarnings("unused") L2Character attacker)
  196. {
  197. return false;
  198. }
  199. }