Reload questers only after save, fixes #1707 #1811

This commit is contained in:
PikaMug 2022-01-04 04:51:14 -05:00
parent 0850f4c404
commit 3222c42999
2 changed files with 55 additions and 24 deletions

View File

@ -111,6 +111,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutionException;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
@ -1647,13 +1648,15 @@ public class Quests extends JavaPlugin {
* Reload quests, actions, config settings, lang and modules, and player data
*/
public void reload(final ReloadCallback<Boolean> callback) {
if (loading) {
getLogger().warning(ChatColor.YELLOW + Lang.get("errorLoading"));
return;
}
loading = true;
reloadConfig();
final CompletableFuture<Void> saveFuture = getStorage().saveOfflineQuesters();
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
try {
for (final Quester quester : questers) {
quester.saveData();
}
Lang.clear();
settings.init();
Lang.init(Quests.this);
@ -1663,34 +1666,50 @@ public class Quests extends JavaPlugin {
loadQuests();
loadActions();
loadConditions();
for (final Quester quester : questers) {
final CompletableFuture<Quester> cf = getStorage().loadQuester(quester.getUUID());
final Quester loaded = cf.get();
for (final Quest q : loaded.currentQuests.keySet()) {
loaded.checkQuest(q);
final CompletableFuture<Void> loadFuture = saveFuture.thenRunAsync(() -> {
try {
for (final Quester quester : questers) {
final CompletableFuture<Quester> cf = getStorage().loadQuester(quester.getUUID());
final Quester loaded = cf.get();
for (final Quest q : loaded.currentQuests.keySet()) {
loaded.checkQuest(q);
}
}
} catch (InterruptedException | ExecutionException e) {
finishLoading(callback, false, e);
}
}
loadModules();
importQuests();
if (callback != null) {
Bukkit.getScheduler().runTask(Quests.this, () -> {
loading = false;
callback.execute(true);
});
}
});
loadFuture.thenRunAsync(() -> {
loadModules();
importQuests();
finishLoading(callback, true, null);
});
} catch (final Exception e) {
e.printStackTrace();
if (callback != null) {
Bukkit.getScheduler().runTask(Quests.this, () -> {
loading = false;
callback.execute(false);
});
}
finishLoading(callback, false, e);
}
loading = false;
});
}
/**
* Execute finishing task and print provided exception
*
* @param callback Callback to execute
* @param result Result to pass through callback
* @param exception Exception to print, or null
*/
private void finishLoading(final ReloadCallback<Boolean> callback, boolean result, final Exception exception) {
if (exception != null) {
exception.printStackTrace();
}
if (callback != null) {
Bukkit.getScheduler().runTask(Quests.this, () -> {
loading = false;
callback.execute(result);
});
}
}
/**
* Load quests from file
*/

View File

@ -99,6 +99,18 @@ public class Storage {
});
}
public CompletableFuture<Void> saveOfflineQuesters() {
return makeFuture(() -> {
try {
for (Quester quester : plugin.getOfflineQuesters()) {
implementation.saveQuester(quester);
}
} catch (final Exception e) {
e.printStackTrace();
}
});
}
public CompletableFuture<Void> deleteQuester(final UUID uniqueId) {
return makeFuture(() -> {
try {