aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/config
diff options
context:
space:
mode:
authorCow <cow@volloeko.de>2020-09-24 18:41:22 +0200
committerCow <cow@volloeko.de>2020-09-24 18:41:22 +0200
commitf6d584431f878b931bf9f745a02b43dd8035cb44 (patch)
tree06607d9f47346cc77ce083dfadefa1fb45b5e3c6 /src/main/java/de/cowtipper/cowlection/config
parent7d4f2b900254d961b847f573237e3e6f40bff8e9 (diff)
downloadCowlection-f6d584431f878b931bf9f745a02b43dd8035cb44.tar.gz
Cowlection-f6d584431f878b931bf9f745a02b43dd8035cb44.tar.bz2
Cowlection-f6d584431f878b931bf9f745a02b43dd8035cb44.zip
Config (gui) rework #2
somehow missed these files ¯\_(ツ)_/¯
Diffstat (limited to 'src/main/java/de/cowtipper/cowlection/config')
-rw-r--r--src/main/java/de/cowtipper/cowlection/config/CredentialStorage.java73
-rw-r--r--src/main/java/de/cowtipper/cowlection/config/MooConfigCategory.java145
2 files changed, 218 insertions, 0 deletions
diff --git a/src/main/java/de/cowtipper/cowlection/config/CredentialStorage.java b/src/main/java/de/cowtipper/cowlection/config/CredentialStorage.java
new file mode 100644
index 0000000..c7cf35f
--- /dev/null
+++ b/src/main/java/de/cowtipper/cowlection/config/CredentialStorage.java
@@ -0,0 +1,73 @@
+package de.cowtipper.cowlection.config;
+
+import de.cowtipper.cowlection.Cowlection;
+import de.cowtipper.cowlection.util.ApiUtils;
+import de.cowtipper.cowlection.util.Utils;
+import net.minecraft.util.EnumChatFormatting;
+import net.minecraftforge.common.config.Configuration;
+import net.minecraftforge.common.config.Property;
+
+/**
+ * Key and secret holder in its own file to avoid people leaking their keys accidentally.
+ */
+public class CredentialStorage {
+ public static String moo;
+ public static boolean isMooValid;
+ private Property propMoo;
+ private Property propIsMooValid;
+ private final Configuration cfg;
+
+ public CredentialStorage(Configuration configuration) {
+ cfg = configuration;
+ initConfig();
+ }
+
+ private void initConfig() {
+ cfg.load();
+ propMoo = cfg.get(Configuration.CATEGORY_CLIENT,
+ "moo", "", "Don't share this with anybody! Do not edit this entry manually either!", Utils.VALID_UUID_PATTERN)
+ .setShowInGui(false);
+ propMoo.setLanguageKey(Cowlection.MODID + ".config." + propMoo.getName());
+
+ propIsMooValid = cfg.get(Configuration.CATEGORY_CLIENT,
+ "isMooValid", false, "Is the value valid?")
+ .setShowInGui(false);
+ moo = propMoo.getString();
+ isMooValid = propIsMooValid.getBoolean();
+ if (cfg.hasChanged()) {
+ cfg.save();
+ }
+ }
+
+ public void setMooIfValid(String moo, boolean commandTriggered) {
+ ApiUtils.fetchApiKeyInfo(moo, hyApiKey -> {
+ if (hyApiKey != null && hyApiKey.isSuccess()) {
+ // api key is valid!
+ Cowlection.getInstance().getMoo().setMoo(moo);
+ if (commandTriggered) {
+ Cowlection.getInstance().getChatHelper().sendMessage(EnumChatFormatting.GREEN, "Successfully verified API key ✔");
+ }
+ } else if (commandTriggered) {
+ // api key is invalid
+ String cause = hyApiKey != null ? hyApiKey.getCause() : null;
+ Cowlection.getInstance().getChatHelper().sendMessage(EnumChatFormatting.RED, "Failed to verify API key: " + (cause != null ? cause : "unknown cause :c"));
+ }
+ });
+ }
+
+ private void setMoo(String moo) {
+ CredentialStorage.moo = moo;
+ propMoo.set(moo);
+ setMooValidity(true);
+ }
+
+ public void setMooValidity(boolean isMooValid) {
+ CredentialStorage.isMooValid = isMooValid;
+ propIsMooValid.set(isMooValid);
+ cfg.save();
+ }
+
+ public Property getPropIsMooValid() {
+ return propIsMooValid;
+ }
+}
diff --git a/src/main/java/de/cowtipper/cowlection/config/MooConfigCategory.java b/src/main/java/de/cowtipper/cowlection/config/MooConfigCategory.java
new file mode 100644
index 0000000..ea2eb37
--- /dev/null
+++ b/src/main/java/de/cowtipper/cowlection/config/MooConfigCategory.java
@@ -0,0 +1,145 @@
+package de.cowtipper.cowlection.config;
+
+import de.cowtipper.cowlection.Cowlection;
+import de.cowtipper.cowlection.config.gui.MooConfigPreview;
+import net.minecraft.util.EnumChatFormatting;
+import net.minecraftforge.common.config.Property;
+import net.minecraftforge.fml.client.config.GuiSlider;
+
+import java.util.*;
+
+/**
+ * A config category, contains elements and logic for one specific config category
+ */
+public class MooConfigCategory {
+ private final String displayName;
+ private final String configName;
+ private String menuDisplayName;
+ private final List<SubCategory> subCategories;
+
+ public MooConfigCategory(String displayName, String configName) {
+ this.displayName = displayName;
+ this.configName = configName;
+ this.menuDisplayName = displayName;
+ subCategories = new ArrayList<>();
+ }
+
+ public void setMenuDisplayName(String menuDisplayName) {
+ this.menuDisplayName = menuDisplayName;
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+
+ public String getConfigName() {
+ return this.configName;
+ }
+
+ public String getMenuDisplayName() {
+ return menuDisplayName;
+ }
+
+ public List<SubCategory> getSubCategories() {
+ return subCategories;
+ }
+
+ public SubCategory addSubCategory(String subCatName) {
+ SubCategory subCategory = new SubCategory(subCatName);
+ subCategories.add(subCategory);
+ return subCategory;
+ }
+
+
+ public static class SubCategory {
+ private final String displayName;
+ private final List<String> explanations = new ArrayList<>();
+ private final List<Property> configEntries = new ArrayList<>();
+ private final Map<String, GuiSliderExtra> guiSliderExtras = new HashMap<>();
+ /**
+ * Index 0: preview for sub category
+ * Index >0: preview for one specific property
+ */
+ private final Map<Integer, MooConfigPreview> previews = new HashMap<>();
+
+ public SubCategory(String subCatName) {
+ this.displayName = subCatName;
+ }
+
+ public Property addConfigEntry(Property configEntry, MooConfigPreview configEntryPreview) {
+ Property property = addConfigEntry(configEntry);
+ addPropertyPreview(property, configEntryPreview);
+ return property;
+ }
+
+ public Property addConfigEntry(Property property, String prefix, String suffix, GuiSlider.ISlider onChangeSliderValue) {
+ guiSliderExtras.put(property.getLanguageKey(), new GuiSliderExtra(prefix, suffix, onChangeSliderValue));
+ return addConfigEntry(property);
+ }
+
+ public Property addConfigEntry(Property configEntry) {
+ configEntry.setLanguageKey(Cowlection.MODID + ".config." + configEntry.getName());
+ configEntries.add(configEntry);
+ return configEntry;
+ }
+
+ public void addPropertyPreview(Property property, MooConfigPreview preview) {
+ int propId = configEntries.indexOf(property);
+ if (propId > -1) {
+ this.previews.put(propId + 1, preview);
+ }
+ }
+
+ public void addExplanations(String... explanations) {
+ if (this.explanations.isEmpty()) {
+ // first line is only used for the explanations tooltips, not for in-text:
+ this.explanations.add(EnumChatFormatting.DARK_GREEN + "❢ " + EnumChatFormatting.GOLD + EnumChatFormatting.BOLD + displayName);
+ }
+ this.explanations.addAll(Arrays.asList(explanations));
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+
+ public List<String> getExplanations() {
+ return explanations;
+ }
+
+ public List<Property> getConfigEntries() {
+ return configEntries;
+ }
+
+ public GuiSliderExtra getGuiSliderExtra(String propertyKey) {
+ return guiSliderExtras.get(propertyKey);
+ }
+
+ public Map<Integer, MooConfigPreview> getPreviews() {
+ return previews;
+ }
+
+ public static class GuiSliderExtra {
+ private final String prefix;
+ private final String suffix;
+ private final GuiSlider.ISlider onChangeSliderValue;
+
+ public GuiSliderExtra(String prefix, String suffix, GuiSlider.ISlider onChangeSliderValue) {
+ this.prefix = prefix;
+ this.suffix = suffix;
+ this.onChangeSliderValue = onChangeSliderValue;
+ }
+
+ public String getPrefix() {
+ return prefix != null ? prefix : "";
+ }
+
+ public String getSuffix() {
+ return suffix != null ? suffix : "";
+ }
+
+ public GuiSlider.ISlider getOnChangeSliderValue() {
+ return onChangeSliderValue;
+ }
+ }
+ }
+}