Correctly cancel notify tasks and add notifications to commands

This commit is contained in:
dordsor21 2020-10-09 17:24:59 +01:00
parent a451d2d6f2
commit a7a29eaf97
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
10 changed files with 76 additions and 33 deletions

View File

@ -137,20 +137,23 @@ public final class BukkitChunkCoordinator extends ChunkCoordinator {
final int expected = this.expectedSize.addAndGet(-processedChunks); final int expected = this.expectedSize.addAndGet(-processedChunks);
final float progress = ((float) totalSize - (float) expected) / (float) totalSize;
for (final ProgressSubscriber subscriber : this.progressSubscribers) {
subscriber.notifyProgress(this, progress);
}
if (expected <= 0) { if (expected <= 0) {
try { try {
this.whenDone.run(); this.whenDone.run();
} catch (final Throwable throwable) { } catch (final Throwable throwable) {
this.throwableConsumer.accept(throwable); this.throwableConsumer.accept(throwable);
} finally {
for (final ProgressSubscriber subscriber : this.progressSubscribers) {
subscriber.notifyEnd();
}
this.cancel();
} }
this.cancel();
} else { } else {
if (this.availableChunks.size() < processedChunks) { if (this.availableChunks.size() < processedChunks) {
final double progress = ((double) totalSize - (double) expected) / (double) totalSize;
for (final ProgressSubscriber subscriber : this.progressSubscribers) {
subscriber.notifyProgress(this, progress);
}
this.requestBatch(); this.requestBatch();
} }
} }

View File

@ -26,10 +26,12 @@
package com.plotsquared.core.command; package com.plotsquared.core.command;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.backup.BackupManager; import com.plotsquared.core.backup.BackupManager;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.StaticCaption; import com.plotsquared.core.configuration.caption.StaticCaption;
import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.inject.factory.ProgressSubscriberFactory;
import com.plotsquared.core.permissions.Permission; import com.plotsquared.core.permissions.Permission;
import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
@ -45,19 +47,18 @@ import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.world.block.BlockCategory; import com.sk89q.worldedit.world.block.BlockCategory;
import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypes;
import java.util.Collections;
import java.util.LinkedList;
import net.kyori.adventure.text.minimessage.Template; import net.kyori.adventure.text.minimessage.Template;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
@CommandDeclaration(command = "set", @CommandDeclaration(command = "set",
aliases = {"s"}, aliases = {"s"},
@ -158,7 +159,14 @@ public class Set extends SubCommand {
for (final Plot current : plot.getConnectedPlots()) { for (final Plot current : plot.getConnectedPlots()) {
current.getPlotModificationManager().setComponent(component, pattern, player, queue); current.getPlotModificationManager().setComponent(component, pattern, player, queue);
} }
queue.setCompleteTask(plot::removeRunning); queue.setCompleteTask(() -> {
plot.removeRunning();
player.sendMessage(TranslatableCaption.of("working.component_complete"));
});
if (Settings.QUEUE.NOTIFY_PROGRESS) {
queue.addProgressSubscriber(
PlotSquared.platform().getInjector().getInstance(ProgressSubscriberFactory.class).createWithActor(player));
}
queue.enqueue(); queue.enqueue();
player.sendMessage(TranslatableCaption.of("working.generating_component")); player.sendMessage(TranslatableCaption.of("working.generating_component"));
}); });

View File

@ -29,6 +29,7 @@ import com.google.common.collect.Sets;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.command.Template; import com.plotsquared.core.command.Template;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.inject.factory.ProgressSubscriberFactory;
import com.plotsquared.core.location.Location; import com.plotsquared.core.location.Location;
import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
@ -62,11 +63,15 @@ public class HybridPlotManager extends ClassicPlotManager {
private final HybridPlotWorld hybridPlotWorld; private final HybridPlotWorld hybridPlotWorld;
private final RegionManager regionManager; private final RegionManager regionManager;
private final ProgressSubscriberFactory subscriberFactory;
public HybridPlotManager(@Nonnull final HybridPlotWorld hybridPlotWorld, @Nonnull final RegionManager regionManager) { public HybridPlotManager(@Nonnull final HybridPlotWorld hybridPlotWorld,
@Nonnull final RegionManager regionManager,
@Nonnull ProgressSubscriberFactory subscriberFactory) {
super(hybridPlotWorld, regionManager); super(hybridPlotWorld, regionManager);
this.hybridPlotWorld = hybridPlotWorld; this.hybridPlotWorld = hybridPlotWorld;
this.regionManager = regionManager; this.regionManager = regionManager;
this.subscriberFactory = subscriberFactory;
} }
@Override public void exportTemplate() throws IOException { @Override public void exportTemplate() throws IOException {
@ -210,7 +215,6 @@ public class HybridPlotManager extends ClassicPlotManager {
return true; return true;
} }
} }
final String world = hybridPlotWorld.getWorldName();
final Location pos1 = plot.getBottomAbs(); final Location pos1 = plot.getBottomAbs();
final Location pos2 = plot.getExtendedTopAbs(); final Location pos2 = plot.getExtendedTopAbs();
// If augmented // If augmented
@ -245,6 +249,9 @@ public class HybridPlotManager extends ClassicPlotManager {
queue.setRegenRegion(new CuboidRegion(pos1.getBlockVector3(), pos2.getBlockVector3())); queue.setRegenRegion(new CuboidRegion(pos1.getBlockVector3(), pos2.getBlockVector3()));
} }
pastePlotSchematic(queue, pos1, pos2); pastePlotSchematic(queue, pos1, pos2);
if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) {
queue.addProgressSubscriber(subscriberFactory.createWithActor(actor));
}
if (whenDone != null) { if (whenDone != null) {
queue.setCompleteTask(whenDone); queue.setCompleteTask(whenDone);
} }

View File

@ -31,6 +31,7 @@ import com.plotsquared.core.configuration.ConfigurationSection;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.file.YamlConfiguration; import com.plotsquared.core.configuration.file.YamlConfiguration;
import com.plotsquared.core.inject.annotations.WorldConfig; import com.plotsquared.core.inject.annotations.WorldConfig;
import com.plotsquared.core.inject.factory.ProgressSubscriberFactory;
import com.plotsquared.core.location.Location; import com.plotsquared.core.location.Location;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.PlotArea;
@ -138,7 +139,8 @@ public class HybridPlotWorld extends ClassicPlotWorld {
} }
@Nonnull @Override protected PlotManager createManager() { @Nonnull @Override protected PlotManager createManager() {
return new HybridPlotManager(this, PlotSquared.platform().getRegionManager()); return new HybridPlotManager(this, PlotSquared.platform().getRegionManager(),
PlotSquared.platform().getInjector().getInstance(ProgressSubscriberFactory.class));
} }
public Location getSignLocation(@Nonnull Plot plot) { public Location getSignLocation(@Nonnull Plot plot) {

View File

@ -85,7 +85,6 @@ public final class PlotModificationManager {
this.subscriberFactory = PlotSquared.platform().getInjector().getInstance(ProgressSubscriberFactory.class); this.subscriberFactory = PlotSquared.platform().getInjector().getInstance(ProgressSubscriberFactory.class);
} }
/** /**
* Copy a plot to a location, both physically and the settings * Copy a plot to a location, both physically and the settings
* *
@ -239,13 +238,10 @@ public final class PlotModificationManager {
manager.claimPlot(current, queue); manager.claimPlot(current, queue);
} }
} }
if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) {
queue.addProgressSubscriber(subscriberFactory.createWithActor(actor));
}
if (queue.size() > 0) { if (queue.size() > 0) {
queue.setCompleteTask(run);
queue.enqueue(); queue.enqueue();
} }
TaskManager.runTask(run);
return; return;
} }
Plot current = queue.poll(); Plot current = queue.poll();
@ -813,6 +809,7 @@ public final class PlotModificationManager {
return true; return true;
} }
/**
/** /**
* Sets components such as border, wall, floor. * Sets components such as border, wall, floor.
* (components are generator specific) * (components are generator specific)

View File

@ -29,14 +29,16 @@ import com.plotsquared.core.util.task.PlotSquaredTask;
public abstract class ChunkCoordinator implements PlotSquaredTask { public abstract class ChunkCoordinator implements PlotSquaredTask {
private boolean cancelled = false;
@Override public abstract void runTask(); @Override public abstract void runTask();
@Override public boolean isCancelled() { @Override public boolean isCancelled() {
return false; return cancelled;
} }
@Override public void cancel() { @Override public void cancel() {
// Do nothing this.cancelled = true;
} }
/** /**

View File

@ -53,6 +53,7 @@ public class DefaultProgressSubscriber implements ProgressSubscriber {
@Nonnull private final AtomicDouble progress = new AtomicDouble(0); @Nonnull private final AtomicDouble progress = new AtomicDouble(0);
@Nonnull private final AtomicBoolean started = new AtomicBoolean(false); @Nonnull private final AtomicBoolean started = new AtomicBoolean(false);
@Nonnull private final AtomicBoolean cancelled = new AtomicBoolean(false);
@Nonnull private final TaskTime interval; @Nonnull private final TaskTime interval;
@Nonnull private final TaskTime wait; @Nonnull private final TaskTime wait;
@Nonnull private final PlotPlayer<?> actor; @Nonnull private final PlotPlayer<?> actor;
@ -91,7 +92,7 @@ public class DefaultProgressSubscriber implements ProgressSubscriber {
} }
} }
@Override public void notifyProgress(@Nonnull ChunkCoordinator coordinator, float progress) { @Override public void notifyProgress(@Nonnull ChunkCoordinator coordinator, double progress) {
this.progress.set(progress); this.progress.set(progress);
if (coordinator.isCancelled() || progress >= 1) { if (coordinator.isCancelled() || progress >= 1) {
if (task != null) { if (task != null) {
@ -99,11 +100,25 @@ public class DefaultProgressSubscriber implements ProgressSubscriber {
} }
} else if (started.compareAndSet(false, true)) { } else if (started.compareAndSet(false, true)) {
TaskManager.getPlatformImplementation().taskLater(() -> task = TaskManager.getPlatformImplementation().taskRepeat(() -> { TaskManager.getPlatformImplementation().taskLater(() -> task = TaskManager.getPlatformImplementation().taskRepeat(() -> {
if (!started.get()) { if (!started.get()) {
return; return;
} }
actor.sendMessage(caption, Template.of("progress", String.valueOf((double) Math.round(this.progress.doubleValue() * 100) / 100))); if (cancelled.get()) {
task.cancel();
}
actor.sendMessage(caption, Template.of("progress", String.valueOf((double) Math.round(this.progress.doubleValue() * 10000) / 100)));
}, interval), wait); }, interval), wait);
} }
} }
public void notifyEnd() {
cancel();
}
public void cancel() {
this.cancelled.set(true);
if (this.task != null) {
task.cancel();
}
}
} }

View File

@ -29,7 +29,6 @@ import com.plotsquared.core.queue.ChunkCoordinator;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@FunctionalInterface
public interface ProgressSubscriber { public interface ProgressSubscriber {
/** /**
@ -38,6 +37,10 @@ public interface ProgressSubscriber {
* @param coordinator Coordinator instance that triggered the notification * @param coordinator Coordinator instance that triggered the notification
* @param progress Progress in the range [0, 1] * @param progress Progress in the range [0, 1]
*/ */
void notifyProgress(@Nonnull final ChunkCoordinator coordinator, final float progress); void notifyProgress(@Nonnull final ChunkCoordinator coordinator, final double progress);
/**
* Notify the subscriber that its parent ChunkCoordinator has finished
*/
void notifyEnd();
} }

View File

@ -29,6 +29,7 @@ import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.StaticCaption; import com.plotsquared.core.configuration.caption.StaticCaption;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.inject.factory.ProgressSubscriberFactory; import com.plotsquared.core.inject.factory.ProgressSubscriberFactory;
import com.plotsquared.core.location.Location; import com.plotsquared.core.location.Location;
import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.player.PlotPlayer;
@ -247,22 +248,22 @@ public abstract class RegionManager {
fromQueue1.setCompleteTask(fromQueue2::enqueue); fromQueue1.setCompleteTask(fromQueue2::enqueue);
if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) { if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) {
fromQueue1.addProgressSubscriber(subscriberFactory.createFull(actor, Settings.QUEUE.NOTIFY_INTERVAL, Settings.QUEUE.NOTIFY_WAIT, fromQueue1.addProgressSubscriber(subscriberFactory.createFull(actor, Settings.QUEUE.NOTIFY_INTERVAL, Settings.QUEUE.NOTIFY_WAIT,
StaticCaption.of("<prefix><gray>Current region 1 copy progress: </gray><gold><progress></gold><gray>%</gray>"))); TranslatableCaption.of("swap.progress_region1_copy")));
} }
fromQueue2.setCompleteTask(toQueue1::enqueue); fromQueue2.setCompleteTask(toQueue1::enqueue);
if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) { if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) {
fromQueue2.addProgressSubscriber(subscriberFactory.createFull(actor, Settings.QUEUE.NOTIFY_INTERVAL, Settings.QUEUE.NOTIFY_WAIT, fromQueue2.addProgressSubscriber(subscriberFactory.createFull(actor, Settings.QUEUE.NOTIFY_INTERVAL, Settings.QUEUE.NOTIFY_WAIT,
StaticCaption.of("<prefix><gray>Current region 2 copy progress: </gray><gold><progress></gold><gray>%</gray>"))); TranslatableCaption.of("swap.progress_region2_copy")));
} }
toQueue1.setCompleteTask(toQueue2::enqueue); toQueue1.setCompleteTask(toQueue2::enqueue);
if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) { if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) {
toQueue1.addProgressSubscriber(subscriberFactory.createFull(actor, Settings.QUEUE.NOTIFY_INTERVAL, Settings.QUEUE.NOTIFY_WAIT, toQueue1.addProgressSubscriber(subscriberFactory.createFull(actor, Settings.QUEUE.NOTIFY_INTERVAL, Settings.QUEUE.NOTIFY_WAIT,
StaticCaption.of("<prefix><gray>Current region 1 paste progress: </gray><gold><progress></gold><gray>%</gray>"))); TranslatableCaption.of("swap.progress_region1_paste")));
} }
toQueue2.setCompleteTask(whenDone); toQueue2.setCompleteTask(whenDone);
if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) { if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) {
toQueue2.addProgressSubscriber(subscriberFactory.createFull(actor, Settings.QUEUE.NOTIFY_INTERVAL, Settings.QUEUE.NOTIFY_WAIT, toQueue2.addProgressSubscriber(subscriberFactory.createFull(actor, Settings.QUEUE.NOTIFY_INTERVAL, Settings.QUEUE.NOTIFY_WAIT,
StaticCaption.of("<prefix><gray>Current region 2 paste progress: </gray><gold><progress></gold><gray>%</gray>"))); TranslatableCaption.of("swap.progress_region2_paste")));
} }
fromQueue1.enqueue(); fromQueue1.enqueue();
} }

View File

@ -74,6 +74,10 @@
"swap.swap_overlap": "<prefix><red>The proposed areas are not allowed to overlap.</red>", "swap.swap_overlap": "<prefix><red>The proposed areas are not allowed to overlap.</red>",
"swap.swap_success": "<prefix><dark_aqua>Successfully swapped plots.</dark_aqua>", "swap.swap_success": "<prefix><dark_aqua>Successfully swapped plots.</dark_aqua>",
"swap.swap_merged": "<prefix><red>Merged plots may not be swapped. Please unmerge the plots before performing the swap.</red>", "swap.swap_merged": "<prefix><red>Merged plots may not be swapped. Please unmerge the plots before performing the swap.</red>",
"swap.progress_region1_copy": "<prefix><gray>Current region 1 copy progress: </gray><gold><progress></gold><gray>%</gray>",
"swap.progress_region2_copy": "<prefix><gray>Current region 2 copy progress: </gray><gold><progress></gold><gray>%</gray",
"swap.progress_region1_paste": "<prefix><gray>Current region 1 paste progress: </gray><gold><progress></gold><gray>%</gray>",
"swap.progress_region2_paste": "<prefix><gray>Current region 2 paste progress: </gray><gold><progress></gold><gray>%</gray>",
"comment.inbox_notification": "<prefix><dark_aqua><amount> </dark_aqua><gray>unread messages. Use <command>.</gray>", "comment.inbox_notification": "<prefix><dark_aqua><amount> </dark_aqua><gray>unread messages. Use <command>.</gray>",
"comment.not_valid_inbox_index": "<prefix><gray>No comment at index <number>.</gray>", "comment.not_valid_inbox_index": "<prefix><gray>No comment at index <number>.</gray>",
@ -412,6 +416,7 @@
"working.plot_is_claimed": "<prefix><gray>This plot is already claimed.</gray>", "working.plot_is_claimed": "<prefix><gray>This plot is already claimed.</gray>",
"working.claimed": "<prefix><dark_aqua>You successfully claimed the plot.</dark_aqua>", "working.claimed": "<prefix><dark_aqua>You successfully claimed the plot.</dark_aqua>",
"working.progress": "<prefix><gray>Current progress: </gray><gold><progress></gold><gray>%</gray>", "working.progress": "<prefix><gray>Current progress: </gray><gold><progress></gold><gray>%</gray>",
"working.component_complete": "<prefix><gold>Component generation has finished.</gold>",
"list.comment_list_header_paged": "<gray>(Page </gray><gold><cur></gold><gray>/</gray><gold><max></gold><gray>) </gray><gold>List of <amount> comments</gold>", "list.comment_list_header_paged": "<gray>(Page </gray><gold><cur></gold><gray>/</gray><gold><max></gold><gray>) </gray><gold>List of <amount> comments</gold>",
"list.comment_list_comment": "<dark_gray>[</dark_gray><gray>#<number></gray><dark_gray>[</dark_gray><gray><world>;<plot_id></gray><dark_gray>][</dark_gray><gold><commenter></gold><dark_gray>]</dark_gray><comment>\n", "list.comment_list_comment": "<dark_gray>[</dark_gray><gray>#<number></gray><dark_gray>[</dark_gray><gray><world>;<plot_id></gray><dark_gray>][</dark_gray><gold><commenter></gold><dark_gray>]</dark_gray><comment>\n",