Update listeners and main class.
This commit is contained in:
@@ -6,7 +6,7 @@ import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
|
||||
import net.minecraft.server.v1_4_R1.NBTBase;
|
||||
import net.minecraft.server.v1_4_R1.NBTTagCompound;
|
||||
import net.minecraft.server.v1_4_R1.NBTTagList;
|
||||
@@ -16,18 +16,15 @@ import org.bukkit.craftbukkit.v1_4_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class Serialization
|
||||
{
|
||||
public class Serialization {
|
||||
|
||||
public static String toBase64(Inventory inventory)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
for (int i = 0; i < inventory.getSize(); i++) {
|
||||
NBTTagCompound outputObject = new NBTTagCompound();
|
||||
CraftItemStack craft = getCraftVersion(inventory.getItem(i));
|
||||
|
||||
@@ -44,18 +41,17 @@ public class Serialization
|
||||
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||
}
|
||||
|
||||
public static Inventory fromBase64(String data)
|
||||
{
|
||||
public static Inventory fromBase64(String data) {
|
||||
if(data.isEmpty())
|
||||
return null;
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||
NBTTagList itemList = (NBTTagList) NBTBase.b(new DataInputStream(inputStream));
|
||||
Inventory inventory = new CraftInventoryCustom(null, itemList.size());
|
||||
|
||||
for (int i = 0; i < itemList.size(); i++)
|
||||
{
|
||||
for (int i = 0; i < itemList.size(); i++) {
|
||||
NBTTagCompound inputObject = (NBTTagCompound) itemList.get(i);
|
||||
|
||||
if (!inputObject.isEmpty())
|
||||
{
|
||||
if (!inputObject.isEmpty()) {
|
||||
inventory.setItem(i, CraftItemStack.asCraftMirror(net.minecraft.server.v1_4_R1.ItemStack.createStack(inputObject)));
|
||||
}
|
||||
}
|
||||
@@ -64,10 +60,8 @@ public class Serialization
|
||||
return inventory;
|
||||
}
|
||||
|
||||
private static CraftItemStack getCraftVersion(ItemStack stack)
|
||||
{
|
||||
private static CraftItemStack getCraftVersion(ItemStack stack) {
|
||||
if (stack instanceof CraftItemStack)
|
||||
|
||||
return (CraftItemStack) stack;
|
||||
else if (stack != null)
|
||||
return CraftItemStack.asCraftCopy(stack);
|
||||
|
||||
@@ -14,14 +14,14 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
public class VaultManager
|
||||
{
|
||||
public class VaultManager {
|
||||
|
||||
public Main plugin;
|
||||
public VaultManager(Main instance) {
|
||||
this.plugin = instance;
|
||||
}
|
||||
String title;
|
||||
private static String directory = "plugins" + File.separator + "PlayerVaults" + File.separator + "vaults";
|
||||
|
||||
/**
|
||||
* Method to save player's vault.
|
||||
@@ -30,18 +30,15 @@ public class VaultManager
|
||||
* @param player
|
||||
* @throws IOException
|
||||
*/
|
||||
public void saveVault(Inventory inv, Player player, int number) throws IOException
|
||||
{
|
||||
if(plugin.inVault().containsKey(player.getName()))
|
||||
{
|
||||
public void saveVault(Inventory inv, Player player, int number) throws IOException {
|
||||
if(plugin.inVault().containsKey(player.getName())) {
|
||||
// Get the player's file and serialize the inventory.
|
||||
String ser = Serialization.toBase64(inv);
|
||||
YamlConfiguration file = plugin.playerVaultFile(player.getName());
|
||||
YamlConfiguration file = playerVaultFile(player.getName());
|
||||
|
||||
// Prepare to save D:
|
||||
file.set("vault" + number + "", ser);
|
||||
if(plugin.debugMode())
|
||||
{
|
||||
if(plugin.debugMode()) {
|
||||
plugin.getLogger().log(Level.INFO, "[PlayerVaults] Saved " + " " + number + " for " + player.getName());
|
||||
}
|
||||
}
|
||||
@@ -53,36 +50,47 @@ public class VaultManager
|
||||
*
|
||||
* TODO: Check to see if the path exists before we get it!
|
||||
*/
|
||||
public void loadVault(CommandSender sender, String target, int number)
|
||||
{
|
||||
YamlConfiguration playerFile = plugin.playerVaultFile(target);
|
||||
public void loadVault(CommandSender sender, String target, int number) {
|
||||
YamlConfiguration playerFile = playerVaultFile(target);
|
||||
String data = playerFile.getString("vault" + "" + number + "");
|
||||
Inventory inv = Serialization.fromBase64(data);
|
||||
Player player = (Player) sender;
|
||||
player.openInventory(inv);
|
||||
player.sendMessage(title + " Opening " + ChatColor.GREEN + " " + number);
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
public void deleteVault(CommandSender sender, String target, int number) throws IOException
|
||||
{
|
||||
public void deleteVault(CommandSender sender, String target, int number) throws IOException {
|
||||
String name = target.toLowerCase();
|
||||
File file = new File(plugin.getDataFolder() + File.separator + "vaults" + name + ".yml");
|
||||
File file = new File(directory + name + ".yml");
|
||||
FileConfiguration playerFile = YamlConfiguration.loadConfiguration(file);
|
||||
if(file.exists())
|
||||
{
|
||||
if(file.exists()) {
|
||||
ConfigurationSection section = playerFile.getConfigurationSection("vault" + number);
|
||||
section.set(null, null);
|
||||
sender.sendMessage(title + "Deleting " + ChatColor.GREEN + " " + number);
|
||||
playerFile.save(file);
|
||||
return;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
sender.sendMessage(title + " That doesn't exist!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public YamlConfiguration playerVaultFile(String player) {
|
||||
File folder = new File(directory);
|
||||
if(!folder.exists()) {
|
||||
folder.mkdir();
|
||||
}
|
||||
File file = new File(directory + File.separator + player.toLowerCase() + ".yml");
|
||||
if(!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
YamlConfiguration playerFile = YamlConfiguration.loadConfiguration(file);
|
||||
return playerFile;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user