Просмотр исходного кода

Add default GUI title + more null checks

Lortseam 4 лет назад
Родитель
Сommit
8a5b243e7a

+ 6 - 1
src/main/java/me/lortseam/completeconfig/ModController.java

@@ -39,14 +39,19 @@ public final class ModController {
         return controllers.get(metadata.getId());
     }
 
+    @Getter
     private final ModMetadata metadata;
     @Getter
-    private TypeSerializerCollection typeSerializers = GLOBAL_TYPE_SERIALIZERS.childBuilder().build();
+    private TypeSerializerCollection typeSerializers = TypeSerializerCollection.builder().registerAll(GLOBAL_TYPE_SERIALIZERS).build();
 
     public String getID() {
         return metadata.getId();
     }
 
+    public String getName() {
+        return metadata.getName();
+    }
+
     /**
      * Registers custom type serializers, applied to all following mod configs.
      *

+ 4 - 0
src/main/java/me/lortseam/completeconfig/data/Config.java

@@ -18,6 +18,10 @@ public class Config extends CollectionMap {
         }
     }
 
+    public String getModID() {
+        return source.getModID();
+    }
+
     public TranslationIdentifier getTranslation() {
         return translation;
     }

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

@@ -115,14 +115,14 @@ public class Entry<T> extends EntryBase<T> implements DataPart {
 
     private T getFieldValue() {
         try {
-            return (T) Objects.requireNonNull(field.get(parentObject));
+            return (T) Objects.requireNonNull(field.get(parentObject), "Entry field value may never be null: " + field);
         } catch (IllegalAccessException e) {
             throw new RuntimeException(e);
         }
     }
 
     public void setValue(T value) {
-        update(value);
+        update(Objects.requireNonNull(value, "Entry value may never be null: " + field));
     }
 
     private boolean update() {
@@ -229,7 +229,10 @@ public class Entry<T> extends EntryBase<T> implements DataPart {
     @Override
     public void apply(CommentedConfigurationNode node) {
         try {
-            setValue((T) node.get(type));
+            T value = (T) node.get(type);
+            // value could be null despite the virtual() check (see https://github.com/SpongePowered/Configurate/issues/187)
+            if(value == null) return;
+            setValue(value);
         } catch (SerializationException e) {
             LOGGER.error("[CompleteConfig] Failed to apply value to entry!", e);
         }

+ 7 - 3
src/main/java/me/lortseam/completeconfig/gui/cloth/ClothGuiBuilder.java

@@ -1,9 +1,11 @@
 package me.lortseam.completeconfig.gui.cloth;
 
 import lombok.Getter;
+import me.lortseam.completeconfig.ModController;
 import me.lortseam.completeconfig.data.Collection;
 import me.lortseam.completeconfig.data.Config;
 import me.lortseam.completeconfig.data.Entry;
+import me.lortseam.completeconfig.data.gui.TranslationIdentifier;
 import me.lortseam.completeconfig.gui.GuiBuilder;
 import me.shedaniel.clothconfig2.api.AbstractConfigListEntry;
 import me.shedaniel.clothconfig2.api.ConfigBuilder;
@@ -13,6 +15,7 @@ import me.shedaniel.clothconfig2.impl.builders.SubCategoryBuilder;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
 import net.minecraft.client.gui.screen.Screen;
+import net.minecraft.text.TranslatableText;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -35,10 +38,11 @@ public class ClothGuiBuilder implements GuiBuilder {
 
     @Override
     public Screen buildScreen(Screen parentScreen, Config config, Runnable savingRunnable) {
-        ConfigBuilder builder = supplier.get();
-        builder.setParentScreen(parentScreen)
-                .setTitle(config.getTranslation().append("title").translate())
+        ConfigBuilder builder = supplier.get()
+                .setParentScreen(parentScreen)
                 .setSavingRunnable(savingRunnable);
+        TranslationIdentifier customTranslation = config.getTranslation().append("title");
+        builder.setTitle(customTranslation.exists() ? customTranslation.translate() : new TranslatableText("completeconfig.gui.defaultTitle", ModController.of(config.getModID()).getName()));
         for(Collection collection : config.values()) {
             ConfigCategory configCategory = builder.getOrCreateCategory(collection.getText());
             for (AbstractConfigListEntry<?> entry : buildCollection(collection)) {

+ 3 - 0
src/main/resources/assets/completeconfig/lang/de_de.json

@@ -0,0 +1,3 @@
+{
+  "completeconfig.gui.defaultTitle": "%s Konfiguration"
+}

+ 3 - 0
src/main/resources/assets/completeconfig/lang/en_us.json

@@ -0,0 +1,3 @@
+{
+  "completeconfig.gui.defaultTitle": "%s Config"
+}