problem with saving custom inventory

Discussion in 'Plugin Development' started by Dalcart, Dec 14, 2019.

Thread Status:
Not open for further replies.
  1. Hi,

    I actualy got a probleme when i save my custum inventory in yml.
    When the player leave the game, i lose the save of item 0, 1, 2 ,3 and 4.

    This is just after close custom inventory
    Code:
    Inventaire:
      '0':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '1':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '2':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '3':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '4':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '5':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '6':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '7':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '8':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
    
    
    And this just after i leave the game
    Code:
    Inventaire:
      '5':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '6':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '7':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
      '8':
        ==: org.bukkit.inventory.ItemStack
        v: 1976
        type: ANDESITE_SLAB
    
    If i don't leave the game, the YML is ok and if i use more of 9 slot in custom inventory the probléme is the same.

    This is my code when i save all items
    Code:
    public class use implements Listener {
       private static Main main;
           @SuppressWarnings("static-access")
           public use (Main main) {
               this.main = main;
           }
       
           //Ouverture Backpack
       @EventHandler
               public void onInteract(PlayerInteractEvent sac) throws IOException {
    
           ItemStack Item = Main.Item();
           Player Player = sac.getPlayer();
           @SuppressWarnings("unused")
           Action action = sac.getAction();
           @SuppressWarnings("unused")
           ItemStack it = sac.getItem();
           String name = Player.getName();
       
               if (Item.isSimilar(Player.getInventory().getItemInMainHand())) {
                   Player.sendMessage("Utilisation !");
                   Inventory inv = Bukkit.createInventory(null, 9, "Binvenue dans votre sac "+ name);
                   Player.openInventory(inv);
                   File playerYml = new File(main.getDataFolder()+"/Inventaire", name + ".yml");
                   FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerYml);
                   for (int i = 0; i < inv.getSize();i++) {
                       ItemStack item = (ItemStack) playerConfig.get("Inventaire."+i,inv.getItem(i));
                       inv.setItem(i, item);
                       playerConfig.save(playerYml);
                   }
               }
               
       }
    
           // Fermeture Backpack
       @EventHandler
               public void InventoryCloseEvent (InventoryCloseEvent  save) throws IOException, InvalidConfigurationException {
           //Initialisation
               Player player = (Player) save.getPlayer();
               String name = player.getName();
               player.sendMessage("Sauvegarde !");
               Inventory inv = save.getInventory();
       
           //Boucle sauvegarde de l'inventaire et YML
               for (int i = 0; i < inv.getSize();i++) {
                   File playerYml = new File(main.getDataFolder()+"/Inventaire", name + ".yml");
                       if (playerYml.exists()) {       
                           FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerYml);
                       }
                       else {
                           if(createPlayerYml(player, playerYml, true)) {
                               System.out.println("Created YML file for: " + player.getName());
                           }
                           else {
                               System.out.println("Failed to create YML file for: " + player.getName());
                           }
                       }
                   FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerYml);
               
           //Configuration
               playerConfig.set("INFO.UUID",player.getUniqueId().toString());
               playerConfig.set("INFO.Last_use",use.Date().toString());
               playerConfig.set("Inventaire."+i,inv.getItem(i));
               playerConfig.save(playerYml);
            }
           
       }
    
    So i would like to keep all item of the custom inventory when player leave the server.
    Aand i don't have join or leave event in my plugin.

    Sorry for my bad English :/ but thanks for your time :).
     
    Last edited: Dec 14, 2019
  2. Offline

    Strahan

    A lot of things to unpack here. First, don't suppress things. If the compiler is trying to tell you something, it isn't for shits n' giggles. Fix the problems it is complaining about. Second, don't use static unnecessarily. There is no reason for your plugin instance to be static, and fixing that would fix one of the things you are suppressing. Third, obey naming conventions. Variables and methods shouldn't be PascalCase, they should be camelCase. Classes are PascalCase. Fourth, your close event is performing file operations for each iteration of the inventory. I doubt that's really what you want. Fifth, you didn't give us the code for the createPlayerYml method which would be rather important given that it's what is apparently generating the data to save.
     
  3. Thanks for your reply,

    This is createPlayerYml method
    Code:
    public static Boolean createPlayerYml(Player player, File playerYml, Boolean online) {
      try {
      playerYml.createNewFile();
      } catch (IOException e) {
      return false;  
      }
    
    When the player leave the game, i didn't got error in the consol, i just see I lose data in yml.
     
  4. Resolve can you close pls :D
     
    Last edited: Dec 20, 2019
Thread Status:
Not open for further replies.

Share This Page