diff options
| author | LMBishop <13875753+LMBishop@users.noreply.github.com> | 2021-02-16 20:14:29 +0000 |
|---|---|---|
| committer | LMBishop <13875753+LMBishop@users.noreply.github.com> | 2021-02-16 20:14:29 +0000 |
| commit | ebd6f8700ec7cc5204153d5e3bd5e5aaa4e29059 (patch) | |
| tree | 2fecf3074e133af86da7ed7a7755c231e049154f /src/main/java/com | |
| parent | c3bf441402baeae30a0e63a5e2f7910bb10f6835 (diff) | |
Change save behaviour
Diffstat (limited to 'src/main/java/com')
7 files changed, 72 insertions, 28 deletions
diff --git a/src/main/java/com/leonardobishop/quests/Quests.java b/src/main/java/com/leonardobishop/quests/Quests.java index 9dd39187..48b7c86f 100644 --- a/src/main/java/com/leonardobishop/quests/Quests.java +++ b/src/main/java/com/leonardobishop/quests/Quests.java @@ -223,7 +223,7 @@ public class Quests extends JavaPlugin { } catch (Exception ignored) { } } for (QPlayer qPlayer : qPlayerManager.getQPlayers()) { - qPlayer.getQuestProgressFile().saveToDisk(true); + qPlayer.getQuestProgressFile().saveToDisk(false); } if (placeholderAPIHook != null) placeholderAPIHook.unregisterExpansion(); } @@ -250,9 +250,7 @@ public class Quests extends JavaPlugin { } if (autosaveTaskCancelled) { questAutosaveTask = Bukkit.getScheduler().runTaskTimer(this, () -> { - for (QPlayer qPlayer : qPlayerManager.getQPlayers()) { - qPlayer.getQuestProgressFile().saveToDisk(false); - } + new QuestsAutosaveRunnable(this); }, autosaveInterval, autosaveInterval); } diff --git a/src/main/java/com/leonardobishop/quests/QuestsAutosaveRunnable.java b/src/main/java/com/leonardobishop/quests/QuestsAutosaveRunnable.java new file mode 100644 index 00000000..38a9f140 --- /dev/null +++ b/src/main/java/com/leonardobishop/quests/QuestsAutosaveRunnable.java @@ -0,0 +1,45 @@ +package com.leonardobishop.quests; + +import com.leonardobishop.quests.player.QPlayer; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.Queue; +import java.util.UUID; + +public class QuestsAutosaveRunnable extends BukkitRunnable { + + private final Queue<UUID> queue = new LinkedList<>(); + private final Quests plugin; + + public QuestsAutosaveRunnable(Quests plugin) { + for (Player player : Bukkit.getOnlinePlayers()) { + queue.add(player.getUniqueId()); + } + + this.plugin = plugin; + + this.runTaskTimer(plugin, 2L, 2L); + } + + @Override + public void run() { + UUID player = queue.poll(); + if (player == null) { + try { + super.cancel(); + } catch (Exception ignored) {} + return; + } + + if (Bukkit.getPlayer(player) != null) { + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player); + qPlayer.getQuestProgressFile().saveToDisk(true); + } + } + + +} diff --git a/src/main/java/com/leonardobishop/quests/commands/CommandQuests.java b/src/main/java/com/leonardobishop/quests/commands/CommandQuests.java index 6eabc12e..ca64aa3f 100644 --- a/src/main/java/com/leonardobishop/quests/commands/CommandQuests.java +++ b/src/main/java/com/leonardobishop/quests/commands/CommandQuests.java @@ -135,7 +135,7 @@ public class CommandQuests implements TabExecutor { plugin.getPlayerManager().loadPlayer(uuid); QPlayer qPlayer = plugin.getPlayerManager().getPlayer(uuid); qPlayer.getQuestProgressFile().clean(); - qPlayer.getQuestProgressFile().saveToDisk(false, true); + qPlayer.getQuestProgressFile().saveToDisk(false); if (Bukkit.getPlayer(uuid) == null) { plugin.getPlayerManager().dropPlayer(uuid); } @@ -317,7 +317,7 @@ public class CommandQuests implements TabExecutor { } if (args[2].equalsIgnoreCase("reset")) { questProgressFile.generateBlankQuestProgress(quest.getId()); - questProgressFile.saveToDisk(false, true); + questProgressFile.saveToDisk(false); sender.sendMessage(Messages.COMMAND_QUEST_ADMIN_RESET_SUCCESS.getMessage().replace("{player}", name).replace("{quest}", quest.getId())); success = true; } else if (args[2].equalsIgnoreCase("start")) { diff --git a/src/main/java/com/leonardobishop/quests/events/EventPlayerJoin.java b/src/main/java/com/leonardobishop/quests/events/EventPlayerJoin.java index 52020ef7..092289ca 100644 --- a/src/main/java/com/leonardobishop/quests/events/EventPlayerJoin.java +++ b/src/main/java/com/leonardobishop/quests/events/EventPlayerJoin.java @@ -6,6 +6,7 @@ import com.leonardobishop.quests.obj.Options; import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerPreLoginEvent; import org.bukkit.event.player.PlayerJoinEvent; import java.util.UUID; @@ -25,7 +26,7 @@ public class EventPlayerJoin implements Listener { if (Options.SOFT_CLEAN_QUESTSPROGRESSFILE_ON_JOIN.getBooleanValue()) { plugin.getPlayerManager().getPlayer(playerUuid).getQuestProgressFile().clean(); if (Options.PUSH_SOFT_CLEAN_TO_DISK.getBooleanValue()) { - plugin.getPlayerManager().getPlayer(playerUuid).getQuestProgressFile().saveToDisk(false, true); + plugin.getPlayerManager().getPlayer(playerUuid).getQuestProgressFile().saveToDisk(false); } } if (plugin.getDescription().getVersion().contains("beta") && event.getPlayer().hasPermission("quests.admin")) { @@ -35,6 +36,7 @@ public class EventPlayerJoin implements Listener { // delay for a bit so they actually see the message Bukkit.getScheduler().runTaskLater(this.plugin, () -> event.getPlayer().sendMessage(plugin.getUpdater().getMessage()), 50L); } + // run a full check to check for any missed quest completions plugin.getQuestCompleter().queueFullCheck(plugin.getPlayerManager().getPlayer(playerUuid).getQuestProgressFile()); } diff --git a/src/main/java/com/leonardobishop/quests/obj/Options.java b/src/main/java/com/leonardobishop/quests/obj/Options.java index d108d875..409a60bf 100644 --- a/src/main/java/com/leonardobishop/quests/obj/Options.java +++ b/src/main/java/com/leonardobishop/quests/obj/Options.java @@ -23,6 +23,7 @@ public enum Options { GUITITLE_QUEST_CANCEL("options.guinames.quest-cancel"), ALLOW_QUEST_CANCEL("options.allow-quest-cancel"), ALLOW_QUEST_TRACK("options.allow-quest-track"), + QUEST_AUTOSAVE_ASYNC("options.performance-tweaking.quests-autosave-async"), SOFT_CLEAN_QUESTSPROGRESSFILE_ON_JOIN("options.soft-clean-questsprogressfile-on-join"), PUSH_SOFT_CLEAN_TO_DISK("options.tab-completion.push-soft-clean-to-disk"), TAB_COMPLETE_ENABLED("options.tab-completion.enabled"), diff --git a/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java b/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java index dfbb4bab..5cb94a1e 100644 --- a/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java +++ b/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java @@ -6,6 +6,7 @@ import com.leonardobishop.quests.player.questprogressfile.QPlayerPreferences; import com.leonardobishop.quests.player.questprogressfile.QuestProgress; import com.leonardobishop.quests.player.questprogressfile.QuestProgressFile; import com.leonardobishop.quests.player.questprogressfile.TaskProgress; +import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; diff --git a/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java b/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java index 06388a71..02337cfa 100644 --- a/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java +++ b/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java @@ -13,6 +13,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; import java.io.File; import java.io.IOException; @@ -414,15 +415,7 @@ public class QuestProgressFile { return playerPreferences; } - public void saveToDisk() { - saveToDisk(false, false); - } - - public void saveToDisk(boolean disable) { - saveToDisk(disable, false); - } - - public void saveToDisk(boolean disable, boolean fullWrite) { + public void saveToDisk(boolean async) { plugin.getQuestsLogger().debug("Saving player " + playerUUID + " to disk."); File directory = new File(plugin.getDataFolder() + File.separator + "playerdata"); if (!directory.exists() && !directory.isDirectory()) { @@ -451,21 +444,25 @@ public class QuestProgressFile { .getProgress()); } } +// + synchronized (this) { - try { - data.save(file); - if (disable) - synchronized (this.questProgress) { - for (QuestProgress questProgress : questProgress.values()) { - questProgress.resetModified(); + // TODO + if (async && Options.QUEST_AUTOSAVE_ASYNC.getBooleanValue()) { + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + try { + data.save(file); + } catch (IOException e) { + e.printStackTrace(); } + }); + } else { + try { + data.save(file); + } catch (IOException e) { + e.printStackTrace(); } - else - for (QuestProgress questProgress : questProgress.values()) { - questProgress.resetModified(); - } - } catch (IOException e) { - e.printStackTrace(); + } } } |
