Make it so vaults being viewed at the same time update in real time

This commit is contained in:
gomeow
2013-05-23 14:36:16 -07:00
parent 5901d45310
commit 12f0a25565
4 changed files with 36 additions and 20 deletions
@@ -48,11 +48,14 @@ public class Listeners implements Listener {
public void saveVault(Player p) { public void saveVault(Player p) {
if(PlayerVaults.IN_VAULT.containsKey(p.getName())) { if(PlayerVaults.IN_VAULT.containsKey(p.getName())) {
Inventory inv = p.getOpenInventory().getTopInventory(); Inventory inv = p.getOpenInventory().getTopInventory();
VaultViewInfo info = PlayerVaults.IN_VAULT.get(p.getName()); if(inv.getViewers().size() == 1) {
try { VaultViewInfo info = PlayerVaults.IN_VAULT.get(p.getName());
vm.saveVault(inv, info.getHolder(), info.getNumber()); try {
} catch(IOException e) { vm.saveVault(inv, info.getHolder(), info.getNumber());
e.printStackTrace(); } catch(IOException e) {
e.printStackTrace();
}
PlayerVaults.OPENINVENTORIES.remove(info.toString());
} }
PlayerVaults.IN_VAULT.remove(p.getName()); PlayerVaults.IN_VAULT.remove(p.getName());
} }
@@ -10,6 +10,7 @@ import java.util.logging.Logger;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@@ -31,6 +32,7 @@ public class PlayerVaults extends JavaPlugin {
Commands commands; Commands commands;
public static HashMap<String, SignSetInfo> SET_SIGN = new HashMap<String, SignSetInfo>(); public static HashMap<String, SignSetInfo> SET_SIGN = new HashMap<String, SignSetInfo>();
public static HashMap<String, VaultViewInfo> IN_VAULT = new HashMap<String, VaultViewInfo>(); public static HashMap<String, VaultViewInfo> IN_VAULT = new HashMap<String, VaultViewInfo>();
public static HashMap<String, Inventory> OPENINVENTORIES = new HashMap<String, Inventory>();
public static Economy ECON = null; public static Economy ECON = null;
public static boolean DROP_ON_DEATH = false; public static boolean DROP_ON_DEATH = false;
public static int INVENTORIES_TO_DROP = 0; public static int INVENTORIES_TO_DROP = 0;
@@ -17,5 +17,9 @@ public class VaultViewInfo {
public int getNumber() { public int getNumber() {
return this.i; return this.i;
} }
public String toString() {
return this.s + " " + this.i;
}
} }
@@ -14,6 +14,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import com.drtshock.playervaults.PlayerVaults; import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.commands.VaultViewInfo;
public class VaultManager { public class VaultManager {
@@ -51,23 +52,29 @@ public class VaultManager {
* TODO: Check to see if the path exists before we get it! * TODO: Check to see if the path exists before we get it!
*/ */
public void loadVault(Player player, String holder, int number) { public void loadVault(Player player, String holder, int number) {
VaultViewInfo info = new VaultViewInfo(holder, number);
Inventory inv = null; Inventory inv = null;
YamlConfiguration playerFile = playerVaultFile(holder); if(PlayerVaults.OPENINVENTORIES.containsKey(info.toString())) {
if(playerFile.getConfigurationSection("vault" + number) == null) { inv = PlayerVaults.OPENINVENTORIES.get(info.toString());
inv = Bukkit.createInventory(player, 54, ChatColor.DARK_RED + "Vault #" + String.valueOf(number)); } else {
} YamlConfiguration playerFile = playerVaultFile(holder);
else { if(playerFile.getConfigurationSection("vault" + number) == null) {
List<String> data = new ArrayList<String>(); inv = Bukkit.createInventory(player, 54, ChatColor.DARK_RED + "Vault #" + String.valueOf(number));
for(int x = 0; x < 54; x++) {
String line = playerFile.getString("vault" + number + "." + x);
if(line != null) {
data.add(line);
}
else {
data.add("null");
}
} }
inv = Serialization.toInventory(data, number); else {
List<String> data = new ArrayList<String>();
for(int x = 0; x < 54; x++) {
String line = playerFile.getString("vault" + number + "." + x);
if(line != null) {
data.add(line);
}
else {
data.add("null");
}
}
inv = Serialization.toInventory(data, number);
}
PlayerVaults.OPENINVENTORIES.put(info.toString(), inv);
} }
player.openInventory(inv); player.openInventory(inv);
} }