Fix opening of other player's vaults. Resolves #190.

This commit is contained in:
Trent Hensler
2016-06-14 14:08:25 -05:00
parent 472ff03b2c
commit edc6b0f8ae
3 changed files with 30 additions and 9 deletions
@@ -403,8 +403,9 @@ public class PlayerVaults extends JavaPlugin {
}
public static void debug(String s, long start) {
if (DEBUG) {
Bukkit.getLogger().log(Level.INFO, "At {0}. Time since start: {1}ms", new Object[]{s, (System.currentTimeMillis() - start)});
long elapsed = System.currentTimeMillis() - start;
if (DEBUG || elapsed > 4) {
Bukkit.getLogger().log(Level.INFO, "At {0}. Time since start: {1}ms", new Object[]{s, (elapsed)});
}
}
@@ -187,7 +187,20 @@ public class UUIDVaultManager {
}
}
/**
* Checks if a vault exists.
*
* @param holder holder of the vault.
* @param number vault number.
*
* @return true if the vault file and vault number exist in that file, otherwise false.
*/
public boolean vaultExists(String holder, int number) {
File file = new File(directory, holder + ".yml");
if (!file.exists()) {
return false;
}
return getPlayerVaultFile(holder).contains("vault" + number);
}
@@ -191,6 +191,10 @@ public class VaultOperations {
if (isLocked()) {
return false;
}
long time = System.currentTimeMillis();
String nicename = holder;
int number = 0;
try {
number = Integer.parseInt(arg);
@@ -202,23 +206,26 @@ public class VaultOperations {
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.
/*
// Try to get the vault of an OfflinePlayer if we don't find one with the name first.
if (!UUIDVaultManager.getInstance().vaultExists(holder, number)) {
player.sendMessage(Lang.TITLE.toString() + Lang.VAULT_DOES_NOT_EXIST.toString());
return false;
OfflinePlayer targetPlayer = Bukkit.getOfflinePlayer(holder);
if (targetPlayer.hasPlayedBefore()) {
holder = targetPlayer.getUniqueId().toString();
}
*/
}
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.openInventory(inv);
player.sendMessage(Lang.TITLE.toString() + Lang.OPEN_OTHER_VAULT.toString().replace("%v", arg).replace("%p", holder));
player.sendMessage(Lang.TITLE.toString() + Lang.OPEN_OTHER_VAULT.toString().replace("%v", arg).replace("%p", nicename));
PlayerVaults.debug("opening other vault", time);
return true;
}
PlayerVaults.debug("opening other vault returning false", time);
return false;
}