3

I am making my first bukkit plugin. I would like to programmatically create a YAML file that represents a HashMap. How can I set and get this data structure?

The HashMap parameters look like <Signature, Location>, where Signature is my class that stores 4 integers, and Location is org.bukkit.Location

I think I would like the YAML file to look like this, but I am not sure if this structure is best:

MyPlugin:
    ListOfData:
        - signature: [1,2,3,4]    # this is a unique set of 4 integers
          location: [122,64,254]  # non-unique set of 3 integers
        - signature: [4,2,1,2]
          location: [91,62,101]
        - signature: [3,3,1,3]
          location: [190,64,321]

Signature can be modified as necessary, and I can create a wrapper for Location if necessary.

Thanks!

1 Answer 1

2

This is a suggested solution. I don't know if it is the best way...:) You may want to consider this as your yaml structure:

MyPlugin:
    ListOfData:
        '[1,2,3,4]': '[122,64,254]'
        '[4,2,1,2]': '[91,62,101]'
        '[3,3,1,3]': '[190,64,321]'
        anothersignature:anotherlocation
        ...

This will let you read the "ListOfData" in using the normal technique for reading hash map from a YAMLConfiguration (see below).

You'll have to treat the incoming information from the file as a HashMap of <String, String> and do any translation (e.g. turn 122,64,254 into a location) you need from there.

For reading a HashMap:

this.getConfig().getConfigurationSection("path.to.map").getValues(false)

For writing a HashMap (saveConfig() will still need to be called to write to disk):

this.getConfig().createSection("path.to.map", MyMap)

There's some details and subtleties here, its worth reading these carefully (same page, but different non-contiguous sections):

http://wiki.bukkit.org/Configuration_API_Reference#HashMaps http://wiki.bukkit.org/Configuration_API_Reference#HashMaps_2

3
  • My initial answer had a syntax error in the example YAML, I left out the quotes around the strings, and a space is required after the colon (:) in each HashMap pairing. I've corrected the text.
    – Iain
    Jul 22, 2013 at 18:12
  • Some potential pitfalls: 1) since you're storing locations, consider whether you need to store a the world name in addition to the coordinates (to be multi-world compatible). 2) getConfigurationSection will return null if it can't find the specified sectionpath, so if you're not 100% certain the sectionpath will be there, you should do that step separately and check for null.
    – Iain
    Jul 22, 2013 at 18:24
  • I decided to just create a wrapper and serialize the data (since it is not absolutely necessary that it be text-editable), but I will consider your answer for the future. Thank you!
    – BLuFeNiX
    Jul 23, 2013 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.