Fix async save on shutdown

This commit is contained in:
Gabriele C 2020-10-28 16:11:56 +01:00
parent f60b738248
commit 57f91cd95f
2 changed files with 16 additions and 7 deletions

View File

@ -49,7 +49,7 @@ public class Main extends JavaPlugin implements Listener {
public void onDisable() {
int file = 0;
Bukkit.getScheduler().cancelTask(file);
Files.DATA.saveFile();
Files.DATA.saveFile(true);
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args) {

View File

@ -200,7 +200,7 @@ public class FileManager {
/**
* Saves the file from the loaded state to the file system.
*/
public void saveFile(Files file) {
public void saveFile(Files file, boolean sync) {
try {
File targetFile = files.get(file);
FileConfiguration configuration = configurations.get(file);
@ -208,7 +208,7 @@ public class FileManager {
YamlConfiguration copy = new YamlConfiguration();
configuration.getValues(false).forEach(copy::set);
new BukkitRunnable() {
BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
try {
@ -218,7 +218,12 @@ public class FileManager {
e.printStackTrace();
}
}
}.runTaskAsynchronously(plugin);
};
if (sync) {
runnable.run();
} else {
runnable.runTaskAsynchronously(plugin);
}
} catch (NullPointerException e) {
System.out.println(prefix + "File is null " + file.getFileName() + "!");
e.printStackTrace();
@ -352,10 +357,14 @@ public class FileManager {
/**
* Saves the file from the loaded state to the file system.
*/
public void saveFile() {
getInstance().saveFile(this);
public void saveFile(boolean sync) {
getInstance().saveFile(this, sync);
}
public void saveFile() {
getInstance().saveFile(this, false);
}
/**
* Overrides the loaded state file and loads the file systems file.
*/