Cannot load config with custom class after reload

Discussion in 'Bukkit Help' started by TGamingStudio, Jun 1, 2020.

Thread Status:
Not open for further replies.
  1. Hello, I'm having an issue.
    Code:
    Show Spoiler
    Code:
    ArrayList<AuctionItem> items = (ArrayList) getConfig().get("items");
    if(items == null) items = new ArrayList();
    AuctionItem ai = new AuctionItem(p, i, Price);
    items.add(ai);
    getConfig().set("items", items);
    saveConfig();


    The class
    Show Spoiler
    Code:
    package tomdev.auctions;
    
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    public class AuctionItem {
        public String Player;
        public ItemStack Item;
        public int Price;
       
        public AuctionItem(Player player, ItemStack is, int price) {
            this.Player = player.getName();
            this.Item = is;
            this.Price = price;
        }
    }
    

    Saving works just fine:
    Show Spoiler
    Code:
    items:
    - !!tomdev.auctions.AuctionItem
      Item:
        ==: org.bukkit.inventory.ItemStack
        v: 2230
        type: SNOW
      Player: TGamingStudio
      Price: 50
    - !!tomdev.auctions.AuctionItem
      Item:
        ==: org.bukkit.inventory.ItemStack
        v: 2230
        type: GRASS_BLOCK
      Player: TGamingStudio
      Price: 50
    - !!tomdev.auctions.AuctionItem
      Item:
        ==: org.bukkit.inventory.ItemStack
        v: 2230
        type: OAK_LEAVES
      Player: TGamingStudio
      Price: 50  
    

    When i load it after adding the item before reloading, it also works And returns an class object back.

    But after I reload the server an error occurs:
    Code:
    org.bukkit.configuration.InvalidConfigurationException: could not determine a constructor for the tag tag:yaml.org,2002:tomdev.auctions.AuctionItem
     in 'string', line 2, column 3:
        - !!tomdev.auctions.AuctionItem
    
    What should I make so it will determine the constructor after reloading?

    Is it the right way of doing it? I found the way here: https://bukkit.org/threads/configuration-list-of-objects.424241/
    Is there another (better) way of doing this? I would like it to be the same format...
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. @timtower
    I'm not sure how to implement this. Should I make an ArrayList of maps?
    Always put data on the map like this?

    Code:
    items.put("Item", i);
    items.put("Player", p);
    items.put("Price",Price);
     
  4. Offline

    timtower Administrator Administrator Moderator

    @TGamingStudio Don't know exactly, I have my own config methods for handling configs.
    What I would do in your case is just store and load them manually.
     
  5. @timtower
    I don't understand what you mean by that, but I found a way.

    Creating Arraylist of maps
    Code:
    List<Map<String, Object>> items = (ArrayList) getConfig().get("items");
    if(items == null) items = new ArrayList<Map<String, Object>>();
    
    Then create new hashmap and add it to the list
    Code:
    Map<String, Object> item = new HashMap<String, Object>();
    item.put("Player", p.getName());
    item.put("Price", Price);
    item.put("Item", i);
    items.add(item);
    
    then saving it
    Code:
    getConfig().set("items", items);
    And it also works and loads after a reload

    Code:
    List<Map<String, Object>> items = (ArrayList) getConfig().get("items");
    if(items == null) items = new ArrayList<Map<String, Object>>();
    Inventory auctions = Bukkit.createInventory(null, 54, inventoryName);
    for(Map<String, Object> item : items) {
        ItemMeta m = ((ItemStack) item.get("Item")).getItemMeta();
        ...
    
    ¯\_(ツ)_/¯
     
  6. Offline

    timtower Administrator Administrator Moderator

    @TGamingStudio Call config.set and config.get yourself, don't serialize it with that.
     
Thread Status:
Not open for further replies.

Share This Page