Save items in my custom inventory?

Discussion in 'Plugin Development' started by jusjus112, Mar 14, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    jusjus112

    Is there an way to save items in an inventory? When im adding an item to my custom inventory, and i do /reload, then its gone! How do i save it, or something?
    Here is my inventory and my add item method:
    Code:java
    1. public static Inventory rides = Bukkit.createInventory(null, 9, ChatColor.AQUA + "" + ChatColor.BOLD + "Magic-Saga Navigator");
    2.  
    3. public void createRideToMenu(String name, Player p) {
    4. ItemStack chest1 = new ItemStack(Material.EMERALD_BLOCK);
    5. ItemMeta plate1 = chest1.getItemMeta();
    6. plate1.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "=====[ " + name + " ]=====");
    7. ArrayList<String> iron2 = new ArrayList<String>();
    8. iron2.add(ChatColor.GRAY + "");
    9. iron2.add(ChatColor.GRAY + "Click to go to: " + ChatColor.GOLD + name);
    10. plate1.setLore(iron2);
    11. chest1.setItemMeta(plate1);
    12.  
    13. rides.contains(chest1);
    14. rides.addItem(chest1);
    15.  
    16. FileConfiguration c = plugin.newConfigz;
    17. Location loc = p.getLocation();
    18. String world = p.getWorld().getName();
    19. int x, y, z;
    20. float yaw, pitch;
    21.  
    22. x = loc.getBlockX();
    23. y = loc.getBlockY();
    24. z = loc.getBlockZ();
    25. yaw = loc.getYaw();
    26. pitch = loc.getPitch();
    27.  
    28. c.set("Rides." + name + ".x", x);
    29. c.set("Rides." + name + ".y", y);
    30. c.set("Rides." + name + ".z", z);
    31. c.set("Rides." + name + ".yaw", yaw);
    32. c.set("Rides." + name + ".pitch", pitch);
    33. c.set("Rides." + name + ".world", world);
    34. plugin.saveNewConfig();
    35. }
     
  2. Offline

    Maulss

    inventory.getContents(); - Returns ItemStack[] of the inventory (excluding armor).
    inventory.getArmorContents(); - Returns ItemStack[] of the armor in the inventory.
     
  3. Offline

    jusjus112

    Maulss
    i mean, when i do /reload in the server. all the items are gone!
    But how do i save the items to the custom inventory, when i do /reload, the items stay in the inventory!
     
  4. Offline

    TehVoyager

    What maulss was saying is get all the items with those methods then save them
     
  5. Offline

    Dudemister1999

    I, personally, would create a special .yml file dedicated to storing inventories. Or multiple, depending on the amount of inventories you want to save.
     
  6. Offline

    jeussa

    Remember that when reloading or restarting your server, all data stored in list, hashmaps, etc. will be gone. So does your inventory

    Thats why you need to save your custom inventory using for example a .yml file in the datafolder of your plugin.

    Let me give you a copy of that which I use to save and load config files. It requires you to make 2 class files. One named SaveFile and the other named Saves. You won't need to edit the SaveFile.java

    SaveFile.java

    Code:java
    1. package net.invasioncraft.dvz.config;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.logging.Level;
    6. import java.util.logging.Logger;
    7.  
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class SaveFile {
    13.  
    14. private FileConfiguration saveData = null;
    15. private File saveFile = null;
    16. protected String fileName;
    17.  
    18. public SaveFile(String fileName)
    19. {
    20. this.fileName = fileName;
    21. }
    22.  
    23. public SaveFile(String fileName, FileConfiguration saveData){
    24. this.fileName = fileName;
    25. this.saveData = saveData;
    26. }
    27.  
    28. public boolean contains(String key)
    29. {
    30. return saveData.contains(key);
    31. }
    32.  
    33. public void set(String key, Object value)
    34. {
    35. saveData.set(key, value);
    36. save();
    37. }
    38.  
    39. public Object get(String key){
    40. if(saveData.contains(key))
    41. return saveData.get(key);
    42. return null;
    43. }
    44.  
    45. public FileConfiguration getRawData(){
    46. return saveData;
    47. }
    48.  
    49. public void purge()
    50. {
    51. saveData.set(".", null);
    52. save();
    53. }
    54.  
    55. public void load(File dataFolder)
    56. {
    57. if (saveFile == null)
    58. saveFile = new File(dataFolder, fileName);
    59.  
    60. saveData = YamlConfiguration.loadConfiguration(saveFile);
    61. }
    62.  
    63. public void save()
    64. {
    65. if (saveFile == null || saveData == null)
    66. return;
    67.  
    68. try
    69. {
    70. saveData.save(saveFile);
    71. } catch (IOException e)
    72. {
    73. Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Saving (" + saveFile + ") has failed.", e);
    74. }
    75. }
    76. }
    77.  




    Saves.java

    Code:java
    1. package net.invasioncraft.dvz.config;
    2.  
    3. import java.io.File;
    4. import java.util.HashMap;
    5. import java.util.Map;
    6.  
    7. public class Saves {
    8.  
    9. private static Map<String, SaveFile> saves;
    10.  
    11. public static void load(File dataFolder){
    12. if(saves == null){
    13. saves = new HashMap<String, SaveFile>();
    14. saves.put("database", new SaveFile("DataBase.yml"));
    15. }
    16. for(SaveFile s : saves.values()){
    17. s.load(dataFolder);
    18. }
    19. }
    20.  
    21. public static SaveFile getDataBase(){
    22. return saves.get("database");
    23. }
    24. }



    Next you need to make sure the existing files are being loaded. And if those files do not exist, you create them. Add this to your main class:

    Code:java
    1. public void onLoad(){
    2. //If the DataBase.yml has not been created yet, we copy it from the plugin resources
    3. if(!new File(getDataFolder().getPath() + "/DataBase.yml").exists()){
    4. saveResource("DataBase.yml", true); //Make sure you add a DataBase.yml next to your plugin.ml
    5. }
    6.  
    7. Saves.load(getDataFolder());
    8. }




    Now you can load and save data from config files. Such as an inventory. Use the following to load and save your inventory:

    Code:java
    1. public void onEnable(){
    2. try{
    3. //Try load the inventory from the database
    4. Inventory inventory = (inventory)Saves.getDataBase().get("inventory");
    5. }
    6. catch(Exception e){
    7. //If the plugin failed to load the inventory from the database (often when there is no saved inventory), we create a new one
    8. Inventory inventory = Bukkit.createInventory(null, 9, ChatColor.AQUA + "" + ChatColor.BOLD + "Magic-Saga Navigator");
    9. }
    10. }
    11.  
    12. public void onDisable(){
    13. Saves.getDataBase().set("inventory", inventory);
    14. }
     
  7. Offline

    Dudemister1999

    jeussa likes this.
  8. Offline

    jusjus112

    jeussa
    i have now this in my onEnable:
    Code:java
    1. public void onEnable(){
    2. try{
    3. //Try load the inventory from the database
    4. Inventory inventory = (Inventory)Saves.getDataBase().get("inventory");
    5. }
    6. catch(Exception e){
    7. //If the plugin failed to load the inventory from the database (often when there is no saved inventory), we create a new one
    8. Inventory inventory = Bukkit.createInventory(null, 9, ChatColor.AQUA + "" + ChatColor.BOLD + "Magic-Saga Navigator");
    9. }
    10. }
    11.  
    12. public void onDisable(){
    13. Saves.getDataBase().set("inventory", inventory);
    14. }
    15.  
    16. public void onLoad(){
    17. //If the DataBase.yml has not been created yet, we copy it from the plugin resources
    18. if(!new File(getDataFolder().getPath() + "/DataBase.yml").exists()){
    19. saveResource("DataBase.yml", true); //Make sure you add a DataBase.yml next to your plugin.ml
    20. }
    21.  
    22. Saves.load(getDataFolder());
    23. }

    And this is how i set the item:
    Code:java
    1. plugin.inventory.contains(chest1);
    2. plugin.inventory.addItem(chest1);

    But when i do /reload, it dont save the items!
    I have your 2 classes!
     
Thread Status:
Not open for further replies.

Share This Page