PunishmentTask.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.concurrent.ScheduledFuture;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.ThreadPoolManager;
  30. import com.l2jserver.gameserver.handler.IPunishmentHandler;
  31. import com.l2jserver.gameserver.handler.PunishmentHandler;
  32. import com.l2jserver.gameserver.instancemanager.PunishmentManager;
  33. /**
  34. * @author UnAfraid
  35. */
  36. public class PunishmentTask implements Runnable
  37. {
  38. protected static final Logger _log = Logger.getLogger(PunishmentTask.class.getName());
  39. private static final String INSERT_QUERY = "INSERT INTO punishments (`key`, `affect`, `type`, `expiration`, `reason`, `punishedBy`) VALUES (?, ?, ?, ?, ?, ?)";
  40. private static final String UPDATE_QUERY = "UPDATE punishments SET expiration = ? WHERE id = ?";
  41. private int _id;
  42. private final String _key;
  43. private final PunishmentAffect _affect;
  44. private final PunishmentType _type;
  45. private final long _expirationTime;
  46. private final String _reason;
  47. private final String _punishedBy;
  48. private boolean _isStored;
  49. private ScheduledFuture<?> _task = null;
  50. public PunishmentTask(Object key, PunishmentAffect affect, PunishmentType type, long expirationTime, String reason, String punishedBy)
  51. {
  52. this(0, key, affect, type, expirationTime, reason, punishedBy, false);
  53. }
  54. public PunishmentTask(int id, Object key, PunishmentAffect affect, PunishmentType type, long expirationTime, String reason, String punishedBy, boolean isStored)
  55. {
  56. _id = id;
  57. _key = String.valueOf(key);
  58. _affect = affect;
  59. _type = type;
  60. _expirationTime = expirationTime;
  61. _reason = reason;
  62. _punishedBy = punishedBy;
  63. _isStored = isStored;
  64. startPunishment();
  65. }
  66. /**
  67. * @return affection value charId, account, ip, etc..
  68. */
  69. public Object getKey()
  70. {
  71. return _key;
  72. }
  73. /**
  74. * @return {@link PunishmentAffect} affection type, account, character, ip, etc..
  75. */
  76. public PunishmentAffect getAffect()
  77. {
  78. return _affect;
  79. }
  80. /**
  81. * @return {@link PunishmentType} type of current punishment.
  82. */
  83. public PunishmentType getType()
  84. {
  85. return _type;
  86. }
  87. /**
  88. * @return milliseconds to the end of the current punishment, -1 for infinity.
  89. */
  90. public final long getExpirationTime()
  91. {
  92. return _expirationTime;
  93. }
  94. /**
  95. * @return the reason for this punishment.
  96. */
  97. public String getReason()
  98. {
  99. return _reason;
  100. }
  101. /**
  102. * @return name of the punishment issuer.
  103. */
  104. public String getPunishedBy()
  105. {
  106. return _punishedBy;
  107. }
  108. /**
  109. * @return {@code true} if current punishment task is stored in database, {@code false} otherwise.
  110. */
  111. public boolean isStored()
  112. {
  113. return _isStored;
  114. }
  115. /**
  116. * @return {@code true} if current punishment task has expired, {@code false} otherwise.
  117. */
  118. public final boolean isExpired()
  119. {
  120. return (_expirationTime > 0) && (System.currentTimeMillis() > _expirationTime);
  121. }
  122. /**
  123. * Activates the punishment task.
  124. */
  125. private void startPunishment()
  126. {
  127. if (isExpired())
  128. {
  129. return;
  130. }
  131. onStart();
  132. if (_expirationTime > 0) // Has expiration?
  133. {
  134. _task = ThreadPoolManager.getInstance().scheduleGeneral(this, (_expirationTime - System.currentTimeMillis()));
  135. }
  136. }
  137. /**
  138. * Stops the punishment task.
  139. */
  140. public final void stopPunishment()
  141. {
  142. abortTask();
  143. onEnd();
  144. }
  145. /**
  146. * Aborts the scheduled task.
  147. */
  148. private void abortTask()
  149. {
  150. if (_task != null)
  151. {
  152. if (!_task.isCancelled() && !_task.isDone())
  153. {
  154. _task.cancel(false);
  155. }
  156. _task = null;
  157. }
  158. }
  159. /**
  160. * Store and activate punishment upon start.
  161. */
  162. private void onStart()
  163. {
  164. if (!_isStored)
  165. {
  166. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  167. PreparedStatement st = con.prepareStatement(INSERT_QUERY, Statement.RETURN_GENERATED_KEYS))
  168. {
  169. st.setString(1, _key);
  170. st.setString(2, _affect.name());
  171. st.setString(3, _type.name());
  172. st.setLong(4, _expirationTime);
  173. st.setString(5, _reason);
  174. st.setString(6, _punishedBy);
  175. st.execute();
  176. try (ResultSet rset = st.getGeneratedKeys())
  177. {
  178. if (rset.next())
  179. {
  180. _id = rset.getInt(1);
  181. }
  182. }
  183. _isStored = true;
  184. }
  185. catch (SQLException e)
  186. {
  187. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't store punishment task for: " + _affect + " " + _key, e);
  188. }
  189. }
  190. final IPunishmentHandler handler = PunishmentHandler.getInstance().getHandler(_type);
  191. if (handler != null)
  192. {
  193. handler.onStart(this);
  194. }
  195. }
  196. /**
  197. * Remove and deactivate punishment when it ends.
  198. */
  199. private void onEnd()
  200. {
  201. if (_isStored)
  202. {
  203. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  204. PreparedStatement st = con.prepareStatement(UPDATE_QUERY))
  205. {
  206. st.setLong(1, System.currentTimeMillis());
  207. st.setLong(2, _id);
  208. st.execute();
  209. }
  210. catch (SQLException e)
  211. {
  212. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't update punishment task for: " + _affect + " " + _key + " id: " + _id, e);
  213. }
  214. }
  215. final IPunishmentHandler handler = PunishmentHandler.getInstance().getHandler(_type);
  216. if (handler != null)
  217. {
  218. handler.onEnd(this);
  219. }
  220. }
  221. /**
  222. * Runs when punishment task ends in order to stop and remove it.
  223. */
  224. @Override
  225. public final void run()
  226. {
  227. PunishmentManager.getInstance().stopPunishment(_key, _affect, _type);
  228. }
  229. }