L2Transformation.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. return _collisionRadius;
  82. }
  83. /**
  84. * @return Returns the collisionHeight.
  85. */
  86. public double getCollisionHeight()
  87. {
  88. return _collisionHeight;
  89. }
  90. /**
  91. * @param duration The duration to set.
  92. */
  93. public void setDuration(long duration)
  94. {
  95. _duration = duration;
  96. }
  97. /**
  98. * @return Returns the total duration in miliseconds.
  99. */
  100. public long getDuration()
  101. {
  102. return _duration;
  103. }
  104. /**
  105. * @return The remaining transformed time in miliseconds. An zero or negative value if the transformation has already ended.
  106. */
  107. public long getRemainingTime()
  108. {
  109. return (getStartTime() + this.getDuration()) - System.currentTimeMillis();
  110. }
  111. // Scriptable Events
  112. public abstract void onTransform();
  113. public abstract void onUntransform();
  114. /**
  115. * @param player The player to set.
  116. */
  117. private void setPlayer(L2PcInstance player)
  118. {
  119. _player = player;
  120. }
  121. /**
  122. * @return Returns the player.
  123. */
  124. public L2PcInstance getPlayer()
  125. {
  126. return _player;
  127. }
  128. /**
  129. * @param startTime The startTime to set.
  130. */
  131. public void setStartTime(long startTime)
  132. {
  133. _startTime = startTime;
  134. }
  135. /**
  136. * @return Returns the startTime.
  137. */
  138. private long getStartTime()
  139. {
  140. return _startTime;
  141. }
  142. /**
  143. * @param future The future to set.
  144. */
  145. public void setFuture(ScheduledFuture<?> future)
  146. {
  147. _future = future;
  148. }
  149. /**
  150. * @return Returns the future.
  151. */
  152. public ScheduledFuture<?> getFuture()
  153. {
  154. return _future;
  155. }
  156. public void start()
  157. {
  158. this.setStartTime(System.currentTimeMillis());
  159. this.resume();
  160. }
  161. public void resume()
  162. {
  163. this.setFuture(ThreadPoolManager.getInstance().scheduleGeneral(this, this.getRemainingTime()));
  164. this.getPlayer().transform(this);
  165. }
  166. public void run()
  167. {
  168. this.stop();
  169. }
  170. public void stop()
  171. {
  172. if (this.getRemainingTime() > 0)
  173. {
  174. this.getPlayer().untransform();
  175. }
  176. }
  177. public L2Transformation createTransformationForPlayer(L2PcInstance player)
  178. {
  179. try
  180. {
  181. L2Transformation transformation = (L2Transformation) this.clone();
  182. transformation.setPlayer(player);
  183. return transformation;
  184. }
  185. catch (CloneNotSupportedException e)
  186. {
  187. // should never happen
  188. return null;
  189. }
  190. }
  191. }