Search in sources :

Example 1 with Coordinate

use of org.cubeengine.module.log.action.BaseAction.Coordinate 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)

Example 2 with Coordinate

use of org.cubeengine.module.log.action.BaseAction.Coordinate 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)2 LinkedHashSet (java.util.LinkedHashSet)2 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 BaseAction (org.cubeengine.module.log.action.BaseAction)2 Coordinate (org.cubeengine.module.log.action.BaseAction.Coordinate)2 Redoable (org.cubeengine.module.log.action.Redoable)1 Rollbackable (org.cubeengine.module.log.action.Rollbackable)1 DeathMonster (org.cubeengine.module.log.action.death.DeathMonster)1