11package io.github.rothes.esu.core.module
22
3+ import io.github.rothes.esu.core.EsuBootstrap
34import io.github.rothes.esu.core.configuration.ConfigurationPart
45import io.github.rothes.esu.core.configuration.MultiLangConfiguration
56import io.github.rothes.esu.core.configuration.data.MessageData
@@ -33,15 +34,14 @@ interface Feature<C: ConfigurationPart, L: ConfigurationPart> {
3334 val available = isAvailable()
3435
3536 if (available.value && ! enabled) {
36- onEnable()
37- setEnabled(true )
37+ enableInternal()
3838 } else if (! available.value && enabled) {
39- setEnabled(false )
40- onDisable()
39+ disableInternal()
4140 }
4241
4342 return available
4443 }
44+
4545 fun getFeatureMap (): Map <String , Feature <* , * >>
4646 fun getFeatures (): List <Feature <* , * >>
4747 fun getFeature (name : String ): Feature <* , * >?
@@ -88,4 +88,27 @@ interface Feature<C: ConfigurationPart, L: ConfigurationPart> {
8888 }
8989 }
9090
91+ private companion object {
92+
93+ private fun Feature <* , * >.enableInternal () {
94+ try {
95+ onEnable()
96+ setEnabled(true )
97+ } catch (e: Throwable ) {
98+ EsuBootstrap .instance.err(" An exception occurred while enabling $name " , e)
99+ disableInternal()
100+ }
101+ }
102+
103+ private fun Feature <* , * >.disableInternal () {
104+ try {
105+ setEnabled(false )
106+ onDisable()
107+ } catch (e: Throwable ) {
108+ EsuBootstrap .instance.err(" An exception occurred while disabling $name " , e)
109+ }
110+ }
111+
112+ }
113+
91114}
0 commit comments