Fixed rollback again :D

This commit is contained in:
Robin Kupper committed 2011-03-30 00:30:27 +02:00
1 parent 4e58bcad63
commit 6b2eaeaa2d
2 files changed
+64 -24

No files matched your search

+4 -4
View File
@@ -193,7 +193,7 @@ public class LogBlock extends JavaPlugin
if (args.length == 5) if (args.length == 5)
minutes = parseTimeSpec(args[3], args[4]); minutes = parseTimeSpec(args[3], args[4]);
player.sendMessage(ChatColor.GREEN + "Rolling back " + args[2] + " by " + minutes + " minutes."); player.sendMessage(ChatColor.GREEN + "Rolling back " + args[2] + " by " + minutes + " minutes.");
new Thread(new Rollback(player, conn, args[2], minutes, table)).start(); getServer().getScheduler().scheduleAsyncDelayedTask(this, new Rollback(player, conn, this, args[2], minutes, table));
} else } else
player.sendMessage(ChatColor.RED + "Usage: /lb rollback player [name] <time> <minutes|hours|days>"); player.sendMessage(ChatColor.RED + "Usage: /lb rollback player [name] <time> <minutes|hours|days>");
} else if (args[1].equalsIgnoreCase("area")) { } else if (args[1].equalsIgnoreCase("area")) {
@@ -202,7 +202,7 @@ public class LogBlock extends JavaPlugin
minutes = parseTimeSpec(args[3], args[4]); minutes = parseTimeSpec(args[3], args[4]);
if (isInt(args[2])) { if (isInt(args[2])) {
player.sendMessage(ChatColor.GREEN + "Rolling back area within " + args[2] + " blocks of you by " + minutes + " minutes."); player.sendMessage(ChatColor.GREEN + "Rolling back area within " + args[2] + " blocks of you by " + minutes + " minutes.");
getServer().getScheduler().scheduleSyncDelayedTask(this, new Rollback(player, conn, Integer.parseInt(args[2]), minutes, table)); getServer().getScheduler().scheduleAsyncDelayedTask(this, new Rollback(player, conn, this, Integer.parseInt(args[2]), minutes, table));
} else } else
player.sendMessage(ChatColor.RED + "Can't parse to an int: " + args[2]); player.sendMessage(ChatColor.RED + "Can't parse to an int: " + args[2]);
} else } else
@@ -213,7 +213,7 @@ public class LogBlock extends JavaPlugin
minutes = parseTimeSpec(args[4], args[5]); minutes = parseTimeSpec(args[4], args[5]);
if (isInt(args[3])) { if (isInt(args[3])) {
player.sendMessage(ChatColor.GREEN + "Rolling back " + args[2] + " within " + args[3] + " blocks by " + minutes + " minutes."); player.sendMessage(ChatColor.GREEN + "Rolling back " + args[2] + " within " + args[3] + " blocks by " + minutes + " minutes.");
getServer().getScheduler().scheduleSyncDelayedTask(this, new Rollback(player, conn, args[2], Integer.parseInt(args[3]), minutes, table)); getServer().getScheduler().scheduleAsyncDelayedTask(this, new Rollback(player, conn, this, args[2], Integer.parseInt(args[3]), minutes, table));
} else } else
player.sendMessage(ChatColor.RED + "Can't parse to an int: " + args[3]); player.sendMessage(ChatColor.RED + "Can't parse to an int: " + args[3]);
} else } else
@@ -228,7 +228,7 @@ public class LogBlock extends JavaPlugin
if (sel != null) { if (sel != null) {
if (sel instanceof CuboidSelection) { if (sel instanceof CuboidSelection) {
player.sendMessage(ChatColor.GREEN + "Rolling back selection by " + minutes + " minutes."); player.sendMessage(ChatColor.GREEN + "Rolling back selection by " + minutes + " minutes.");
getServer().getScheduler().scheduleSyncDelayedTask(this, new Rollback(player, conn, sel.getMinimumPoint(), sel.getMaximumPoint(), minutes, table)); getServer().getScheduler().scheduleAsyncDelayedTask(this, new Rollback(player, conn, this, sel.getMinimumPoint(), sel.getMaximumPoint(), minutes, table));
} else } else
player.sendMessage(ChatColor.RED + "You have to define a cuboid selection"); player.sendMessage(ChatColor.RED + "You have to define a cuboid selection");
} else } else
+60 -20
View File
@@ -16,14 +16,16 @@ import org.bukkit.entity.Player;
public class Rollback implements Runnable public class Rollback implements Runnable
{ {
private LinkedBlockingQueue<Edit> edits = new LinkedBlockingQueue<Edit>();
PreparedStatement ps = null; PreparedStatement ps = null;
private Player player; private Player player;
private Connection conn = null; private Connection conn;
private LogBlock logblock;
Rollback(Player player, Connection conn, String name, int minutes, String table) { Rollback(Player player, Connection conn, LogBlock logblock, String name, int minutes, String table) {
this.player = player; this.player = player;
this.conn = conn; this.conn = conn;
this.logblock = logblock;
try { try {
conn.setAutoCommit(false); conn.setAutoCommit(false);
ps = conn.prepareStatement("SELECT type, data, replaced, x, y, z FROM `" + table + "` INNER JOIN `lb-players` USING (`playerid`) WHERE playername = ? AND date > date_sub(now(), INTERVAL ? MINUTE) ORDER BY date DESC"); ps = conn.prepareStatement("SELECT type, data, replaced, x, y, z FROM `" + table + "` INNER JOIN `lb-players` USING (`playerid`) WHERE playername = ? AND date > date_sub(now(), INTERVAL ? MINUTE) ORDER BY date DESC");
@@ -35,10 +37,11 @@ public class Rollback implements Runnable
return; return;
} }
} }
Rollback(Player player, Connection conn, String name, int radius, int minutes, String table) { Rollback(Player player, Connection conn, LogBlock logblock, String name, int radius, int minutes, String table) {
this.player = player; this.player = player;
this.conn = conn; this.conn = conn;
this.logblock = logblock;
try { try {
conn.setAutoCommit(false); conn.setAutoCommit(false);
ps = conn.prepareStatement("SELECT type, data, replaced, x, y, z FROM `" + table + "` INNER JOIN `lb-players` USING (`playerid`) WHERE playername = ? AND x > ? AND x < ? AND z > ? AND z < ? AND date > date_sub(now(), INTERVAL ? MINUTE) ORDER BY date DESC"); ps = conn.prepareStatement("SELECT type, data, replaced, x, y, z FROM `" + table + "` INNER JOIN `lb-players` USING (`playerid`) WHERE playername = ? AND x > ? AND x < ? AND z > ? AND z < ? AND date > date_sub(now(), INTERVAL ? MINUTE) ORDER BY date DESC");
@@ -55,9 +58,10 @@ public class Rollback implements Runnable
} }
} }
Rollback(Player player, Connection conn, int radius, int minutes, String table) { Rollback(Player player, Connection conn, LogBlock logblock, int radius, int minutes, String table) {
this.player = player; this.player = player;
this.conn = conn; this.conn = conn;
this.logblock = logblock;
try { try {
conn.setAutoCommit(false); conn.setAutoCommit(false);
ps = conn.prepareStatement("SELECT type, data, replaced, x, y, z FROM `" + table + "` WHERE x > ? AND x < ? AND z > ? AND z < ? AND date > date_sub(now(), INTERVAL ? MINUTE) ORDER BY date DESC"); ps = conn.prepareStatement("SELECT type, data, replaced, x, y, z FROM `" + table + "` WHERE x > ? AND x < ? AND z > ? AND z < ? AND date > date_sub(now(), INTERVAL ? MINUTE) ORDER BY date DESC");
@@ -72,10 +76,11 @@ public class Rollback implements Runnable
return; return;
} }
} }
Rollback(Player player, Connection conn, Location loc1, Location loc2, int minutes, String table) { Rollback(Player player, Connection conn, LogBlock logblock, Location loc1, Location loc2, int minutes, String table) {
this.player = player; this.player = player;
this.conn = conn; this.conn = conn;
this.logblock = logblock;
try { try {
conn.setAutoCommit(false); conn.setAutoCommit(false);
ps = conn.prepareStatement("SELECT type, data, replaced, x, y, z FROM `" + table + "` WHERE x >= ? AND x <= ? AND y >= ? AND y <= ? AND z >= ? AND z <= ? AND date > date_sub(now(), INTERVAL ? MINUTE) ORDER BY date DESC", Statement.RETURN_GENERATED_KEYS); ps = conn.prepareStatement("SELECT type, data, replaced, x, y, z FROM `" + table + "` WHERE x >= ? AND x <= ? AND y >= ? AND y <= ? AND z >= ? AND z <= ? AND date > date_sub(now(), INTERVAL ? MINUTE) ORDER BY date DESC", Statement.RETURN_GENERATED_KEYS);
@@ -92,9 +97,10 @@ public class Rollback implements Runnable
return; return;
} }
} }
public void run() { public void run() {
ResultSet rs = null; ResultSet rs = null;
LinkedBlockingQueue<Edit> edits = new LinkedBlockingQueue<Edit>();
edits.clear(); edits.clear();
try { try {
rs = ps.executeQuery(); rs = ps.executeQuery();
@@ -104,7 +110,7 @@ public class Rollback implements Runnable
} }
} catch (SQLException ex) { } catch (SQLException ex) {
LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception", ex); LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception", ex);
player.sendMessage("§cError, check server logs."); player.sendMessage(ChatColor.RED + "§cError, check server logs.");
return; return;
} finally { } finally {
try { try {
@@ -116,26 +122,60 @@ public class Rollback implements Runnable
conn.close(); conn.close();
} catch (SQLException ex) { } catch (SQLException ex) {
LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception on close", ex); LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception on close", ex);
player.sendMessage("§cError, check server logs."); player.sendMessage(ChatColor.RED + "Error, check server logs.");
return; return;
} }
} }
int changes = edits.size(); int changes = edits.size();
int rolledBack = 0;
player.sendMessage(ChatColor.GREEN + "" + changes + " Changes found."); player.sendMessage(ChatColor.GREEN + "" + changes + " Changes found.");
PerformRollback perform = new PerformRollback(edits, this);
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Edit e = edits.poll(); int taskID = logblock.getServer().getScheduler().scheduleSyncRepeatingTask(logblock, perform, 0, 1);
while (e != null) if (taskID == -1) {
{ player.sendMessage(ChatColor.RED + "Failed to schedule rollback task");
if (e.perform()) return;
rolledBack++;
e = edits.poll();
} }
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
LogBlock.log.severe("[LogBlock Rollback] Interrupted");
}
}
logblock.getServer().getScheduler().cancelTask(taskID);
player.sendMessage(ChatColor.GREEN + "Rollback finished successfully"); player.sendMessage(ChatColor.GREEN + "Rollback finished successfully");
player.sendMessage(ChatColor.GREEN + "Undid " + rolledBack + " of " + changes + " changes"); player.sendMessage(ChatColor.GREEN + "Undid " + perform.rolledBack + " of " + changes + " changes");
player.sendMessage(ChatColor.GREEN + "Took: " + (System.currentTimeMillis() - start) + "ms"); player.sendMessage(ChatColor.GREEN + "Took: " + (System.currentTimeMillis() - start) + "ms");
} }
private class PerformRollback implements Runnable
{
private LinkedBlockingQueue<Edit> edits;
private Rollback rollback;
int rolledBack = 0;
PerformRollback(LinkedBlockingQueue<Edit> edits, Rollback rollback) {
this.edits = edits;
this.rollback = rollback;
}
@Override
public void run() {
int counter = 0;
while (!edits.isEmpty() && counter < 1000)
{
if (edits.poll().perform())
rolledBack++;
counter++;
}
if (edits.isEmpty()) {
synchronized (rollback) {
rollback.notify();
}
}
}
}
private class Edit private class Edit
{ {
int type, replaced; int type, replaced;
@@ -152,7 +192,7 @@ public class Rollback implements Runnable
this.z = z; this.z = z;
this.world = world; this.world = world;
} }
public boolean perform() { public boolean perform() {
if (type > 0 && type == replaced) if (type > 0 && type == replaced)
return false; return false;