forked from bootswithdefer/LogBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rollback.java
executable file
·98 lines (85 loc) · 2.32 KB
/
Rollback.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.*;
import java.sql.*;
public class Rollback implements Runnable
{
static final Logger log = Logger.getLogger("Minecraft");
private LinkedBlockingQueue<Edit> edits = new LinkedBlockingQueue<Edit>();
Rollback(Connection conn, String name, int minutes)
{
String query = "select type, replaced, x, y, z from blocks where player = ? and date > date_sub(now(), interval ? minute) order by date desc";
PreparedStatement ps = null;
ResultSet rs = null;
edits.clear();
try {
conn.setAutoCommit(false);
ps = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, name);
ps.setInt(2, minutes);
rs = ps.executeQuery();
while (rs.next())
{
Edit e = new Edit(rs.getInt("type"), rs.getInt("replaced"), rs.getInt("x"), rs.getInt("y"), rs.getInt("z"));
edits.offer(e);
}
} catch (SQLException ex) {
log.log(Level.SEVERE, this.getClass().getName() + " SQL exception", ex);
} finally {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
log.log(Level.SEVERE, this.getClass().getName() + " SQL exception on close", ex);
}
}
}
public int count()
{
return edits.size();
}
public void run()
{
Edit e = edits.poll();
while (e != null)
{
e.perform();
e.log();
e = edits.poll();
}
}
private class Edit
{
int type, replaced;
int x, y, z;
Edit(int type, int replaced, int x, int y, int z)
{
this.type = type;
this.replaced = replaced;
this.x = x;
this.y = y;
this.z = z;
}
public void perform()
{
if (etc.getServer().getBlockIdAt(x, y, z) == type)
{
if (etc.getServer().setBlockAt(replaced, x, y, z))
log.info("R (" + x + ", " + y + ", " + z + ") " + replaced + " " + type);
else
log.info("r (" + x + ", " + y + ", " + z + ") " + replaced + " " + type);
}
}
public void log()
{
int current = etc.getServer().getBlockIdAt(x, y, z);
if (current == type)
log.info("+ (" + x + ", " + y + ", " + z + ") " + replaced + " " + type);
else
log.info("- (" + x + ", " + y + ", " + z + ") " + replaced + " " + type);
}
}
}