Fix disable dupe. Consistent spacing.

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