Solved Load Inventory from Config

Discussion in 'Plugin Development' started by Der_Zauberer, Aug 20, 2019.

Thread Status:
Not open for further replies.
  1. Hello, (sorry for bad english) I have a problem with loading inventories from the config file. I have already seen several forum posts with the similar problem, but did not help ...

    Code:
    public static void getInventory(Player player, WorldGroup worldGroup) {
            String configarmor = "WorldGroups." + worldGroup.getName() + "." + player.getUniqueId() + ".Inventory.Armor";
            String configcontent = "WorldGroups." + worldGroup.getName() + "." + player.getUniqueId() + ".Inventory.Armor";
            try {
                player.getInventory().clear();
                ItemStack[] armorcontent = (ItemStack[]) config.get(configarmor);
                player.getInventory().setArmorContents(armorcontent);
                ItemStack[] content = (ItemStack[]) config.get(configcontent);
                player.getInventory().setContents(content);
            } catch (NullPointerException e) {
                saveInventory(player, worldGroup);
            }
        }
    (The path is definitely correct ) When I reload the server, I get a ClassCastException on the first execution in the line where the variable "configarmor" is declared (Line41) ...

    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'world' in plugin ServerSystem v1.1
      at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
      ...
      at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
    Caused by: java.lang.ClassCastException: [Lorg.bukkit.inventory.ItemStack; cannot be cast to java.util.List
      at serversystem.main.SaveConfig.getInventory(SaveConfig.java:41) ~[?:?]
      at serversystem.utilities.WorldGroup.onPlayerJoin(WorldGroup.java:33) ~[?:?]
      at serversystem.utilities.WorldGroupHandler.teleportPlayer(WorldGroupHandler.java:15) ~[?:?]
      at serversystem.commands.WorldCommand.onCommand(WorldCommand.java:29) ~[?:?]
      at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
      ... 17 more
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @Der_Zauberer You can do getItemStack()
    And I am assuming that you have a list of ItemStacks, not a single one. Think that that is the issue.
     
  3. @timtower Ok, but how can I get the ItemStack from a list?

    This is my config, with the saved inventories:

    Code:
    WorldGroups:
      world_the_end:
        8e1f0a29-7279-412d-a6a6-4266164d6a87:
          Inventory:
            Armor:
            - null
            - null
            - null
            - null
            Content:
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: DIAMOND_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: DIAMOND_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: DIAMOND_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: DIAMOND_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: DIAMOND_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: STONE_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: STONE_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: STONE_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: STONE_SWORD
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
      world:
        8e1f0a29-7279-412d-a6a6-4266164d6a87:
          Inventory:
            Armor:
            - null
            - null
            - null
            - null
            Content:
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: WOODEN_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: WOODEN_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: WOODEN_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: WOODEN_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: WOODEN_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: WOODEN_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: WOODEN_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: DIAMOND_SWORD
            - ==: org.bukkit.inventory.ItemStack
              v: 1976
              type: DIAMOND_SWORD
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
        26f832e1-c438-444d-a35a-fedb1327cca9:
          Inventory:
            Armor:
            - null
            - null
            - null
            - null
            Content:
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
     
  4. Online

    timtower Administrator Administrator Moderator

    @Der_Zauberer What I do in situations like this is storing the index as well. Then you can call getItemStack.
     
  5. It works! Thank you very much ;-)

    Code:
    public static void saveInventory(Player player, WorldGroup worldGroup) {
            String configarmor = "WorldGroups." + worldGroup.getName() + "." + player.getUniqueId() + ".Inventory.Armor";
            String configcontent = "WorldGroups." + worldGroup.getName() + "." + player.getUniqueId() + ".Inventory.Content";
            for (int i = 0; i < player.getInventory().getArmorContents().length; i++) {
                config.set(configarmor + "." + i, player.getInventory().getArmorContents()[i]);
            }
            for (int i = 0; i < player.getInventory().getContents().length; i++) {
                config.set(configcontent + "." + i, player.getInventory().getContents()[i]);
            }
            player.getInventory().clear();
            saveConfig();
        }
       
        public static void getInventory(Player player, WorldGroup worldGroup) {
            String configarmor = "WorldGroups." + worldGroup.getName() + "." + player.getUniqueId() + ".Inventory.Armor";
            String configcontent = "WorldGroups." + worldGroup.getName() + "." + player.getUniqueId() + ".Inventory.Content";
            player.getInventory().clear();
            ItemStack[] content = new ItemStack[4];
            for (int i = 0; i < 4; i++) {
                if(config.getItemStack(configarmor + "." + i) != null) {
                    content[i] = config.getItemStack(configarmor + "." + i);
                }
            }
            player.getInventory().setArmorContents(content);
            content = new ItemStack[41];
            for (int i = 0; i < 41; i++) {
                content[i] = config.getItemStack(configcontent + "." + i);
            }
            player.getInventory().setContents(content);   
        }
     
Thread Status:
Not open for further replies.

Share This Page