Search in sources :

Example 1 with Rollbackable

use of org.cubeengine.module.log.action.Rollbackable in project modules-extra by CubeEngine.

the class QueryResults method rollback.

public void rollback(LogAttachment attachment, boolean preview) {
    // Find the oldest entry at a location
    Map<Coordinate, Rollbackable> finalBlock = new HashMap<>();
    Map<Coordinate, LinkedList<Rollbackable>> blockChanges = new HashMap<>();
    TreeSet<Rollbackable> filteredLogs = new TreeSet<>();
    for (BaseAction logEntry : this.logEntries.descendingSet()) {
        if (// can rollback
        logEntry instanceof Rollbackable) {
            if (logEntry.coord.getWorld() == null) {
                continue;
            }
            if (logEntry instanceof DeathMonster && !this.lookup.getQueryParameter().containsAction(DeathMonster.class)) {
                // ignoring Monster-respawning when not explicitly wanted
                continue;
            }
            if (((Rollbackable) logEntry).isBlockBound()) {
                if (((Rollbackable) logEntry).isStackable()) {
                    LinkedList<Rollbackable> changes = blockChanges.get(logEntry.coord);
                    if (changes == null) {
                        changes = new LinkedList<>();
                        blockChanges.put(logEntry.coord, changes);
                    }
                    changes.add((Rollbackable) logEntry);
                } else {
                    // Clear blockChanges when new final block
                    blockChanges.remove(logEntry.coord);
                    finalBlock.put(logEntry.coord, (Rollbackable) logEntry);
                }
            } else {
                // Not a block change at the location -> do rollback
                filteredLogs.add((Rollbackable) logEntry);
            }
        }
    }
    // Finished filtering! Merge back together...
    for (LinkedList<Rollbackable> entries : blockChanges.values()) {
        filteredLogs.addAll(entries);
    }
    filteredLogs.addAll(finalBlock.values());
    // Start Rollback 1st Round
    Set<Rollbackable> rollbackRound2 = new LinkedHashSet<>();
    // Rollback normal blocks
    for (Rollbackable logEntry : filteredLogs.descendingSet()) {
        if (!logEntry.rollback(attachment, false, preview)) {
            // Rollback failed (cannot set yet (torches etc)) try again later
            rollbackRound2.add(logEntry);
        }
    }
    ShowParameter show = new ShowParameter();
    // Start Rollback 2nd Round (Attachables etc.)
    for (// Rollback attached blocks
    Rollbackable logEntry : // Rollback attached blocks
    rollbackRound2) {
        if (!logEntry.rollback(attachment, true, preview)) {
            attachment.getHolder().sendTranslated(NEGATIVE, "Could not Rollback:");
            ((BaseAction) logEntry).showAction(attachment.getHolder(), show);
            CubeEngine.getLog().warn("Could not rollback!");
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Rollbackable(org.cubeengine.module.log.action.Rollbackable) HashMap(java.util.HashMap) BaseAction(org.cubeengine.module.log.action.BaseAction) LinkedList(java.util.LinkedList) DeathMonster(org.cubeengine.module.log.action.death.DeathMonster) Coordinate(org.cubeengine.module.log.action.BaseAction.Coordinate) TreeSet(java.util.TreeSet)

Aggregations

HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 TreeSet (java.util.TreeSet)1 BaseAction (org.cubeengine.module.log.action.BaseAction)1 Coordinate (org.cubeengine.module.log.action.BaseAction.Coordinate)1 Rollbackable (org.cubeengine.module.log.action.Rollbackable)1 DeathMonster (org.cubeengine.module.log.action.death.DeathMonster)1