Mass changes, upgrade to v3.0.0
- Change storage method
- Add converter for both methods of storage
- Dropping upgrade support possibly in the future
- Change backup location
This commit is contained in:
@@ -4,18 +4,22 @@ package com.drtshock.playervaults;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.drtshock.playervaults.commands.Commands;
|
||||
import com.drtshock.playervaults.util.BackwardsCompatibility;
|
||||
import com.drtshock.playervaults.util.Lang;
|
||||
import com.drtshock.playervaults.util.Metrics;
|
||||
import com.drtshock.playervaults.util.Serialization;
|
||||
import com.drtshock.playervaults.util.Updater;
|
||||
|
||||
|
||||
@@ -33,10 +37,16 @@ public class Main extends JavaPlugin {
|
||||
public static FileConfiguration config;
|
||||
public static YamlConfiguration lang;
|
||||
public static File langFile;
|
||||
public static String directory = "plugins" + File.separator + "PlayerVaults" + File.separator + "vaults";
|
||||
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
transferVaults();
|
||||
try {
|
||||
transferVaults();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
loadLang();
|
||||
log = getServer().getLogger();
|
||||
getServer().getPluginManager().registerEvents(new Listeners(this), this);
|
||||
@@ -62,24 +72,28 @@ public class Main extends JavaPlugin {
|
||||
inventoriesToDrop = getConfig().getInt("drop-on-death.inventories");
|
||||
}
|
||||
|
||||
new File(directory + File.separator + "backups").mkdirs();
|
||||
config = getConfig();
|
||||
|
||||
}
|
||||
|
||||
public void transferVaults() {
|
||||
public void transferVaults() throws IOException {
|
||||
File f = new File(getDataFolder() + File.separator + "vaults.yml");
|
||||
if(f.exists() && !new File(getDataFolder() + File.separator + "vaults").exists()) {
|
||||
YamlConfiguration vaults = YamlConfiguration.loadConfiguration(f);
|
||||
for(String key:vaults.getKeys(false)) {
|
||||
YamlConfiguration newPerson = new YamlConfiguration();
|
||||
for(String key2:vaults.getConfigurationSection(key).getKeys(false)) {
|
||||
newPerson.set(key2, vaults.getString(key + "." + key2));
|
||||
}
|
||||
try {
|
||||
newPerson.save(new File(getDataFolder() + File.separator + "vaults" + File.separator + key + ".yml"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
for(String person:vaults.getKeys(false)) {
|
||||
YamlConfiguration yaml = new YamlConfiguration();
|
||||
for(String vault:vaults.getConfigurationSection(person).getKeys(false)) {
|
||||
String data = vaults.getString(person + "." + vault);
|
||||
Inventory inv = BackwardsCompatibility.pre2_0_0ToCurrent(data);
|
||||
List<String> 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"))
|
||||
yaml.set(vault + "." + x, ser[x]);
|
||||
}
|
||||
}
|
||||
yaml.save(new File(directory + File.separator + person + ".yml"));
|
||||
}
|
||||
getLogger().warning("Found old storage format used! Converting to new format!");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.drtshock.playervaults.util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInputStream;
|
||||
|
||||
import net.minecraft.server.v1_5_R1.NBTBase;
|
||||
import net.minecraft.server.v1_5_R1.NBTTagCompound;
|
||||
import net.minecraft.server.v1_5_R1.NBTTagList;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.craftbukkit.v1_5_R1.inventory.CraftInventoryCustom;
|
||||
import org.bukkit.craftbukkit.v1_5_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import com.drtshock.playervaults.Main;
|
||||
|
||||
public class BackwardsCompatibility {
|
||||
|
||||
Main plugin;
|
||||
|
||||
public BackwardsCompatibility(Main m) {
|
||||
this.plugin = m;
|
||||
}
|
||||
|
||||
public static Inventory pre2_0_0ToCurrent(String invString) {
|
||||
String[] serializedBlocks = invString.split(";");
|
||||
Inventory deserializedInventory = Bukkit.getServer().createInventory(null, 54);
|
||||
|
||||
for (int i = 1; i < serializedBlocks.length; i++) {
|
||||
String[] serializedBlock = serializedBlocks[i].split("#");
|
||||
int stackPosition = Integer.valueOf(serializedBlock[0]).intValue();
|
||||
|
||||
if (stackPosition < deserializedInventory.getSize()) {
|
||||
ItemStack is = null;
|
||||
Boolean createdItemStack = Boolean.valueOf(false);
|
||||
|
||||
String[] serializedItemStack = serializedBlock[1].split(":");
|
||||
for (String itemInfo : serializedItemStack) {
|
||||
String[] itemAttribute = itemInfo.split("@");
|
||||
if (itemAttribute[0].equals("t")) {
|
||||
is = new ItemStack(Material.getMaterial(Integer.valueOf(itemAttribute[1]).intValue()));
|
||||
createdItemStack = Boolean.valueOf(true);
|
||||
} else if ((itemAttribute[0].equals("d")) && (createdItemStack.booleanValue())) {
|
||||
is.setDurability(Short.valueOf(itemAttribute[1]).shortValue());
|
||||
} else if ((itemAttribute[0].equals("a")) && (createdItemStack.booleanValue())) {
|
||||
is.setAmount(Integer.valueOf(itemAttribute[1]).intValue());
|
||||
} else if ((itemAttribute[0].equals("e")) && (createdItemStack.booleanValue())) {
|
||||
is.addEnchantment(Enchantment.getById(Integer.valueOf(itemAttribute[1]).intValue()), Integer.valueOf(itemAttribute[2]).intValue());
|
||||
} else {
|
||||
if ((!itemAttribute[0].equals("i")) || (!createdItemStack.booleanValue()))
|
||||
continue;
|
||||
ItemMeta meta = is.getItemMeta();
|
||||
meta.setDisplayName(itemAttribute[1]);
|
||||
is.setItemMeta(meta);
|
||||
}
|
||||
}
|
||||
deserializedInventory.setItem(stackPosition, is);
|
||||
}
|
||||
}
|
||||
return deserializedInventory;
|
||||
}
|
||||
|
||||
public static Inventory pre3_0_0ToCurrent(String data) {
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||
NBTTagList itemList = (NBTTagList) NBTBase.b(new DataInputStream(inputStream));
|
||||
Inventory inventory = new CraftInventoryCustom(null, itemList.size(), ChatColor.DARK_RED + "Vault");
|
||||
|
||||
for (int i = 0; i < itemList.size(); i++) {
|
||||
NBTTagCompound inputObject = (NBTTagCompound) itemList.get(i);
|
||||
|
||||
if (!inputObject.isEmpty()) {
|
||||
inventory.setItem(i, CraftItemStack.asCraftMirror(net.minecraft.server.v1_5_R1.ItemStack.createStack(inputObject)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return inventory;
|
||||
}
|
||||
|
||||
private static CraftItemStack getCraftVersion(org.bukkit.inventory.ItemStack stack) {
|
||||
if ((stack instanceof CraftItemStack))
|
||||
return (CraftItemStack) stack;
|
||||
if (stack != null) {
|
||||
return CraftItemStack.asCraftCopy(stack);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +1,129 @@
|
||||
package com.drtshock.playervaults.util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
|
||||
import net.minecraft.server.v1_5_R1.NBTBase;
|
||||
import net.minecraft.server.v1_5_R1.NBTTagCompound;
|
||||
import net.minecraft.server.v1_5_R1.NBTTagList;
|
||||
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.craftbukkit.v1_5_R1.inventory.CraftInventoryCustom;
|
||||
import org.bukkit.craftbukkit.v1_5_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Serialization {
|
||||
|
||||
/*
|
||||
* All normal functions
|
||||
*/
|
||||
|
||||
public static String toBase64(Inventory inventory) {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
DataOutputStream dataOutput = new DataOutputStream(outputStream);
|
||||
NBTTagList itemList = new NBTTagList();
|
||||
|
||||
// Save every element in the list
|
||||
for (int i = 0; i < inventory.getSize(); i++) {
|
||||
NBTTagCompound outputObject = new NBTTagCompound();
|
||||
CraftItemStack craft = getCraftVersion(inventory.getItem(i));
|
||||
|
||||
// Convert the item stack to a NBT compound
|
||||
if (craft != null)
|
||||
CraftItemStack.asNMSCopy(craft).save(outputObject);
|
||||
itemList.add(outputObject);
|
||||
@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)));
|
||||
}
|
||||
|
||||
// Now save the list
|
||||
NBTBase.a(itemList, dataOutput);
|
||||
|
||||
// Serialize that array
|
||||
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Inventory fromBase64(String data, int number) {
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||
NBTTagList itemList = (NBTTagList) NBTBase.b(new DataInputStream(inputStream));
|
||||
Inventory inventory = new CraftInventoryCustom(null, itemList.size(), ChatColor.DARK_RED + "Vault #" + String.valueOf(number));
|
||||
for (int i = 0; i < itemList.size(); i++) {
|
||||
NBTTagCompound inputObject = (NBTTagCompound) itemList.get(i);
|
||||
|
||||
if (!inputObject.isEmpty()) {
|
||||
inventory.setItem(i, CraftItemStack.asCraftMirror(net.minecraft.server.v1_5_R1.ItemStack.createStack(inputObject)));
|
||||
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) {
|
||||
Inventory inv = Bukkit.createInventory(null, 54, ChatColor.RED + "Vault #" + number);
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize that array
|
||||
return inventory;
|
||||
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()) {
|
||||
// Check if any of its sub-maps are ConfigurationSerializable. They need to be done first.
|
||||
if (entry.getValue() instanceof Map && ((Map)entry.getValue()).containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
|
||||
entry.setValue(deserialize((Map)entry.getValue()));
|
||||
}
|
||||
}
|
||||
return ConfigurationSerialization.deserializeObject(map);
|
||||
}
|
||||
|
||||
private static CraftItemStack getCraftVersion(ItemStack stack) {
|
||||
if (stack instanceof CraftItemStack)
|
||||
return (CraftItemStack) stack;
|
||||
else if (stack != null)
|
||||
return CraftItemStack.asCraftCopy(stack);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* All old methods for transferring
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.drtshock.playervaults.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
@@ -30,9 +32,14 @@ public class VaultManager {
|
||||
* @throws IOException
|
||||
*/
|
||||
public void saveVault(Inventory inv, String player, int number) throws IOException {
|
||||
String ser = Serialization.toBase64(inv);
|
||||
YamlConfiguration yaml = playerVaultFile(player);
|
||||
yaml.set("vault" + number + "", ser);
|
||||
yaml.set("vault" + number, null);
|
||||
List<String> 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"))
|
||||
yaml.set("vault" + number + "." + x, ser[x]);
|
||||
}
|
||||
saveFile(player, yaml);
|
||||
}
|
||||
|
||||
@@ -43,15 +50,29 @@ public class VaultManager {
|
||||
* TODO: Check to see if the path exists before we get it!
|
||||
*/
|
||||
public void loadVault(Player player, String holder, int number) {
|
||||
Inventory inv = null;
|
||||
YamlConfiguration playerFile = playerVaultFile(holder);
|
||||
String data = playerFile.getString("vault" + number);
|
||||
if(data == null) {
|
||||
Inventory inv = Bukkit.createInventory(player, 54, ChatColor.DARK_RED + "Vault #" + String.valueOf(number));
|
||||
player.openInventory(inv);
|
||||
} else {
|
||||
Inventory inv = Serialization.fromBase64(data, number);
|
||||
player.openInventory(inv);
|
||||
if(!playerFile.getString("vault" + number, "~").equals("~") && playerFile.getString("vault" + number, "~").trim().endsWith("=")) {
|
||||
String data = playerFile.getString("vault" + number);
|
||||
inv = BackwardsCompatibility.pre3_0_0ToCurrent(data);
|
||||
}
|
||||
else if(playerFile.getConfigurationSection("vault" + number) == null) {
|
||||
inv = Bukkit.createInventory(player, 54, ChatColor.DARK_RED + "Vault #" + String.valueOf(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);
|
||||
}
|
||||
player.openInventory(inv);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,12 +84,12 @@ public class VaultManager {
|
||||
*/
|
||||
public Inventory getVault(Player player, int number) {
|
||||
YamlConfiguration playerFile = playerVaultFile(player.getName());
|
||||
String data = playerFile.getString("vault" + number);
|
||||
List<String> data = playerFile.getStringList("vault" + number);
|
||||
if(data == null) {
|
||||
Inventory inv = Bukkit.createInventory(player, 54, ChatColor.GREEN + "Vault #" + String.valueOf(number));
|
||||
return inv;
|
||||
} else {
|
||||
Inventory inv = Serialization.fromBase64(data, number);
|
||||
Inventory inv = Serialization.toInventory(data, number);
|
||||
return inv;
|
||||
}
|
||||
}
|
||||
@@ -128,7 +149,7 @@ public class VaultManager {
|
||||
public void saveFile(String name, YamlConfiguration yaml) throws IOException {
|
||||
File file = new File(directory + File.separator + name.toLowerCase() + ".yml");
|
||||
if (file.exists()) {
|
||||
file.renameTo(new File(directory + File.separator + name.toLowerCase() + ".yml.bak"));
|
||||
file.renameTo(new File(directory + File.separator + "backups" + File.separator + name.toLowerCase() + ".yml"));
|
||||
}
|
||||
yaml.save(file);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user