Solved Retrieving multiple Objects in config.yml

Discussion in 'Spigot Plugin Development' started by DecisionsYT, Aug 7, 2018.

  1. In my plugin, I have created an Object called SmelterObject.
    Code (Text):
    public class SmelterObject {

        Location location;
        String ownerUUID;
        int efficiencyLevel, durabilityLevel, fortuneLevel;
        boolean isLocked;

        public Location getLocation() {
            return location;
        }

        public void setLocation(Location location) {
            this.location = location;
        }

        public String getOwnerUUID() {
            return ownerUUID;
        }

        public void setOwnerUUID(String ownerUUID) {
            this.ownerUUID = ownerUUID;
        }

        public int getEfficiencyLevel() {
            return efficiencyLevel;
        }

        public void setEfficiencyLevel(int efficiencyLevel) {
            this.efficiencyLevel = efficiencyLevel;
        }

        public int getDurabilityLevel() {
            return durabilityLevel;
        }

        public void setDurabilityLevel(int durabilityLevel) {
            this.durabilityLevel = durabilityLevel;
        }

        public int getFortuneLevel() {
            return fortuneLevel;
        }

        public void setFortuneLevel(int fortuneLevel) {
            this.fortuneLevel = fortuneLevel;
        }

        public boolean isLocked() {
            return isLocked;
        }

        public void setLocked(boolean locked) {
            isLocked = locked;
        }
    }
    I have created 3 of these objects, all with different values for the variables. I have added these 3 objects to an ArrayList and then added that to my config.yml.

    The output in my config.yml is:
    Code (YAML):
    smelters:
    - !!com.idleappsinc.furnacespro.SmelterObject
      durabilityLevel
    : 1
      efficiencyLevel
    : 1
      fortuneLevel
    : 1
      location
    :
        ==
    : org.bukkit.Location
        world
    : world
        x
    : -230.0
        y
    : 68.0
        z
    : 339.0
        pitch
    : 0.0
        yaw
    : 0.0
      locked
    : false
      ownerUUID
    : 1443b61a-6898-452d-acf2-8b50d802c1c8
    - !!com.idleappsinc.furnacespro.SmelterObject
      durabilityLevel
    : 2
      efficiencyLevel
    : 2
      fortuneLevel
    : 2
      location
    :
        ==
    : org.bukkit.Location
        world
    : world
        x
    : -230.0
        y
    : 68.0
        z
    : 339.0
        pitch
    : 0.0
        yaw
    : 0.0
      locked
    : true
      ownerUUID
    : 1443b61a-6898-452d-acf2-8b50d802c1c8
    - !!com.idleappsinc.furnacespro.SmelterObject
      durabilityLevel
    : 3
      efficiencyLevel
    : 3
      fortuneLevel
    : 3
      location
    :
        ==
    : org.bukkit.Location
        world
    : world
        x
    : -230.0
        y
    : 68.0
        z
    : 339.0
        pitch
    : 0.0
        yaw
    : 0.0
      locked
    : true
      ownerUUID
    : 1443b61a-6898-452d-acf2-8b50d802c1c8
    Which all looks normal to me.

    I now need to know how I can retrieve an ArrayList of these objects. I have tried what I though would work, which is:
    Code (Text):
    ArrayList<SmelterObject> retrievedSmeltersArrayList = (ArrayList<SmelterObject>) plugin.getConfig().getStringList("smelters");
    However, this produces an error of: https://prnt.sc/kfsj24

    Any help is greatly appreciated!
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    I believe getStringList() actually just casts to List<String> (or attempts to) which your object is not. Instead, use:
    Code (Text):
    List<SmelterObject> retrievedSmeltersList = (List<SmelterObject>) plugin.getConfig().get("smelters");
    Also, I believe you need to import it into an ArrayList since it won't return a List directly (I may be wrong).
     
  3. Use
    Code (Text):
    ArrayList<SmelterObject> retrievedSmeltersArrayList = (ArrayList<SmelterObject>) plugin.getConfig().get("smelters");
    Instead. You were original getting it as a string list which it isn't. There for it doesn't work as it cannot convert it.
    If that doesn't work you can try .getList("smelters");

    Damn @ScarabCoder getting in a minute before me
     
  4. *FACEPALM*!!!! Thank you very much. I can't believe I didn't see that. :p
     
    • Funny Funny x 1