diff options
author | Cow <cow@volloeko.de> | 2020-09-24 18:41:22 +0200 |
---|---|---|
committer | Cow <cow@volloeko.de> | 2020-09-24 18:41:22 +0200 |
commit | f6d584431f878b931bf9f745a02b43dd8035cb44 (patch) | |
tree | 06607d9f47346cc77ce083dfadefa1fb45b5e3c6 /src/main/java/de/cowtipper/cowlection/config/CredentialStorage.java | |
parent | 7d4f2b900254d961b847f573237e3e6f40bff8e9 (diff) | |
download | Cowlection-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/CredentialStorage.java')
-rw-r--r-- | src/main/java/de/cowtipper/cowlection/config/CredentialStorage.java | 73 |
1 files changed, 73 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; + } +} |