3
3
import com .cryptomorin .xseries .XSound ;
4
4
import io .github .thatsmusic99 .configurationmaster .api .ConfigFile ;
5
5
import io .github .thatsmusic99 .configurationmaster .api .ConfigSection ;
6
+ import io .github .thatsmusic99 .configurationmaster .impl .CMMemorySection ;
6
7
import me .xginko .aef .AnarchyExploitFixes ;
7
8
import me .xginko .aef .utils .LocaleUtil ;
8
9
import me .xginko .aef .utils .MathUtil ;
12
13
13
14
import java .io .File ;
14
15
import java .time .Duration ;
15
- import java .util .List ;
16
- import java .util .Locale ;
17
- import java .util .Map ;
16
+ import java .util .*;
18
17
19
18
public class Config {
20
19
21
- private final ConfigFile config ;
20
+ private final AnarchyExploitFixes plugin ;
21
+ private final HashMap <String , ConfigFile > configFiles = new HashMap <>();
22
+ private static final Set <String > configSections = new HashSet <>(Arrays .asList (
23
+ "language" , "general" , "misc" , "chat" , "elytra" ,
24
+ "chunk-limits" , "lag-preventions" , "patches" , "illegals" ,
25
+ "dupe-preventions" , "preventions" , "combat"
26
+ ));
22
27
public final Locale default_lang ;
23
28
public final Sound elytra_too_fast_sound ;
24
29
public final Component cmd_say_format ;
@@ -33,16 +38,28 @@ public class Config {
33
38
elytra_actionbar_enabled , elytra_show_chunkage , elytra_play_too_fast_sound ,
34
39
elytra_teleport_back , elytra_calculate_3D , permissions_hook , update_checker_enabled ;
35
40
41
+ public static Set <String > getConfigSections () {
42
+ return Collections .unmodifiableSet (configSections );
43
+ }
44
+
36
45
public Config () throws Exception {
37
- AnarchyExploitFixes plugin = AnarchyExploitFixes .getInstance ();
38
- // Load config.yml with ConfigMaster
39
- this .config = ConfigFile .loadConfig (new File (plugin .getDataFolder (), "config.yml" ));
46
+ this .plugin = AnarchyExploitFixes .getInstance ();
47
+
48
+ File settingsDir = new File (plugin .getDataFolder (), "settings" );
49
+ if (!settingsDir .exists ()) {
50
+ settingsDir .mkdirs ();
51
+ }
52
+
53
+ // Initialize all config files
54
+ initConfigFile ("config" );
55
+ for (String section : configSections ) {
56
+ initConfigFile ("settings/" + section );
57
+ }
40
58
41
- config .set ("plugin-version" , plugin .getPluginMeta ().getVersion ());
42
- config .set ("server-version" , plugin .getServer ().getVersion ());
59
+ loadAllConfigs ();
43
60
44
- // Pre-structure to force order
45
- structureConfig ( );
61
+ getConfig ( "config" ). set ( "plugin-version" , plugin . getPluginMeta (). getVersion ());
62
+ getConfig ( "config" ). set ( "server-version" , plugin . getServer (). getVersion () );
46
63
47
64
// Language Settings
48
65
this .default_lang = LocaleUtil .localeForLanguageTag (getString ("language.default-language" , "en_us" , """
@@ -76,7 +93,7 @@ The time in ticks (1 sec = 20 ticks) a checked tps will be cached\s
76
93
Help command that shows a small command overview for players.""" );
77
94
this .cmd_toggleConMsgs_enabled = getBoolean ("general.commands.toggleconnectionmsgs.enable" , true , """
78
95
If you don't use join leave/messages, you can set this to false.""" );
79
- config . addComment ("general.commands" , """
96
+ addComment ("general.commands" , """
80
97
A server restart is required when changing a command's enable status!""" );
81
98
82
99
// Elytra Speed
@@ -115,121 +132,167 @@ The time in ticks (1 sec = 20 ticks) a checked tps will be cached\s
115
132
this .elytra_enable_netherceiling = getBoolean ("elytra.elytra-speed.Nether-Ceiling.enable" , true );
116
133
117
134
// Misc
118
- config . addDefault ("misc.join-leave-messages.enable" , true ); // add default here so enable option shows up first.
135
+ addDefault ("misc.join-leave-messages.enable" , true ); // add default here so enable option shows up first.
119
136
this .connectionMsgsAreOnByDefault = getBoolean ("misc.join-leave-messages.connection-messages-on-by-default" , true , """
120
137
If set to true, players will see join/leave messages by default\s
121
138
and enter /toggleconnectionmsgs to disable them.\s
122
139
If set to false will work the other way around.""" );
123
- config .addDefault ("misc.join-leave-messages.show-in-console" , false ); // add default here so show-in-console option is not misplaced.
140
+ addDefault ("misc.join-leave-messages.show-in-console" , false ); // add default here so show-in-console option is not misplaced.
141
+
142
+ saveAllConfigs ();
143
+ }
144
+
145
+ private void initConfigFile (String name ) throws Exception {
146
+ ConfigFile configFile = ConfigFile .loadConfig (new File (plugin .getDataFolder (), name + ".yml" ));
147
+ configFiles .put (name , configFile );
148
+ }
149
+
150
+ private ConfigFile getConfig (String name ) {
151
+ return configFiles .getOrDefault (name , null );
124
152
}
125
153
126
154
public void saveConfig () {
127
155
try {
128
- config . save ();
156
+ saveAllConfigs ();
129
157
} catch (Exception e ) {
130
- AnarchyExploitFixes .prefixedLogger ().error ("Failed to save config file !" , e );
158
+ AnarchyExploitFixes .prefixedLogger ().error ("Failed to save config files !" , e );
131
159
}
132
160
}
133
161
134
- private void structureConfig () {
135
- createTitledSection ("Language" , "language" );
136
- createTitledSection ("General" , "general" );
137
- createTitledSection ("Miscellaneous" , "misc" );
138
- createTitledSection ("Chat" , "chat" );
139
- createTitledSection ("Elytra" , "elytra" );
140
- createTitledSection ("Chunk Limits" , "chunk-limits" );
141
- createTitledSection ("Lag Preventions" , "lag-preventions" );
142
- createTitledSection ("Patches" , "patches" );
143
- createTitledSection ("Illegals" , "illegals" );
144
- createTitledSection ("Dupe Preventions" , "dupe-preventions" );
145
- createTitledSection ("Preventions" , "preventions" );
146
- createTitledSection ("Combat" , "combat" );
162
+ private void loadAllConfigs () {
163
+ for (ConfigFile config : configFiles .values ()) {
164
+ try {
165
+ config .load ();
166
+ } catch (Exception e ) {
167
+ AnarchyExploitFixes .prefixedLogger ().error ("Failed to load config file!" , e );
168
+ }
169
+ }
147
170
}
148
171
149
- public ConfigFile master () {
150
- return config ;
172
+ private void saveAllConfigs () {
173
+ for (ConfigFile config : configFiles .values ()) {
174
+ try {
175
+ config .save ();
176
+ } catch (Exception e ) {
177
+ AnarchyExploitFixes .prefixedLogger ().error ("Failed to save config file!" , e );
178
+ }
179
+ }
180
+ }
181
+
182
+ private String [] getConfigNameAndPath (String path ) {
183
+ if (!path .contains ("." )) {
184
+ return new String []{"config" , path };
185
+ }
186
+
187
+ String section = path .substring (0 , path .indexOf ("." ));
188
+ String newPath = path .substring (path .indexOf ("." ) + 1 );
189
+
190
+ if (configSections .contains (section )) {
191
+ return new String []{"settings/" + section , newPath };
192
+ } else {
193
+ return new String []{"config" , path };
194
+ }
151
195
}
152
196
153
- public void createTitledSection (String title , String path ) {
154
- config .addSection (title );
155
- config .addDefault (path , null );
197
+ private <T > T getConfigValue (String path , T defaultValue , String comment , ConfigValueGetter <T > getter ) {
198
+ String [] configAndPath = getConfigNameAndPath (path );
199
+ ConfigFile config = getConfig (configAndPath [0 ]);
200
+ if (config == null ) return defaultValue ;
201
+
202
+ if (comment != null ) {
203
+ config .addDefault (configAndPath [1 ], defaultValue , comment );
204
+ } else {
205
+ config .addDefault (configAndPath [1 ], defaultValue );
206
+ }
207
+
208
+ return getter .get (config , configAndPath [1 ], defaultValue );
156
209
}
157
210
158
211
public boolean getBoolean (String path , boolean def , String comment ) {
159
- config .addDefault (path , def , comment );
160
- return config .getBoolean (path , def );
212
+ return getConfigValue (path , def , comment , CMMemorySection ::getBoolean );
161
213
}
162
214
163
215
public boolean getBoolean (String path , boolean def ) {
164
- config .addDefault (path , def );
165
- return config .getBoolean (path , def );
216
+ return getConfigValue (path , def , null , CMMemorySection ::getBoolean );
166
217
}
167
218
168
219
public String getString (String path , String def , String comment ) {
169
- config .addDefault (path , def , comment );
170
- return config .getString (path , def );
220
+ return getConfigValue (path , def , comment , CMMemorySection ::getString );
171
221
}
172
222
173
223
public String getString (String path , String def ) {
174
- config .addDefault (path , def );
175
- return config .getString (path , def );
224
+ return getConfigValue (path , def , null , CMMemorySection ::getString );
176
225
}
177
226
178
227
public double getDouble (String path , double def , String comment ) {
179
- config .addDefault (path , def , comment );
180
- return config .getDouble (path , def );
228
+ return getConfigValue (path , def , comment , CMMemorySection ::getDouble );
181
229
}
182
230
183
231
public double getDouble (String path , double def ) {
184
- config .addDefault (path , def );
185
- return config .getDouble (path , def );
232
+ return getConfigValue (path , def , null , CMMemorySection ::getDouble );
186
233
}
187
234
188
235
public int getInt (String path , int def , String comment ) {
189
- config .addDefault (path , def , comment );
190
- return config .getInteger (path , def );
236
+ return getConfigValue (path , def , comment , CMMemorySection ::getInteger );
191
237
}
192
238
193
239
public int getInt (String path , int def ) {
194
- config .addDefault (path , def );
195
- return config .getInteger (path , def );
240
+ return getConfigValue (path , def , null , CMMemorySection ::getInteger );
196
241
}
197
242
198
243
public long getLong (String path , long def , String comment ) {
199
- config .addDefault (path , def , comment );
200
- return config .getLong (path , def );
244
+ return getConfigValue (path , def , comment , CMMemorySection ::getLong );
201
245
}
202
246
203
247
public long getLong (String path , long def ) {
204
- config .addDefault (path , def );
205
- return config .getLong (path , def );
248
+ return getConfigValue (path , def , null , CMMemorySection ::getLong );
206
249
}
207
250
208
251
public List <String > getList (String path , List <String > def , String comment ) {
209
- config .addDefault (path , def , comment );
210
- return config .getStringList (path );
252
+ return getConfigValue (path , def , comment , (config , p , d ) -> config .getStringList (p ));
211
253
}
212
254
213
255
public List <String > getList (String path , List <String > def ) {
214
- config .addDefault (path , def );
215
- return config .getStringList (path );
256
+ return getConfigValue (path , def , null , (config , p , d ) -> config .getStringList (p ));
216
257
}
217
258
218
259
public ConfigSection getConfigSection (String path , Map <String , Object > defaultKeyValue ) {
219
- config .addDefault (path , null );
220
- config .makeSectionLenient (path );
221
- defaultKeyValue .forEach ((string , object ) -> config .addExample (path +"." +string , object ));
222
- return config .getConfigSection (path );
260
+ String [] configAndPath = getConfigNameAndPath (path );
261
+ ConfigFile config = getConfig (configAndPath [0 ]);
262
+ if (config == null ) return null ;
263
+ config .addDefault (configAndPath [1 ], null );
264
+ config .makeSectionLenient (configAndPath [1 ]);
265
+ defaultKeyValue .forEach ((string , object ) -> config .addExample (configAndPath [1 ] + "." + string , object ));
266
+ return config .getConfigSection (configAndPath [1 ]);
223
267
}
224
268
225
269
public ConfigSection getConfigSection (String path , Map <String , Object > defaultKeyValue , String comment ) {
226
- config .addDefault (path , null , comment );
227
- config .makeSectionLenient (path );
228
- defaultKeyValue .forEach ((string , object ) -> config .addExample (path +"." +string , object ));
229
- return config .getConfigSection (path );
270
+ String [] configAndPath = getConfigNameAndPath (path );
271
+ ConfigFile config = getConfig (configAndPath [0 ]);
272
+ if (config == null ) return null ;
273
+ config .addDefault (configAndPath [1 ], null , comment );
274
+ config .makeSectionLenient (configAndPath [1 ]);
275
+ defaultKeyValue .forEach ((string , object ) -> config .addExample (configAndPath [1 ] + "." + string , object ));
276
+ return config .getConfigSection (configAndPath [1 ]);
230
277
}
231
278
232
279
public void addComment (String path , String comment ) {
233
- config .addComment (path , comment );
280
+ String [] configAndPath = getConfigNameAndPath (path );
281
+ ConfigFile config = getConfig (configAndPath [0 ]);
282
+ if (config != null ) {
283
+ config .addComment (configAndPath [1 ], comment );
284
+ }
285
+ }
286
+
287
+ public void addDefault (String path , Object value ) {
288
+ String [] configAndPath = getConfigNameAndPath (path );
289
+ ConfigFile config = getConfig (configAndPath [0 ]);
290
+ if (config != null ) {
291
+ config .addDefault (configAndPath [1 ], value );
292
+ }
293
+ }
294
+
295
+ private interface ConfigValueGetter <T > {
296
+ T get (ConfigFile config , String path , T defaultValue );
234
297
}
235
- }
298
+ }
0 commit comments