Document valid values for MultipleChoiceSettings in config file

This commit is contained in:
Jonas Herzig
2021-02-28 00:31:13 +01:00
parent 4ca4f2def6
commit c6b41d76fa
3 changed files with 30 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package com.replaymod.core;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
@@ -164,6 +165,15 @@ class SettingsRegistryBackend {
}
if (value instanceof String) {
category.addProperty(key.getKey(), (String) value);
if (key instanceof SettingsRegistry.MultipleChoiceSettingKey) {
@SuppressWarnings("unchecked")
List<String> choices = ((SettingsRegistry.MultipleChoiceSettingKey<String>) key).getChoices();
JsonArray array = new JsonArray();
for (String choice : choices) {
array.add(choice);
}
category.add(key.getKey() + "_valid_values", array);
}
}
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();

View File

@@ -1,8 +1,10 @@
package com.replaymod.core;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
class SettingsRegistryBackend {
@@ -36,7 +38,14 @@ class SettingsRegistryBackend {
} else if (key.getDefault() instanceof Double) {
value = configuration.get(key.getCategory(), key.getKey(), (Double) key.getDefault()).getDouble();
} else if (key.getDefault() instanceof String) {
value = configuration.get(key.getCategory(), key.getKey(), (String) key.getDefault()).getString();
Property property = configuration.get(key.getCategory(), key.getKey(), (String) key.getDefault());
value = property.getString();
if (key instanceof SettingsRegistry.MultipleChoiceSettingKey) {
@SuppressWarnings("unchecked")
List<String> choices = ((SettingsRegistry.MultipleChoiceSettingKey<String>) key).getChoices();
property.setValidValues(choices.toArray(new String[0]));
property.setComment("Valid values: " + String.join(", ", choices));
}
} else {
throw new IllegalArgumentException("Default type " + key.getDefault().getClass() + " not supported.");
}

View File

@@ -7,6 +7,7 @@ import net.minecraft.entity.EntityLivingBase;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderLivingEvent;
import net.minecraftforge.common.config.Property;
class Patterns {
@Pattern
@@ -44,4 +45,13 @@ class Patterns {
//$$ return event.type;
//#endif
}
@Pattern
private static void setComment(Property property, String comment) {
//#if MC>=10900
property.setComment(comment);
//#else
//$$ property.comment = comment;
//#endif
}
}