@@ -121,13 +121,18 @@ public void setDirty(boolean dirty) {
121121 this .dirty = dirty ;
122122 }
123123
124- void load () throws IOException {
124+ boolean load () throws IOException {
125125 if (!this .filePath .toFile ().exists ()) {
126- this . save () ;
126+ return false ;
127127 }
128- IFormatReader reader = this .format .createReader (this .filePath );
129- this .load (this , reader );
130- reader .close ();
128+ try {
129+ IFormatReader reader = this .format .createReader (this .filePath );
130+ this .load (this , reader );
131+ reader .close ();
132+ } catch (Exception e ) {
133+ return false ;
134+ }
135+ return true ;
131136 }
132137
133138 private void load (ConfigGroup group , IFormatReader reader ) {
@@ -166,92 +171,97 @@ void save() throws IOException {
166171
167172 private void save (ConfigGroup group , IFormatWriter writer ) {
168173 for (IConfigField <?, ?> field : group .getFields ()) {
169- for (String c : field .comments ()) {
170- writer .write (c );
171- }
172-
173- if (field instanceof ConfigGroup g ) {
174- writer .push (g .name ());
175- this .save (g , writer );
176- writer .pop ();
177- continue ;
178- }
174+ try {
175+ for (String c : field .comments ()) {
176+ writer .write (c );
177+ }
179178
180- if (field instanceof BaseNumberField <?> numberField ) {
181- // SET MATH ALLOW COMMENTS
182- if (numberField .math ()) {
183- writer .write (COMMENT_ALLOWS_MATH + (numberField .strictMath () ? COMMENT_ALLOWS_MATH_STRICT : "" ));
179+ if (field instanceof ConfigGroup g ) {
180+ writer .push (g .name ());
181+ this .save (g , writer );
182+ writer .pop ();
183+ continue ;
184184 }
185185
186- // SET MIN/MAX VALUE COMMENTS
187- String min = numberField .minValueString ();
188- String max = numberField .maxValueString ();
186+ if (field instanceof BaseNumberField <?> numberField ) {
187+ // SET MATH ALLOW COMMENTS
188+ if (numberField .math ()) {
189+ writer .write (COMMENT_ALLOWS_MATH + (numberField .strictMath () ? COMMENT_ALLOWS_MATH_STRICT : "" ));
190+ }
189191
190- if (min == null && max != null ) {
191- writer .write (String .format (COMMENT_MUST_BE_GREATER_THAN , max ));
192- } else if (min != null && max == null ) {
193- writer .write (String .format (COMMENT_MUST_BE_LESS_THAN , min ));
194- } else if (min != null ) {
195- writer .write (String .format (COMMENT_MUST_BE_IN_RANGE , min , max ));
196- }
197- }
192+ // SET MIN/MAX VALUE COMMENTS
193+ String min = numberField .minValueString ();
194+ String max = numberField .maxValueString ();
198195
199- if (field instanceof StringField stringField ) {
200- // SET STRING STARTWITH COMMENT
201- if (stringField .startsWith .isEmpty () && !stringField .endsWith .isEmpty ()) {
202- writer .write (String .format (COMMENT_MUST_END_WITH , stringField .endsWith ));
203- } else if (!stringField .startsWith .isEmpty () && stringField .endsWith .isEmpty ()) {
204- writer .write (String .format (COMMENT_MUST_START_WITH , stringField .startsWith ));
205- } else if (!stringField .startsWith .isEmpty ()) {
206- writer .write (String .format (COMMENT_MUST_START_AND_END_WITH , stringField .startsWith , stringField .endsWith ));
196+ if (min == null && max != null ) {
197+ writer .write (String .format (COMMENT_MUST_BE_GREATER_THAN , max ));
198+ } else if (min != null && max == null ) {
199+ writer .write (String .format (COMMENT_MUST_BE_LESS_THAN , min ));
200+ } else if (min != null ) {
201+ writer .write (String .format (COMMENT_MUST_BE_IN_RANGE , min , max ));
202+ }
207203 }
208204
209- // SET STRING CONDITION AND MODE COMMENT
210- if (stringField .condition != null ) {
211- String comment = switch (stringField .mode ) {
212- case CONTAINS -> COMMENT_MUST_CONTAIN ;
213- case EQUALS -> COMMENT_MUST_EQUALS ;
214- case REGEX -> COMMENT_MUST_MATCH ;
215- case NOT_CONTAINS -> COMMENT_MUST_NOT_CONTAIN ;
216- case NOT_EQUALS -> COMMENT_MUST_NOT_EQUALS ;
217- case NOT_REGEX -> COMMENT_MUST_NOT_MATCH ;
218- };
219- writer .write (String .format (comment , stringField .condition ));
220- }
205+ if (field instanceof StringField stringField ) {
206+ // SET STRING STARTWITH COMMENT
207+ if (stringField .startsWith .isEmpty () && !stringField .endsWith .isEmpty ()) {
208+ writer .write (String .format (COMMENT_MUST_END_WITH , stringField .endsWith ));
209+ } else if (!stringField .startsWith .isEmpty () && stringField .endsWith .isEmpty ()) {
210+ writer .write (String .format (COMMENT_MUST_START_WITH , stringField .startsWith ));
211+ } else if (!stringField .startsWith .isEmpty ()) {
212+ writer .write (String .format (COMMENT_MUST_START_AND_END_WITH , stringField .startsWith , stringField .endsWith ));
213+ }
221214
222- // SET STRING ALLOW EMPTY COMMENT
223- writer .write (stringField .allowEmpty ? COMMENT_ALLOW_EMPTY : COMMENT_DENY_EMPTY );
224- }
215+ // SET STRING CONDITION AND MODE COMMENT
216+ if (stringField .condition != null ) {
217+ String comment = switch (stringField .mode ) {
218+ case CONTAINS -> COMMENT_MUST_CONTAIN ;
219+ case EQUALS -> COMMENT_MUST_EQUALS ;
220+ case REGEX -> COMMENT_MUST_MATCH ;
221+ case NOT_CONTAINS -> COMMENT_MUST_NOT_CONTAIN ;
222+ case NOT_EQUALS -> COMMENT_MUST_NOT_EQUALS ;
223+ case NOT_REGEX -> COMMENT_MUST_NOT_MATCH ;
224+ };
225+ writer .write (String .format (comment , stringField .condition ));
226+ }
225227
226- if (field instanceof ListField <?> listField ) {
227- // SET LIST ALLOW EMPTY AND UNIQUE COMMENT
228- writer .write ((listField .allowEmpty
229- ? COMMENT_ARRAY_ALLOW_EMPTY
230- : COMMENT_ARRAY_DENY_EMPTY )
231- + (listField .unique ? " " + COMMENT_ARRAY_VALUES_MUST_BE_UNIQUE : "" )
232- );
228+ // SET STRING ALLOW EMPTY COMMENT
229+ writer .write (stringField .allowEmpty ? COMMENT_ALLOW_EMPTY : COMMENT_DENY_EMPTY );
230+ }
233231
234- // SET LIST LIMIT COMMENT
235- writer .write (String .format (COMMENT_ARRAY_SIZE_MUST_BE_GREATER_THAN , listField .limit ));
236- }
232+ if (field instanceof ListField <?> listField ) {
233+ // SET LIST ALLOW EMPTY AND UNIQUE COMMENT
234+ writer .write ((listField .allowEmpty
235+ ? COMMENT_ARRAY_ALLOW_EMPTY
236+ : COMMENT_ARRAY_DENY_EMPTY )
237+ + (listField .unique ? " " + COMMENT_ARRAY_VALUES_MUST_BE_UNIQUE : "" )
238+ );
237239
238- if ( field instanceof EnumField <?> enumField ) {
239- writer .write (String .format (COMMENT_ENUM_VALID_VALUES , Arrays . toString ( enumField . type (). getEnumConstants ()) ));
240- }
240+ // SET LIST LIMIT COMMENT
241+ writer .write (String .format (COMMENT_ARRAY_SIZE_MUST_BE_GREATER_THAN , listField . limit ));
242+ }
241243
242- if (field instanceof PathField pathField ) {
243- writer .write (pathField .runtimePath ? COMMENT_PATH_RUNTIME : COMMENT_PATH_STATIC );
244- if (pathField .fileExists ) {
245- writer .write (COMMENT_PATH_FILE_EXISTS );
244+ if (field instanceof EnumField <?> enumField ) {
245+ writer .write (String .format (COMMENT_ENUM_VALID_VALUES , Arrays .toString (enumField .type ().getEnumConstants ())));
246246 }
247- }
248247
249- if (field instanceof ListField <?> listField ) {
250- writer .write (field .name (), OmegaConfig .tryEncode (listField .get ().toArray (), field .type (), field .subType ()), field .type (), field .subType ());
251- } else if (field instanceof ArrayField <?> arrayField ) {
252- writer .write (field .name (), OmegaConfig .tryEncode (arrayField .get (), field .type (), field .subType ()), field .type (), field .subType ());
253- } else {
254- writer .write (field .name (), OmegaConfig .tryEncode (field .get (), field .subType ()), field .type (), field .subType ());
248+ if (field instanceof PathField pathField ) {
249+ writer .write (pathField .runtimePath ? COMMENT_PATH_RUNTIME : COMMENT_PATH_STATIC );
250+ if (pathField .fileExists ) {
251+ writer .write (COMMENT_PATH_FILE_EXISTS );
252+ }
253+ }
254+
255+
256+ if (field instanceof ListField <?> listField ) {
257+ writer .write (field .name (), OmegaConfig .tryEncode (listField .get ().toArray (), field .type (), field .subType ()), field .type (), field .subType ());
258+ } else if (field instanceof ArrayField <?> arrayField ) {
259+ writer .write (field .name (), OmegaConfig .tryEncode (arrayField .get (), field .type (), field .subType ()), field .type (), field .subType ());
260+ } else {
261+ writer .write (field .name (), OmegaConfig .tryEncode (field .get (), field .subType ()), field .type (), field .subType ());
262+ }
263+ } catch (Exception e ) {
264+ throw new RuntimeException ("Failed to save field '" + field .id () + "' in config spec '" + this .name () + "'" , e );
255265 }
256266 }
257267 this .dirty = false ;
0 commit comments