Accept Strings as inputs instead of UUIDs.
This leaves options for other plugins to hook into us to use our vault operations. I did a first shot of this with FactionsUUID. * This needs to be tested more in depth before being pushed to a release version as it changes how we handle a lot of our dupe checks.
This commit is contained in:
@@ -47,7 +47,7 @@ public class EconomyOperations {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!UUIDVaultManager.getInstance().vaultExists(player.getUniqueId(), number)) {
|
||||
if (!UUIDVaultManager.getInstance().vaultExists(player.getUniqueId().toString(), number)) {
|
||||
return payToCreate(player);
|
||||
} else {
|
||||
double cost = BUKKIT_CONFIG.getDouble("economy.cost-to-open", 10);
|
||||
|
||||
@@ -31,24 +31,24 @@ public class UUIDVaultManager {
|
||||
}
|
||||
|
||||
private final File directory = PlayerVaults.getInstance().getVaultData();
|
||||
private final Map<UUID, YamlConfiguration> cachedVaultFiles = new ConcurrentHashMap<>();
|
||||
private final Map<String, YamlConfiguration> cachedVaultFiles = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Saves the inventory to the specified player and vault number.
|
||||
*
|
||||
* @param inventory The inventory to be saved.
|
||||
* @param player The player of whose file to save to.
|
||||
* @param target The player of whose file to save to.
|
||||
* @param number The vault number.
|
||||
*
|
||||
* @throws java.io.IOException Uh oh!
|
||||
*/
|
||||
public void saveVault(Inventory inventory, UUID player, int number) throws IOException {
|
||||
saveVault(inventory, player, number, true);
|
||||
public void saveVault(Inventory inventory, String target, int number) throws IOException {
|
||||
saveVault(inventory, target, number, true);
|
||||
}
|
||||
|
||||
public void saveVault(Inventory inventory, UUID player, int number, boolean async) {
|
||||
public void saveVault(Inventory inventory, String target, int number, boolean async) {
|
||||
int size = inventory.getSize();
|
||||
YamlConfiguration yaml = getPlayerVaultFile(player);
|
||||
YamlConfiguration yaml = getPlayerVaultFile(target);
|
||||
if (size == 54) {
|
||||
yaml.set("vault" + number, null);
|
||||
} else {
|
||||
@@ -64,9 +64,9 @@ public class UUIDVaultManager {
|
||||
}
|
||||
}
|
||||
if (async) {
|
||||
saveFile(player, yaml);
|
||||
saveFile(target, yaml);
|
||||
} else {
|
||||
saveFileSync(player, yaml);
|
||||
saveFileSync(target, yaml);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,26 +117,22 @@ public class UUIDVaultManager {
|
||||
* @param holder The holder of the vault.
|
||||
* @param number The vault number.
|
||||
*/
|
||||
public Inventory loadOtherVault(UUID holder, int number, int size) {
|
||||
public Inventory loadOtherVault(String holder, int number, int size) {
|
||||
if (size % 9 != 0) {
|
||||
size = 54;
|
||||
}
|
||||
String title = Lang.VAULT_TITLE.toString().replace("%number", String.valueOf(number)).replace("%p", Bukkit.getOfflinePlayer(holder).getName());
|
||||
String title = Lang.VAULT_TITLE.toString().replace("%number", String.valueOf(number)).replace("%p", PlayerVaults.getInstance().getNameIfPlayer(holder));
|
||||
VaultViewInfo info = new VaultViewInfo(holder, number);
|
||||
Inventory inv;
|
||||
if (PlayerVaults.getInstance().getOpenInventories().containsKey(info.toString())) {
|
||||
inv = PlayerVaults.getInstance().getOpenInventories().get(info.toString());
|
||||
} else {
|
||||
YamlConfiguration playerFile = getPlayerVaultFile(holder);
|
||||
if (playerFile.getConfigurationSection("vault" + number) == null) {
|
||||
Inventory i = getInventory(playerFile, size, number, title);
|
||||
if (i == null) {
|
||||
return null;
|
||||
} else {
|
||||
Inventory i = getInventory(playerFile, size, number, title);
|
||||
if (i == null) {
|
||||
return null;
|
||||
} else {
|
||||
inv = i;
|
||||
}
|
||||
inv = i;
|
||||
}
|
||||
PlayerVaults.getInstance().getOpenInventories().put(info.toString(), inv);
|
||||
}
|
||||
@@ -191,7 +187,7 @@ public class UUIDVaultManager {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean vaultExists(UUID holder, int number) {
|
||||
public boolean vaultExists(String holder, int number) {
|
||||
return getPlayerVaultFile(holder).contains("vault" + number);
|
||||
}
|
||||
|
||||
@@ -204,11 +200,11 @@ public class UUIDVaultManager {
|
||||
*
|
||||
* @throws IOException Uh oh!
|
||||
*/
|
||||
public void deleteVault(CommandSender sender, final UUID holder, final int number) throws IOException {
|
||||
public void deleteVault(CommandSender sender, final String holder, final int number) throws IOException {
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
File file = new File(directory, holder.toString() + ".yml");
|
||||
File file = new File(directory, holder + ".yml");
|
||||
if (!file.exists()) {
|
||||
return;
|
||||
}
|
||||
@@ -236,23 +232,31 @@ public class UUIDVaultManager {
|
||||
}
|
||||
}
|
||||
|
||||
PlayerVaults.getInstance().getOpenInventories().remove(new VaultViewInfo(holder.toString(), number).toString());
|
||||
PlayerVaults.getInstance().getOpenInventories().remove(new VaultViewInfo(holder, number).toString());
|
||||
}
|
||||
|
||||
// Should only be run asynchronously
|
||||
public void cachePlayerVaultFile(UUID holder) {
|
||||
public void cachePlayerVaultFile(String holder) {
|
||||
YamlConfiguration config = this.loadPlayerVaultFile(holder, false);
|
||||
if (config != null) {
|
||||
this.cachedVaultFiles.put(holder, config);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeCachedPlayerVaultFile(UUID holder) {
|
||||
public void removeCachedPlayerVaultFile(String holder) {
|
||||
if (cachedVaultFiles.containsKey(holder)) {
|
||||
cachedVaultFiles.remove(holder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use below method for getting it via String.
|
||||
*/
|
||||
@Deprecated
|
||||
public YamlConfiguration getPlayerVaultFile(UUID holder) {
|
||||
return getPlayerVaultFile(holder.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the holder's vault file. Create if doesn't exist.
|
||||
*
|
||||
@@ -260,23 +264,23 @@ public class UUIDVaultManager {
|
||||
*
|
||||
* @return The holder's vault config file.
|
||||
*/
|
||||
public YamlConfiguration getPlayerVaultFile(UUID holder) {
|
||||
public YamlConfiguration getPlayerVaultFile(String holder) {
|
||||
if (cachedVaultFiles.containsKey(holder)) {
|
||||
return cachedVaultFiles.get(holder);
|
||||
}
|
||||
return loadPlayerVaultFile(holder);
|
||||
}
|
||||
|
||||
public YamlConfiguration loadPlayerVaultFile(UUID holder) {
|
||||
public YamlConfiguration loadPlayerVaultFile(String holder) {
|
||||
return this.loadPlayerVaultFile(holder, true);
|
||||
}
|
||||
|
||||
public YamlConfiguration loadPlayerVaultFile(UUID uniqueId, boolean createIfNotFound) {
|
||||
public YamlConfiguration loadPlayerVaultFile(String uniqueId, boolean createIfNotFound) {
|
||||
if (!this.directory.exists()) {
|
||||
this.directory.mkdir();
|
||||
}
|
||||
|
||||
File file = new File(this.directory, uniqueId.toString() + ".yml");
|
||||
File file = new File(this.directory, uniqueId + ".yml");
|
||||
if (!file.exists()) {
|
||||
if (createIfNotFound) {
|
||||
try {
|
||||
@@ -298,38 +302,38 @@ public class UUIDVaultManager {
|
||||
* @param holder The vault holder of whose file to save.
|
||||
* @param yaml The config to save.
|
||||
*/
|
||||
public void saveFile(final UUID holder, final YamlConfiguration yaml) {
|
||||
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.toString() + ".yml");
|
||||
final File file = new File(directory, holder + ".yml");
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (file.exists() && backups) {
|
||||
file.renameTo(new File(backupsFolder, holder.toString() + ".yml"));
|
||||
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.toString());
|
||||
PlayerVaults.getInstance().getLogger().log(Level.SEVERE, "Failed to save vault file for: " + holder);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}.runTaskAsynchronously(PlayerVaults.getInstance());
|
||||
}
|
||||
|
||||
public void saveFileSync(final UUID holder, final YamlConfiguration yaml) {
|
||||
public void saveFileSync(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.toString() + ".yml");
|
||||
final File file = new File(directory, holder + ".yml");
|
||||
if (file.exists() && backups) {
|
||||
file.renameTo(new File(backupsFolder, holder.toString() + ".yml"));
|
||||
file.renameTo(new File(backupsFolder, holder + ".yml"));
|
||||
}
|
||||
try {
|
||||
yaml.save(file);
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.drtshock.playervaults.vaultmanagement;
|
||||
|
||||
import com.drtshock.playervaults.PlayerVaults;
|
||||
import com.drtshock.playervaults.util.Lang;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -26,6 +27,7 @@ import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryView;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class VaultOperations {
|
||||
@@ -83,6 +85,24 @@ public class VaultOperations {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the max size vault a player is allowed to have.
|
||||
*
|
||||
* @param target that is having his permissions checked.
|
||||
*
|
||||
* @return max size as integer. If no max size is set then it will default to 54.
|
||||
*/
|
||||
public static int getMaxVaultSize(String target) {
|
||||
UUID uuid;
|
||||
try {
|
||||
uuid = UUID.fromString(target);
|
||||
} catch (Exception e) {
|
||||
return 54;
|
||||
}
|
||||
|
||||
return getMaxVaultSize(Bukkit.getOfflinePlayer(uuid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the max size vault a player is allowed to have.
|
||||
*
|
||||
@@ -167,36 +187,38 @@ public class VaultOperations {
|
||||
*
|
||||
* @return Whether or not the player was allowed to open it.
|
||||
*/
|
||||
public static boolean openOtherVault(Player player, OfflinePlayer holder, String arg) {
|
||||
public static boolean openOtherVault(Player player, String holder, String arg) {
|
||||
if (isLocked()) {
|
||||
return false;
|
||||
}
|
||||
if (player.hasPermission("playervaults.admin")) {
|
||||
if (!holder.hasPlayedBefore()) {
|
||||
int number = 0;
|
||||
try {
|
||||
number = Integer.parseInt(arg);
|
||||
if (number < 1) {
|
||||
player.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
return false;
|
||||
}
|
||||
} catch (NumberFormatException nfe) {
|
||||
player.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
}
|
||||
|
||||
// TODO: Add back way to check for vault existing for players but not factions.
|
||||
/*
|
||||
if (!UUIDVaultManager.getInstance().vaultExists(holder, number)) {
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.VAULT_DOES_NOT_EXIST.toString());
|
||||
return false;
|
||||
}
|
||||
int number = 0;
|
||||
try {
|
||||
number = Integer.parseInt(arg);
|
||||
if (number < 1) {
|
||||
player.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
return false;
|
||||
}
|
||||
} catch (NumberFormatException nfe) {
|
||||
player.sendMessage(Lang.TITLE.toString() + ChatColor.RED + Lang.MUST_BE_NUMBER);
|
||||
}
|
||||
Inventory inv = UUIDVaultManager.getInstance().loadOtherVault(holder.getUniqueId(), number, getMaxVaultSize(holder));
|
||||
if (inv == null) {
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.VAULT_DOES_NOT_EXIST.toString());
|
||||
} else {
|
||||
player.openInventory(inv);
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.OPEN_OTHER_VAULT.toString().replace("%v", arg).replace("%p", holder.getName()));
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
Inventory inv = UUIDVaultManager.getInstance().loadOtherVault(holder, number, getMaxVaultSize(holder));
|
||||
if (inv == null) {
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.VAULT_DOES_NOT_EXIST.toString());
|
||||
} else {
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
|
||||
player.openInventory(inv);
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.OPEN_OTHER_VAULT.toString().replace("%v", arg).replace("%p", holder));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -224,7 +246,7 @@ public class VaultOperations {
|
||||
|
||||
try {
|
||||
if (EconomyOperations.refundOnDelete(player, number)) {
|
||||
UUIDVaultManager.getInstance().deleteVault(player, player.getUniqueId(), number);
|
||||
UUIDVaultManager.getInstance().deleteVault(player, player.getUniqueId().toString(), number);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
player.sendMessage(Lang.TITLE.toString() + Lang.DELETE_VAULT_ERROR);
|
||||
@@ -259,7 +281,7 @@ public class VaultOperations {
|
||||
}
|
||||
|
||||
try {
|
||||
UUIDVaultManager.getInstance().deleteVault(sender, holder.getUniqueId(), number);
|
||||
UUIDVaultManager.getInstance().deleteVault(sender, holder.getUniqueId().toString(), number);
|
||||
} catch (IOException e) {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.DELETE_VAULT_ERROR);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ public class VaultViewInfo {
|
||||
final int number;
|
||||
|
||||
/**
|
||||
* Make a VaultViewObject
|
||||
* Make a VaultViewObject.
|
||||
*
|
||||
* @param s The holder of the vault.
|
||||
* @param i The vault number.
|
||||
|
||||
Reference in New Issue
Block a user