Last active
December 20, 2021 09:04
-
-
Save Swedz/c37db87f9c5cf40f9e031d3d87d935b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.swedz.tesseract.Assert; | |
import net.swedz.tesseract.plugin.Tesseract; | |
import org.bukkit.Bukkit; | |
import org.bukkit.scheduler.BukkitTask; | |
import java.util.concurrent.CompletableFuture; | |
public final class BukkitThreading | |
{ | |
public static boolean isMainThread() | |
{ | |
return Thread.currentThread().getId() == Tesseract.getInstance().getThread().getId(); | |
} | |
public static void requireThread(boolean main) | |
{ | |
boolean isMain = isMainThread(); | |
if(main && !isMain) | |
throw new IllegalStateException("Cannot be called on a thread other than the main thread."); | |
if(!main && isMain) | |
throw new IllegalStateException("Cannot be called on the main thread."); | |
} | |
public static void alwaysSync(Runnable runnable) | |
{ | |
Assert.notNull(runnable); | |
if(isMainThread()) | |
{ | |
runnable.run(); | |
} | |
else | |
{ | |
Bukkit.getScheduler().runTask(Tesseract.getInstance().getJavaPlugin(), runnable); | |
} | |
} | |
public static void alwaysSyncWait(Runnable runnable) | |
{ | |
Assert.notNull(runnable); | |
if(isMainThread()) | |
{ | |
runnable.run(); | |
} | |
else | |
{ | |
CompletableFuture<Void> future = new CompletableFuture<>(); | |
Runnable fullRunnable = () -> | |
{ | |
try | |
{ | |
runnable.run(); | |
future.complete(null); | |
} | |
catch (Throwable t) | |
{ | |
future.completeExceptionally(t); | |
} | |
}; | |
Bukkit.getScheduler().runTask(Tesseract.getInstance().getJavaPlugin(), fullRunnable); | |
try | |
{ | |
future.get(); | |
} | |
catch (Throwable throwable) | |
{ | |
if(throwable instanceof RuntimeException) | |
{ | |
throw (RuntimeException) throwable; | |
} | |
else | |
{ | |
RuntimeException ex = new RuntimeException("An exception occurred", throwable); | |
ex.setStackTrace(new StackTraceElement[0]); | |
throw ex; | |
} | |
} | |
} | |
} | |
public static void alwaysAsync(Runnable runnable) | |
{ | |
Assert.notNull(runnable); | |
Bukkit.getScheduler().runTaskAsynchronously(Tesseract.getInstance().getJavaPlugin(), runnable); | |
} | |
public static BukkitTask runLater(Runnable runnable, long ticks) | |
{ | |
Assert.notNull(runnable); | |
return Bukkit.getScheduler().runTaskLater(Tesseract.getInstance().getJavaPlugin(), runnable, ticks); | |
} | |
public static BukkitTask runLaterAsync(Runnable runnable, long ticks) | |
{ | |
Assert.notNull(runnable); | |
return Bukkit.getScheduler().runTaskLaterAsynchronously(Tesseract.getInstance().getJavaPlugin(), runnable, ticks); | |
} | |
public static BukkitTask runTimer(Runnable runnable, long delay, long period) | |
{ | |
Assert.notNull(runnable); | |
return Bukkit.getScheduler().runTaskTimer(Tesseract.getInstance().getJavaPlugin(), runnable, delay, period); | |
} | |
public static BukkitTask runLaterTimer(Runnable runnable, long ticks) | |
{ | |
return runTimer(runnable, ticks, ticks); | |
} | |
public static BukkitTask runTimerAsync(Runnable runnable, long delay, long period) | |
{ | |
Assert.notNull(runnable); | |
return Bukkit.getScheduler().runTaskTimerAsynchronously(Tesseract.getInstance().getJavaPlugin(), runnable, delay, period); | |
} | |
public static BukkitTask runLaterTimerAsync(Runnable runnable, long ticks) | |
{ | |
return runTimerAsync(runnable, ticks, ticks); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment