Skip to content

Commit

Permalink
feat: add two more interception points for speaking hovered tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
boholder committed Jun 1, 2024
1 parent 0490509 commit 95f8d53
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import com.github.khanshoaib3.minecraft_access.MainClass;
import com.github.khanshoaib3.minecraft_access.config.config_maps.InventoryControlsConfigMap;
import net.minecraft.client.MinecraftClient;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.item.TooltipData;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -18,15 +19,17 @@

/**
* Speak hovered tooltip when Inventory Controls is disabled.
* Need to get text before they turned into "OrderedText" or "TooltipComponent"
* (can't easily extract text from these types).
* So we intercept every "drawTooltip" invoke that in "drawHoverEvent" method.
*/
@Mixin(DrawContext.class)
public class DrawContextMixin {
@Unique private static String minecraft_access$previousTooltipText = "";
@Unique
private static String minecraft_access$previousTooltipText = "";

@Inject(at = @At("HEAD"), method = "drawTooltip(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/text/Text;II)V")
private void speakHoveredTooltip(TextRenderer textRenderer, Text text, int x, int y, CallbackInfo ci) {
if (MinecraftClient.getInstance() == null) return;
if (MinecraftClient.getInstance().currentScreen == null) return;
if (InventoryControlsConfigMap.getInstance().isEnabled()) return;

minecraft_access$checkAndSpeak(text.getString());
Expand All @@ -35,8 +38,11 @@ private void speakHoveredTooltip(TextRenderer textRenderer, Text text, int x, in
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
@Inject(at = @At("HEAD"), method = "drawTooltip(Lnet/minecraft/client/font/TextRenderer;Ljava/util/List;Ljava/util/Optional;II)V")
private void speakHoveredTooltip2(TextRenderer textRenderer, List<Text> text, Optional<TooltipData> data, int x, int y, CallbackInfo ci) {
if (MinecraftClient.getInstance() == null) return;
if (MinecraftClient.getInstance().currentScreen == null) return;
minecraft_access$speakTextList(text);
}

@Unique
private static void minecraft_access$speakTextList(List<Text> text) {
if (InventoryControlsConfigMap.getInstance().isEnabled()) return;

StringBuilder toSpeak = new StringBuilder();
Expand All @@ -47,6 +53,18 @@ private void speakHoveredTooltip2(TextRenderer textRenderer, List<Text> text, Op
minecraft_access$checkAndSpeak(toSpeak.toString());
}

@Inject(at = @At("HEAD"), method = "drawTooltip(Lnet/minecraft/client/font/TextRenderer;Ljava/util/List;II)V")
private void speakHoveredTooltip3(TextRenderer textRenderer, List<Text> text, int x, int y, CallbackInfo ci) {
minecraft_access$speakTextList(text);
}

@Inject(method = "drawHoverEvent", at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/gui/DrawContext;drawOrderedTooltip(Lnet/minecraft/client/font/TextRenderer;Ljava/util/List;II)V"))
private void speakHoveredTooltip4(TextRenderer textRenderer, Style style, int x, int y, CallbackInfo ci, @Local Text text) {
if (InventoryControlsConfigMap.getInstance().isEnabled()) return;
minecraft_access$checkAndSpeak(text.getString());
}

@Unique
private static void minecraft_access$checkAndSpeak(String toSpeak) {
if (minecraft_access$previousTooltipText.equals(toSpeak)) return;
Expand Down

0 comments on commit 95f8d53

Please sign in to comment.