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!");
}
}
}
Aggregations