Search in sources :

Example 1 with Redoable

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

the class QueryResults method redo.

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

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 Redoable (org.cubeengine.module.log.action.Redoable)1