From 50db93d77aff4c678fffd8604375497ca27845eb Mon Sep 17 00:00:00 2001 From: YannickMG Date: Tue, 11 Jan 2022 01:38:28 -0500 Subject: The tooltips your mod wishes it had (#858) * Add network support for chanining GT Tile GUIs * Implemented Cover Tabs for IGregTechTileEntity See GTNewHorizons/GT-New-Horizons-Modpack#9367 for details * Added IGuiIcon For easier addon extensibility of GT_GuiIcon Also fixed Ghost Circuit tab tooltip overlapping right-side cover tabs * Typo fix * Fixed unintended scala import * Tabs -> Spaces on the files I've touched * Propagate needsSteamVenting to UIs * Note special slot usage Determine whether a machine's recipe list ever makes use of the special slot * Add 2 configurable tooltip verbosity levels * Readability pass * New tooltip cache for flexibility It loads a configurable amount of lines per key based on the user's chosen verbosity levels * Let GT_GuiTooltipJava to use verbosity and LSHIFT * "Smart" auto-hiding tooltips * GT_GUIContainerMetaTile_Machine tooltip support * Rework Basic Machine tooltips * Wordy tooltips -> extended tooltips (clearer) * Fixed off-by-one error on power slot tooltips * Cleanup --- src/main/java/gregtech/api/util/GT_Recipe.java | 15 ++++ .../gregtech/api/util/GT_TooltipDataCache.java | 100 +++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/main/java/gregtech/api/util/GT_TooltipDataCache.java (limited to 'src/main/java/gregtech/api/util') diff --git a/src/main/java/gregtech/api/util/GT_Recipe.java b/src/main/java/gregtech/api/util/GT_Recipe.java index aa16340f68..3b143bab4c 100644 --- a/src/main/java/gregtech/api/util/GT_Recipe.java +++ b/src/main/java/gregtech/api/util/GT_Recipe.java @@ -849,6 +849,11 @@ public class GT_Recipe implements Comparable { */ private boolean mHasFluidOutputs = false; + /** + * Whether this recipe map contains special slot inputs. + */ + private boolean mUsesSpecialSlot = false; + /** * Initialises a new type of Recipe Handler. * @@ -955,6 +960,9 @@ public class GT_Recipe implements Comparable { if (aRecipe.mFluidOutputs.length != 0) { this.mHasFluidOutputs = true; } + if (aRecipe.mSpecialItems != null) { + this.mUsesSpecialSlot = true; + } return addToItemMap(aRecipe); } @@ -1100,6 +1108,13 @@ public class GT_Recipe implements Comparable { public boolean hasFluidInputs() { return mRecipeFluidNameMap.size() != 0; } + + /** + * Whether this recipe map contains special slot inputs. + */ + public boolean usesSpecialSlot() { + return mUsesSpecialSlot; + } } // ----------------------------------------------------------------------------------------------------------------- diff --git a/src/main/java/gregtech/api/util/GT_TooltipDataCache.java b/src/main/java/gregtech/api/util/GT_TooltipDataCache.java new file mode 100644 index 0000000000..7844bb04fd --- /dev/null +++ b/src/main/java/gregtech/api/util/GT_TooltipDataCache.java @@ -0,0 +1,100 @@ +package gregtech.api.util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import gregtech.GT_Mod; +import net.minecraft.util.StatCollector; + +public class GT_TooltipDataCache { + + public static class TooltipData { + public List text; + public List shiftText; + + public TooltipData(List text, List shiftText) { + this.text = text; + this.shiftText = shiftText; + } + } + private final Map 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 + */ + private TooltipData getUncachedTooltipData(String key, Object... args) { + List lines = getAllLines(key, args); + int normalLines = lines.size(); + if (Math.max(GT_Mod.gregtechproxy.mTooltipVerbosity, GT_Mod.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(GT_Mod.gregtechproxy.mTooltipVerbosity, normalLines, lines.size())), + lines.subList(0, getVerbosityIndex(GT_Mod.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 getAllLines(String key, Object... args) { + List 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; + } +} -- cgit