Remove async saving

Fixes #245, #223, #246, #247
This commit is contained in:
Byteflux
2017-01-06 20:26:30 -08:00
committed by kashike
parent 261314bbc0
commit 25120bef1a
4 changed files with 9 additions and 56 deletions
@@ -129,7 +129,7 @@ public class PlayerVaults extends JavaPlugin {
Inventory inventory = player.getOpenInventory().getTopInventory(); Inventory inventory = player.getOpenInventory().getTopInventory();
if (inventory.getViewers().size() == 1) { if (inventory.getViewers().size() == 1) {
VaultViewInfo info = this.inVault.get(player.getUniqueId().toString()); VaultViewInfo info = this.inVault.get(player.getUniqueId().toString());
UUIDVaultManager.getInstance().saveVault(inventory, player.getUniqueId().toString(), info.getNumber(), false); UUIDVaultManager.getInstance().saveVault(inventory, player.getUniqueId().toString(), info.getNumber());
this.openInventories.remove(info.toString()); this.openInventories.remove(info.toString());
} }
@@ -82,13 +82,8 @@ public class BackpackConverter implements Converter {
// Overwrite // Overwrite
vault.setItem(Integer.parseInt(key.split(" ")[1]), item); vault.setItem(Integer.parseInt(key.split(" ")[1]), item);
} }
try { vaults.saveVault(vault, uuid.toString(), intoVaultNum);
vaults.saveVault(vault, uuid.toString(), intoVaultNum); converted++;
converted++;
} catch (IOException e) {
plugin.getLogger().severe("Error converting Backpack: " + file.getName());
e.printStackTrace();
}
if (System.currentTimeMillis() - lastUpdate >= 1500) { if (System.currentTimeMillis() - lastUpdate >= 1500) {
plugin.getLogger().info(converted + " backpacks have been converted in " + worldFolder.getAbsolutePath()); plugin.getLogger().info(converted + " backpacks have been converted in " + worldFolder.getAbsolutePath());
@@ -54,12 +54,8 @@ public class Listeners implements Listener {
Inventory inv = player.getOpenInventory().getTopInventory(); Inventory inv = player.getOpenInventory().getTopInventory();
if (inv.getViewers().size() == 1) { if (inv.getViewers().size() == 1) {
VaultViewInfo info = plugin.getInVault().get(player.getUniqueId().toString()); VaultViewInfo info = plugin.getInVault().get(player.getUniqueId().toString());
try { String target = info.getHolderUUID() != null ? info.getHolderUUID().toString() : info.getHolder();
String target = info.getHolderUUID() != null ? info.getHolderUUID().toString() : info.getHolder(); vaultManager.saveVault(inv, target, info.getNumber());
vaultManager.saveVault(inv, target, info.getNumber());
} catch (IOException e) {
// ignore
}
plugin.getOpenInventories().remove(info.toString()); plugin.getOpenInventories().remove(info.toString());
} }
@@ -39,14 +39,8 @@ public class UUIDVaultManager {
* @param inventory The inventory to be saved. * @param inventory The inventory to be saved.
* @param target The player of whose file to save to. * @param target The player of whose file to save to.
* @param number The vault number. * @param number The vault number.
*
* @throws java.io.IOException Uh oh!
*/ */
public void saveVault(Inventory inventory, String target, int number) throws IOException { public void saveVault(Inventory inventory, String target, int number) {
saveVault(inventory, target, number, true);
}
public void saveVault(Inventory inventory, String target, int number, boolean async) {
int size = inventory.getSize(); int size = inventory.getSize();
YamlConfiguration yaml = getPlayerVaultFile(target); YamlConfiguration yaml = getPlayerVaultFile(target);
if (size == 54) { if (size == 54) {
@@ -63,11 +57,7 @@ public class UUIDVaultManager {
yaml.set("vault" + number + "." + x, ser[x]); yaml.set("vault" + number + "." + x, ser[x]);
} }
} }
if (async) { saveFileSync(target, yaml);
saveFile(target, yaml);
} else {
saveFileSync(target, yaml);
}
} }
/** /**
@@ -309,35 +299,6 @@ public class UUIDVaultManager {
return YamlConfiguration.loadConfiguration(file); return YamlConfiguration.loadConfiguration(file);
} }
/**
* Save the players vault file.
*
* @param holder The vault holder of whose file to save.
* @param yaml The config to save.
*/
public void saveFile(final String holder, final YamlConfiguration yaml) {
if (cachedVaultFiles.containsKey(holder)) {
cachedVaultFiles.put(holder, yaml);
}
final boolean backups = PlayerVaults.getInstance().isBackupsEnabled();
final File backupsFolder = PlayerVaults.getInstance().getBackupsFolder();
final File file = new File(directory, holder + ".yml");
new BukkitRunnable() {
@Override
public void run() {
if (file.exists() && backups) {
file.renameTo(new File(backupsFolder, holder + ".yml"));
}
try {
yaml.save(file);
} catch (Exception e) {
PlayerVaults.getInstance().getLogger().log(Level.SEVERE, "Failed to save vault file for: " + holder);
e.printStackTrace();
}
}
}.runTaskAsynchronously(PlayerVaults.getInstance());
}
public void saveFileSync(final String holder, final YamlConfiguration yaml) { public void saveFileSync(final String holder, final YamlConfiguration yaml) {
if (cachedVaultFiles.containsKey(holder)) { if (cachedVaultFiles.containsKey(holder)) {
cachedVaultFiles.put(holder, yaml); cachedVaultFiles.put(holder, yaml);
@@ -350,7 +311,8 @@ public class UUIDVaultManager {
} }
try { try {
yaml.save(file); yaml.save(file);
} catch (IOException ignored) { } catch (IOException e) {
PlayerVaults.getInstance().getLogger().log(Level.SEVERE, "Failed to save vault file for: " + holder, e);
} }
} }