-1

I develope a Minecraft Bukkit Plugin and I want to save informations in a configuration file. I want to save an array in my yaml configuration file and load the data. This is my yaml file:

# Locations
Locations:
  - name: "Survival"
    position: 5
  - 
  - name: "PVP"
    position: 3

This is the code trying to load the array:

List<String> locations = config.getConfig().getStringList("Locations");
for(int i = 0; i < locations.size(); i++) {
    String[] location = locations.toArray(new String[0]);
    player.sendMessage("Name" + location[0] + ", Position" + location[1]);
}

But I don't get a chat message. If I send a message over for I get a message.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Can Spielt
  • 27
  • 8

1 Answers1

0

You could use this :

FileUtil.loadYamlFromResource("myLocationFile.yml", Location.class);

Location.getLocation("location1").getSomething()

And have this Location.class

public class Location implements Serializable {

    private static final long serialVersionUID = -2360488391508732184L;

    private Map<String, Map<String, Double>> locations = new HashMap<>();

    public Float getLocation(String location) throws LocationNotFoundException {
            return locations.get(location);
    }

    /**
     * @return the locations 
     */
    public Map<String, Map<String, Double>> getLocations() {
        return locations ;
    }

    public void setLocations (Map<String, Map<String, Double>> locations ) {
        this.locations = locations ;
    }
}

And in FileUtil.class

public static <T> T loadYamlFromResource(String resourcePath, Class<T> yamlClazz) {

    Yaml yaml = null;

    yaml = new Yaml(new CustomClassLoaderConstructor(yamlClazz.getClassLoader())); 

    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);

    return yaml.loadAs(in, yamlClazz);
}
Fundhor
  • 3,369
  • 1
  • 25
  • 47