浏览代码

BETA: Fixed problem with removing permanent punishments now will update expiration time to current time in order to make it to expire.
* Reported by: takhs7
* Tested by: takhs7

Rumen Nikiforov 12 年之前
父节点
当前提交
63ab834c7a

+ 4 - 4
L2J_Server_BETA/java/com/l2jserver/gameserver/instancemanager/PunishmentManager.java

@@ -64,7 +64,8 @@ public class PunishmentManager
 		{
 			while (rset.next())
 			{
-				final Object key = rset.getObject("key");
+				final int id = rset.getInt("id");
+				final String key = rset.getString("key");
 				final PunishmentAffect affect = PunishmentAffect.getByName(rset.getString("affect"));
 				final PunishmentType type = PunishmentType.getByName(rset.getString("type"));
 				final long expirationTime = rset.getLong("expiration");
@@ -72,15 +73,14 @@ public class PunishmentManager
 				final String punishedBy = rset.getString("punishedBy");
 				if ((type != null) && (affect != null))
 				{
-					PunishmentTask task = new PunishmentTask(key, affect, type, expirationTime, reason, punishedBy, true);
-					if (task.isExpired())
+					if ((expirationTime > 0) && (System.currentTimeMillis() > expirationTime)) // expired task.
 					{
 						expired++;
 					}
 					else
 					{
 						initiated++;
-						_tasks.get(affect).addPunishment(task);
+						_tasks.get(affect).addPunishment(new PunishmentTask(id, key, affect, type, expirationTime, reason, punishedBy, true));
 					}
 				}
 			}

+ 33 - 6
L2J_Server_BETA/java/com/l2jserver/gameserver/model/punishment/PunishmentTask.java

@@ -20,7 +20,9 @@ package com.l2jserver.gameserver.model.punishment;
 
 import java.sql.Connection;
 import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Statement;
 import java.util.concurrent.ScheduledFuture;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -39,8 +41,10 @@ public class PunishmentTask implements Runnable
 	protected static final Logger _log = Logger.getLogger(PunishmentTask.class.getName());
 	
 	private static final String INSERT_QUERY = "INSERT INTO punishments (`key`, `affect`, `type`, `expiration`, `reason`, `punishedBy`) VALUES (?, ?, ?, ?, ?, ?)";
+	private static final String UPDATE_QUERY = "UPDATE punishments SET expiration = ? WHERE id = ?";
 	
-	private final Object _key;
+	private int _id;
+	private final String _key;
 	private final PunishmentAffect _affect;
 	private final PunishmentType _type;
 	private final long _expirationTime;
@@ -51,12 +55,13 @@ public class PunishmentTask implements Runnable
 	
 	public PunishmentTask(Object key, PunishmentAffect affect, PunishmentType type, long expirationTime, String reason, String punishedBy)
 	{
-		this(key, affect, type, expirationTime, reason, punishedBy, false);
+		this(0, key, affect, type, expirationTime, reason, punishedBy, false);
 	}
 	
-	public PunishmentTask(Object key, PunishmentAffect affect, PunishmentType type, long expirationTime, String reason, String punishedBy, boolean isStored)
+	public PunishmentTask(int id, Object key, PunishmentAffect affect, PunishmentType type, long expirationTime, String reason, String punishedBy, boolean isStored)
 	{
-		_key = key;
+		_id = id;
+		_key = String.valueOf(key);
 		_affect = affect;
 		_type = type;
 		_expirationTime = expirationTime;
@@ -180,15 +185,22 @@ public class PunishmentTask implements Runnable
 		if (!_isStored)
 		{
 			try (Connection con = L2DatabaseFactory.getInstance().getConnection();
-				PreparedStatement st = con.prepareStatement(INSERT_QUERY))
+				PreparedStatement st = con.prepareStatement(INSERT_QUERY, Statement.RETURN_GENERATED_KEYS))
 			{
-				st.setObject(1, _key);
+				st.setString(1, _key);
 				st.setString(2, _affect.name());
 				st.setString(3, _type.name());
 				st.setLong(4, _expirationTime);
 				st.setString(5, _reason);
 				st.setString(6, _punishedBy);
 				st.execute();
+				try (ResultSet rset = st.getGeneratedKeys())
+				{
+					if (rset.next())
+					{
+						_id = rset.getInt(1);
+					}
+				}
 				_isStored = true;
 			}
 			catch (SQLException e)
@@ -209,6 +221,21 @@ public class PunishmentTask implements Runnable
 	 */
 	private void onEnd()
 	{
+		if (_isStored)
+		{
+			try (Connection con = L2DatabaseFactory.getInstance().getConnection();
+				PreparedStatement st = con.prepareStatement(UPDATE_QUERY))
+			{
+				st.setLong(1, System.currentTimeMillis());
+				st.setLong(2, _id);
+				st.execute();
+			}
+			catch (SQLException e)
+			{
+				_log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't update punishment task for: " + _affect + " " + _key + " id: " + _id, e);
+			}
+		}
+		
 		final IPunishmentHandler handler = PunishmentHandler.getInstance().getHandler(_type);
 		if (handler != null)
 		{