Plex-FAWE/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistoryCommands.java

318 lines
14 KiB
Java
Raw Normal View History

/*
2014-04-04 22:03:18 +00:00
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
2014-04-04 22:03:18 +00:00
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
2014-04-04 22:03:18 +00:00
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
2014-04-04 22:03:18 +00:00
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2014-04-04 22:03:18 +00:00
*/
package com.sk89q.worldedit.command;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.FaweAPI;
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.logging.rollback.RollbackOptimizedHistory;
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.MainUtil;
import com.boydti.fawe.util.MathMan;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.entity.Player;
2019-10-23 16:58:36 +00:00
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.internal.annotation.Range;
2019-01-09 07:13:44 +00:00
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.World;
import java.io.File;
2019-09-21 01:52:35 +00:00
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
import org.enginehub.piston.annotation.param.Arg;
import org.enginehub.piston.annotation.param.Switch;
import org.enginehub.piston.inject.InjectedValueAccess;
2019-07-21 11:39:36 +00:00
2019-04-23 22:35:05 +00:00
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Commands to undo, redo, and clear history.
*/
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
public class HistoryCommands {
2019-07-21 11:39:36 +00:00
private final WorldEdit worldEdit;
/**
* Create a new instance.
*
* @param worldEdit reference to WorldEdit
*/
public HistoryCommands(WorldEdit worldEdit) {
2019-07-21 11:39:36 +00:00
checkNotNull(worldEdit);
this.worldEdit = worldEdit;
}
@Command(
name = "fawerollback",
2019-07-17 10:50:54 +00:00
aliases = {"frb", "/fawerollback", "/rollback"},
desc = "Undo a specific edit. " +
" - The time uses s, m, h, d, y.\n" +
" - Import from disk: /frb #import"
)
@CommandPermissions("worldedit.history.rollback")
public void faweRollback(Player player, LocalSession session, @Arg(desc = "String user") String user, @Arg(def = "0", desc = "radius") @Range(min = 0) int radius, @Arg(name = "time", desc = "String", def = "0") String time, @Switch(name = 'r', desc = "TODO") boolean restore) throws WorldEditException {
if (!Settings.IMP.HISTORY.USE_DATABASE) {
BBC.SETTING_DISABLE.send(player, "history.use-database (Import with /frb #import )");
return;
}
if (user.charAt(0) == '#') {
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.IMP.PATHS.HISTORY);
2019-09-21 01:52:35 +00:00
if (folder.exists()) {
for (File worldFolder : Objects.requireNonNull(folder.listFiles())) {
if (worldFolder != null && worldFolder.isDirectory()) {
String worldName = worldFolder.getName();
World world = FaweAPI.getWorld(worldName);
if (world != null) {
for (File userFolder : worldFolder.listFiles()) {
if (!userFolder.isDirectory()) {
continue;
}
2019-09-21 01:52:35 +00:00
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(), false);
if (summary != null) {
rollback.setDimensions(
BlockVector3.at(summary.minX, 0, summary.minZ),
BlockVector3
.at(summary.maxX, 255, summary.maxZ));
rollback.setTime(historyFile.lastModified());
RollbackDatabase db = DBHandler.IMP
.getDatabase(world);
db.logEdit(rollback);
player.print("Logging: " + historyFile);
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
}
}
2019-09-21 01:52:35 +00:00
player.print("Done import!");
}
return;
}
String toParse = user.substring(1);
if (!MathMan.isInteger(toParse)) {
BBC.COMMAND_SYNTAX.send(player, "/frb #<index>");
2019-06-11 20:07:43 +00:00
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()) {
if (restore) file.redo(player);
else file.undo(player);
BBC.ROLLBACK_ELEMENT.send(player, world.getName() + "/" + user + "-" + index);
} else {
BBC.TOOL_INSPECT_INFO_FOOTER.send(player, 0);
}
return;
}
UUID other = Fawe.imp().getUUID(user);
if (other == null) {
BBC.PLAYER_NOT_FOUND.send(player, user);
return;
}
if (radius == 0) {
BBC.COMMAND_SYNTAX.send(player, "/frb " + user + " <radius> <time>");
return;
}
long timeDiff = MainUtil.timeToSec(time) * 1000;
if (timeDiff == 0) {
BBC.COMMAND_SYNTAX.send(player, "/frb " + user + " " + radius + " <time>");
return;
}
radius = Math.max(Math.min(500, radius), 0);
final World world = player.getWorld();
Location origin = player.getLocation();
2019-03-31 16:09:20 +00:00
BlockVector3 bot = origin.toBlockPoint().subtract(radius, radius, radius);
2019-01-09 07:13:44 +00:00
bot = bot.withY(Math.max(0, bot.getY()));
2019-03-31 16:09:20 +00:00
BlockVector3 top = origin.toBlockPoint().add(radius, radius, radius);
2019-01-09 07:13:44 +00:00
bot = bot.withY(Math.min(255, top.getY()));
RollbackDatabase database = DBHandler.IMP.getDatabase(world);
final AtomicInteger count = new AtomicInteger();
Region[] allowedRegions = player.getCurrentRegions(FaweMaskManager.MaskType.OWNER);
if (allowedRegions == null) {
2019-11-02 19:53:25 +00:00
player.printError(BBC.NO_REGION.s());
return;
}
// TODO mask the regions bot / top to the bottom and top coord in the allowedRegions
// TODO: then mask the edit to the bot / top
// if (allowedRegions.length != 1 || !allowedRegions[0].isGlobal()) {
2019-07-18 20:49:29 +00:00
// finalQueue = new MaskedIQueueExtent(SetQueue.IMP.getNewQueue(fp.getWorld(), true, false), allowedRegions);
// } else {
// finalQueue = SetQueue.IMP.getNewQueue(fp.getWorld(), true, false);
// }
database.getPotentialEdits(other, System.currentTimeMillis() - timeDiff, bot, top, new RunnableVal<DiskStorageHistory>() {
@Override
public void run(DiskStorageHistory edit) {
edit.undo(player, allowedRegions);
2019-06-19 03:43:06 +00:00
BBC.ROLLBACK_ELEMENT.send(player, edit.getWorld().getName() + "/" + user + "-" + edit.getIndex());
count.incrementAndGet();
}
2019-06-19 03:43:06 +00:00
}, () -> BBC.TOOL_INSPECT_INFO_FOOTER.send(player, count), true, restore);
}
@Command(
name = "fawerestore",
2019-07-17 10:50:54 +00:00
aliases = {"/fawerestore", "/frestore"},
desc = "Redo a specific edit. " +
" - The time uses s, m, h, d, y.\n" +
" - Import from disk: /frb #import"
)
@CommandPermissions("worldedit.history.rollback")
public void restore(Player player, LocalSession session, @Arg(desc = "String user") String user, @Arg(def = "0", desc = "radius") @Range(min = 0) int radius, @Arg(name = "time", desc = "String", def = "0") String time) throws WorldEditException {
faweRollback(player, session, user, radius, time, true);
}
@Command(
name = "/undo",
aliases = { "/un", "/ud", "undo" },
desc = "Undoes the last action (from history)"
)
} else {
undoSession = session;
}
int finalTimes = times;
player.checkConfirmation(() -> {
EditSession undone = null;
int i = 0;
for (; i < finalTimes; ++i) {
undone = undoSession.undo(undoSession.getBlockBag(player), player);
if (undone == null) break;
@CommandPermissions({"worldedit.history.undo", "worldedit.history.undo.self"})
public void undo(Player player, LocalSession session,
2019-07-28 19:26:44 +00:00
@Range(min = 1) @Arg(desc = "Number of undoes to perform", def = "1")
int times,
@Arg(name = "player", desc = "Undo this player's operations", def = "")
String playerName,
InjectedValueAccess context) throws WorldEditException {
times = Math.max(1, times);
LocalSession undoSession;
2019-04-05 13:53:00 +00:00
if (session.hasFastMode()) {
2019-10-23 16:58:36 +00:00
player.print(BBC.COMMAND_UNDO_DISABLED.s());
2019-04-05 13:53:00 +00:00
return;
}
2019-07-21 11:39:36 +00:00
if (playerName != null && !playerName.isEmpty()) {
2019-07-17 16:31:13 +00:00
player.checkPermission("worldedit.history.undo.other");
undoSession = worldEdit.getSessionManager().findByName(playerName);
if (undoSession == null) {
BBC.COMMAND_HISTORY_OTHER_ERROR.send(player, playerName);
return;
}
2019-11-14 16:01:38 +00:00
worldEdit.flushBlockBag(player, undone);
2018-10-13 04:21:39 +00:00
}
if (undone == null) i--;
if (i > 0) {
BBC.COMMAND_UNDO_SUCCESS.send(player, i == 1 ? "" : " x" + i);
}
if (undone == null) {
2019-10-23 16:58:36 +00:00
player.printError(BBC.COMMAND_UNDO_ERROR.s());
}
2019-08-06 15:29:49 +00:00
}, "undo", times, 50, context);
}
@Command(
name = "/redo",
aliases = { "/do", "/rd", "redo" },
desc = "Redoes the last action (from history)"
)
@CommandPermissions({"worldedit.history.redo", "worldedit.history.redo.self"})
public void redo(Player player, LocalSession session,
2019-07-28 19:26:44 +00:00
@Range(min = 1) @Arg(desc = "Number of redoes to perform", def = "1")
int times,
@Arg(name = "player", desc = "Redo this player's operations", def = "")
String playerName) throws WorldEditException {
times = Math.max(1, times);
LocalSession redoSession = session;
if (playerName != null) {
player.checkPermission("worldedit.history.redo.other");
redoSession = worldEdit.getSessionManager().findByName(playerName);
if (redoSession == null) {
2019-07-17 16:31:13 +00:00
BBC.COMMAND_HISTORY_OTHER_ERROR.send(player, playerName);
return;
}
2018-10-13 04:21:39 +00:00
}
int timesRedone = 0;
for (int i = 0; i < times; ++i) {
EditSession redone = redoSession.redo(redoSession.getBlockBag(player), player);
if (redone != null) {
timesRedone++;
worldEdit.flushBlockBag(player, redone);
} else {
break;
}
2018-10-13 04:21:39 +00:00
}
if (timesRedone > 0) {
2019-07-17 16:31:13 +00:00
BBC.COMMAND_REDO_SUCCESS.send(player, timesRedone == 1 ? "" : " x" + timesRedone);
} else {
2019-10-23 16:58:36 +00:00
player.printError(BBC.COMMAND_REDO_ERROR.s());
}
}
@Command(
name = "clearhistory",
aliases = { "/clearhistory" },
desc = "Clear your history"
)
@CommandPermissions("worldedit.history.clear")
2019-10-23 16:58:36 +00:00
public void clearHistory(Actor actor, LocalSession session) {
session.clearHistory();
actor.print("History cleared.");
}
}