@@ -86,7 +86,43 @@ default <T> T get(String path, Class<T> clazz) {
8686 */
8787 @ SuppressWarnings ("unchecked" )
8888 default <T > T get (String path , T def ) {
89- return (T ) getConfiguration ().get (path , def );
89+ try {
90+ return (T ) getConfiguration ().get (path , def );
91+ } catch (Exception e ) {
92+ return def ;
93+ }
94+ }
95+
96+ /**
97+ * Retrieves a list of values from the configuration at the specified path, with a guaranteed non-empty result.
98+ * <p>
99+ * Behavior:
100+ * <ul>
101+ * <li>If the value at {@code path} is a list, each element is attempted to be cast to {@code T} and
102+ * collected into the resulting list. Elements that fail to cast are ignored.</li>
103+ * <li>If the list exists but ends up empty after casting, a singleton list containing {@code def} is returned.</li>
104+ * <li>If the value at {@code path} is not a list, this method returns a singleton list containing the value
105+ * at {@code path} (or {@code def} if the path is absent).</li>
106+ * </ul>
107+ *
108+ * @param path the configuration path to read.
109+ * @param def the fallback element used when no valid values are found.
110+ * @param <T> the expected element type of the list.
111+ * @return a non-empty {@link List} of {@code T} derived from the configuration, or a singleton list with {@code def}.
112+ */
113+ @ SuppressWarnings ("unchecked" )
114+ default <T > List <T > getList (String path , T def ) {
115+ if (getConfiguration ().isList (path )) {
116+ List <T > list = new ArrayList <>();
117+ for (Object o : getConfiguration ().getList (path , new ArrayList <>()))
118+ try {
119+ list .add ((T ) o );
120+ } catch (Exception ignored ) {}
121+
122+ return (list .isEmpty ()) ? new ArrayList <>(Collections .singletonList (def )) : list ;
123+ }
124+
125+ return new ArrayList <>(Collections .singletonList (get (path , def )));
90126 }
91127
92128 /**
@@ -132,6 +168,18 @@ default ConfigurationSection getSection(String path) {
132168 return StringUtils .isBlank (path ) ? getConfiguration () : getConfiguration ().getConfigurationSection (path );
133169 }
134170
171+ /**
172+ * Retrieves a list of strings from the configuration at the specified path.
173+ *
174+ * @param path the path to the list.
175+ * @param defaultList the default list if the path doesn't exist
176+ *
177+ * @return a list of strings from the configuration, or defaultList if the path does not exist.
178+ */
179+ default List <String > toStringList (String path , List <String > defaultList ) {
180+ return toStringList (getConfiguration (), path , defaultList );
181+ }
182+
135183 /**
136184 * Retrieves a list of strings from the configuration at the specified path.
137185 *
@@ -176,13 +224,14 @@ default List<String> getKeys(String path) {
176224 @ NotNull
177225 default Map <String , ConfigurationSection > getSections (String path , boolean deep ) {
178226 Map <String , ConfigurationSection > map = new LinkedHashMap <>();
227+
179228 ConfigurationSection section = getSection (path );
180- if (section != null ) {
229+ if (section != null )
181230 for (String key : section .getKeys (deep )) {
182231 ConfigurationSection c = section .getConfigurationSection (key );
183232 if (c != null ) map .put (key , c );
184233 }
185- }
234+
186235 return map ;
187236 }
188237
@@ -231,16 +280,21 @@ default <U extends ConfigurableUnit> UnitMappable.Set<U> asUnitMap(String path,
231280 */
232281 static List <String > toStringList (ConfigurationSection section , String path , List <String > def ) {
233282 if (section == null ) return def ;
283+
234284 if (!section .isList (path )) {
235- final Object temp = section .get (path );
236- return temp != null ? Collections .singletonList (temp .toString ()) : def ;
285+ Object temp = section .get (path );
286+ return temp != null ?
287+ new ArrayList <>(Collections .singletonList (temp .toString ())) :
288+ def ;
237289 }
290+
238291 List <?> raw = section .getList (path , new ArrayList <>());
239292 if (!raw .isEmpty ()) {
240293 List <String > list = new ArrayList <>();
241294 raw .forEach (o -> list .add (o .toString ()));
242295 return list ;
243296 }
297+
244298 return def ;
245299 }
246300
@@ -270,22 +324,27 @@ static List<String> toStringList(ConfigurationSection section, String path) {
270324 static SectionMappable .Set toSectionMap (@ Nullable ConfigurationSection section , @ Nullable String path ) {
271325 if (StringUtils .isNotBlank (path ) && section != null )
272326 section = section .getConfigurationSection (path );
273- if (section == null )
274- return SectionMappable .asSet ();
327+
328+ if (section == null ) return SectionMappable .asSet ();
329+
275330 Set <String > sectionKeys = section .getKeys (false );
276- if (sectionKeys .isEmpty ())
277- return SectionMappable . asSet ();
331+ if (sectionKeys .isEmpty ()) return SectionMappable . asSet ();
332+
278333 Map <Integer , Set <ConfigurationSection >> map = new HashMap <>();
279334 for (String key : sectionKeys ) {
280335 ConfigurationSection id = section .getConfigurationSection (key );
281336 if (id == null ) continue ;
337+
282338 String perm = id .getString ("permission" , "DEFAULT" );
339+
283340 int def = perm .matches ("(?i)default" ) ? 0 : 1 ;
284341 int priority = id .getInt ("priority" , def );
342+
285343 Set <ConfigurationSection > m = map .getOrDefault (priority , new LinkedHashSet <>());
286344 m .add (id );
287345 map .put (priority , m );
288346 }
347+
289348 return SectionMappable .asSet (map ).order (false );
290349 }
291350
0 commit comments