Skip to content

Instantly share code, notes, and snippets.

@Wowserman Wowserman/Tasky.java
Last active Jun 30, 2018

Embed
What would you like to do?
Util for BukkitRunnables
package com.wowserman;
import java.lang.ref.WeakReference;
import java.util.function.Consumer;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
/**
* Tasky.java
*
* Requires Java 8, uses lambda expressions.
*
* @author PLARSON (Wowserman)
* https://www.spigotmc.org/members/wowserman.124342/
*
* Created Jun 29, 2018
*/
public final class Tasky {
/// MARK – Make sure you set the plugin. Hopefully you read this or notice you need to.
private static WeakReference<Plugin> plugin;
public static void setPlugin(final Plugin plugin) {
Tasky.plugin = new WeakReference<Plugin>(plugin);
}
/// MARK
public static BukkitRunnable async(final Consumer<BukkitRunnable> block) {
final BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
block.accept(this);
}
};
runnable.runTaskAsynchronously(plugin.get());
return runnable;
}
public static BukkitRunnable sync(final Consumer<BukkitRunnable> block) {
final BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
block.accept(this);
}
};
runnable.runTask(plugin.get());
return runnable;
}
public static BukkitRunnable async(final Consumer<BukkitRunnable> block, final long delay) {
final BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
block.accept(this);
}
};
runnable.runTaskLaterAsynchronously(plugin.get(), delay);
return runnable;
}
public static BukkitRunnable sync(final Consumer<BukkitRunnable> block, final long delay) {
final BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
block.accept(this);
}
};
runnable.runTaskLater(plugin.get(), delay);
return runnable;
}
public static BukkitRunnable sync(final Consumer<BukkitRunnable> block, final long delay, final long period) {
final BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
block.accept(this);
}
};
runnable.runTaskTimer(plugin.get(), delay, period);
return runnable;
}
public static BukkitRunnable async(final Consumer<BukkitRunnable> block, final long delay, final long period) {
final BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
block.accept(this);
}
};
runnable.runTaskTimerAsynchronously(plugin.get(), delay, period);
return runnable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.