L2Potion.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.model;
  16. import java.util.concurrent.Future;
  17. import java.util.logging.Logger;
  18. import net.sf.l2j.Config;
  19. import net.sf.l2j.gameserver.ThreadPoolManager;
  20. /**
  21. *
  22. * This class ...
  23. *
  24. * @version $Revision: 1.2.2.1.2.5 $ $Date: 2005/03/27 15:29:30 $
  25. */
  26. public class L2Potion extends L2Object
  27. {
  28. protected static final Logger _log = Logger.getLogger(L2Character.class.getName());
  29. @SuppressWarnings("unused")
  30. private L2Character _target;
  31. private Future<?> _potionhpRegTask;
  32. private Future<?> _potionmpRegTask;
  33. protected int _milliseconds;
  34. protected double _effect;
  35. protected int _duration;
  36. private int _potion;
  37. protected Object _mpLock = new Object();
  38. protected Object _hpLock = new Object();
  39. class PotionHpHealing implements Runnable
  40. {
  41. L2Character _instance;
  42. public PotionHpHealing(L2Character instance)
  43. {
  44. _instance = instance;
  45. }
  46. public void run()
  47. {
  48. try
  49. {
  50. synchronized(_hpLock)
  51. {
  52. double nowHp = _instance.getCurrentHp();
  53. if(_duration == 0)
  54. {
  55. stopPotionHpRegeneration();
  56. }
  57. if (_duration != 0)
  58. {
  59. nowHp += _effect;
  60. _instance.setCurrentHp(nowHp);
  61. _duration = _duration - (_milliseconds / 1000);
  62. setCurrentHpPotion2();
  63. }
  64. }
  65. }
  66. catch (Exception e)
  67. {
  68. _log.warning("Error in hp potion task:"+e);
  69. }
  70. }
  71. }
  72. public L2Potion(int objectId)
  73. {
  74. super(objectId);
  75. }
  76. public void stopPotionHpRegeneration()
  77. {
  78. if (_potionhpRegTask != null)
  79. {
  80. _potionhpRegTask.cancel(false);
  81. }
  82. _potionhpRegTask = null;
  83. if (Config.DEBUG) _log.fine("Potion HP regen stop");
  84. }
  85. public void setCurrentHpPotion2()
  86. {
  87. if (_duration == 0)
  88. {
  89. stopPotionHpRegeneration();
  90. }
  91. }
  92. public void setCurrentHpPotion1(L2Character activeChar, int item)
  93. {
  94. _potion = item;
  95. _target = activeChar;
  96. switch (_potion)
  97. {
  98. case (1540):
  99. double nowHp = activeChar.getCurrentHp();
  100. nowHp+=435;
  101. if (nowHp>= activeChar.getMaxHp())
  102. {
  103. nowHp = activeChar.getMaxHp();
  104. }
  105. activeChar.setCurrentHp(nowHp);
  106. break;
  107. case (728):
  108. double nowMp = activeChar.getMaxMp();
  109. nowMp+=435;
  110. if (nowMp>= activeChar.getMaxMp())
  111. {
  112. nowMp = activeChar.getMaxMp();
  113. }
  114. activeChar.setCurrentMp(nowMp);
  115. break;
  116. case (726):
  117. _milliseconds = 500;
  118. _duration = 15;
  119. _effect = 1.5;
  120. startPotionMpRegeneration(activeChar);
  121. break;
  122. }
  123. }
  124. class PotionMpHealing implements Runnable
  125. {
  126. L2Character _instance;
  127. public PotionMpHealing(L2Character instance)
  128. {
  129. _instance = instance;
  130. }
  131. public void run()
  132. {
  133. try
  134. {
  135. synchronized(_mpLock)
  136. {
  137. double nowMp = _instance.getCurrentMp();
  138. if(_duration == 0)
  139. {
  140. stopPotionMpRegeneration();
  141. }
  142. if (_duration != 0)
  143. {
  144. nowMp+=_effect;
  145. _instance.setCurrentMp(nowMp);
  146. _duration=(_duration-(_milliseconds/1000));
  147. setCurrentMpPotion2();
  148. }
  149. }
  150. }
  151. catch (Exception e)
  152. {
  153. _log.warning("error in mp potion task:"+e);
  154. }
  155. }
  156. }
  157. private void startPotionMpRegeneration(L2Character activeChar)
  158. {
  159. _potionmpRegTask = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(
  160. new PotionMpHealing(activeChar), 1000, _milliseconds);
  161. if (Config.DEBUG) _log.fine("Potion MP regen Started");
  162. }
  163. public void stopPotionMpRegeneration()
  164. {
  165. if (_potionmpRegTask != null)
  166. {
  167. _potionmpRegTask.cancel(false);
  168. }
  169. _potionmpRegTask = null;
  170. if (Config.DEBUG) _log.fine("Potion MP regen stop");
  171. }
  172. public void setCurrentMpPotion2()
  173. {
  174. if (_duration == 0)
  175. {
  176. stopPotionMpRegeneration();
  177. }
  178. }
  179. public void setCurrentMpPotion1(L2Character activeChar, int item)
  180. {
  181. _potion = item;
  182. _target = activeChar;
  183. switch (_potion)
  184. {
  185. }
  186. }
  187. /* (non-Javadoc)
  188. * @see net.sf.l2j.gameserver.model.L2Object#isAttackable()
  189. */
  190. @Override
  191. public boolean isAutoAttackable(@SuppressWarnings("unused") L2Character attacker)
  192. {
  193. return false;
  194. }
  195. }