Add rollback by id

This commit is contained in:
Jesse Boyd 2016-12-31 18:46:55 +11:00
parent 142fad42cd
commit 3f70fb6585
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
10 changed files with 105 additions and 68 deletions

View File

@ -154,7 +154,7 @@ import javax.management.NotificationListener;
public class Fawe {
/**
* The FAWE instance;
*/
*/
private static Fawe INSTANCE;
/**
@ -566,8 +566,8 @@ public class Fawe {
return this.thread;
}
public boolean isMainThread() {
return Thread.currentThread() == thread;
public static boolean isMainThread() {
return INSTANCE != null ? INSTANCE.thread == Thread.currentThread() : true;
}
/**

View File

@ -410,7 +410,7 @@ public abstract class FaweQueue {
*/
public void flush(int time) {
if (size() > 0) {
if (Fawe.get().isMainThread()) {
if (Fawe.isMainThread()) {
SetQueue.IMP.flush(this);
} else {
if (enqueue()) {

View File

@ -5,13 +5,20 @@ import com.boydti.fawe.config.Settings;
import com.boydti.fawe.database.DBHandler;
import com.boydti.fawe.database.RollbackDatabase;
import com.boydti.fawe.object.FaweInputStream;
import com.boydti.fawe.object.FawePlayer;
import com.boydti.fawe.object.IntegerPair;
import com.boydti.fawe.object.RegionWrapper;
import com.boydti.fawe.util.MainUtil;
import com.sk89q.jnbt.NBTInputStream;
import com.sk89q.jnbt.NBTOutputStream;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.world.World;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@ -97,6 +104,13 @@ public class DiskStorageHistory extends FaweStreamChangeSet {
initFiles(folder);
}
public DiskStorageHistory(File folder, String world, UUID uuid, int i) {
super(world);
this.uuid = uuid;
this.index = i;
initFiles(folder);
}
private void initFiles(File folder) {
nbtfFile = new File(folder, index + ".nbtf");
nbttFile = new File(folder, index + ".nbtt");
@ -129,6 +143,12 @@ public class DiskStorageHistory extends FaweStreamChangeSet {
enttFile.delete();
}
public void undo(FawePlayer fp) {
EditSession session = toEditSession(fp);
session.undo(session);
deleteFiles();
}
public UUID getUUID() {
return uuid;
}

View File

@ -7,7 +7,6 @@ import com.boydti.fawe.config.Settings;
import com.boydti.fawe.object.FaweChunk;
import com.boydti.fawe.object.FawePlayer;
import com.boydti.fawe.object.FaweQueue;
import com.boydti.fawe.object.RegionWrapper;
import com.boydti.fawe.object.RunnableVal2;
import com.boydti.fawe.util.EditSessionBuilder;
import com.boydti.fawe.util.MainUtil;
@ -51,14 +50,14 @@ public abstract class FaweChangeSet implements ChangeSet {
public FaweChangeSet(String world) {
this.worldName = world;
this.mainThread = Fawe.get().isMainThread();
this.mainThread = (Fawe.get() != null) ? Fawe.isMainThread() : true;
this.layers = FaweChunk.HEIGHT >> 4;
}
public FaweChangeSet(World world) {
this.world = world;
this.worldName = Fawe.imp().getWorldName(world);
this.mainThread = Fawe.get().isMainThread();
this.mainThread = Fawe.isMainThread();
this.layers = (this.world.getMaxY() + 1) >> 4;
}
@ -88,7 +87,7 @@ public abstract class FaweChangeSet implements ChangeSet {
public boolean flush() {
try {
if (!Fawe.get().isMainThread()) {
if (!Fawe.isMainThread()) {
while (waitingAsync.get() > 0) {
synchronized (lockAsync) {
lockAsync.wait(1000);
@ -130,7 +129,7 @@ public abstract class FaweChangeSet implements ChangeSet {
public void delete() {};
public EditSession toEditSession(FawePlayer player) {
EditSession editSession = new EditSessionBuilder(world).player(player).autoQueue(false).fastmode(false).checkMemory(false).changeSet(this).allowedRegions(RegionWrapper.GLOBAL().toArray()).build();
EditSession editSession = new EditSessionBuilder(getWorld()).player(player).autoQueue(false).fastmode(false).checkMemory(false).changeSet(this).limitUnlimited().allowedRegionsEverywhere().build();
editSession.setSize(1);
return editSession;
}

View File

@ -17,7 +17,7 @@ public class SlowExtent extends AbstractDelegateExtent {
@Override
public boolean setBlock(Vector location, BaseBlock block) throws WorldEditException {
if (!Fawe.get().isMainThread()) try {
if (!Fawe.isMainThread()) try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();

View File

@ -191,7 +191,7 @@ public abstract class TaskManager {
* @param r
*/
public void taskNowAsync(final Runnable r) {
taskNow(r, Fawe.get().isMainThread());
taskNow(r, Fawe.isMainThread());
}
/**
@ -297,7 +297,7 @@ public abstract class TaskManager {
}
public void taskWhenFree(Runnable run) {
if (Fawe.get().isMainThread()) {
if (Fawe.isMainThread()) {
run.run();
} else {
SetQueue.IMP.addTask(run);

View File

@ -257,7 +257,7 @@ public class TaskBuilder extends Metadatable {
return;
case SYNC:
case SYNC_PARALLEL:
if (!Fawe.get().isMainThread()) {
if (!Fawe.isMainThread()) {
TaskManager.IMP.sync(new RunnableVal() {
@Override
public void run(Object value) {
@ -270,7 +270,7 @@ public class TaskBuilder extends Metadatable {
case SYNC_WHEN_FREE:
case ASYNC:
case ASYNC_PARALLEL:
if (Fawe.get().isMainThread()) {
if (Fawe.isMainThread()) {
TaskManager.IMP.async(new Runnable() {
@Override
public void run() {

View File

@ -1308,7 +1308,7 @@ public class EditSession extends AbstractWorld implements HasFaweQueue, Lighting
queue.dequeue();
return;
}
if (Fawe.get().isMainThread()) {
if (Fawe.isMainThread()) {
SetQueue.IMP.flush(queue);
} else {
queue.flush();

View File

@ -33,8 +33,8 @@ import com.boydti.fawe.object.RegionWrapper;
import com.boydti.fawe.object.RunnableVal;
import com.boydti.fawe.object.changeset.DiskStorageHistory;
import com.boydti.fawe.regions.FaweMaskManager;
import com.boydti.fawe.util.EditSessionBuilder;
import com.boydti.fawe.util.MainUtil;
import com.boydti.fawe.util.MathMan;
import com.boydti.fawe.util.SetQueue;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
@ -75,61 +75,86 @@ public class HistoryCommands {
aliases = { "/frb", "frb", "fawerollback", "/fawerollback" },
usage = "<user> <radius> <time>",
desc = "Undo edits within a radius",
min = 3,
min = 1,
max = 3
)
@CommandPermissions("worldedit.history.rollback")
public void faweRollback(final Player player, LocalSession session, final String user, int radius, String time) throws WorldEditException {
public void faweRollback(final Player player, LocalSession session, final String user, Integer radius, String time) throws WorldEditException {
if (!Settings.HISTORY.USE_DATABASE) {
BBC.SETTING_DISABLE.send(player, "history.use-database");
return;
}
if (user.equals("#import") && player.hasPermission("worldedit.history.import")) {
if (!player.hasPermission("fawe.rollback.import")) {
BBC.NO_PERM.send(player, "fawe.rollback.import");
return;
}
File folder = MainUtil.getFile(Fawe.imp().getDirectory(), Settings.PATHS.HISTORY);
if (!folder.exists()) {
return;
}
for (File worldFolder : folder.listFiles()) {
if (!worldFolder.isDirectory()) {
continue;
}
String worldName = worldFolder.getName();
World world = FaweAPI.getWorld(worldName);
if (world != null) {
for (File userFolder : worldFolder.listFiles()) {
if (!userFolder.isDirectory()) {
switch (user.charAt(0)) {
case '#': {
if (user.equals("#import")) {
if (!player.hasPermission("fawe.rollback.import")) {
BBC.NO_PERM.send(player, "fawe.rollback.import");
return;
}
File folder = MainUtil.getFile(Fawe.imp().getDirectory(), Settings.PATHS.HISTORY);
if (!folder.exists()) {
return;
}
for (File worldFolder : folder.listFiles()) {
if (!worldFolder.isDirectory()) {
continue;
}
String userUUID = userFolder.getName();
try {
UUID uuid = UUID.fromString(userUUID);
for (File historyFile : userFolder.listFiles()) {
String name = historyFile.getName();
if (!name.endsWith(".bd")) {
String worldName = worldFolder.getName();
World world = FaweAPI.getWorld(worldName);
if (world != null) {
for (File userFolder : worldFolder.listFiles()) {
if (!userFolder.isDirectory()) {
continue;
}
RollbackOptimizedHistory rollback = new RollbackOptimizedHistory(world, uuid, Integer.parseInt(name.substring(0, name.length() - 3)));
DiskStorageHistory.DiskStorageSummary summary = rollback.summarize(RegionWrapper.GLOBAL(), true);
if (summary != null) {
rollback.setDimensions(new Vector(summary.minX, 0, summary.minZ), new Vector(summary.maxX, 255, summary.maxZ));
rollback.setTime(historyFile.lastModified());
RollbackDatabase db = DBHandler.IMP.getDatabase(world);
db.logEdit(rollback);
player.print(BBC.getPrefix() + "Logging: " + historyFile);
String userUUID = userFolder.getName();
try {
UUID uuid = UUID.fromString(userUUID);
for (File historyFile : userFolder.listFiles()) {
String name = historyFile.getName();
if (!name.endsWith(".bd")) {
continue;
}
RollbackOptimizedHistory rollback = new RollbackOptimizedHistory(world, uuid, Integer.parseInt(name.substring(0, name.length() - 3)));
DiskStorageHistory.DiskStorageSummary summary = rollback.summarize(RegionWrapper.GLOBAL(), true);
if (summary != null) {
rollback.setDimensions(new Vector(summary.minX, 0, summary.minZ), new Vector(summary.maxX, 255, summary.maxZ));
rollback.setTime(historyFile.lastModified());
RollbackDatabase db = DBHandler.IMP.getDatabase(world);
db.logEdit(rollback);
player.print(BBC.getPrefix() + "Logging: " + historyFile);
}
}
} catch (IllegalArgumentException e) {
continue;
}
}
} catch (IllegalArgumentException e) {
continue;
}
}
player.print(BBC.getPrefix() + "Done import!");
return;
}
String toParse = user.substring(1);
if (!MathMan.isInteger(toParse)) {
BBC.COMMAND_SYNTAX.send(player, "/frb <user> <radius> <time>");
return;
}
int index = Integer.parseInt(toParse);
final World world = player.getWorld();
UUID uuid = player.getUniqueId();
DiskStorageHistory file = new DiskStorageHistory(world, uuid, index);
if (file.getBDFile().exists()) {
file.undo(FawePlayer.wrap(player));
BBC.ROLLBACK_ELEMENT.send(player, Fawe.imp().getWorldName(world) + "/" + user + "-" + index);
} else {
BBC.TOOL_INSPECT_INFO_FOOTER.send(player, 0);
}
return;
}
player.print(BBC.getPrefix() + "Done import!");
return;
default:
if (radius == null) {
BBC.COMMAND_SYNTAX.send(player, "/frb <user> <radius> <time>");
return;
}
}
UUID other = Fawe.imp().getUUID(user);
if (other == null) {
@ -162,18 +187,7 @@ public class HistoryCommands {
database.getPotentialEdits(other, System.currentTimeMillis() - timeDiff, bot, top, new RunnableVal<DiskStorageHistory>() {
@Override
public void run(DiskStorageHistory edit) {
EditSession session = new EditSessionBuilder(world)
.player(fp)
.autoQueue(false)
.fastmode(false)
.checkMemory(false)
.changeSet(edit)
.limitUnlimited()
.queue(finalQueue)
.build();
session.setSize(1);
session.undo(session);
edit.deleteFiles();
edit.undo(fp);
BBC.ROLLBACK_ELEMENT.send(player, Fawe.imp().getWorldName(edit.getWorld()) + "/" + user + "-" + edit.getIndex());
count.incrementAndGet();
}
@ -186,6 +200,10 @@ public class HistoryCommands {
);
}
private void rollBack(DiskStorageHistory file) {
}
@Command(
aliases = { "/undo", "undo" },
usage = "[times] [player]",

View File

@ -372,7 +372,7 @@ public final class CommandManager {
public void run() {
handleCommandOnCurrentThread(event, true);
}
}, Fawe.get().isMainThread());
}, Fawe.isMainThread());
event.setCancelled(true);
}