Add CachedVaults
Only writes vaults to file on server shutdown or player quit, rather than every inventory close event. Also caches for read() events.
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package com.drtshock.playervaults.vaultmanagement;
|
||||
|
||||
import com.drtshock.playervaults.PlayerVaults;
|
||||
import com.drtshock.playervaults.util.Lang;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -10,11 +15,8 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import com.drtshock.playervaults.PlayerVaults;
|
||||
import com.drtshock.playervaults.util.Lang;
|
||||
|
||||
/**
|
||||
* Class to handle vault operations with new UUIDs.
|
||||
@@ -22,13 +24,19 @@ import java.util.UUID;
|
||||
public class UUIDVaultManager {
|
||||
|
||||
private static UUIDVaultManager instance;
|
||||
private static CachedVaults cachedVaults;
|
||||
|
||||
public UUIDVaultManager() {
|
||||
instance = this;
|
||||
cachedVaults = new CachedVaults();
|
||||
}
|
||||
|
||||
private final File directory = PlayerVaults.getInstance().getVaultData();
|
||||
|
||||
public CachedVaults getCachedVaults(){
|
||||
return cachedVaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the inventory to the specified player and vault number.
|
||||
*
|
||||
@@ -65,6 +73,10 @@ public class UUIDVaultManager {
|
||||
* @param number The vault number.
|
||||
*/
|
||||
public Inventory loadOwnVault(Player player, int number, int size) {
|
||||
if(cachedVaults.hasVaultCached(player.getUniqueId(), number)){
|
||||
return cachedVaults.getCachedVault(player.getUniqueId(), number);
|
||||
}
|
||||
|
||||
if (size % 9 != 0) {
|
||||
size = 54;
|
||||
}
|
||||
@@ -96,6 +108,7 @@ public class UUIDVaultManager {
|
||||
PlayerVaults.getInstance().getOpenInventories().put(info.toString(), inv);
|
||||
}
|
||||
|
||||
cachedVaults.setCachedVault(player.getUniqueId(), number, inv);
|
||||
return inv;
|
||||
}
|
||||
|
||||
@@ -106,6 +119,10 @@ public class UUIDVaultManager {
|
||||
* @param number The vault number.
|
||||
*/
|
||||
public Inventory loadOtherVault(UUID holder, int number, int size) {
|
||||
if(cachedVaults.hasVaultCached(holder, number)){
|
||||
return cachedVaults.getCachedVault(holder, number);
|
||||
}
|
||||
|
||||
if (size % 9 != 0) {
|
||||
size = 54;
|
||||
}
|
||||
@@ -128,6 +145,8 @@ public class UUIDVaultManager {
|
||||
}
|
||||
PlayerVaults.getInstance().getOpenInventories().put(info.toString(), inv);
|
||||
}
|
||||
|
||||
cachedVaults.setCachedVault(holder, number, inv);
|
||||
return inv;
|
||||
}
|
||||
|
||||
@@ -193,10 +212,12 @@ public class UUIDVaultManager {
|
||||
* @throws IOException Uh oh!
|
||||
*/
|
||||
public void deleteVault(CommandSender sender, UUID holder, int number) throws IOException {
|
||||
File file = new File(directory, holder.toString() + ".yml");
|
||||
File file = new File(directory, holder.toString() + ".yml");
|
||||
if (!file.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cachedVaults.clearVaultCache(holder);
|
||||
|
||||
FileConfiguration playerFile = YamlConfiguration.loadConfiguration(file);
|
||||
if (file.exists()) {
|
||||
|
||||
Reference in New Issue
Block a user