Skip to content

Commit

Permalink
Add attemptRunAsync(...) and attemptTimerAsync(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed May 7, 2024
1 parent bb64ef8 commit b8df0f2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
36 changes: 36 additions & 0 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -354,6 +355,41 @@ public void loadDataManger(boolean saveCache) {
}
}

/**
* Attempt to run a task asynchronously if the plugin is enabled, otherwise run it synchronously
*
* @param runnable the task to run
*
* @return {@code true} if the task was run asynchronously, {@code false} if it was run synchronously
*/
public boolean attemptRunAsync(@NotNull Runnable runnable) {
if (isEnabled()) {
Bukkit.getScheduler().runTaskAsynchronously(this, runnable);
return true;
}
runnable.run();
return false;
}

/**
* Attempt to start a timer asynchronously if the plugin is enabled, otherwise start it synchronously
*
* @param runnable the task to run
* @param delay the delay before the first run (in ticks)
* @param period the period between each run (in ticks)
*
* @return {@code true} if the timer was started asynchronously, {@code false} if it was started synchronously
*/
public boolean attemptTimerAsync(@NotNull Runnable runnable, long delay, long period) {
final BukkitScheduler scheduler = Bukkit.getScheduler();
if (isEnabled()) {
scheduler.runTaskTimerAsynchronously(this, runnable, delay, period);
return true;
}
scheduler.runTaskTimer(this, runnable, delay, period);
return false;
}

/**
* Logs a message with the specified level and throwable to the console
*
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/xyz/srnyx/annoyingapi/data/DataManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package xyz.srnyx.annoyingapi.data;

import org.bukkit.scheduler.BukkitRunnable;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -82,12 +80,7 @@ public DataManager(@NotNull AnnoyingPlugin plugin) throws ConnectionException {
// Start cache saver interval
if (plugin.options.dataOptions.cache.saveOn.contains(DataOptions.Cache.SaveOn.INTERVAL)) {
final long interval = plugin.options.dataOptions.cache.saveOnInterval;
new BukkitRunnable() {
@Override
public void run() {
saveCache();
}
}.runTaskTimerAsynchronously(plugin, interval, interval);
plugin.attemptTimerAsync(this::saveCache, interval, interval);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -78,8 +77,7 @@ public AnnoyingPlugin getAnnoyingPlugin() {
public void downloadPlugins(@Nullable Runnable finishRunnable) {
this.finishRunnable = finishRunnable;
this.remaining = dependencies.size();
final BukkitScheduler scheduler = Bukkit.getScheduler();
dependencies.forEach(dependency -> scheduler.runTaskAsynchronously(plugin, () -> attemptDownload(dependency)));
dependencies.forEach(dependency -> plugin.attemptRunAsync(() -> attemptDownload(dependency)));
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/xyz/srnyx/annoyingapi/file/AnnoyingData.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package xyz.srnyx.annoyingapi.file;

import org.bukkit.Bukkit;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -40,7 +38,7 @@ public AnnoyingData(@NotNull AnnoyingPlugin plugin, @NotNull String path) {
@Override
public void create() {
final Path filePath = file.toPath();
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
plugin.attemptRunAsync(() -> {
try {
Files.createDirectories(filePath.getParent());
Files.createFile(filePath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package xyz.srnyx.annoyingapi.file;

import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -36,7 +35,7 @@ public AnnoyingResource(@NotNull AnnoyingPlugin plugin, @NotNull String path, @N
final InputStream input = plugin.getResource(path);
if (input == null) return;
final Path defaultPath = plugin.getDataFolder().toPath().resolve("default/" + path);
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
plugin.attemptRunAsync(() -> {
try {
Files.createDirectories(defaultPath.getParent());
Files.copy(input, defaultPath, StandardCopyOption.REPLACE_EXISTING);
Expand Down

0 comments on commit b8df0f2

Please sign in to comment.