فهرست منبع

Improve messages

Lortseam 4 سال پیش
والد
کامیت
0d48af79ef

+ 1 - 1
lib/src/main/java/me/lortseam/completeconfig/data/BaseCollection.java

@@ -78,7 +78,7 @@ abstract class BaseCollection implements ParentDataPart {
                 try {
                     return (ConfigContainer) ReflectionUtils.instantiateClass(nestedClass);
                 } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
-                    throw new RuntimeException("Failed to instantiate nested class " + nestedClass, e);
+                    throw new RuntimeException("Failed to instantiate nested " + nestedClass, e);
                 }
             }).toArray(ConfigContainer[]::new));
         }

+ 8 - 11
lib/src/main/java/me/lortseam/completeconfig/data/Config.java

@@ -103,10 +103,9 @@ public final class Config extends BaseCollection {
          * @return this builder
          */
         public Builder add(@NonNull ConfigContainer... containers) {
-            Arrays.stream(containers).forEach(Objects::requireNonNull);
             for (ConfigContainer container : containers) {
-                if (!children.add(container)) {
-                    throw new IllegalArgumentException("Duplicate container");
+                if (!children.add(Objects.requireNonNull(container))) {
+                    throw new IllegalArgumentException("Duplicate container " + container.getClass().getSimpleName());
                 }
             }
             return this;
@@ -135,25 +134,23 @@ public final class Config extends BaseCollection {
         /**
          * Creates and loads the config.
          *
-         * @return the created config
+         * @return the created config, or null if empty
          */
         public Config build() {
-            if (children.isEmpty()) {
-                logger.warn("Mod " + modID + " tried to create an empty config");
-                return null;
+            Config config = null;
+            if (!children.isEmpty()) {
+                config = new Config(new ConfigSource(modID, branch), children.toArray(new ConfigContainer[0]));
             }
-            Config config = new Config(new ConfigSource(modID, branch), children.toArray(new ConfigContainer[0]));
-            if (config.isEmpty()) {
-                logger.warn("Config of " + config.source + " is empty");
+            if (config == null || config.isEmpty()) {
                 return null;
             }
-            config.load();
             if (main || branch.length == 0 && !mainConfigs.containsKey(modID)) {
                 mainConfigs.put(modID, config);
             }
             if (saveOnExit) {
                 saveOnExitConfigs.add(config);
             }
+            config.load();
             return config;
         }
 

+ 8 - 3
lib/src/main/java/me/lortseam/completeconfig/data/Entry.java

@@ -129,7 +129,7 @@ public class Entry<T> implements DataPart, Identifiable {
         try {
             return (T) Objects.requireNonNull(field.get(parentObject), field.toString());
         } catch (IllegalAccessException e) {
-            throw new RuntimeException(e);
+            throw new RuntimeException("Failed to get entry value", e);
         }
     }
 
@@ -202,9 +202,14 @@ public class Entry<T> implements DataPart, Identifiable {
             String[] customTooltipTranslationKeys = annotation.tooltipTranslationKeys();
             if (customTooltipTranslationKeys.length > 0) {
                 if (Arrays.stream(customTooltipTranslationKeys).anyMatch(StringUtils::isBlank)) {
-                    throw new IllegalAnnotationParameterException("Entry tooltip translation key(s) must not be blank");
+
                 }
-                customTooltipTranslation = Arrays.stream(customTooltipTranslationKeys).map(key -> parentTranslation.root().append(key)).toArray(TranslationIdentifier[]::new);
+                customTooltipTranslation = Arrays.stream(customTooltipTranslationKeys).map(key -> {
+                    if (StringUtils.isBlank(key)) {
+                        throw new IllegalAnnotationParameterException("Tooltip translation key of entry " + field + " may not be blank");
+                    }
+                    return parentTranslation.root().append(key);
+                }).toArray(TranslationIdentifier[]::new);
             }
             requiresRestart = annotation.requiresRestart();
             String comment = annotation.comment();

+ 1 - 1
lib/src/main/java/me/lortseam/completeconfig/gui/cloth/ClothConfigScreenBuilder.java

@@ -65,7 +65,7 @@ public final class ClothConfigScreenBuilder extends ConfigScreenBuilder {
 
     private AbstractConfigListEntry<?> buildEntry(Entry<?> entry) {
         return registry.findBuilder(entry).orElseThrow(() -> {
-            return new UnsupportedOperationException("Could not generate GUI for field " + entry.getField());
+            return new UnsupportedOperationException("Could not generate GUI for entry " + entry.getField());
         }).build(entry);
     }
 

+ 1 - 2
lib/src/main/java/me/lortseam/completeconfig/io/ConfigSource.java

@@ -15,7 +15,6 @@ import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
 import org.spongepowered.configurate.serialize.TypeSerializerCollection;
 
 import java.nio.file.Path;
-import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -51,7 +50,7 @@ public final class ConfigSource {
         this.modID = modID;
         this.branch = branch;
         if (!sources.add(this)) {
-            throw new IllegalArgumentException("A config of the mod " + modID + " with the specified branch " + Arrays.toString(branch) + " already exists");
+            throw new IllegalArgumentException("A config of " + this + " already exists");
         }
         Path path = FabricLoader.getInstance().getConfigDir();
         String[] subPath = ArrayUtils.add(branch, 0, modID);

+ 1 - 8
lib/src/test/java/me/lortseam/completeconfig/data/ConfigTest.java

@@ -8,7 +8,6 @@ import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
-import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.*;
 
 public class ConfigTest {
@@ -60,15 +59,9 @@ public class ConfigTest {
         }
 
         @Test
-        public void build_logWarningAndReturnNullIfChildrenEmpty() {
+        public void build_returnNullIfEmpty() {
             assertNull(builder.build());
-            assertThat(logCaptor.getWarnLogs()).contains("Mod " + MOD_ID + " tried to create an empty config");
-        }
-
-        @Test
-        public void build_logWarningAndReturnNullIfEmpty() {
             assertNull(builder.add(new EmptyContainer()).build());
-            assertThat(logCaptor.getWarnLogs()).containsExactly("Config of ConfigSource(modID=" + MOD_ID + ", branch=[]) is empty");
         }
 
     }