aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/config/CredentialStorage.java
blob: c7cf35f4a58f5914344565de1717dce02a26c99b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
    }
}