From 514b28caa4a8249830e9bd83ad0c4223f9c156f9 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sun, 7 Aug 2016 07:19:02 +1000 Subject: [PATCH] Rollback changes --- core/src/main/java/com/boydti/fawe/Fawe.java | 1 - .../main/java/com/boydti/fawe/config/BBC.java | 6 +++-- .../fawe/database/RollbackDatabase.java | 23 +++++++++++++--- .../fawe/object/brush/InspectBrush.java | 2 +- .../worldedit/command/HistoryCommands.java | 27 +++++++++++++++---- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/com/boydti/fawe/Fawe.java b/core/src/main/java/com/boydti/fawe/Fawe.java index 54056c8d..220e2662 100644 --- a/core/src/main/java/com/boydti/fawe/Fawe.java +++ b/core/src/main/java/com/boydti/fawe/Fawe.java @@ -67,7 +67,6 @@ import java.util.Collection; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; -import java.util.stream.Stream; import javax.management.InstanceAlreadyExistsException; import javax.management.Notification; import javax.management.NotificationEmitter; diff --git a/core/src/main/java/com/boydti/fawe/config/BBC.java b/core/src/main/java/com/boydti/fawe/config/BBC.java index db34ad55..637174e3 100644 --- a/core/src/main/java/com/boydti/fawe/config/BBC.java +++ b/core/src/main/java/com/boydti/fawe/config/BBC.java @@ -92,9 +92,11 @@ public enum BBC { BRUSH_SMOOTH("Smooth brush equipped (%s0 x %s1 using %s2).", "WorldEdit.Brush"), BRUSH_SPHERE("Sphere brush shape equipped (%s0).", "WorldEdit.Brush"), + ROLLBACK_ELEMENT("Undoing %s0", "WorldEdit.Rollback"), + TOOL_INSPECT("Inspect tool bound to %s0.", "WorldEdit.Tool"), - TOOL_INSPECT_INFO("&7%s0 changed %s1 to %s2 %s3 ago","Info"), - TOOL_INSPECT_INFO_FOOTER("&6Total: &7%s0 changes","Info"), + TOOL_INSPECT_INFO("&7%s0 changed %s1 to %s2 %s3 ago","WorldEdit.Tool"), + TOOL_INSPECT_INFO_FOOTER("&6Total: &7%s0 changes","WorldEdit.Tool"), TOOL_NONE("Tool unbound from your current item.", "WorldEdit.Tool"), TOOL_INFO("Info tool bound to %s0.", "WorldEdit.Tool"), TOOL_TREE("Tree tool bound to %s0.", "WorldEdit.Tool"), diff --git a/core/src/main/java/com/boydti/fawe/database/RollbackDatabase.java b/core/src/main/java/com/boydti/fawe/database/RollbackDatabase.java index f8a8fa89..efa88ad2 100644 --- a/core/src/main/java/com/boydti/fawe/database/RollbackDatabase.java +++ b/core/src/main/java/com/boydti/fawe/database/RollbackDatabase.java @@ -33,6 +33,7 @@ public class RollbackDatabase { // private String GET_EDITS_POINT; private String GET_EDITS; private String GET_EDITS_USER; + private String DELETE_EDITS_USER; private String PURGE; private ConcurrentLinkedQueue historyChanges = new ConcurrentLinkedQueue<>(); @@ -47,8 +48,9 @@ public class RollbackDatabase { INSERT_EDIT = "INSERT INTO `" + prefix + "edits` (`player`,`id`,`x1`,`y1`,`z1`,`x2`,`y2`,`z2`,`time`) VALUES(?,?,?,?,?,?,?,?,?)"; PURGE = "DELETE FROM `" + prefix + "edits` WHERE `time`=? AND `x1`<=? AND `y2`>=? AND `y1`<=? AND `z2`>=? AND `z1`<=?"; - GET_EDITS = "SELECT `player`,`id` FROM `" + prefix + "edits` WHERE `x2`>=? AND `x1`<=? AND `y2`>=? AND `y1`<=? AND `z2`>=? AND `z1`<=? AND `time`>?"; - GET_EDITS_USER = "SELECT `player`,`id` FROM `" + prefix + "edits` WHERE `x2`>=? AND `x1`<=? AND `y2`>=? AND `y1`<=? AND `z2`>=? AND `z1`<=? AND `time`>? AND `player`=?"; + GET_EDITS = "SELECT `player`,`id` FROM `" + prefix + "edits` WHERE `x2`>=? AND `x1`<=? AND `y2`>=? AND `y1`<=? AND `z2`>=? AND `z1`<=? AND `time`>? ORDER BY `time` DESC, `id` DESC"; + GET_EDITS_USER = "SELECT `player`,`id` FROM `" + prefix + "edits` WHERE `x2`>=? AND `x1`<=? AND `y2`>=? AND `y1`<=? AND `z2`>=? AND `z1`<=? AND `time`>? AND `player`=? ORDER BY `time` DESC, `id` DESC"; + DELETE_EDITS_USER = "DELETE FROM `" + prefix + "edits` WHERE `x2`>=? AND `x1`<=? AND `y2`>=? AND `y1`<=? AND `z2`>=? AND `z1`<=? AND `time`>? AND `player`=?"; init(); purge((int) TimeUnit.DAYS.toMillis(Settings.HISTORY.DELETE_AFTER_DAYS)); TaskManager.IMP.async(new Runnable() { @@ -103,7 +105,7 @@ public class RollbackDatabase { }); } - public void getPotentialEdits(final UUID uuid, final long minTime, final Vector pos1, final Vector pos2, final RunnableVal onEach, final Runnable whenDone) { + public void getPotentialEdits(final UUID uuid, final long minTime, final Vector pos1, final Vector pos2, final RunnableVal onEach, final Runnable whenDone, final boolean delete) { final World world = FaweAPI.getWorld(this.world); addTask(new Runnable() { @Override @@ -140,6 +142,21 @@ public class RollbackDatabase { } catch (SQLException e) { e.printStackTrace(); } + if (delete && uuid != null) { + try (PreparedStatement stmt = connection.prepareStatement(DELETE_EDITS_USER)) { + stmt.setInt(1, pos1.getBlockX()); + stmt.setInt(2, pos2.getBlockX()); + stmt.setByte(3, (byte) (pos1.getBlockY() - 128)); + stmt.setByte(4, (byte) (pos2.getBlockY() - 128)); + stmt.setInt(5, pos1.getBlockZ()); + stmt.setInt(6, pos2.getBlockZ()); + stmt.setInt(7, (int) (minTime / 1000)); + byte[] uuidBytes = ByteBuffer.allocate(16).putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits()).array(); + stmt.setBytes(8, uuidBytes); + } catch (SQLException e) { + e.printStackTrace(); + } + } } }); } diff --git a/core/src/main/java/com/boydti/fawe/object/brush/InspectBrush.java b/core/src/main/java/com/boydti/fawe/object/brush/InspectBrush.java index 95ab87f7..c54ac908 100644 --- a/core/src/main/java/com/boydti/fawe/object/brush/InspectBrush.java +++ b/core/src/main/java/com/boydti/fawe/object/brush/InspectBrush.java @@ -106,7 +106,7 @@ public class InspectBrush extends BrushTool implements DoubleActionTraceTool { public void run() { BBC.TOOL_INSPECT_INFO_FOOTER.send(fp, count); } - }); + }, false); return true; } diff --git a/core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java b/core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java index 4517687a..75507de2 100644 --- a/core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java +++ b/core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java @@ -24,6 +24,8 @@ import com.boydti.fawe.config.BBC; import com.boydti.fawe.config.Settings; import com.boydti.fawe.database.DBHandler; import com.boydti.fawe.database.RollbackDatabase; +import com.boydti.fawe.object.RunnableVal; +import com.boydti.fawe.object.changeset.DiskStorageHistory; import com.boydti.fawe.util.MainUtil; import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.CommandContext; @@ -37,6 +39,7 @@ import com.sk89q.worldedit.WorldVector; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.world.World; import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; import static com.google.common.base.Preconditions.checkNotNull; @@ -66,7 +69,7 @@ public class HistoryCommands { max = 3 ) @CommandPermissions("worldedit.history.undo") - public void faweRollback(Player player, LocalSession session, String user, int radius, String time) throws WorldEditException { + public void faweRollback(final Player player, LocalSession session, final String user, int radius, String time) throws WorldEditException { if (!Settings.HISTORY.USE_DATABASE) { BBC.SETTING_DISABLE.send(player, "history.use-database"); return; @@ -87,10 +90,24 @@ public class HistoryCommands { Vector bot = origin.subtract(radius, radius, radius); Vector top = origin.add(radius, radius, radius); RollbackDatabase database = DBHandler.IMP.getDatabase(Fawe.imp().getWorldName(world)); - - - - // TODO + final AtomicInteger count = new AtomicInteger(); + System.out.println("ROLLING BACK"); + database.getPotentialEdits(other, System.currentTimeMillis() - timeDiff, bot, top, new RunnableVal() { + @Override + public void run(DiskStorageHistory edit) { + EditSession session = edit.toEditSession(null); + session.undo(session); + edit.deleteFiles(); + BBC.ROLLBACK_ELEMENT.send(player, Fawe.imp().getWorldName(edit.getWorld()) + "/" + user + "-" + edit.getIndex()); + count.incrementAndGet(); + } + }, new Runnable() { + @Override + public void run() { + BBC.TOOL_INSPECT_INFO_FOOTER.send(player, count); + } + }, true + ); } @Command(