Advertisement
Evyatar12

GlobalInventoryPlugin.java

Jul 3rd, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. package com.thebukkitarea.globalInventory;
  2.  
  3. import java.util.Map.Entry;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Material;
  7. import org.bukkit.command.Command;
  8. import org.bukkit.command.CommandSender;
  9. import org.bukkit.configuration.MemorySection;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.inventory.Inventory;
  12. import org.bukkit.inventory.ItemStack;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14.  
  15. public class GlobalInventoryPlugin extends JavaPlugin {
  16.    
  17.     private Inventory global;
  18.    
  19.     @Override
  20.     public void onEnable() {
  21.         global = Bukkit.createInventory(null, 9*6, "Global Inventory");
  22.         loadInventoryFromConfig();
  23.     }
  24.    
  25.     @Override
  26.     public void onDisable() {
  27.         saveInventoryToConfig();
  28.     }
  29.    
  30.     @Override
  31.     public boolean onCommand(CommandSender sender, Command command,
  32.             String label, String[] args) {
  33.        
  34.         if (!(sender instanceof Player)) {
  35.             sender.sendMessage("You can't use this command.");
  36.             return true;
  37.         }
  38.        
  39.         Player player = (Player) sender;
  40.         player.openInventory(global);
  41.        
  42.         return true;
  43.     }
  44.    
  45.     private void clearConfig() {
  46.         for (String main_path : getConfig().getKeys(false)) {
  47.             getConfig().set(main_path, null);
  48.         }
  49.     }
  50.    
  51.     private void saveInventoryToConfig() {
  52.         clearConfig();
  53.        
  54.         for (int slot = 0; slot < global.getSize(); slot++) {
  55.             ItemStack stack = global.getItem(slot);
  56.            
  57.             if (stack == null || stack.getType() == Material.AIR) {
  58.                 continue;
  59.             }
  60.            
  61.             getConfig().set(String.valueOf(slot), stack.serialize());
  62.         }
  63.        
  64.         saveConfig();
  65.     }
  66.    
  67.     private void loadInventoryFromConfig() {
  68.         for (Entry<String, Object> entry : getConfig().getValues(false).entrySet()) {
  69.             int slot;
  70.            
  71.             try { slot = Integer.parseInt(entry.getKey()); }
  72.             catch (NumberFormatException ex) {
  73.                 getLogger().severe("An error has occurred while parsing slot " + entry.getKey());
  74.                 continue;
  75.             }
  76.            
  77.             if (slot + 1 >= global.getSize()) {
  78.                 getLogger().severe("Slot " + slot + " cannot be in the inventory");
  79.                 continue;
  80.             }
  81.            
  82.             MemorySection memorySection = (MemorySection) entry.getValue();
  83.            
  84.             ItemStack deserialized = ItemStack.deserialize(memorySection.getValues(false));
  85.            
  86.             global.setItem(slot, deserialized);
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement