Search in sources :

Example 6 with RollbackOptimizedHistory

use of com.fastasyncworldedit.core.history.RollbackOptimizedHistory in project FastAsyncWorldEdit by IntellectualSites.

the class RollbackDatabase method getEdits.

public Iterable<Supplier<RollbackOptimizedHistory>> getEdits(UUID uuid, long minTime, BlockVector3 pos1, BlockVector3 pos2, boolean delete, boolean ascending) {
    YieldIterable<Supplier<RollbackOptimizedHistory>> yieldIterable = new YieldIterable<>();
    Future<Integer> future = call(() -> {
        try {
            int count = 0;
            String stmtStr = ascending ? uuid == null ? "SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND `x2`>=? AND" + " `x1`<=? AND `z2`>=? AND `z1`<=? AND `y2`>=? AND `y1`<=? ORDER BY `time` , `id`" : "SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND" + " `x2`>=? AND `x1`<=? AND `z2`>=? AND `z1`<=? AND `y2`>=? AND `y1`<=? AND `player`=? ORDER BY `time` ASC, `id` ASC" : uuid == null ? "SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND `x2`>=? AND `x1`<=? AND `z2`>=? " + "AND `z1`<=? AND `y2`>=? AND `y1`<=? ORDER BY `time` DESC, `id` DESC" : "SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND `x2`>=? AND `x1`<=? AND" + " `z2`>=? AND `z1`<=? AND `y2`>=? AND `y1`<=? AND `player`=? ORDER BY `time` DESC, `id` DESC";
            try (PreparedStatement stmt = connection.prepareStatement(stmtStr)) {
                stmt.setInt(1, (int) (minTime / 1000));
                stmt.setInt(2, pos1.getBlockX());
                stmt.setInt(3, pos2.getBlockX());
                stmt.setInt(4, pos1.getBlockZ());
                stmt.setInt(5, pos2.getBlockZ());
                stmt.setByte(6, (byte) (pos1.getBlockY() - 128));
                stmt.setByte(7, (byte) (pos2.getBlockY() - 128));
                if (uuid != null) {
                    byte[] uuidBytes = toBytes(uuid);
                    stmt.setBytes(8, uuidBytes);
                }
                ResultSet result = stmt.executeQuery();
                if (!result.next()) {
                    return 0;
                }
                do {
                    count++;
                    Supplier<RollbackOptimizedHistory> history = create(result);
                    yieldIterable.accept(history);
                } while (result.next());
            }
            if (delete && uuid != null) {
                try (PreparedStatement stmt = connection.prepareStatement("DELETE FROM`" + this.prefix + "edits` WHERE `player`=? AND `time`>? AND `x2`>=? AND `x1`<=? AND `y2`>=? AND `y1`<=? AND `z2`>=? AND `z1`<=?")) {
                    stmt.setInt(1, (int) (minTime / 1000));
                    stmt.setInt(2, pos1.getBlockX());
                    stmt.setInt(3, pos2.getBlockX());
                    stmt.setInt(4, pos1.getBlockZ());
                    stmt.setInt(5, pos2.getBlockZ());
                    stmt.setByte(6, (byte) (pos1.getBlockY() - 128));
                    stmt.setByte(7, (byte) (pos2.getBlockY() - 128));
                    byte[] uuidBytes = ByteBuffer.allocate(16).putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits()).array();
                    stmt.setBytes(8, uuidBytes);
                }
            }
            return count;
        } finally {
            yieldIterable.close();
        }
    });
    yieldIterable.setFuture(future);
    return yieldIterable;
}
Also used : ResultSet(java.sql.ResultSet) Supplier(java.util.function.Supplier) PreparedStatement(java.sql.PreparedStatement) YieldIterable(com.fastasyncworldedit.core.util.collection.YieldIterable) RollbackOptimizedHistory(com.fastasyncworldedit.core.history.RollbackOptimizedHistory)

Example 7 with RollbackOptimizedHistory

use of com.fastasyncworldedit.core.history.RollbackOptimizedHistory in project FastAsyncWorldEdit by IntellectualSites.

the class HistorySubCommands method summary.

@Command(name = "info", aliases = { "summary", "summarize" }, desc = "Summarize an edit")
@CommandPermissions("worldedit.history.info")
public synchronized void summary(Player player, RollbackDatabase database, Arguments arguments, @Arg(desc = "Player uuid/name") UUID other, @Arg(desc = "edit index") Integer index) throws WorldEditException, ExecutionException, InterruptedException {
    RollbackOptimizedHistory edit = database.getEdit(other, index).get();
    if (edit == null) {
        player.print(Caption.of("fawe.worldedit.schematic.schematic.none"));
        return;
    }
    Location origin = player.getLocation();
    String name = Fawe.platform().getName(edit.getUUID());
    String cmd = edit.getCommand();
    BlockVector3 pos1 = edit.getMinimumPoint();
    BlockVector3 pos2 = edit.getMaximumPoint();
    double distanceX = Math.min(Math.abs(pos1.getX() - origin.getX()), Math.abs(pos2.getX() - origin.getX()));
    double distanceZ = Math.min(Math.abs(pos1.getZ() - origin.getZ()), Math.abs(pos2.getZ() - origin.getZ()));
    int distance = (int) Math.sqrt(distanceX * distanceX + distanceZ * distanceZ);
    BlockVector2 dirVec = BlockVector2.at(edit.getOriginX() - origin.getX(), edit.getOriginZ() - origin.getZ());
    Direction direction = Direction.findClosest(dirVec.toVector3(), Direction.Flag.ALL);
    long seconds = (System.currentTimeMillis() - edit.getBDFile().lastModified()) / 1000;
    String timeStr = MainUtil.secToTime(seconds);
    int size = edit.size();
    boolean biomes = edit.getBioFile().exists();
    boolean createdEnts = edit.getEnttFile().exists();
    boolean removedEnts = edit.getEntfFile().exists();
    boolean createdTiles = edit.getNbttFile().exists();
    boolean removedTiles = edit.getNbtfFile().exists();
    TranslatableComponent header = Caption.of("fawe.worldedit.history.find.element", name, timeStr, distance, direction.name(), cmd);
    String sizeStr = StringMan.humanReadableByteCountBin(edit.getSizeOnDisk());
    String extra = "";
    if (biomes) {
        extra += "biomes, ";
    }
    if (createdEnts) {
        extra += "+entity, ";
    }
    if (removedEnts) {
        extra += "-entity, ";
    }
    if (createdTiles) {
        extra += "+tile, ";
    }
    if (removedTiles) {
        extra += "-tile, ";
    }
    TranslatableComponent body = Caption.of("fawe.worldedit.history.find.element.more", size, edit.getMinimumPoint(), edit.getMaximumPoint(), extra.trim(), sizeStr);
    Component distr = TextComponent.of("/history distr").clickEvent(ClickEvent.suggestCommand("//history distr " + other + " " + index));
    TextComponentProducer content = new TextComponentProducer().append(header).newline().append(body).newline().append(distr);
    player.print(content.create());
}
Also used : TextComponentProducer(com.sk89q.worldedit.util.formatting.component.TextComponentProducer) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BlockVector2(com.sk89q.worldedit.math.BlockVector2) Direction(com.sk89q.worldedit.util.Direction) RollbackOptimizedHistory(com.fastasyncworldedit.core.history.RollbackOptimizedHistory) TranslatableComponent(com.sk89q.worldedit.util.formatting.text.TranslatableComponent) Component(com.sk89q.worldedit.util.formatting.text.Component) TranslatableComponent(com.sk89q.worldedit.util.formatting.text.TranslatableComponent) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) Location(com.sk89q.worldedit.util.Location) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 8 with RollbackOptimizedHistory

use of com.fastasyncworldedit.core.history.RollbackOptimizedHistory in project FastAsyncWorldEdit by IntellectualSites.

the class HistorySubCommands method rollback.

@Command(name = "rollback", desc = "Undo a specific edit. " + " - The time uses s, m, h, d, y.")
@CommandPermissions("worldedit.history.rollback")
@Confirm
public synchronized void rollback(Player player, World world, RollbackDatabase database, @AllowedRegion Region[] allowedRegions, @ArgFlag(name = 'u', desc = "String user", def = "") UUID other, @ArgFlag(name = 'r', def = "0", desc = "radius") @Range(from = 0, to = Integer.MAX_VALUE) int radius, @ArgFlag(name = 't', desc = "Time e.g. 20s", def = "0") @Time long timeDiff, @Switch(name = 'f', desc = "Restore instead of rollback") boolean restore) throws WorldEditException {
    if (!Settings.settings().HISTORY.USE_DATABASE) {
        player.print(Caption.of("fawe.error.setting.disable", "history.use-database (Import with /history import )"));
        return;
    }
    checkCommandArgument(radius > 0, "Radius must be >= 0");
    checkCommandArgument(timeDiff > 0, "Time must be >= 0");
    if (other == null) {
        other = player.getUniqueId();
    }
    if (!other.equals(player.getUniqueId())) {
        player.checkPermission("worldedit.history.undo.other");
    }
    if (other == Identifiable.EVERYONE) {
        other = null;
    }
    Location origin = player.getLocation();
    BlockVector3 bot = origin.toBlockPoint().subtract(radius, radius, radius);
    BlockVector3 top = origin.toBlockPoint().add(radius, radius, radius);
    bot = bot.clampY(world.getMinY(), world.getMaxY());
    top = top.clampY(world.getMinY(), world.getMaxY());
    // 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()) {
    // finalQueue = new MaskedIQueueExtent(SetQueue.IMP.getNewQueue(fp.getWorld(), true, false), allowedRegions);
    // } else {
    // finalQueue = SetQueue.IMP.getNewQueue(fp.getWorld(), true, false);
    // }
    int count = 0;
    UUID finalOther = other;
    long minTime = System.currentTimeMillis() - timeDiff;
    for (Supplier<RollbackOptimizedHistory> supplier : database.getEdits(other, minTime, bot, top, !restore, restore)) {
        count++;
        RollbackOptimizedHistory edit = supplier.get();
        if (restore) {
            edit.redo(player, allowedRegions);
        } else {
            edit.undo(player, allowedRegions);
        }
        String path = edit.getWorld().getName() + "/" + finalOther + "-" + edit.getIndex();
        player.print(Caption.of("fawe.worldedit.rollback.rollback.element", path));
    }
    player.print(Caption.of("fawe.worldedit.tool.tool.inspect.info.footer", count));
}
Also used : BlockVector3(com.sk89q.worldedit.math.BlockVector3) UUID(java.util.UUID) Location(com.sk89q.worldedit.util.Location) RollbackOptimizedHistory(com.fastasyncworldedit.core.history.RollbackOptimizedHistory) Command(org.enginehub.piston.annotation.Command) Confirm(com.sk89q.worldedit.command.util.annotation.Confirm) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 9 with RollbackOptimizedHistory

use of com.fastasyncworldedit.core.history.RollbackOptimizedHistory in project FastAsyncWorldEdit by IntellectualSites.

the class HistorySubCommands method distr.

@Command(name = "distr", aliases = { "distribution" }, desc = "View block distribution for an edit")
@CommandPermissions("worldedit.history.distr")
public void distr(Player player, LocalSession session, RollbackDatabase database, Arguments arguments, @Arg(desc = "Player uuid/name") UUID other, @Arg(desc = "edit index") Integer index, @ArgFlag(name = 'p', desc = "Page to view.", def = "") Integer page) throws ExecutionException, InterruptedException {
    String pageCommand = "/" + arguments.get().replaceAll("-p [0-9]+", "").trim();
    Reference<PaginationBox> cached = player.getMeta(pageCommand);
    PaginationBox pages = cached == null ? null : cached.get();
    if (page == null || pages == null) {
        RollbackOptimizedHistory edit = database.getEdit(other, index).get();
        SimpleChangeSetSummary summary = edit.summarize(RegionWrapper.GLOBAL(), false);
        if (summary != null) {
            List<Countable<BlockState>> distr = summary.getBlockDistributionWithData();
            SelectionCommands.BlockDistributionResult distrPages = new SelectionCommands.BlockDistributionResult(distr, true, pageCommand);
            pages = new PaginationBox.MergedPaginationBox("Block Distribution", pageCommand, pages, distrPages);
            player.setMeta(pageCommand, new SoftReference<>(pages));
        }
        page = 1;
    }
    if (pages == null) {
        player.print(Caption.of("fawe.worldedit.history.distr.summary_null"));
    } else {
        player.print(pages.create(page));
    }
}
Also used : Countable(com.sk89q.worldedit.util.Countable) SimpleChangeSetSummary(com.fastasyncworldedit.core.history.changeset.SimpleChangeSetSummary) PaginationBox(com.sk89q.worldedit.util.formatting.component.PaginationBox) RollbackOptimizedHistory(com.fastasyncworldedit.core.history.RollbackOptimizedHistory) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Aggregations

RollbackOptimizedHistory (com.fastasyncworldedit.core.history.RollbackOptimizedHistory)9 UUID (java.util.UUID)6 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)4 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)4 World (com.sk89q.worldedit.world.World)4 Command (org.enginehub.piston.annotation.Command)3 RollbackDatabase (com.fastasyncworldedit.core.database.RollbackDatabase)2 SimpleChangeSetSummary (com.fastasyncworldedit.core.history.changeset.SimpleChangeSetSummary)2 YieldIterable (com.fastasyncworldedit.core.util.collection.YieldIterable)2 Confirm (com.sk89q.worldedit.command.util.annotation.Confirm)2 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)2 Location (com.sk89q.worldedit.util.Location)2 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)2 TranslatableComponent (com.sk89q.worldedit.util.formatting.text.TranslatableComponent)2 File (java.io.File)2 IOException (java.io.IOException)2 Fawe (com.fastasyncworldedit.core.Fawe)1 Settings (com.fastasyncworldedit.core.configuration.Settings)1 DisallowedBlocksExtent (com.fastasyncworldedit.core.extent.DisallowedBlocksExtent)1 FaweRegionExtent (com.fastasyncworldedit.core.extent.FaweRegionExtent)1