diff --git a/pom.xml b/pom.xml index 39df28eb1..a3cd98487 100644 --- a/pom.xml +++ b/pom.xml @@ -383,7 +383,7 @@ com.tcoded FoliaLib - 0.2.5 + 0.3.0 compile diff --git a/src/main/java/com/gmail/nossr50/util/CancellableRunnable.java b/src/main/java/com/gmail/nossr50/util/CancellableRunnable.java new file mode 100644 index 000000000..a47cdcda3 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/util/CancellableRunnable.java @@ -0,0 +1,28 @@ +package com.gmail.nossr50.util; + +import com.tcoded.folialib.wrapper.task.WrappedTask; + +import java.util.function.Consumer; + +public abstract class CancellableRunnable implements Consumer { + private boolean cancelled = false; + + public void cancel() { + cancelled = true; + } + + public abstract void run(); + + @Override + public void accept(WrappedTask wrappedTask) { + // Run the task if it hasn't been cancelled + if (!cancelled) { + run(); + } + + // Cancel the task if it has been cancelled after running + if (cancelled) { + wrappedTask.cancel(); + } + } +}