PunishmentTask.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.punishment;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.SQLException;
  23. import java.util.concurrent.ScheduledFuture;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.L2DatabaseFactory;
  27. import com.l2jserver.gameserver.ThreadPoolManager;
  28. import com.l2jserver.gameserver.handler.IPunishmentHandler;
  29. import com.l2jserver.gameserver.handler.PunishmentHandler;
  30. import com.l2jserver.gameserver.instancemanager.PunishmentManager;
  31. /**
  32. * @author UnAfraid
  33. */
  34. public class PunishmentTask implements Runnable
  35. {
  36. protected static final Logger _log = Logger.getLogger(PunishmentTask.class.getName());
  37. private static final String INSERT_QUERY = "INSERT INTO punishments (`key`, `affect`, `type`, `expiration`, `reason`, `punishedBy`) VALUES (?, ?, ?, ?, ?, ?)";
  38. private final Object _key;
  39. private final PunishmentAffect _affect;
  40. private final PunishmentType _type;
  41. private final long _expirationTime;
  42. private final String _reason;
  43. private final String _punishedBy;
  44. private boolean _isStored;
  45. private ScheduledFuture<?> _task = null;
  46. public PunishmentTask(Object key, PunishmentAffect affect, PunishmentType type, long expirationTime, String reason, String punishedBy)
  47. {
  48. this(key, affect, type, expirationTime, reason, punishedBy, false);
  49. }
  50. public PunishmentTask(Object key, PunishmentAffect affect, PunishmentType type, long expirationTime, String reason, String punishedBy, boolean isStored)
  51. {
  52. _key = key;
  53. _affect = affect;
  54. _type = type;
  55. _expirationTime = expirationTime;
  56. _reason = reason;
  57. _punishedBy = punishedBy;
  58. _isStored = isStored;
  59. startPunishment();
  60. }
  61. /**
  62. * @return affection value charId, account, ip, etc..
  63. */
  64. public Object getKey()
  65. {
  66. return _key;
  67. }
  68. /**
  69. * @return {@link PunishmentAffect} affection type, account, character, ip, etc..
  70. */
  71. public PunishmentAffect getAffect()
  72. {
  73. return _affect;
  74. }
  75. /**
  76. * @return {@link PunishmentType} type of current punishment.
  77. */
  78. public PunishmentType getType()
  79. {
  80. return _type;
  81. }
  82. /**
  83. * @return milliseconds to the end of the current punishment, -1 for infinity.
  84. */
  85. public final long getExpirationTime()
  86. {
  87. return _expirationTime;
  88. }
  89. /**
  90. * @return the reason for this punishment.
  91. */
  92. public String getReason()
  93. {
  94. return _reason;
  95. }
  96. /**
  97. * @return name of the punishment issuer.
  98. */
  99. public String getPunishedBy()
  100. {
  101. return _punishedBy;
  102. }
  103. /**
  104. * @return {@code true} if current punishment task is stored in database, {@code false} otherwise.
  105. */
  106. public boolean isStored()
  107. {
  108. return _isStored;
  109. }
  110. /**
  111. * @return {@code true} if current punishment task has expired, {@code false} otherwise.
  112. */
  113. public final boolean isExpired()
  114. {
  115. return (_expirationTime > 0) && (System.currentTimeMillis() > _expirationTime);
  116. }
  117. /**
  118. * Activates the punishment task.
  119. */
  120. private void startPunishment()
  121. {
  122. if (isExpired())
  123. {
  124. return;
  125. }
  126. onStart();
  127. if (_expirationTime > 0) // Has expiration?
  128. {
  129. _task = ThreadPoolManager.getInstance().scheduleGeneral(this, (_expirationTime - System.currentTimeMillis()));
  130. }
  131. }
  132. /**
  133. * Stops the punishment task.
  134. */
  135. public final void stopPunishment()
  136. {
  137. abortTask();
  138. onEnd();
  139. }
  140. /**
  141. * Aborts the scheduled task.
  142. */
  143. private void abortTask()
  144. {
  145. if (_task != null)
  146. {
  147. if (!_task.isCancelled() && !_task.isDone())
  148. {
  149. _task.cancel(false);
  150. }
  151. _task = null;
  152. }
  153. }
  154. /**
  155. * Store and activate punishment upon start.
  156. */
  157. private void onStart()
  158. {
  159. if (!_isStored)
  160. {
  161. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  162. PreparedStatement st = con.prepareStatement(INSERT_QUERY))
  163. {
  164. st.setString(1, String.valueOf(_key));
  165. st.setString(2, _affect.name());
  166. st.setString(3, _type.name());
  167. st.setLong(4, _expirationTime);
  168. st.setString(5, _reason);
  169. st.setString(6, _punishedBy);
  170. st.execute();
  171. _isStored = true;
  172. }
  173. catch (SQLException e)
  174. {
  175. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't store punishment task for: " + _affect + " " + _key, e);
  176. }
  177. }
  178. final IPunishmentHandler handler = PunishmentHandler.getInstance().getHandler(_type);
  179. if (handler != null)
  180. {
  181. handler.onStart(this);
  182. }
  183. }
  184. /**
  185. * Remove and deactivate punishment when it ends.
  186. */
  187. private void onEnd()
  188. {
  189. final IPunishmentHandler handler = PunishmentHandler.getInstance().getHandler(_type);
  190. if (handler != null)
  191. {
  192. handler.onEnd(this);
  193. }
  194. }
  195. /**
  196. * Runs when punishment task ends in order to stop and remove it.
  197. */
  198. @Override
  199. public final void run()
  200. {
  201. PunishmentManager.getInstance().stopPunishment(_key, _affect, _type);
  202. }
  203. }