#937 Add option for AuthMe to run in sync
- Create BukkitService#runTaskOptionallyAsync and BukkitService#scheduleSyncTaskFromOptionallyAsyncTask whose behavior depends on a new setting - Use the new methods where applicable - Declare events async or sync depending on the new setting
This commit is contained in:
@@ -2,6 +2,9 @@ package fr.xephi.authme.util;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.initialization.SettingsDependent;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import org.bukkit.BanEntry;
|
||||
import org.bukkit.BanList;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -23,9 +26,9 @@ import java.util.Date;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Service for operations requiring server entities, such as for scheduling.
|
||||
* Service for operations requiring the Bukkit API, such as for scheduling.
|
||||
*/
|
||||
public class BukkitService {
|
||||
public class BukkitService implements SettingsDependent {
|
||||
|
||||
/** Number of ticks per second in the Bukkit main thread. */
|
||||
public static final int TICKS_PER_SECOND = 20;
|
||||
@@ -35,10 +38,12 @@ public class BukkitService {
|
||||
private final AuthMe authMe;
|
||||
private final boolean getOnlinePlayersIsCollection;
|
||||
private Method getOnlinePlayers;
|
||||
private boolean useAsyncTasks;
|
||||
|
||||
public BukkitService(AuthMe authMe) {
|
||||
public BukkitService(AuthMe authMe, Settings settings) {
|
||||
this.authMe = authMe;
|
||||
getOnlinePlayersIsCollection = initializeOnlinePlayersIsCollectionField();
|
||||
reload(settings);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,6 +71,21 @@ public class BukkitService {
|
||||
return Bukkit.getScheduler().scheduleSyncDelayedTask(authMe, task, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules a synchronous task if async tasks are enabled; if not, it runs the task immediately.
|
||||
* Use this when {@link #runTaskOptionallyAsync(Runnable) optionally asynchronous tasks} have to
|
||||
* run something synchronously.
|
||||
*
|
||||
* @param task the task to be run
|
||||
*/
|
||||
public void scheduleSyncTaskFromOptionallyAsyncTask(Runnable task) {
|
||||
if (useAsyncTasks) {
|
||||
scheduleSyncDelayedTask(task);
|
||||
} else {
|
||||
task.run();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a task that will run on the next server tick.
|
||||
*
|
||||
@@ -92,6 +112,20 @@ public class BukkitService {
|
||||
return Bukkit.getScheduler().runTaskLater(authMe, task, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules this task to run asynchronously or immediately executes it based on
|
||||
* AuthMe's configuration.
|
||||
*
|
||||
* @param task the task to run
|
||||
*/
|
||||
public void runTaskOptionallyAsync(Runnable task) {
|
||||
if (useAsyncTasks) {
|
||||
runTaskAsynchronously(task);
|
||||
} else {
|
||||
task.run();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <b>Asynchronous tasks should never access any API in Bukkit. Great care
|
||||
* should be taken to assure the thread-safety of asynchronous tasks.</b>
|
||||
@@ -237,6 +271,11 @@ public class BukkitService {
|
||||
return Bukkit.getWorld(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload(Settings settings) {
|
||||
useAsyncTasks = settings.getProperty(PluginSettings.USE_ASYNC_TASKS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method run upon initialization to verify whether or not the Bukkit implementation
|
||||
* returns the online players as a Collection.
|
||||
|
||||
Reference in New Issue
Block a user