Skip to content

Commit

Permalink
Make trim cancellable and add progress messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pop4959 committed Jan 18, 2024
1 parent 29c92a8 commit 10f1478
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
5 changes: 5 additions & 0 deletions common/src/main/java/org/popcraft/chunky/Chunky.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class Chunky {
private final Selection.Builder selection;
private final TaskScheduler scheduler = new TaskScheduler();
private final Map<String, GenerationTask> generationTasks = new ConcurrentHashMap<>();
private final Map<String, TrimCommand.Task> trimTasks = new ConcurrentHashMap<>();
private final Map<String, PendingAction> pendingActions = new HashMap<>();
private final RegionCache regionCache = new RegionCache();
private final double limit;
Expand Down Expand Up @@ -151,6 +152,10 @@ public Map<String, GenerationTask> getGenerationTasks() {
return generationTasks;
}

public Map<String, TrimCommand.Task> getTrimTasks() {
return trimTasks;
}

public Map<String, ChunkyCommand> getCommands() {
return commands;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public CancelCommand(final Chunky chunky) {
@Override
public void execute(final Sender sender, final CommandArguments arguments) {
final Map<String, GenerationTask> generationTasks = chunky.getGenerationTasks();
if (generationTasks.isEmpty() && chunky.getTaskLoader().loadTasks().stream().allMatch(GenerationTask::isCancelled)) {
final Map<String, TrimCommand.Task> trimTasks = chunky.getTrimTasks();
if (generationTasks.isEmpty()
&& chunky.getTaskLoader().loadTasks().stream().allMatch(GenerationTask::isCancelled)
&& trimTasks.isEmpty()) {
sender.sendMessagePrefixed(TranslationKey.FORMAT_CANCEL_NO_TASKS);
return;
}
Expand All @@ -39,6 +42,9 @@ public void execute(final Sender sender, final CommandArguments arguments) {
if (chunky.getGenerationTasks().containsKey(world.get().getName())) {
chunky.getGenerationTasks().remove(world.get().getName()).stop(true);
}
if (chunky.getTrimTasks().containsKey(world.get().getName())) {
chunky.getTrimTasks().remove(world.get().getName()).setCancelled(true);
}
};
} else {
cancelAction = () -> {
Expand All @@ -47,6 +53,8 @@ public void execute(final Sender sender, final CommandArguments arguments) {
chunky.getGenerationTasks().values().forEach(generationTask -> generationTask.stop(true));
chunky.getGenerationTasks().clear();
chunky.getScheduler().cancelTasks();
chunky.getTrimTasks().values().forEach(trimTask -> trimTask.setCancelled(true));
chunky.getTrimTasks().clear();
};
}
chunky.setPendingAction(sender, cancelAction);
Expand Down
39 changes: 37 additions & 2 deletions common/src/main/java/org/popcraft/chunky/command/TrimCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,42 @@ public void execute(final Sender sender, final CommandArguments arguments) {
final Shape shape = ShapeFactory.getShape(selection);
final Runnable deletionAction = () -> chunky.getScheduler().runTask(() -> {
sender.sendMessagePrefixed(TranslationKey.FORMAT_START, selection.world().getName(), translate("shape_" + selection.shape()), Formatting.number(selection.centerX()), Formatting.number(selection.centerZ()), Formatting.radius(selection));
TrimCommand.Task trimTask = new TrimCommand.Task();
chunky.getTrimTasks().put(selection.world().getName(), trimTask);
final Optional<Path> regionPath = selection.world().getRegionDirectory();
final AtomicLong finishedRegions = new AtomicLong();
final AtomicLong deleted = new AtomicLong();
final long startTime = System.currentTimeMillis();
final AtomicLong updateTime = new AtomicLong(startTime);
try {
if (regionPath.isPresent()) {
try (final Stream<Path> regionWalker = Files.walk(regionPath.get())) {
regionWalker.forEach(region -> deleted.getAndAdd(checkRegion(selection.world(), region.getFileName().toString(), shape, inside, inhabitedTimeCheck, inhabitedTime)));
try (final Stream<Path> files = Files.list(regionPath.get())) {
final List<Path> regions = files
.filter(file -> tryRegionCoordinate(file.getFileName().toString()).isPresent())
.toList();
final long totalRegions = regions.size();
for (final Path region: regions) {
if (trimTask.isCancelled()) {
break;
}
deleted.getAndAdd(checkRegion(selection.world(), region.getFileName().toString(), shape, inside, inhabitedTimeCheck, inhabitedTime));
finishedRegions.getAndIncrement();
if (!trimTask.isCancelled() && !chunky.getConfig().isSilent()) {
final long currentTime = System.currentTimeMillis();
final boolean updateIntervalElapsed = ((currentTime - updateTime.get()) / 1e3) > chunky.getConfig().getUpdateInterval();
if (updateIntervalElapsed) {
sender.sendMessagePrefixed(TranslationKey.TASK_TRIM_UPDATE, selection.world().getName(), finishedRegions.get(), String.format("%.2f", 100f * finishedRegions.get() / totalRegions));
updateTime.set(currentTime);
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
final long totalTime = System.currentTimeMillis() - startTime;
chunky.getTrimTasks().remove(selection.world().getName());
sender.sendMessagePrefixed(TranslationKey.TASK_TRIM, deleted.get(), selection.world().getName(), String.format("%.3f", totalTime / 1e3f));
});
chunky.setPendingAction(sender, deletionAction);
Expand Down Expand Up @@ -261,4 +284,16 @@ public List<String> suggestions(final CommandArguments arguments) {
}
return List.of();
}

public static final class Task {
private boolean cancelled;

public boolean isCancelled() {
return cancelled;
}

public void setCancelled(final boolean cancelled) {
this.cancelled = cancelled;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public final class TranslationKey {
public static final String SHAPE_STAR = "shape_star";
public static final String SHAPE_TRIANGLE = "shape_triangle";
public static final String TASK_TRIM = "task_trim";
public static final String TASK_TRIM_UPDATE = "task_trim_update";
public static final String TASK_DONE = "task_done";
public static final String TASK_STOPPED = "task_stopped";
public static final String TASK_UPDATE = "task_update";
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"shape_star": "star",
"shape_triangle": "triangle",
"task_trim": "Deleted %s chunks from %s in %s seconds.",
"task_trim_update": "Task running for %s. Processed: %s regions (%s%%)",
"task_done": "Task finished for %s. Processed: %s chunks (%s%%), Total time: %s:%s:%s",
"task_stopped": "Task stopped for %s.",
"task_update": "Task running for %s. Processed: %s chunks (%s%%), ETA: %s:%s:%s, Rate: %s cps, Current: %s, %s",
Expand Down

0 comments on commit 10f1478

Please sign in to comment.