Add drop on death for vaults.

Allow configurable drop on death.
Abillity to drop all vaults.
Bypass permission playervaults.ignore.drops
This commit is contained in:
Trent Hensler
2013-03-18 23:21:06 -05:00
parent 03eedace9a
commit 3fc0eb8ba7
7 changed files with 156 additions and 13 deletions
@@ -0,0 +1,35 @@
package com.drtshock.playervaults.util;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import com.drtshock.playervaults.Main;
public class DropOnDeath {
public static Main plugin;
public DropOnDeath(Main instance) {
DropOnDeath.plugin = instance;
}
static VaultManager vm = new VaultManager(plugin);
/**
* Drops all items when a player dies.
* @param player
*/
public static void drop(Player player) {
Location loc = player.getLocation();
for(int count = 1; count <= Main.inventoriesToDrop; count++) {
Inventory inv = vm.getVault(player, count);
ItemStack[] stack = inv.getContents();
for(ItemStack is : stack) {
loc.getWorld().dropItemNaturally(loc, is);
}
}
}
}
@@ -17,7 +17,12 @@ public class EconomyOperations {
EconomyOperations.config = plugin.getConfig();
}
/**
* Have a player pay to open a vault.
* Returns true if successful. Otherwise false.
* @param player
* @return transaction success
*/
public static boolean payToOpen(Player player) {
if(!config.getBoolean("economy.enabled") || player.hasPermission("playervaults.free"))
return true;
@@ -31,7 +36,13 @@ public class EconomyOperations {
return false;
}
/**
* Have a player pay to make a vault.
* Returns true if successful. Otherwise false.
* @param player
* @return transaction success
*/
public static boolean payToMake(Player player) {
if(!config.getBoolean("economy.enabled") || player.hasPermission("playervaults.free"))
return true;
@@ -45,7 +56,13 @@ public class EconomyOperations {
return false;
}
/**
* Have a player get his money back when vault is deleted.
* Returns true if successful. Otherwise false.
* @param player
* @return transaction success.
*/
public static boolean refundOnDelete(Player player) {
if(!config.getBoolean("economy.enabled") || player.hasPermission("playervaults.free"))
return true;
@@ -55,6 +55,32 @@ public class VaultManager {
}
}
/**
* Gets an inventory without opening it.
* Used for dropping a players inventories on death.
* @param player
* @param number
* @return the inventory
*/
public Inventory getVault(Player player, int number) {
YamlConfiguration playerFile = playerVaultFile(player.getName());
String data = playerFile.getString("vault" + number);
if(data == null) {
Inventory inv = Bukkit.createInventory(player, 54, ChatColor.DARK_RED + "Vault #" + String.valueOf(number));
return inv;
} else {
Inventory inv = Serialization.fromBase64(data);
return inv;
}
}
/**
* Deletes a players vault.
* @param sender
* @param target
* @param number
* @throws IOException
*/
public void deleteVault(CommandSender sender, String target, int number) throws IOException {
String name = target.toLowerCase();
File file = new File(directory + File.separator + name.toLowerCase() + ".yml");
@@ -71,6 +97,12 @@ public class VaultManager {
}
}
/**
* 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()) {
@@ -88,6 +120,12 @@ public class VaultManager {
return playerFile;
}
/**
* 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");
yaml.save(file);