From f6c6283ac13e1fe6a582223371b706adfd9ed3fa Mon Sep 17 00:00:00 2001 From: galacticwarrior9 Date: Fri, 30 Jul 2021 16:56:02 +0100 Subject: [PATCH] Async saving --- .../gods/managers/BelieverManager.java | 24 +++++++------ .../gods/managers/HolyBookManager.java | 24 +++++++------ .../dogonfire/gods/managers/QuestManager.java | 23 ++++++------ .../gods/managers/WhitelistManager.java | 35 ++++++++++--------- 4 files changed, 60 insertions(+), 46 deletions(-) diff --git a/src/com/dogonfire/gods/managers/BelieverManager.java b/src/com/dogonfire/gods/managers/BelieverManager.java index 4e2ffe3..31d92b2 100644 --- a/src/com/dogonfire/gods/managers/BelieverManager.java +++ b/src/com/dogonfire/gods/managers/BelieverManager.java @@ -1,6 +1,7 @@ package com.dogonfire.gods.managers; import java.io.File; +import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -17,6 +18,7 @@ import com.dogonfire.gods.Gods; import com.dogonfire.gods.config.GodsConfiguration; import com.dogonfire.gods.managers.LanguageManager.LANGUAGESTRING; +import org.bukkit.scheduler.BukkitRunnable; public class BelieverManager { @@ -659,18 +661,20 @@ public void removePrayer(UUID believerId) public void save() { this.lastSaveTime = System.currentTimeMillis(); - if ((this.believersConfig == null) || (this.believersConfigFile == null)) - { + if ((this.believersConfig == null) || (this.believersConfigFile == null)) { return; } - try - { - this.believersConfig.save(this.believersConfigFile); - } - catch (Exception ex) - { - Gods.instance().log("Could not save config to " + this.believersConfigFile.getName() + ": " + ex.getMessage()); - } + new BukkitRunnable() { + @Override + public void run() { + try { + believersConfig.save(believersConfigFile); + } + catch (IOException ex) { + Gods.instance().log("Could not save config to " + believersConfigFile.getName() + ": " + ex.getMessage()); + } + } + }.runTaskAsynchronously(Gods.instance()); } public void saveTimed() diff --git a/src/com/dogonfire/gods/managers/HolyBookManager.java b/src/com/dogonfire/gods/managers/HolyBookManager.java index 1c11ca3..2462b24 100644 --- a/src/com/dogonfire/gods/managers/HolyBookManager.java +++ b/src/com/dogonfire/gods/managers/HolyBookManager.java @@ -1,6 +1,7 @@ package com.dogonfire.gods.managers; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -13,6 +14,7 @@ import com.dogonfire.gods.Gods; import com.dogonfire.gods.HolyBook; import com.dogonfire.gods.config.GodsConfiguration; +import org.bukkit.scheduler.BukkitRunnable; public class HolyBookManager { @@ -236,18 +238,20 @@ public void load() public void save() { - if ((this.biblesConfig == null) || (this.biblesConfigFile == null)) - { + if ((this.biblesConfig == null) || (this.biblesConfigFile == null)) { return; } - try - { - this.biblesConfig.save(this.biblesConfigFile); - } - catch (Exception ex) - { - Gods.instance().log("Could not save config to " + this.biblesConfigFile.getName() + ": " + ex.getMessage()); - } + new BukkitRunnable() { + @Override + public void run() { + try { + biblesConfig.save(biblesConfigFile); + } + catch (IOException ex) { + Gods.instance().log("Could not save config to " + biblesConfigFile.getName() + ": " + ex.getMessage()); + } + } + }.runTaskAsynchronously(Gods.instance()); } public boolean setBible(String godName, String priestName) diff --git a/src/com/dogonfire/gods/managers/QuestManager.java b/src/com/dogonfire/gods/managers/QuestManager.java index 0a2ad75..587b2b4 100644 --- a/src/com/dogonfire/gods/managers/QuestManager.java +++ b/src/com/dogonfire/gods/managers/QuestManager.java @@ -27,6 +27,7 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.util.Vector; import com.dogonfire.gods.Gods; @@ -2655,18 +2656,20 @@ public void resetItemRewardValues() public void save() { - if ((this.questsConfig == null) || (this.questsConfigFile == null)) - { + if ((this.questsConfig == null) || (this.questsConfigFile == null)) { return; } - try - { - this.questsConfig.save(this.questsConfigFile); - } - catch (Exception ex) - { - Gods.instance().log("Could not save config to " + this.questsConfigFile.getName() + ": " + ex.getMessage()); - } + new BukkitRunnable() { + @Override + public void run() { + try { + questsConfig.save(questsConfigFile); + } + catch (Exception ex) { + Gods.instance().log("Could not save config to " + questsConfigFile.getName() + ": " + ex.getMessage()); + } + } + }.runTaskAsynchronously(Gods.instance()); } /* diff --git a/src/com/dogonfire/gods/managers/WhitelistManager.java b/src/com/dogonfire/gods/managers/WhitelistManager.java index f2967cc..6305a9b 100644 --- a/src/com/dogonfire/gods/managers/WhitelistManager.java +++ b/src/com/dogonfire/gods/managers/WhitelistManager.java @@ -1,12 +1,14 @@ package com.dogonfire.gods.managers; import java.io.File; +import java.io.IOException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import com.dogonfire.gods.Gods; import com.dogonfire.gods.config.GodsConfiguration; +import org.bukkit.scheduler.BukkitRunnable; public class WhitelistManager { @@ -98,21 +100,22 @@ public void load() public void save() { - try - { - this.whiteList.save(this.whiteListFile); - } - catch (Exception ex) - { - Gods.instance().log("Could not save whitelist to " + this.whiteListFile + ": " + ex.getMessage()); - } - try - { - this.blackList.save(this.blackListFile); - } - catch (Exception ex) - { - Gods.instance().log("Could not save blacklist to " + this.blackListFile + ": " + ex.getMessage()); - } + new BukkitRunnable() { + @Override + public void run() { + try { + whiteList.save(whiteListFile); + } + catch (IOException ex) { + Gods.instance().log("Could not save whitelist to " + whiteListFile + ": " + ex.getMessage()); + } + try { + blackList.save(blackListFile); + } + catch (IOException ex) { + Gods.instance().log("Could not save blacklist to " + blackListFile + ": " + ex.getMessage()); + } + } + }.runTaskAsynchronously(Gods.instance()); } } \ No newline at end of file