1.15.2 About ConfigurationSerializable

Discussion in 'Spigot Plugin Development' started by AtBar666, Mar 14, 2020.

  1. hi, everyone
    i want to konw that How to serialize can use #set and #get methods directly in configuration like ItemStack
    can you give me a example?
    Thanks you so much
     
  2. SteelPhoenix

    Moderator

    You have your serializable object:
    Code (Java):
    public class MySerializableObject implements ConfigurationSerializable {
        public MySerializableObject() {
            // Constructor
        }
        public MySerialziableObject(Map<String, Object> map) {
            // Deserialization constructor
        }
        @Override
        public final Map<String, Object> serialize() {
            // Object serializing
        }
    }
    Which you have to register: (on plugin startup)
    Code (Java):
    ConfigurationSerialization.registerClass(MySerializableObject.class);
    Then, you can just use configuration getter/setters to deal with the object
    Code (Java):
    ConfigurationSection section = ...;
    String path = ...;

    // Retrieving
    MySerializableObject o = (MySerializableObject) section.get(myPath);

    // Setting
    section.set(myPath, o);

     
    Edit: ConfigurationSection#getSerializable(String path, Class<T> clazz) also exists and its version with a default value param.
     
    #2 SteelPhoenix, Mar 14, 2020
    Last edited: Mar 14, 2020
  3. Hey, This has no effect, It is my code
    upload_2020-3-14_21-8-20.png
    upload_2020-3-14_21-9-11.png
    upload_2020-3-14_21-11-22.png
     

    Attached Files:

  4. Hello,

    You forgot to create the deserialization constructor in your Air class:
    Code (Java):
    public MySerialziableObject(Map<String, Object> map) {
      // Deserialization constructor
    }
     
    • Agree Agree x 2
  5. SteelPhoenix

    Moderator

    Javadoc
     
    • Like Like x 1
    • Agree Agree x 1