Guest User

Untitled

a guest
Apr 4th, 2015
177
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.bizarreplatinum.AssassinsCreed;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map.Entry;
  5. import java.util.UUID;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.Material;
  10. import org.bukkit.command.Command;
  11. import org.bukkit.command.CommandSender;
  12. import org.bukkit.configuration.ConfigurationSection;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.event.EventHandler;
  15. import org.bukkit.event.Listener;
  16. import org.bukkit.event.inventory.InventoryType;
  17. import org.bukkit.event.player.PlayerJoinEvent;
  18. import org.bukkit.event.player.PlayerQuitEvent;
  19. import org.bukkit.inventory.Inventory;
  20. import org.bukkit.inventory.ItemStack;
  21. import org.bukkit.inventory.meta.ItemMeta;
  22. import org.bukkit.plugin.java.JavaPlugin;
  23.  
  24. public class Main extends JavaPlugin implements Listener{
  25.  
  26. private HashMap<UUID, Inventory> acinv = new HashMap<UUID, Inventory>();
  27.  
  28. @EventHandler
  29. public void onPlayerJoin(PlayerJoinEvent e) {
  30. Inventory inv = Bukkit.getServer().createInventory(e.getPlayer(), InventoryType.CHEST, "AC Inventory");
  31.  
  32. if(getConfig().contains("backpacks." + e.getPlayer().getUniqueId())) {
  33. for (String item : getConfig().getConfigurationSection("backpacks." + e.getPlayer().getUniqueId()).getKeys(false)) {
  34. inv.addItem(loadItem(getConfig().getConfigurationSection("backpacks." + e.getPlayer().getUniqueId() + "." + item)));
  35. }
  36. }
  37.  
  38. acinv.put(e.getPlayer().getUniqueId(), inv);
  39. }
  40.  
  41. @EventHandler
  42. public void onPlayerLeave(PlayerQuitEvent e) {
  43. if(!getConfig().contains("backpacks." + e.getPlayer().getUniqueId())) {
  44. getConfig().createSection("backpacks." + e.getPlayer().getUniqueId());
  45. }
  46.  
  47. char c = 'a';
  48. for (ItemStack itemstack : acinv.get(e.getPlayer().getUniqueId())) {
  49. if(itemstack != null) {
  50. saveItem(getConfig().createSection("backpacks." + e.getPlayer().getUniqueId() + "." + c++), itemstack);
  51. }
  52. }
  53.  
  54. saveConfig();
  55. }
  56.  
  57. public void onEnable() {
  58. Bukkit.getServer().getPluginManager().registerEvents(this, this);
  59. }
  60.  
  61. public void onDisable() {
  62. for(Entry<UUID, Inventory> entry : acinv.entrySet()) {
  63. if(!getConfig().contains("backpacks." + entry.getKey())) {
  64. getConfig().createSection("backpacks." + entry.getKey());
  65. }
  66.  
  67. char c = 'a';
  68. for (ItemStack itemstack : entry.getValue()) {
  69. if(itemstack != null) {
  70. saveItem(getConfig().createSection("backpacks." + entry.getKey() + "." + c++), itemstack);
  71. }
  72. }
  73.  
  74. saveConfig();
  75. }
  76. }
  77.  
  78. @Override
  79. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  80. if(!(sender instanceof Player)) {
  81. sender.sendMessage(ChatColor.DARK_RED + "Command is exclusive to players only.");
  82. return true;
  83. }
  84.  
  85. Player p = (Player) sender;
  86.  
  87. if(cmd.getName().equalsIgnoreCase("inv")) {
  88. p.openInventory(acinv.get(p.getUniqueId()));
  89. }
  90.  
  91. return true;
  92. }
  93.  
  94. private void saveItem(ConfigurationSection section, ItemStack itemstack) {
  95. section.set("type", itemstack.getType().name());
  96. section.set("amount", itemstack.getAmount());
  97. section.set("damage", itemstack.getDurability());
  98. section.set("displayName", itemstack.getItemMeta().getDisplayName());
  99. section.set("lore", itemstack.getItemMeta().getLore());
  100. }
  101.  
  102. private ItemStack loadItem(ConfigurationSection section) {
  103. ItemStack itemstack = new ItemStack(Material.valueOf(section.getString("type")), section.getInt("amount"));
  104. itemstack.setDurability((short) section.getInt("damage"));
  105. ItemMeta itemMeta = itemstack.getItemMeta();
  106. itemMeta.setDisplayName(section.getString("displayName"));
  107. itemMeta.setLore(section.getStringList("lore"));
  108. itemstack.setItemMeta(itemMeta);
  109.  
  110. return itemstack;
  111. }
  112. }
RAW Paste Data