What's the correct way to register and use ConfigurationSerializables?

Discussion in 'Plugin Development' started by HydroPage90, Aug 7, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    HydroPage90

    I found the section in the Configuration API documentation referring to serializing and deserializing custom objects. I made the class I want to se/deserialize extend ConfigurationSerializable, and I have implemented it properly. I made a constructor that accepts a Map<String, Object> and constructs the instance off of that. I then annotated the class with an alias using @SerializableAs(String), and then registered it into
    ConfigurationSerialization.registerClass(Class, String) using the class and the alias String.

    I went to my config.yml, and wrote out my object's data
    Code:
    map_border:
      corner1:
        - -30
        - 59
        - -34
      corner2:
        - 30
        - 109
        - 34
    Now, I just can't figure out whether I registered it incorrectly, or all the methods I've tried of actually *getting* my deserialized object are incorrect. I've tried getConfig().get(String)/get(String, Object). I just can't find a method. They always return null when I give them what I believe they expect.

    Did I register my object incorrectly? How do I get my deserialized object?

    (Bukkit version 1.8.8)
     
  2. Offline

    Niv-Mizzet

    @HydroPage90 Does this need to be done within the config or can you use another file that is written into and read out of by the code.
     
  3. Offline

    HydroPage90

    @Niv-Mizzet I'm not sure if it's strictly necessary for it to be config.yml. But it wouldn't hurt me to make different files
     
  4. Offline

    Niv-Mizzet

    @HydroPage90 Is your object serializable? Make sure to implement the interface Serializable.
     
  5. Offline

    HydroPage90

    @Niv-Mizzet Wait, really? It has to be java's Serializable? Not ConfigurationSerializable like the documentation says? Why wouldn't the docs mention this. That doesn't sound right
     
    Last edited: Aug 7, 2020
  6. Offline

    The_Spaceman

    How I use it:

    make the object:
    Code:java
    1. public class ColorTheme implements ConfigurationSerializable {
    2.  
    3. public static ColorTheme deserialize(Map<String, Object> args) {
    4. ColorTheme theme = new ColorTheme();
    5. /*put data from args in your object*/
    6. }
    7.  
    8. @Override
    9. public Map<String, Object> serialize() {
    10. HashMap<String, Object> args = new HashMap<>();
    11. /*put data from your object in args*/
    12. return args;
    13. }
    14. }


    and in your onEnable() (or at least before you are accessing your config file) register it:
    Code:java
    1. ConfigurationSerialization.registerClass(ColorTheme.class, "ColorTheme");
     
Thread Status:
Not open for further replies.

Share This Page