aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/util/GTTooltipDataCache.java
blob: a26f7a84d8b5a1614c7f2adc1af9e8bc543e55b0 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package gregtech.api.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.minecraft.util.StatCollector;

import gregtech.GTMod;

public class GTTooltipDataCache {

    public static class TooltipData {

        public List<String> text;
        public List<String> shiftText;

        public TooltipData(List<String> text, List<String> shiftText) {
            this.text = text;
            this.shiftText = shiftText;
        }
    }

    private final Map<String, TooltipData> fetchedTooltipData = new HashMap<>();

    /**
     * Returns tooltip data respecting the user's configured verbosity levels, applying any formatting arguments.
     *
     * @param key  the key to lookup
     * @param args arguments for string formatting (prefer using positional arguments)
     * @return The tooltip data the user asked for
     */
    public TooltipData getData(String key, Object... args) {
        TooltipData tooltipData = fetchedTooltipData.get(key);
        if (tooltipData == null) {
            tooltipData = getUncachedTooltipData(key, args);
            fetchedTooltipData.put(key, tooltipData);
        }
        return tooltipData;
    }

    /**
     * Builds tooltip data respecting the user's configured verbosity levels, applying any formatting arguments.
     *
     * @param key  the key to lookup
     * @param args arguments for string formatting (prefer using positional arguments)
     * @return The tooltip data the user asked for
     */
    public TooltipData getUncachedTooltipData(String key, Object... args) {
        List<String> lines = getAllLines(key, args);
        int normalLines = lines.size();
        if (Math.max(GTMod.gregtechproxy.mTooltipVerbosity, GTMod.gregtechproxy.mTooltipShiftVerbosity) >= 3) {
            lines.addAll(getAllLines(key + ".extended", args)); // Are extended lines enabled? If so add them to the
                                                                // lines
        }
        if (lines.size() == 0) {
            lines.add(key); // Fallback in case no lines could be found at all
        }
        return new TooltipData(
            lines.subList(0, getVerbosityIndex(GTMod.gregtechproxy.mTooltipVerbosity, normalLines, lines.size())),
            lines.subList(0, getVerbosityIndex(GTMod.gregtechproxy.mTooltipShiftVerbosity, normalLines, lines.size())));
    }

    /**
     * Gets all the lines for the given key and every other subsequent consecutive key with a .n suffix, n in {1,2,3...}
     *
     * @param key  the key to lookup
     * @param args arguments for string formatting (prefer using positional arguments)
     * @return The lines for the key and all of it's subkeys
     */
    private List<String> getAllLines(String key, Object... args) {
        List<String> lines = new ArrayList<>();
        String keyToLookup = key;
        int i = 1; // First loop has no .number postfix
        while (StatCollector.canTranslate(keyToLookup)) {
            lines.add(StatCollector.translateToLocalFormatted(keyToLookup, args));
            keyToLookup = key + "." + i++;
        }
        return lines;
    }

    /**
     * Determines how many lines from a tooltip to include from the full line list to respect a given verbosity level.
     *
     * @param tooltipVerbosity the verbosity level we're applying
     * @param defaultIndex     return if tooltipVerbosity is 2
     * @param maxIndex         return if tooltipVerbosity is greater than 2
     * @return verbosity appropriate index
     */
    private static int getVerbosityIndex(int tooltipVerbosity, int defaultIndex, int maxIndex) {
        int index;
        if (tooltipVerbosity < 1) {
            index = 0;
        } else if (tooltipVerbosity == 1) {
            index = 1;
        } else if (tooltipVerbosity == 2) {
            index = defaultIndex;
        } else {
            index = maxIndex;
        }
        return index;
    }
}