Update listeners and main class.
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
# PlayerVaults by drsthock
|
# PlayerVaults by drsthock
|
||||||
check-update: true
|
check-update: true
|
||||||
debug-mode: false
|
debug-mode: false
|
||||||
|
|
||||||
|
# Worlds that players can't access their vaults.
|
||||||
|
# playervaults.worlds.bypass will override this (default to op).
|
||||||
disabled-worlds:
|
disabled-worlds:
|
||||||
- YourAwesomeWorld
|
- YourAwesomeWorld
|
||||||
- Toonville
|
- Toonville
|
||||||
@@ -23,12 +23,10 @@ import org.bukkit.event.player.PlayerQuitEvent;
|
|||||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
|
|
||||||
public class Listeners implements Listener
|
public class Listeners implements Listener {
|
||||||
{
|
|
||||||
|
|
||||||
public Main plugin;
|
public Main plugin;
|
||||||
public Listeners(Main instance)
|
public Listeners(Main instance) {
|
||||||
{
|
|
||||||
this.plugin = instance;
|
this.plugin = instance;
|
||||||
}
|
}
|
||||||
VaultManager vm = new VaultManager(plugin);
|
VaultManager vm = new VaultManager(plugin);
|
||||||
@@ -37,8 +35,7 @@ public class Listeners implements Listener
|
|||||||
|
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onQuit(PlayerQuitEvent event)
|
public void onQuit(PlayerQuitEvent event) {
|
||||||
{
|
|
||||||
if(commands.inVault.containsKey(event.getPlayer().getName())) {
|
if(commands.inVault.containsKey(event.getPlayer().getName())) {
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
Inventory inv = player.getOpenInventory().getTopInventory();
|
Inventory inv = player.getOpenInventory().getTopInventory();
|
||||||
@@ -51,10 +48,9 @@ public class Listeners implements Listener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onJoin(PlayerJoinEvent event)
|
public void onJoin(PlayerJoinEvent event) {
|
||||||
{
|
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
plugin.playerVaultFile(player.getName());
|
vm.playerVaultFile(player.getName());
|
||||||
if(player.isOp() && Main.update)
|
if(player.isOp() && Main.update)
|
||||||
{
|
{
|
||||||
player.sendMessage(ChatColor.GREEN + "Version " + Main.name + " of PlayerVaults is up for download!");
|
player.sendMessage(ChatColor.GREEN + "Version " + Main.name + " of PlayerVaults is up for download!");
|
||||||
@@ -107,8 +103,7 @@ public class Listeners implements Listener
|
|||||||
public void onClose(InventoryCloseEvent event) {
|
public void onClose(InventoryCloseEvent event) {
|
||||||
if(commands.inVault.containsKey(event.getPlayer().getName())) {
|
if(commands.inVault.containsKey(event.getPlayer().getName())) {
|
||||||
HumanEntity he = event.getPlayer();
|
HumanEntity he = event.getPlayer();
|
||||||
if(he instanceof Player)
|
if(he instanceof Player) {
|
||||||
{
|
|
||||||
Player player = (Player) he;
|
Player player = (Player) he;
|
||||||
Inventory inv = player.getOpenInventory().getTopInventory();
|
Inventory inv = player.getOpenInventory().getTopInventory();
|
||||||
int number = Integer.parseInt(commands.inVault.get(player.getName()));
|
int number = Integer.parseInt(commands.inVault.get(player.getName()));
|
||||||
@@ -128,11 +123,9 @@ public class Listeners implements Listener
|
|||||||
* @param event
|
* @param event
|
||||||
*/
|
*/
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onInteract(PlayerInteractEvent event)
|
public void onInteract(PlayerInteractEvent event) {
|
||||||
{
|
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
if(commands.inVault.containsKey(player.getName()) && event.getAction() == Action.RIGHT_CLICK_BLOCK)
|
if(commands.inVault.containsKey(player.getName()) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
||||||
{
|
|
||||||
Block block = event.getClickedBlock();
|
Block block = event.getClickedBlock();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -142,13 +135,9 @@ public class Listeners implements Listener
|
|||||||
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
|
||||||
/**
|
|
||||||
* Storage_minecart and Powered minecart aren't blocks ;)- added to EntityInteractEvent
|
|
||||||
*/
|
|
||||||
|| block.getType() == Material.BURNING_FURNACE
|
|| block.getType() == Material.BURNING_FURNACE
|
||||||
|| block.getType() == Material.BREWING_STAND
|
|| block.getType() == Material.BREWING_STAND
|
||||||
|| block.getType() == Material.BEACON)
|
|| block.getType() == Material.BEACON) {
|
||||||
{
|
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,12 +149,10 @@ public class Listeners implements Listener
|
|||||||
* @param event
|
* @param event
|
||||||
*/
|
*/
|
||||||
@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) && commands.inVault.containsKey(player.getName()))
|
if((type == EntityType.VILLAGER||type==EntityType.MINECART) && commands.inVault.containsKey(player.getName())) {
|
||||||
{
|
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,8 +27,7 @@ public class Main extends JavaPlugin {
|
|||||||
public static String name = "";
|
public static String name = "";
|
||||||
Commands commands = new Commands();
|
Commands commands = new Commands();
|
||||||
|
|
||||||
public void onEnable()
|
public void onEnable() {
|
||||||
{
|
|
||||||
log = getServer().getLogger();
|
log = getServer().getLogger();
|
||||||
PluginManager pm = getServer().getPluginManager();
|
PluginManager pm = getServer().getPluginManager();
|
||||||
pm.registerEvents(new Listeners(this), this);
|
pm.registerEvents(new Listeners(this), this);
|
||||||
@@ -37,42 +36,34 @@ public class Main extends JavaPlugin {
|
|||||||
loadLang();
|
loadLang();
|
||||||
startMetrics();
|
startMetrics();
|
||||||
|
|
||||||
if(updateCheck())
|
if(updateCheck()) {
|
||||||
{
|
|
||||||
Updater updater = new Updater(this, "playervaults", this.getFile(), Updater.UpdateType.NO_DOWNLOAD, false);
|
Updater updater = new Updater(this, "playervaults", this.getFile(), Updater.UpdateType.NO_DOWNLOAD, false);
|
||||||
update = updater.getResult() == Updater.UpdateResult.UPDATE_AVAILABLE;
|
update = updater.getResult() == Updater.UpdateResult.UPDATE_AVAILABLE;
|
||||||
name = updater.getLatestVersionString();
|
name = updater.getLatestVersionString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDisable()
|
public void onDisable() {
|
||||||
{
|
|
||||||
//saveData();
|
//saveData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void startMetrics()
|
public void startMetrics() {
|
||||||
{
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
Metrics metrics = new Metrics(this);
|
Metrics metrics = new Metrics(this);
|
||||||
metrics.start();
|
metrics.start();
|
||||||
}
|
} catch (IOException localIOException) {
|
||||||
catch (IOException localIOException)
|
|
||||||
{
|
|
||||||
localIOException.printStackTrace();
|
localIOException.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadConfig()
|
public void loadConfig() {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Check to see if there's a config.
|
* Check to see if there's a config.
|
||||||
* If not then create a new one.
|
* If not then create a new one.
|
||||||
*/
|
*/
|
||||||
File config = new File(getDataFolder() + File.separator + "config.yml");
|
File config = new File(getDataFolder() + File.separator + "config.yml");
|
||||||
if(!config.exists())
|
if(!config.exists()) {
|
||||||
{
|
|
||||||
try{
|
try{
|
||||||
getDataFolder().mkdir();
|
getDataFolder().mkdir();
|
||||||
config.createNewFile();
|
config.createNewFile();
|
||||||
@@ -99,15 +90,13 @@ public class Main extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadLang()
|
public void loadLang() {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Check to see if there's a config.
|
* Check to see if there's a config.
|
||||||
* If not then create a new one.
|
* If not then create a new one.
|
||||||
*/
|
*/
|
||||||
File lang = new File(getDataFolder() + File.separator + "lang.yml");
|
File lang = new File(getDataFolder() + File.separator + "lang.yml");
|
||||||
if(!lang.exists())
|
if(!lang.exists()) {
|
||||||
{
|
|
||||||
try{
|
try{
|
||||||
getDataFolder().mkdir();
|
getDataFolder().mkdir();
|
||||||
lang.createNewFile();
|
lang.createNewFile();
|
||||||
@@ -140,23 +129,6 @@ public class Main extends JavaPlugin {
|
|||||||
return lang;
|
return lang;
|
||||||
}
|
}
|
||||||
|
|
||||||
public YamlConfiguration playerVaultFile(String player) {
|
|
||||||
File folder = new File(getDataFolder() + File.separator + "vaults");
|
|
||||||
if(!folder.exists()) {
|
|
||||||
folder.mkdir();
|
|
||||||
}
|
|
||||||
File file = new File(getDataFolder() + File.separator + "vaults" + File.separator + player.toLowerCase() + ".yml");
|
|
||||||
if(!file.exists()) {
|
|
||||||
try {
|
|
||||||
file.createNewFile();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
YamlConfiguration playerFile = YamlConfiguration.loadConfiguration(file);
|
|
||||||
return playerFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Methods to get values from the config.
|
* Methods to get values from the config.
|
||||||
* public so any class / plugin can get them.
|
* public so any class / plugin can get them.
|
||||||
|
|||||||
@@ -16,18 +16,15 @@ import org.bukkit.craftbukkit.v1_4_R1.inventory.CraftItemStack;
|
|||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
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();
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
DataOutputStream dataOutput = new DataOutputStream(outputStream);
|
DataOutputStream dataOutput = new DataOutputStream(outputStream);
|
||||||
NBTTagList itemList = new NBTTagList();
|
NBTTagList itemList = new NBTTagList();
|
||||||
|
|
||||||
// Save every element in the list
|
// 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();
|
NBTTagCompound outputObject = new NBTTagCompound();
|
||||||
CraftItemStack craft = getCraftVersion(inventory.getItem(i));
|
CraftItemStack craft = getCraftVersion(inventory.getItem(i));
|
||||||
|
|
||||||
@@ -44,18 +41,17 @@ public class Serialization
|
|||||||
return Base64Coder.encodeLines(outputStream.toByteArray());
|
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));
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||||
NBTTagList itemList = (NBTTagList) NBTBase.b(new DataInputStream(inputStream));
|
NBTTagList itemList = (NBTTagList) NBTBase.b(new DataInputStream(inputStream));
|
||||||
Inventory inventory = new CraftInventoryCustom(null, itemList.size());
|
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);
|
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)));
|
inventory.setItem(i, CraftItemStack.asCraftMirror(net.minecraft.server.v1_4_R1.ItemStack.createStack(inputObject)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,10 +60,8 @@ public class Serialization
|
|||||||
return inventory;
|
return inventory;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CraftItemStack getCraftVersion(ItemStack stack)
|
private static CraftItemStack getCraftVersion(ItemStack stack) {
|
||||||
{
|
|
||||||
if (stack instanceof CraftItemStack)
|
if (stack instanceof CraftItemStack)
|
||||||
|
|
||||||
return (CraftItemStack) stack;
|
return (CraftItemStack) stack;
|
||||||
else if (stack != null)
|
else if (stack != null)
|
||||||
return CraftItemStack.asCraftCopy(stack);
|
return CraftItemStack.asCraftCopy(stack);
|
||||||
|
|||||||
@@ -14,14 +14,14 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
|
|
||||||
public class VaultManager
|
public class VaultManager {
|
||||||
{
|
|
||||||
|
|
||||||
public Main plugin;
|
public Main plugin;
|
||||||
public VaultManager(Main instance) {
|
public VaultManager(Main instance) {
|
||||||
this.plugin = instance;
|
this.plugin = instance;
|
||||||
}
|
}
|
||||||
String title;
|
String title;
|
||||||
|
private static String directory = "plugins" + File.separator + "PlayerVaults" + File.separator + "vaults";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to save player's vault.
|
* Method to save player's vault.
|
||||||
@@ -30,18 +30,15 @@ public class VaultManager
|
|||||||
* @param player
|
* @param player
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void saveVault(Inventory inv, Player player, int number) throws IOException
|
public void saveVault(Inventory inv, Player player, int number) throws IOException {
|
||||||
{
|
if(plugin.inVault().containsKey(player.getName())) {
|
||||||
if(plugin.inVault().containsKey(player.getName()))
|
|
||||||
{
|
|
||||||
// Get the player's file and serialize the inventory.
|
// Get the player's file and serialize the inventory.
|
||||||
String ser = Serialization.toBase64(inv);
|
String ser = Serialization.toBase64(inv);
|
||||||
YamlConfiguration file = plugin.playerVaultFile(player.getName());
|
YamlConfiguration file = playerVaultFile(player.getName());
|
||||||
|
|
||||||
// Prepare to save D:
|
// Prepare to save D:
|
||||||
file.set("vault" + number + "", ser);
|
file.set("vault" + number + "", ser);
|
||||||
if(plugin.debugMode())
|
if(plugin.debugMode()) {
|
||||||
{
|
|
||||||
plugin.getLogger().log(Level.INFO, "[PlayerVaults] Saved " + " " + number + " for " + player.getName());
|
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!
|
* TODO: Check to see if the path exists before we get it!
|
||||||
*/
|
*/
|
||||||
public void loadVault(CommandSender sender, String target, int number)
|
public void loadVault(CommandSender sender, String target, int number) {
|
||||||
{
|
YamlConfiguration playerFile = playerVaultFile(target);
|
||||||
YamlConfiguration playerFile = plugin.playerVaultFile(target);
|
|
||||||
String data = playerFile.getString("vault" + "" + number + "");
|
String data = playerFile.getString("vault" + "" + number + "");
|
||||||
Inventory inv = Serialization.fromBase64(data);
|
Inventory inv = Serialization.fromBase64(data);
|
||||||
Player player = (Player) sender;
|
Player player = (Player) sender;
|
||||||
player.openInventory(inv);
|
player.openInventory(inv);
|
||||||
player.sendMessage(title + " Opening " + ChatColor.GREEN + " " + number);
|
player.sendMessage(title + " Opening " + ChatColor.GREEN + " " + number);
|
||||||
return;
|
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();
|
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);
|
FileConfiguration playerFile = YamlConfiguration.loadConfiguration(file);
|
||||||
if(file.exists())
|
if(file.exists()) {
|
||||||
{
|
|
||||||
ConfigurationSection section = playerFile.getConfigurationSection("vault" + number);
|
ConfigurationSection section = playerFile.getConfigurationSection("vault" + number);
|
||||||
section.set(null, null);
|
section.set(null, null);
|
||||||
sender.sendMessage(title + "Deleting " + ChatColor.GREEN + " " + number);
|
sender.sendMessage(title + "Deleting " + ChatColor.GREEN + " " + number);
|
||||||
playerFile.save(file);
|
playerFile.save(file);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
sender.sendMessage(title + " That doesn't exist!");
|
sender.sendMessage(title + " That doesn't exist!");
|
||||||
return;
|
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