Add configurable vault sizes. Recoded PR 17 to optimize permission checks and put things in correct classes. Updated to version 3.4.0-SNAPSHOT
This commit is contained in:
@@ -8,12 +8,14 @@ import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
/**
|
||||
* A class that contains a method to drop the contents of a player's vault when they die.
|
||||
* A class that contains a method to drop the contents of a player's vault when
|
||||
* they die.
|
||||
*/
|
||||
public class DropOnDeath {
|
||||
|
||||
/**
|
||||
* Drops all items when a player dies.
|
||||
*
|
||||
* @param player The player to drop the inventory of.
|
||||
*/
|
||||
public static void drop(Player player) {
|
||||
@@ -26,5 +28,4 @@ public class DropOnDeath {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
package com.drtshock.playervaults.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Fancy JSON serialization mostly by evilmidget38.
|
||||
* @author evilmidget38, gomeow
|
||||
*
|
||||
*/
|
||||
public class Serialization {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
Iterator<String> keys = object.keys();
|
||||
while (keys.hasNext()) {
|
||||
String key = (String) keys.next();
|
||||
map.put(key, fromJson(object.get(key)));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private static Object fromJson(Object json) throws JSONException {
|
||||
if (json == JSONObject.NULL) {
|
||||
return null;
|
||||
} else if (json instanceof JSONObject) {
|
||||
return toMap((JSONObject) json);
|
||||
} else if (json instanceof JSONArray) {
|
||||
return toList((JSONArray) json);
|
||||
} else {
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Object> toList(JSONArray array) throws JSONException {
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
list.add(fromJson(array.get(i)));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<String> toString(Inventory inv) {
|
||||
List<String> result = new ArrayList<String>();
|
||||
List<ConfigurationSerializable> items = new ArrayList<ConfigurationSerializable>();
|
||||
for (ItemStack is : inv.getContents()) {
|
||||
items.add(is);
|
||||
}
|
||||
for (ConfigurationSerializable cs : items) {
|
||||
if (cs == null) {
|
||||
result.add("null");
|
||||
} else {
|
||||
result.add(new JSONObject(serialize(cs)).toString());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Inventory toInventory(List<String> stringItems, int number) {
|
||||
VaultHolder holder = new VaultHolder(number);
|
||||
Inventory inv = Bukkit.createInventory(holder, 54, ChatColor.RED + "Vault #" + number);
|
||||
holder.setInventory(inv);
|
||||
List<ItemStack> contents = new ArrayList<ItemStack>();
|
||||
for (String piece : stringItems) {
|
||||
if (piece.equalsIgnoreCase("null")) {
|
||||
contents.add(null);
|
||||
} else {
|
||||
try {
|
||||
ItemStack item = (ItemStack) deserialize(toMap(new JSONObject(piece)));
|
||||
contents.add(item);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
ItemStack[] items = new ItemStack[contents.size()];
|
||||
for (int x = 0; x < contents.size(); x++)
|
||||
items[x] = contents.get(x);
|
||||
inv.setContents(items);
|
||||
return inv;
|
||||
}
|
||||
|
||||
public static Map<String, Object> serialize(ConfigurationSerializable cs) {
|
||||
Map<String, Object> serialized = recreateMap(cs.serialize());
|
||||
for (Entry<String, Object> entry : serialized.entrySet()) {
|
||||
if (entry.getValue() instanceof ConfigurationSerializable) {
|
||||
entry.setValue(serialize((ConfigurationSerializable) entry.getValue()));
|
||||
}
|
||||
}
|
||||
serialized.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(cs.getClass()));
|
||||
return serialized;
|
||||
}
|
||||
|
||||
public static Map<String, Object> recreateMap(Map<String, Object> original) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
for (Entry<String, Object> entry : original.entrySet()) {
|
||||
map.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static ConfigurationSerializable deserialize(Map<String, Object> map) {
|
||||
for (Entry<String, Object> entry : map.entrySet()) {
|
||||
if (entry.getValue() instanceof Map && ((Map) entry.getValue()).containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
|
||||
entry.setValue(deserialize((Map) entry.getValue()));
|
||||
}
|
||||
}
|
||||
return ConfigurationSerialization.deserializeObject(map);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.drtshock.playervaults.util;
|
||||
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
|
||||
/**
|
||||
* Represents a VaultHolder to assist in detection of Player Vaults from other
|
||||
* plugins.
|
||||
*/
|
||||
public class VaultHolder implements InventoryHolder {
|
||||
|
||||
private Inventory inventory;
|
||||
private int vaultNumber = 0;
|
||||
|
||||
/**
|
||||
* Creates a new vault holder
|
||||
*
|
||||
* @param vaultNumber the vault number this holder is using
|
||||
*/
|
||||
public VaultHolder(int vaultNumber) {
|
||||
this.vaultNumber = vaultNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the vault number this holder is currently using
|
||||
*
|
||||
* @return the vault number
|
||||
*/
|
||||
public int getVaultNumber() {
|
||||
return vaultNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the inventory this vault holder holds
|
||||
*
|
||||
* @param inventory the inventory, may be null
|
||||
*/
|
||||
public void setInventory(Inventory inventory) {
|
||||
this.inventory = inventory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Inventory getInventory() {
|
||||
return inventory;
|
||||
}
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
package com.drtshock.playervaults.util;
|
||||
|
||||
import com.drtshock.playervaults.PlayerVaults;
|
||||
import com.drtshock.playervaults.commands.VaultViewInfo;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
/**
|
||||
* A class for managing actual IO to the files, loading inventories, and saving
|
||||
* them.
|
||||
*/
|
||||
public class VaultManager {
|
||||
|
||||
public PlayerVaults plugin;
|
||||
|
||||
public VaultManager(PlayerVaults instance) {
|
||||
this.plugin = instance;
|
||||
}
|
||||
private final String directory = "plugins" + File.separator + "PlayerVaults" + File.separator + "vaults";
|
||||
|
||||
/**
|
||||
* Saves the inventory to the specified player and vault number.
|
||||
*
|
||||
* @param inventory The inventory to be saved.
|
||||
* @param player The player of whose file to save to.
|
||||
* @param number The vault number.
|
||||
* @throws IOException Uh oh!
|
||||
*/
|
||||
public void saveVault(Inventory inventory, String player, int number) throws IOException {
|
||||
int size = inventory.getSize();
|
||||
YamlConfiguration yaml = getPlayerVaultFile(player);
|
||||
if (size == 54) {
|
||||
yaml.set("vault" + number, null);
|
||||
} else {
|
||||
for (int x = 0; x < 27; x++) {
|
||||
yaml.set("vault" + number + "." + x, null);
|
||||
}
|
||||
}
|
||||
List<String> list = Serialization.toString(inventory);
|
||||
String[] ser = list.toArray(new String[list.size()]);
|
||||
for (int x = 0; x < ser.length; x++) {
|
||||
if (!ser[x].equalsIgnoreCase("null")) {
|
||||
yaml.set("vault" + number + "." + x, ser[x]);
|
||||
}
|
||||
}
|
||||
saveFile(player, yaml);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the player's vault and return it.
|
||||
*
|
||||
* @param holder The holder of the vault.
|
||||
* @param number The vault number.
|
||||
*/
|
||||
public Inventory loadVault(String holder, int number) {
|
||||
VaultViewInfo info = new VaultViewInfo(holder, number);
|
||||
Inventory inv = null;
|
||||
if (PlayerVaults.OPENINVENTORIES.containsKey(info.toString())) {
|
||||
inv = PlayerVaults.OPENINVENTORIES.get(info.toString());
|
||||
} else {
|
||||
YamlConfiguration playerFile = getPlayerVaultFile(holder);
|
||||
if (playerFile.getConfigurationSection("vault" + number) == null) {
|
||||
VaultHolder vaultHolder = new VaultHolder(number);
|
||||
inv = Bukkit.createInventory(vaultHolder, 54, ChatColor.DARK_RED + "Vault #" + String.valueOf(number));
|
||||
vaultHolder.setInventory(inv);
|
||||
} 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);
|
||||
}
|
||||
return inv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an inventory without storing references to it. Used for dropping a
|
||||
* players inventories on death.
|
||||
*
|
||||
* @param holder The holder of the vault.
|
||||
* @param number The vault number.
|
||||
* @return The inventory of the specified holder and vault number.
|
||||
*/
|
||||
public Inventory getVault(String holder, int number) {
|
||||
YamlConfiguration playerFile = getPlayerVaultFile(holder);
|
||||
List<String> data = playerFile.getStringList("vault" + number);
|
||||
if (data == null) {
|
||||
VaultHolder vaultHolder = new VaultHolder(number);
|
||||
Inventory inv = Bukkit.createInventory(vaultHolder, 54, ChatColor.GREEN + "Vault #" + String.valueOf(number));
|
||||
vaultHolder.setInventory(inv);
|
||||
return inv;
|
||||
} else {
|
||||
Inventory inv = Serialization.toInventory(data, number);
|
||||
return inv;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean vaultExists(String holder, int number) {
|
||||
YamlConfiguration playerFile = getPlayerVaultFile(holder);
|
||||
return playerFile.contains("vault" + number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a players vault.
|
||||
*
|
||||
* @param sender The sender of whom to send messages to.
|
||||
* @param holder The vault holder.
|
||||
* @param number The vault number.
|
||||
* @throws IOException Uh oh!
|
||||
*/
|
||||
public void deleteVault(CommandSender sender, String holder, int number) throws IOException {
|
||||
String name = holder.toLowerCase();
|
||||
File file = new File(directory + File.separator + name.toLowerCase() + ".yml");
|
||||
FileConfiguration playerFile = YamlConfiguration.loadConfiguration(file);
|
||||
if (file.exists()) {
|
||||
playerFile.set("vault" + number, null);
|
||||
playerFile.save(file);
|
||||
}
|
||||
if (sender.getName().equalsIgnoreCase(holder)) {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.DELETE_VAULT.toString().replace("%v", String.valueOf(number)));
|
||||
} else {
|
||||
sender.sendMessage(Lang.TITLE.toString() + Lang.DELETE_OTHER_VAULT.toString().replace("%v", String.valueOf(number)).replace("%p", holder));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the holder's vault file. Create if doesn't exist.
|
||||
*
|
||||
* @param holder The vault holder.
|
||||
* @return The holder's vault config file.
|
||||
*/
|
||||
public YamlConfiguration getPlayerVaultFile(String holder) {
|
||||
File folder = new File(directory);
|
||||
if (!folder.exists()) {
|
||||
folder.mkdir();
|
||||
}
|
||||
File file = new File(directory + File.separator + holder.toLowerCase() + ".yml");
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// Who cares?
|
||||
}
|
||||
}
|
||||
YamlConfiguration playerFile = YamlConfiguration.loadConfiguration(file);
|
||||
return playerFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the players vault file.
|
||||
*
|
||||
* @param holder The vault holder of whose file to save.
|
||||
* @param yaml The config to save.
|
||||
* @throws IOException Uh oh!
|
||||
*/
|
||||
public void saveFile(String holder, YamlConfiguration yaml) throws IOException {
|
||||
File file = new File(directory + File.separator + holder.toLowerCase() + ".yml");
|
||||
if (file.exists()) {
|
||||
file.renameTo(new File(directory + File.separator + "backups" + File.separator + holder.toLowerCase() + ".yml"));
|
||||
}
|
||||
yaml.save(file);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user