L2Transformation.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.ScheduledFuture;
  17. import net.sf.l2j.gameserver.ThreadPoolManager;
  18. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  19. /**
  20. *
  21. * @author KenM
  22. */
  23. public abstract class L2Transformation implements Cloneable, Runnable
  24. {
  25. private final int _id;
  26. private final int _graphicalId;
  27. private final double _collisionRadius;
  28. private final double _collisionHeight;
  29. private long _duration;
  30. public static final int TRANSFORM_ZARICHE = 301;
  31. public static final int TRANSFORM_AKAMANAH = 302;
  32. private L2PcInstance _player;
  33. private long _startTime;
  34. private ScheduledFuture<?> _future;
  35. /**
  36. *
  37. * @param id Internal id that server will use to associate this transformation
  38. * @param graphicalId Client visible transformation id
  39. * @param duration Transformation duration in seconds
  40. * @param collisionRadius Collision Radius of the player while transformed
  41. * @param collisionHeight Collision Height of the player while transformed
  42. */
  43. public L2Transformation(int id, int graphicalId, int duration, double collisionRadius, double collisionHeight)
  44. {
  45. _id = id;
  46. _graphicalId = graphicalId;
  47. _collisionRadius = collisionRadius;
  48. _collisionHeight = collisionHeight;
  49. this.setDuration(duration * 1000);
  50. }
  51. /**
  52. *
  53. * @param id Internal id(will be used also as client graphical id) that server will use to associate this transformation
  54. * @param duration Transformation duration in seconds
  55. * @param collisionRadius Collision Radius of the player while transformed
  56. * @param collisionHeight Collision Height of the player while transformed
  57. */
  58. public L2Transformation(int id, int duration, double collisionRadius, double collisionHeight)
  59. {
  60. this(id, id, duration, collisionRadius, collisionHeight);
  61. }
  62. /**
  63. * @return Returns the id.
  64. */
  65. public int getId()
  66. {
  67. return _id;
  68. }
  69. /**
  70. * @return Returns the graphicalId.
  71. */
  72. public int getGraphicalId()
  73. {
  74. return _graphicalId;
  75. }
  76. /**
  77. * @return Returns the collisionRadius.
  78. */
  79. public double getCollisionRadius()
  80. {
  81. if (getId()>=312 && getId()<=318)
  82. return _player.getBaseTemplate().collisionRadius;
  83. return _collisionRadius;
  84. }
  85. /**
  86. * @return Returns the collisionHeight.
  87. */
  88. public double getCollisionHeight()
  89. {
  90. if (getId()>=312 && getId()<=318)
  91. return _player.getBaseTemplate().collisionHeight;
  92. return _collisionHeight;
  93. }
  94. /**
  95. * @param duration The duration to set.
  96. */
  97. public void setDuration(long duration)
  98. {
  99. _duration = duration;
  100. }
  101. /**
  102. * @return Returns the total duration in miliseconds.
  103. */
  104. public long getDuration()
  105. {
  106. return _duration;
  107. }
  108. /**
  109. * @return The remaining transformed time in miliseconds. An zero or negative value if the transformation has already ended.
  110. */
  111. public long getRemainingTime()
  112. {
  113. return (getStartTime() + this.getDuration()) - System.currentTimeMillis();
  114. }
  115. // Scriptable Events
  116. public abstract void onTransform();
  117. public abstract void onUntransform();
  118. /**
  119. * @param player The player to set.
  120. */
  121. private void setPlayer(L2PcInstance player)
  122. {
  123. _player = player;
  124. }
  125. /**
  126. * @return Returns the player.
  127. */
  128. public L2PcInstance getPlayer()
  129. {
  130. return _player;
  131. }
  132. /**
  133. * @param startTime The startTime to set.
  134. */
  135. public void setStartTime(long startTime)
  136. {
  137. _startTime = startTime;
  138. }
  139. /**
  140. * @return Returns the startTime.
  141. */
  142. private long getStartTime()
  143. {
  144. return _startTime;
  145. }
  146. /**
  147. * @param future The future to set.
  148. */
  149. public void setFuture(ScheduledFuture<?> future)
  150. {
  151. _future = future;
  152. }
  153. /**
  154. * @return Returns the future.
  155. */
  156. public ScheduledFuture<?> getFuture()
  157. {
  158. return _future;
  159. }
  160. public void start()
  161. {
  162. this.setStartTime(System.currentTimeMillis());
  163. this.resume();
  164. }
  165. public void resume()
  166. {
  167. this.setFuture(ThreadPoolManager.getInstance().scheduleGeneral(this, this.getRemainingTime()));
  168. this.getPlayer().transform(this);
  169. }
  170. public void run()
  171. {
  172. this.stop();
  173. }
  174. public void stop()
  175. {
  176. if (this.getRemainingTime() > 0)
  177. {
  178. this.getPlayer().untransform();
  179. }
  180. }
  181. public L2Transformation createTransformationForPlayer(L2PcInstance player)
  182. {
  183. try
  184. {
  185. L2Transformation transformation = (L2Transformation) this.clone();
  186. transformation.setPlayer(player);
  187. return transformation;
  188. }
  189. catch (CloneNotSupportedException e)
  190. {
  191. // should never happen
  192. return null;
  193. }
  194. }
  195. // Override if necessary
  196. public void onLevelUp()
  197. {
  198. }
  199. }