diff --git a/pom.xml b/pom.xml index 5d47dfd..4098e0f 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.drtshock PlayerVaults - 3.3.0-SNAPSHOT + 3.2.1 PlayerVaults http://dev.bukkit.org/server-mods/playervaults/ diff --git a/src/main/java/com/drtshock/playervaults/Listeners.java b/src/main/java/com/drtshock/playervaults/Listeners.java index 72a3e94..50662f5 100644 --- a/src/main/java/com/drtshock/playervaults/Listeners.java +++ b/src/main/java/com/drtshock/playervaults/Listeners.java @@ -29,6 +29,8 @@ import com.drtshock.playervaults.commands.VaultViewInfo; import com.drtshock.playervaults.util.DropOnDeath; import com.drtshock.playervaults.util.Lang; import com.drtshock.playervaults.util.VaultManager; +import org.bukkit.Bukkit; +import org.bukkit.event.server.PluginDisableEvent; public class Listeners implements Listener { @@ -37,23 +39,21 @@ public class Listeners implements Listener { public Listeners(PlayerVaults playerVaults) { this.plugin = playerVaults; } - VaultManager vm = new VaultManager(plugin); /** - * Save a players vault. - * Sends to method in VaultManager class. + * Save a players vault. Sends to method in VaultManager class. + * * @param 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(); - if(inv.getViewers().size() == 1) { + if (inv.getViewers().size() == 1) { VaultViewInfo info = PlayerVaults.IN_VAULT.get(p.getName()); try { vm.saveVault(inv, info.getHolder(), info.getNumber()); - } catch(IOException e) { - e.printStackTrace(); + } catch (IOException e) { } PlayerVaults.OPENINVENTORIES.remove(info.toString()); } @@ -61,6 +61,21 @@ public class Listeners implements Listener { } } + /** + * Save vaults when the plugin disables. Fixes a dupe on server shutdown + * with open vaults. + * + * @param event + */ + @EventHandler + public void onDisableEvent(PluginDisableEvent event) { + for (Player p : Bukkit.getOnlinePlayers()) { + if(PlayerVaults.IN_VAULT.containsKey(p.getName())) { + p.closeInventory(); + } + } + } + @EventHandler public void onTeleport(PlayerTeleportEvent event) { saveVault(event.getPlayer()); @@ -76,7 +91,7 @@ public class Listeners implements Listener { public void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); vm.playerVaultFile(player.getName()); - if(player.isOp() && PlayerVaults.UPDATE) { + if (player.isOp() && PlayerVaults.UPDATE) { player.sendMessage(ChatColor.GREEN + "Version " + PlayerVaults.NEWVERSION + " of PlayerVaults is up for download!"); player.sendMessage(ChatColor.GREEN + PlayerVaults.LINK + " to view the changelog and download!"); } @@ -86,7 +101,7 @@ public class Listeners implements Listener { public void onDeath(PlayerDeathEvent event) { Player player = event.getEntity(); saveVault(player); - if(PlayerVaults.DROP_ON_DEATH && (!player.hasPermission("playervaults.ignore.drops"))) { + if (PlayerVaults.DROP_ON_DEATH && (!player.hasPermission("playervaults.ignore.drops"))) { DropOnDeath.drop(event.getEntity()); } } @@ -94,30 +109,29 @@ public class Listeners implements Listener { @EventHandler public void onClose(InventoryCloseEvent event) { HumanEntity he = event.getPlayer(); - if(he instanceof Player) { + if (he instanceof Player) { Player player = (Player) he; saveVault(player); } } /** - * Check if a player is trying to do something while - * in a vault. - * Don't let them open up another chest. + * Check if a player is trying to do something while in a vault. Don't let + * them open up another chest. + * * @param PlayerInteractEvent */ @EventHandler public void onInteract(PlayerInteractEvent event) { Player player = event.getPlayer(); - if(event.getAction() == Action.RIGHT_CLICK_BLOCK) { - if(PlayerVaults.IN_VAULT.containsKey(player.getName())) { + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + if (PlayerVaults.IN_VAULT.containsKey(player.getName())) { Block block = event.getClickedBlock(); /** - * Different inventories that - * we don't want the player to open. + * Different inventories that we don't want the player to open. */ - if(block.getType() == Material.CHEST + if (block.getType() == Material.CHEST || block.getType() == Material.ENDER_CHEST || block.getType() == Material.FURNACE || block.getType() == Material.BURNING_FURNACE @@ -127,24 +141,24 @@ public class Listeners implements Listener { } } } - if(PlayerVaults.SET_SIGN.containsKey(player.getName())) { + if (PlayerVaults.SET_SIGN.containsKey(player.getName())) { int i = PlayerVaults.SET_SIGN.get(player.getName()).getChest(); boolean self = PlayerVaults.SET_SIGN.get(player.getName()).isSelf(); String owner = null; - if(!self) { + if (!self) { owner = PlayerVaults.SET_SIGN.get(player.getName()).getOwner(); } PlayerVaults.SET_SIGN.remove(player.getName()); event.setCancelled(true); - if(event.getAction() == Action.RIGHT_CLICK_BLOCK) { - if(event.getClickedBlock().getType() == Material.WALL_SIGN || event.getClickedBlock().getType() == Material.SIGN_POST) { + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + if (event.getClickedBlock().getType() == Material.WALL_SIGN || event.getClickedBlock().getType() == Material.SIGN_POST) { Sign s = (Sign) event.getClickedBlock().getState(); Location l = s.getLocation(); String world = l.getWorld().getName(); int x = l.getBlockX(); int y = l.getBlockY(); int z = l.getBlockZ(); - if(self) { + if (self) { plugin.getSigns().set(world + ";;" + x + ";;" + y + ";;" + z + ".self", self); } else { plugin.getSigns().set(world + ";;" + x + ";;" + y + ";;" + z + ".owner", owner); @@ -161,18 +175,18 @@ public class Listeners implements Listener { return; } Block b = event.getClickedBlock(); - if(event.getAction() == Action.RIGHT_CLICK_BLOCK) { - if(b.getType() == Material.WALL_SIGN || b.getType() == Material.SIGN_POST) { + if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { + if (b.getType() == Material.WALL_SIGN || b.getType() == Material.SIGN_POST) { Location l = b.getLocation(); String world = l.getWorld().getName(); int x = l.getBlockX(); int y = l.getBlockY(); int z = l.getBlockZ(); - if(plugin.getSigns().getKeys(false).contains(world + ";;" + x + ";;" + y + ";;" + z)) { - if(player.hasPermission("playervaults.signs.use")) { + if (plugin.getSigns().getKeys(false).contains(world + ";;" + x + ";;" + y + ";;" + z)) { + if (player.hasPermission("playervaults.signs.use")) { boolean self = PlayerVaults.SIGNS.getBoolean(world + ";;" + x + ";;" + y + ";;" + z + ".self", false); String owner = null; - if(!self) { + if (!self) { owner = PlayerVaults.SIGNS.getString(world + ";;" + x + ";;" + y + ";;" + z + ".owner"); } int num = PlayerVaults.SIGNS.getInt(world + ";;" + x + ";;" + y + ";;" + z + ".chest"); @@ -208,21 +222,21 @@ public class Listeners implements Listener { int x = l.getBlockX(); int y = l.getBlockY(); int z = l.getBlockZ(); - if(plugin.getSigns().getKeys(false).contains(world + ";;" + x + ";;" + y + ";;" + z)) { + if (plugin.getSigns().getKeys(false).contains(world + ";;" + x + ";;" + y + ";;" + z)) { plugin.getSigns().set(world + ";;" + x + ";;" + y + ";;" + z, null); plugin.saveSigns(); } } /** - * Don't let a player open a trading inventory OR a minecart - * while he has his vault open. + * Don't let a player open a trading inventory OR a minecart while he has + * his vault open. */ @EventHandler public void onInteractEntity(PlayerInteractEntityEvent event) { Player player = event.getPlayer(); EntityType type = event.getRightClicked().getType(); - if((type == EntityType.VILLAGER || type == EntityType.MINECART) && PlayerVaults.IN_VAULT.containsKey(player.getName())) { + if ((type == EntityType.VILLAGER || type == EntityType.MINECART) && PlayerVaults.IN_VAULT.containsKey(player.getName())) { event.setCancelled(true); } } diff --git a/src/main/java/com/drtshock/playervaults/commands/VaultViewInfo.java b/src/main/java/com/drtshock/playervaults/commands/VaultViewInfo.java index 3425e74..9ea990f 100644 --- a/src/main/java/com/drtshock/playervaults/commands/VaultViewInfo.java +++ b/src/main/java/com/drtshock/playervaults/commands/VaultViewInfo.java @@ -18,6 +18,7 @@ public class VaultViewInfo { return this.i; } + @Override public String toString() { return this.s + " " + this.i; } diff --git a/src/main/java/com/drtshock/playervaults/util/VaultManager.java b/src/main/java/com/drtshock/playervaults/util/VaultManager.java index 5a57f47..04e4152 100644 --- a/src/main/java/com/drtshock/playervaults/util/VaultManager.java +++ b/src/main/java/com/drtshock/playervaults/util/VaultManager.java @@ -23,52 +23,49 @@ public class VaultManager { public VaultManager(PlayerVaults instance) { this.plugin = instance; } - private final String directory = "plugins" + File.separator + "PlayerVaults" + File.separator + "vaults"; /** - * Method to save player's vault. - * Serialize his inventory. - * Save the vaults.yml + * Method to save player's vault. Serialize his inventory. Save the + * vaults.yml + * * @param player - * @throws IOException + * @throws IOException */ public void saveVault(Inventory inv, String player, int number) throws IOException { YamlConfiguration yaml = playerVaultFile(player); yaml.set("vault" + number, null); List list = Serialization.toString(inv); String[] ser = list.toArray(new String[list.size()]); - for(int x = 0; x < ser.length; x++) { - if(!ser[x].equalsIgnoreCase("null")) + for (int x = 0; x < ser.length; x++) { + if (!ser[x].equalsIgnoreCase("null")) { yaml.set("vault" + number + "." + x, ser[x]); + } } saveFile(player, yaml); } /** - * Method to load player's vault. - * Deserialize his inventory - * + * Method to load player's vault. Deserialize his inventory + * * TODO: Check to see if the path exists before we get it! */ public void loadVault(Player player, String holder, int number) { VaultViewInfo info = new VaultViewInfo(holder, number); Inventory inv = null; - if(PlayerVaults.OPENINVENTORIES.containsKey(info.toString())) { + if (PlayerVaults.OPENINVENTORIES.containsKey(info.toString())) { inv = PlayerVaults.OPENINVENTORIES.get(info.toString()); } else { YamlConfiguration playerFile = playerVaultFile(holder); - if(playerFile.getConfigurationSection("vault" + number) == null) { + if (playerFile.getConfigurationSection("vault" + number) == null) { inv = Bukkit.createInventory(player, 54, ChatColor.DARK_RED + "Vault #" + String.valueOf(number)); - } - else { + } else { List data = new ArrayList(); - for(int x = 0; x < 54; x++) { + for (int x = 0; x < 54; x++) { String line = playerFile.getString("vault" + number + "." + x); - if(line != null) { + if (line != null) { data.add(line); - } - else { + } else { data.add("null"); } } @@ -80,8 +77,9 @@ public class VaultManager { } /** - * Gets an inventory without opening it. - * Used for dropping a players inventories on death. + * Gets an inventory without opening it. Used for dropping a players + * inventories on death. + * * @param player * @param number * @return the inventory @@ -89,7 +87,7 @@ public class VaultManager { public Inventory getVault(Player player, int number) { YamlConfiguration playerFile = playerVaultFile(player.getName()); List data = playerFile.getStringList("vault" + number); - if(data == null) { + if (data == null) { Inventory inv = Bukkit.createInventory(player, 54, ChatColor.GREEN + "Vault #" + String.valueOf(number)); return inv; } else { @@ -100,6 +98,7 @@ public class VaultManager { /** * Deletes a players vault. + * * @param sender * @param target * @param number @@ -109,35 +108,33 @@ public class VaultManager { String name = target.toLowerCase(); File file = new File(directory + File.separator + name.toLowerCase() + ".yml"); FileConfiguration playerFile = YamlConfiguration.loadConfiguration(file); - if(file.exists()) { + if (file.exists()) { playerFile.set("vault" + number, null); playerFile.save(file); } - if(sender.getName().equalsIgnoreCase(target)) { + if (sender.getName().equalsIgnoreCase(target)) { sender.sendMessage(Lang.TITLE.toString() + Lang.DELETE_VAULT.toString().replace("%v", String.valueOf(number))); - } - else { + } else { sender.sendMessage(Lang.TITLE.toString() + Lang.DELETE_OTHER_VAULT.toString().replace("%v", String.valueOf(number)).replace("%p", target)); } } /** - * Get the player's vault file. - * Create if doesn't exist. + * Get the player's vault file. Create if doesn't exist. + * * @param player * @return playerVaultFile file. */ public YamlConfiguration playerVaultFile(String player) { File folder = new File(directory); - if(!folder.exists()) { + if (!folder.exists()) { folder.mkdir(); } File file = new File(directory + File.separator + player.toLowerCase() + ".yml"); - if(!file.exists()) { + if (!file.exists()) { try { file.createNewFile(); - } catch(IOException e) { - e.printStackTrace(); + } catch (IOException e) { } } YamlConfiguration playerFile = YamlConfiguration.loadConfiguration(file); @@ -146,13 +143,14 @@ public class VaultManager { /** * Save the players vault file. + * * @param name * @param yaml * @throws IOException */ public void saveFile(String name, YamlConfiguration yaml) throws IOException { File file = new File(directory + File.separator + name.toLowerCase() + ".yml"); - if(file.exists()) { + if (file.exists()) { file.renameTo(new File(directory + File.separator + "backups" + File.separator + name.toLowerCase() + ".yml")); } yaml.save(file);