Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package fr.aragone.framecraft;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6.  
  7. import org.bukkit.configuration.InvalidConfigurationException;
  8. import org.bukkit.configuration.file.FileConfiguration;
  9. import org.bukkit.configuration.file.YamlConfiguration;
  10. import org.bukkit.entity.Player;
  11.  
  12. public class InventorySerializer {
  13.    
  14.     private Main main;
  15.    
  16.     public InventorySerializer(Main main) {
  17.         this.main = main;
  18.     }
  19.  
  20.     public void serializeInventory(Player p) {
  21.        
  22.         File playerInventory = new File(main.getDataFolder() + File.separator + "InventorySerializer" + File.separator + p.getDisplayName() + ".inventory.yml");
  23.        
  24.         if(!playerInventory.exists()) {
  25.             try {
  26.                
  27.                 playerInventory.createNewFile();
  28.                 FileConfiguration configfile = YamlConfiguration.loadConfiguration(playerInventory);
  29.                
  30.                 configfile.set("inv.inv", p.getInventory().getContents());
  31.                 configfile.set("inv.armor", p.getInventory().getArmorContents());
  32.                 configfile.save(playerInventory);
  33.                
  34.             } catch (IOException e) {
  35.                 e.printStackTrace();
  36.             }
  37.            
  38.         }  
  39.     }
  40.    
  41.     public void deserializeInventory(Player p) {
  42.        
  43.         File playerInventory = new File(main.getDataFolder() + File.separator + "InventorySerializer" + File.separator + p.getDisplayName() + ".inventory.yml");
  44.        
  45.         if(playerInventory.exists()) {
  46.             try {
  47.                
  48.                 FileConfiguration configfile = YamlConfiguration.loadConfiguration(playerInventory);
  49.                 configfile.load(playerInventory);
  50.                 p.getInventory().addItem(configfile.getItemStack("inv.inv"));
  51.                
  52.             } catch (FileNotFoundException e) {
  53.                 e.printStackTrace();
  54.             } catch (IOException e) {
  55.                 e.printStackTrace();
  56.             } catch (InvalidConfigurationException e) {
  57.                 e.printStackTrace();
  58.             }
  59.         }
  60.        
  61.     }
  62.    
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement