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

133 lines
4.9 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.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
2019-04-23 22:35:05 +00:00
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.entity.Player;
2019-04-23 22:35:05 +00:00
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
import org.enginehub.piston.annotation.param.Arg;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Commands to undo, redo, and clear history.
*/
2019-04-23 22:35:05 +00:00
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.class)
public class HistoryCommands {
private final WorldEdit worldEdit;
/**
* Create a new instance.
*
* @param worldEdit reference to WorldEdit
*/
public HistoryCommands(WorldEdit worldEdit) {
checkNotNull(worldEdit);
this.worldEdit = worldEdit;
}
@Command(
2019-04-23 22:35:05 +00:00
name = "undo",
aliases = { "/undo" },
desc = "Undoes the last action (from history)"
)
@CommandPermissions("worldedit.history.undo")
2019-04-23 22:35:05 +00:00
public void undo(Player player, LocalSession session,
@Arg(desc = "Number of undoes to perform", def = "1")
int times,
@Arg(name = "player", desc = "Undo this player's operations", def = "")
String playerName) throws WorldEditException {
times = Math.max(1, times);
for (int i = 0; i < times; ++i) {
2019-04-23 22:35:05 +00:00
LocalSession undoSession = session;
if (playerName != null) {
player.checkPermission("worldedit.history.undo.other");
2019-04-23 22:35:05 +00:00
LocalSession sess = worldEdit.getSessionManager().findByName(playerName);
if (sess == null) {
2019-04-23 22:35:05 +00:00
player.printError("Unable to find session for " + playerName);
break;
}
2019-04-23 22:35:05 +00:00
undoSession = session;
}
2019-04-23 22:35:05 +00:00
EditSession undone = undoSession.undo(undoSession.getBlockBag(player), player);
if (undone != null) {
player.print("Undo successful.");
worldEdit.flushBlockBag(player, undone);
} else {
player.printError("Nothing left to undo.");
break;
}
}
}
@Command(
2019-04-23 22:35:05 +00:00
name = "redo",
aliases = { "redo" },
desc = "Redoes the last action (from history)"
)
@CommandPermissions("worldedit.history.redo")
2019-04-23 22:35:05 +00:00
public void redo(Player player, LocalSession session,
@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);
for (int i = 0; i < times; ++i) {
2019-04-23 22:35:05 +00:00
LocalSession redoSession = session;
if (playerName != null) {
player.checkPermission("worldedit.history.redo.other");
2019-04-23 22:35:05 +00:00
LocalSession sess = worldEdit.getSessionManager().findByName(playerName);
if (sess == null) {
2019-04-23 22:35:05 +00:00
player.printError("Unable to find session for " + playerName);
break;
}
2019-04-23 22:35:05 +00:00
redoSession = session;
}
2019-04-23 22:35:05 +00:00
EditSession redone = redoSession.redo(redoSession.getBlockBag(player), player);
if (redone != null) {
player.print("Redo successful.");
worldEdit.flushBlockBag(player, redone);
} else {
player.printError("Nothing left to redo.");
2019-04-23 22:35:05 +00:00
break;
}
}
}
@Command(
2019-04-23 22:35:05 +00:00
name = "clearhistory",
aliases = { "/clearhistory" },
desc = "Clear your history"
)
@CommandPermissions("worldedit.history.clear")
public void clearHistory(Player player, LocalSession session) throws WorldEditException {
session.clearHistory();
player.print("History cleared.");
}
}