Wait if necessary for quester save, per #1947

This commit is contained in:
PikaMug 2022-08-31 20:57:34 -04:00
parent a9446d9168
commit 4b50760c2e
4 changed files with 26 additions and 51 deletions

View File

@ -41,7 +41,6 @@ import me.blackvein.quests.quests.IStage;
import me.blackvein.quests.quests.Objective;
import me.blackvein.quests.quests.Planner;
import me.blackvein.quests.quests.Requirements;
import me.blackvein.quests.storage.Storage;
import me.blackvein.quests.tasks.StageTimer;
import me.blackvein.quests.util.ConfigUtil;
import me.blackvein.quests.util.InventoryUtil;
@ -69,10 +68,8 @@ import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.Crops;
import org.jetbrains.annotations.NotNull;
@ -4221,8 +4218,7 @@ public class Quester implements IQuester {
*/
public boolean saveData() {
try {
final Storage storage = plugin.getStorage();
storage.saveQuester(this);
plugin.getStorage().saveQuester(this).get();
} catch (final Exception e) {
return false;
}

View File

@ -131,9 +131,7 @@ import java.util.Objects;
import java.util.Optional;
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;
@ -293,8 +291,7 @@ public class Quests extends JavaPlugin implements QuestsAPI {
public void onDisable() {
getLogger().info("Saving Quester data...");
for (final Player p : getServer().getOnlinePlayers()) {
final IQuester quester = getQuester(p.getUniqueId());
quester.saveData();
getQuester(p.getUniqueId()).saveData();
}
Bukkit.getScheduler().cancelTasks(this);
getLogger().info("Closing storage...");
@ -1716,9 +1713,9 @@ public class Quests extends JavaPlugin implements QuestsAPI {
}
loading = true;
reloadConfig();
final CompletableFuture<Void> saveFuture = getStorage().saveOfflineQuesters();
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
try {
getStorage().saveOfflineQuesters().get();
Lang.clear();
settings.init();
Lang.init(Quests.this, settings.getLanguage());
@ -1728,24 +1725,15 @@ public class Quests extends JavaPlugin implements QuestsAPI {
loadQuests();
loadActions();
loadConditions();
final CompletableFuture<Void> loadFuture = saveFuture.thenRunAsync(() -> {
try {
for (final IQuester quester : questers) {
final CompletableFuture<IQuester> cf = getStorage().loadQuester(quester.getUUID());
final IQuester loaded = cf.get();
for (final IQuest q : loaded.getCurrentQuestsTemp().keySet()) {
loaded.checkQuest(q);
}
}
} catch (InterruptedException | ExecutionException e) {
finishLoading(callback, false, e);
for (final IQuester quester : questers) {
final IQuester loaded = getStorage().loadQuester(quester.getUUID()).get();
for (final IQuest quest : loaded.getCurrentQuestsTemp().keySet()) {
loaded.checkQuest(quest);
}
});
loadFuture.thenRunAsync(() -> {
loadModules();
importQuests();
finishLoading(callback, true, null);
});
}
loadModules();
importQuests();
finishLoading(callback, true, null);
} catch (final Exception e) {
finishLoading(callback, false, e);
}

View File

@ -63,7 +63,7 @@ public class SeparatedYamlStorage implements StorageImplementation {
@SuppressWarnings("deprecation")
@Override
public IQuester loadQuester(final UUID uniqueId) throws Exception {
public IQuester loadQuester(final UUID uniqueId) throws IOException, InvalidConfigurationException {
final FileConfiguration data = new YamlConfiguration();
IQuester quester = plugin.getQuester(uniqueId);
if (quester != null) {
@ -71,15 +71,10 @@ public class SeparatedYamlStorage implements StorageImplementation {
} else {
quester = new Quester(plugin, uniqueId);
}
try {
final File dataFile = getDataFile(quester);
if (dataFile != null) {
data.load(dataFile);
} else {
return null;
}
} catch (final IOException | InvalidConfigurationException e) {
e.printStackTrace();
final File dataFile = getDataFile(quester);
if (dataFile != null) {
data.load(dataFile);
} else {
return null;
}
if (data.contains("completedRedoableQuests")) {
@ -384,13 +379,9 @@ public class SeparatedYamlStorage implements StorageImplementation {
}
@Override
public void saveQuester(final IQuester quester) {
public void saveQuester(final IQuester quester) throws IOException {
final FileConfiguration data = quester.getBaseData();
try {
data.save(new File(directoryPath + File.separator + quester.getUUID() + ".yml"));
} catch (final IOException e) {
e.printStackTrace();
}
data.save(new File(directoryPath + File.separator + quester.getUUID() + ".yml"));
}
@Override

View File

@ -248,7 +248,7 @@ public class SqlStorage implements StorageImplementation {
}
@Override
public void saveQuester(final IQuester quester) throws Exception {
public void saveQuester(final IQuester quester) throws SQLException {
final UUID uniqueId = quester.getUUID();
final String lastKnownName = quester.getLastKnownName();
final String oldLastKnownName = getQuesterLastKnownName(uniqueId);
@ -418,7 +418,7 @@ public class SqlStorage implements StorageImplementation {
}
@Override
public String getQuesterLastKnownName(final UUID uniqueId) throws Exception {
public String getQuesterLastKnownName(final UUID uniqueId) throws SQLException {
try (final Connection c = connectionFactory.getConnection()) {
try (final PreparedStatement ps = c.prepareStatement(statementProcessor.apply(PLAYER_SELECT_USERNAME))) {
ps.setString(1, uniqueId.toString());
@ -432,7 +432,7 @@ public class SqlStorage implements StorageImplementation {
return null;
}
public ConcurrentHashMap<IQuest, Integer> getQuesterCurrentQuests(final UUID uniqueId) throws Exception {
public ConcurrentHashMap<IQuest, Integer> getQuesterCurrentQuests(final UUID uniqueId) throws SQLException {
final ConcurrentHashMap<IQuest, Integer> currentQuests = new ConcurrentHashMap<>();
try (final Connection c = connectionFactory.getConnection()) {
try (final PreparedStatement ps = c.prepareStatement(statementProcessor.apply(PLAYER_CURRENT_QUESTS_SELECT_BY_UUID))) {
@ -450,7 +450,7 @@ public class SqlStorage implements StorageImplementation {
return currentQuests;
}
public ConcurrentHashMap<IQuest, QuestData> getQuesterQuestData(final UUID uniqueId) throws Exception {
public ConcurrentHashMap<IQuest, QuestData> getQuesterQuestData(final UUID uniqueId) throws SQLException {
final IQuester quester = plugin.getQuester(uniqueId);
final ConcurrentHashMap<IQuest, QuestData> questData = new ConcurrentHashMap<>();
try (final Connection c = connectionFactory.getConnection()) {
@ -505,7 +505,7 @@ public class SqlStorage implements StorageImplementation {
return questData;
}
public ConcurrentSkipListSet<IQuest> getQuesterCompletedQuests(final UUID uniqueId) throws Exception {
public ConcurrentSkipListSet<IQuest> getQuesterCompletedQuests(final UUID uniqueId) throws SQLException {
final ConcurrentSkipListSet<IQuest> completedQuests = new ConcurrentSkipListSet<>();
try (final Connection c = connectionFactory.getConnection()) {
try (final PreparedStatement ps = c.prepareStatement(statementProcessor.apply(PLAYER_COMPLETED_QUESTS_SELECT_BY_UUID))) {
@ -523,7 +523,7 @@ public class SqlStorage implements StorageImplementation {
return completedQuests;
}
public ConcurrentHashMap<IQuest, Long> getQuesterCompletedTimes(final UUID uniqueId) throws Exception {
public ConcurrentHashMap<IQuest, Long> getQuesterCompletedTimes(final UUID uniqueId) throws SQLException {
final ConcurrentHashMap<IQuest, Long> completedTimes = new ConcurrentHashMap<>();
try (final Connection c = connectionFactory.getConnection()) {
try (final PreparedStatement ps = c.prepareStatement(statementProcessor.apply(PLAYER_REDOABLE_QUESTS_SELECT_BY_UUID))) {
@ -541,7 +541,7 @@ public class SqlStorage implements StorageImplementation {
return completedTimes;
}
public ConcurrentHashMap<IQuest, Integer> getQuesterAmountsCompleted(final UUID uniqueId) throws Exception {
public ConcurrentHashMap<IQuest, Integer> getQuesterAmountsCompleted(final UUID uniqueId) throws SQLException {
final ConcurrentHashMap<IQuest, Integer> amountsCompleted = new ConcurrentHashMap<>();
try (final Connection c = connectionFactory.getConnection()) {
try (final PreparedStatement ps = c.prepareStatement(statementProcessor.apply(PLAYER_REDOABLE_QUESTS_SELECT_BY_UUID))) {
@ -560,7 +560,7 @@ public class SqlStorage implements StorageImplementation {
}
@Override
public Collection<UUID> getSavedUniqueIds() throws Exception {
public Collection<UUID> getSavedUniqueIds() throws SQLException {
final Collection<UUID> ids = new ConcurrentSkipListSet<>();
try (final Connection c = connectionFactory.getConnection()) {
try (final PreparedStatement ps = c.prepareStatement(statementProcessor.apply(PLAYER_SELECT_UUID))) {