Resource Kotlin Config Mapper

Discussion in 'Spigot Plugin Development' started by Korwin, Aug 15, 2021.

  1. In short i was lazy setting field values by myself so i created this little utility hope you like it



    #Edit1: Added null safety if config section doesn't exist it wont set it

    Code (Java):
    import org.bukkit.configuration.ConfigurationSection

    object ConfigMapper {
       fun <T : Any> configMapper(config: ConfigurationSection, obj: T) {
          val clazz = obj::class.java
          for (field in clazz.declaredFields) {
             field.isAccessible = true

             val configField = config.get(field.name) ?: continue
             field.set(obj, configField)

             field.isAccessible = false
          }
       }
    }
    }
    Use :

    Code (Java):

    class Something(config: ConfigurationSection) {
       var a: Int = 0
       var b: String = ""
       init {
          ConfigMapper.configMapper(config, this)
       }
    }
     
     
    #1 Korwin, Aug 15, 2021
    Last edited: Aug 15, 2021
    • Useful Useful x 1
  2. SteelPhoenix

    Moderator

    But why wouldn't ConfigurationSerializable do the trick?
     
  3. If i am not mistaken it produces additional code i mean i wanted to have user configurable config that they can add dynamically so i needed some mapper if that additional code would be there user could forgot about this or make mistake