Skip to content

Commit

Permalink
add completion counter
Browse files Browse the repository at this point in the history
  • Loading branch information
Glease committed Nov 6, 2023
1 parent 79b07ef commit 04b9ffb
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 26 deletions.
16 changes: 16 additions & 0 deletions src/main/java/net/glease/tc4tweak/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CommonUtils {
public static String toString(AspectList al) {
return al.aspects.entrySet().stream().filter(e -> e.getKey() != null && e.getValue() != null).map(e -> String.format("%dx%s", e.getValue(), e.getKey().getName())).collect(Collectors.joining(";"));
}

public static String toString(CrucibleRecipe r) {
return "CrucibleRecipe{key="+r.key+",catalyst="+r.catalyst+",output="+r.getRecipeOutput()+",aspects="+toString(r.aspects)+"}";
}
Expand Down Expand Up @@ -50,4 +51,19 @@ public static <T> T reflectGet(Field f, Object instance) {
throw new AssertionError(e);
}
}

public static Field getField(Class<?> clazz, String fieldName, int index) {
try {
Field f = null;
Field[] fields = clazz.getDeclaredFields();
if (index >= 0 && fields.length > index)
f = fields[index];
if (f == null || !f.getName().equalsIgnoreCase(fieldName))
f = clazz.getDeclaredField(fieldName);
f.setAccessible(true);
return f;
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/net/glease/tc4tweak/ConfigurationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public enum ConfigurationHandler {
private int browserWidth = 256;
private InfusionOreDictMode infusionOreDictMode = InfusionOreDictMode.Default;
private List<String> categoryOrder = new ArrayList<>();
private CompletionCounterStyle counterStyle = CompletionCounterStyle.Current;

ConfigurationHandler() {
FMLCommonHandler.instance().bus().register(this);
Expand Down Expand Up @@ -123,6 +124,7 @@ private void loadConfig(boolean send) {
categoryOrder = ImmutableList.copyOf(config.getStringList("categoryOrder", "client", new String[] {"BASICS","THAUMATURGY","ALCHEMY","ARTIFICE","GOLEMANCY","ELDRITCH",}, "Specify a full sorting order of research tabs. An empty list here means the feature is disabled. any research tab not listed here will be appended to the end in their original order. Use NEI utility to dump a list of all research tabs. Default is the list of all vanilla thaumcraft tabs."));
dispenserShootPrimalArrow = config.getBoolean("dispenserShootPrimalArrow", "general", false, "If true, dispenser will shoot primal arrow instead of dropping it into world.");
addClearButton = config.getBoolean("addClearButton", "client", true, "If true, a button will be shown when there is any amount of tc4 notifications AND when sending chat.");
counterStyle = CompletionCounterStyle.get(config.getString("completionCounterStyle", "client", counterStyle.name(), "Select the style of completion counter. Default: Current. None: disable completion progress counter. Current: display how many you have completed already, and only show the total count for this tab when everything here has been learnt. All: show all counters at all times.", Arrays.stream(CompletionCounterStyle.values()).map(Enum::name).toArray(String[]::new)));

// validation
if (inferBrowserScaleLowerBound > inferBrowserScaleUpperBound)
Expand Down Expand Up @@ -232,6 +234,10 @@ public boolean isAddClearButton() {
return addClearButton;
}

public CompletionCounterStyle getResearchCounterStyle() {
return counterStyle;
}

public enum InfusionOreDictMode {
Default {
@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -277,4 +283,19 @@ public static InfusionOreDictMode get(String name) {
return Default;
}
}

public enum CompletionCounterStyle {
None,
Current,
All,
;

public static CompletionCounterStyle get(String name) {
for (CompletionCounterStyle value : values()) {
if (value.name().equals(name))
return value;
}
return Current;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package net.glease.tc4tweak.modules.researchBrowser;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import net.glease.tc4tweak.ClientUtils;
import net.glease.tc4tweak.ConfigurationHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.resources.I18n;
import org.lwjgl.opengl.GL11;
import thaumcraft.api.research.ResearchCategories;
import thaumcraft.api.research.ResearchCategoryList;
import thaumcraft.client.gui.GuiResearchBrowser;

public class DrawResearchBrowserBorders {
Expand All @@ -29,6 +39,29 @@ public static void drawResearchBrowserBorders(GuiResearchBrowser gui, int x, int
}
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(oldDepthFunc);

drawCompletionCounter(gui, x, y);
}

private static void drawCompletionCounter(GuiResearchBrowser gui, int x, int y) {
ConfigurationHandler.CompletionCounterStyle style = ConfigurationHandler.INSTANCE.getResearchCounterStyle();
if (style == ConfigurationHandler.CompletionCounterStyle.None)
return;
// draw completion text progress text
ResearchCategoryList category = ResearchCategories.getResearchList(Utils.getActiveCategory());
// filter away stuff that are auto unlocked but never shown. probably should just filter away virtual research,
// but I'm not entirely sure how that field is actually used in the field, so let's be conservative for now
Set<String> r = category.research.entrySet().stream().filter(e -> !(e.getValue().isAutoUnlock() && e.getValue().isVirtual())).map(Map.Entry::getKey).collect(Collectors.toSet());
int total = r.size();
List<?> collect = GuiResearchBrowser.completedResearch.get(Minecraft.getMinecraft().thePlayer.getCommandSenderName()).stream().filter(r::contains).collect(Collectors.toList());
int completed = collect.size();
String tooltip;
if (style == ConfigurationHandler.CompletionCounterStyle.Current && completed < total)
tooltip = I18n.format("tc4tweaks.gui.progress.partial", completed);
else
tooltip = I18n.format("tc4tweaks.gui.progress", completed, total);
FontRenderer fontRenderer = gui.mc.fontRenderer;
fontRenderer.drawString(tooltip, x + BORDER_WIDTH + 2, y + (BORDER_HEIGHT - fontRenderer.FONT_HEIGHT) / 2, 0x777777, true);
}

public static void drawResearchBrowserBackground(GuiResearchBrowser gui, int x, int y, int u, int v, int width, int height) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import net.glease.tc4tweak.CommonUtils;
import net.glease.tc4tweak.ConfigurationHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
Expand Down Expand Up @@ -38,10 +39,8 @@
*/
public class ThaumonomiconIndexSearcher {
private static final int mouseBufferIdent = 17;
private static final int selectedCategoryIdent = 21;
public static ThaumonomiconIndexSearcher instance;
private static ByteBuffer mouseBuffer;
private static Field f_selectedCategory = null;
private static Field f_mouseBuffer;
private static GuiTextField thaumSearchField;
private static int listDisplayOffset = 0;
Expand All @@ -52,19 +51,7 @@ public static void init() {
instance = new ThaumonomiconIndexSearcher();
MinecraftForge.EVENT_BUS.register(instance);
FMLCommonHandler.instance().bus().register(instance);

try {
f_mouseBuffer = Mouse.class.getDeclaredFields()[mouseBufferIdent];
if (!f_mouseBuffer.getName().equalsIgnoreCase("readBuffer"))
f_mouseBuffer = Mouse.class.getDeclaredField("readBuffer");
f_mouseBuffer.setAccessible(true);
f_selectedCategory = GuiResearchBrowser.class.getDeclaredFields()[selectedCategoryIdent];
if (!f_selectedCategory.getName().equalsIgnoreCase("selectedCategory"))
f_selectedCategory = GuiResearchBrowser.class.getDeclaredField("selectedCategory");
f_selectedCategory.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
f_mouseBuffer = CommonUtils.getField(Mouse.class, "readBuffer", mouseBufferIdent);
}

private static void initMouseEventBuffer() {
Expand Down Expand Up @@ -141,16 +128,6 @@ private static void buildEntryList(String query) {
searchResults = valids;
}

private static String getActiveCategory() {
String s = null;
try {
s = (String) f_selectedCategory.get(null);
} catch (Exception e) {
e.printStackTrace();
}
return s;
}

@SubscribeEvent
public void onGuiInit(GuiScreenEvent.InitGuiEvent.Post event) {
searchResults.clear();
Expand Down Expand Up @@ -264,7 +241,7 @@ public void renderTick(TickEvent.ClientTickEvent event) {
thaumSearchField.textboxKeyTyped(Keyboard.getEventCharacter(), Keyboard.getEventKey());
listDisplayOffset = 0;
if (ConfigurationHandler.INSTANCE.isLimitBookSearchToCategory())
searchCategory = getActiveCategory();
searchCategory = Utils.getActiveCategory();
buildEntryList(thaumSearchField.getText());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.glease.tc4tweak.modules.researchBrowser;

import java.lang.reflect.Field;

import net.glease.tc4tweak.CommonUtils;
import thaumcraft.client.gui.GuiResearchBrowser;

class Utils {
private static final int selectedCategoryIdent = 21;
private static final Field f_selectedCategory = CommonUtils.getField(GuiResearchBrowser.class, "selectedCategory", selectedCategoryIdent);

static String getActiveCategory() {
return CommonUtils.reflectGet(f_selectedCategory, null);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/assets/tc4tweak/lang/en_US.lang
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
tc4tweaks.gui.search=Search
tc4tweaks.gui.progress=Learnt %s out of %s
tc4tweaks.gui.progress.partial=Learnt %s out of §kxx§r
tc4tweaks.gui.clear_notification=Clear Notification
tc4tweaks.enabled_scrolling=Scrolling aspect list with mouse wheel at research table enabled
tc4tweaks.disable_vanilla=Arcane workbench will no longer work as an vanilla crafting table
Expand Down Expand Up @@ -29,6 +31,8 @@ tc4tweaks.config.client.categoryOrder=Reorder Research Tabs
tc4tweaks.config.client.categoryOrder.tooltip=Specify a full sorting order of research tabs. An empty list here means the feature is disabled. any research tab not listed here will be appended to the end in their original order. Use NEI utility to dump a list of all research tabs. Default is the list of all vanilla thaumcraft tabs, i.e. no change.
tc4tweaks.config.client.addClearButton=Add Clear Notification Button
tc4tweaks.config.client.addClearButton.tooltip=If true, a button will be shown when there is any amount of tc4 notifications AND when sending chat.
tc4tweaks.config.client.completionCounterStyle=Completion Counter Style
tc4tweaks.config.client.completionCounterStyle.tooltip=Select the style of completion counter. Default: Current. None: disable completion progress counter. Current: display how many you have completed already, and only show the total count for this tab when everything here has been learnt. All: show all counters at all times.
tc4tweaks.config.general=General Configurations
tc4tweaks.config.general.tooltip=Configurations that can alter the mechanisms of the game
tc4tweaks.config.general.arcaneCraftingHistorySize=Arcane Crafting History Size
Expand Down

0 comments on commit 04b9ffb

Please sign in to comment.