From 2374b609bc4132f759a53fe20b70a3d37f74c75f Mon Sep 17 00:00:00 2001 From: Wide-Cat Date: Sat, 6 Jul 2024 22:24:51 +0100 Subject: [PATCH] fix issues with better tooltips when searching in the creative menu closes #4692 --- .../events/game/ItemStackTooltipEvent.java | 13 +--- .../meteorclient/mixin/ItemGroupsMixin.java | 21 ++++++ .../meteorclient/mixin/ItemStackMixin.java | 4 +- .../modules/render/BetterTooltips.java | 70 ++++++++++++------- src/main/resources/meteor-client.mixins.json | 1 + 5 files changed, 71 insertions(+), 38 deletions(-) create mode 100644 src/main/java/meteordevelopment/meteorclient/mixin/ItemGroupsMixin.java diff --git a/src/main/java/meteordevelopment/meteorclient/events/game/ItemStackTooltipEvent.java b/src/main/java/meteordevelopment/meteorclient/events/game/ItemStackTooltipEvent.java index d6046e2785..c0cf9c5f97 100644 --- a/src/main/java/meteordevelopment/meteorclient/events/game/ItemStackTooltipEvent.java +++ b/src/main/java/meteordevelopment/meteorclient/events/game/ItemStackTooltipEvent.java @@ -10,15 +10,4 @@ import java.util.List; -public class ItemStackTooltipEvent { - private static final ItemStackTooltipEvent INSTANCE = new ItemStackTooltipEvent(); - - public ItemStack itemStack; - public List list; - - public static ItemStackTooltipEvent get(ItemStack itemStack, List list) { - INSTANCE.itemStack = itemStack; - INSTANCE.list = list; - return INSTANCE; - } -} +public record ItemStackTooltipEvent(ItemStack itemStack, List list) {} diff --git a/src/main/java/meteordevelopment/meteorclient/mixin/ItemGroupsMixin.java b/src/main/java/meteordevelopment/meteorclient/mixin/ItemGroupsMixin.java new file mode 100644 index 0000000000..daa0577e9a --- /dev/null +++ b/src/main/java/meteordevelopment/meteorclient/mixin/ItemGroupsMixin.java @@ -0,0 +1,21 @@ +/* + * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client). + * Copyright (c) Meteor Development. + */ + +package meteordevelopment.meteorclient.mixin; + +import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import meteordevelopment.meteorclient.systems.modules.Modules; +import meteordevelopment.meteorclient.systems.modules.render.BetterTooltips; +import net.minecraft.item.ItemGroups; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(ItemGroups.class) +public class ItemGroupsMixin { + @ModifyReturnValue(method = "updateDisplayContext", at = @At("RETURN")) + private static boolean modifyReturn(boolean original) { + return original || Modules.get().get(BetterTooltips.class).updateTooltips(); + } +} diff --git a/src/main/java/meteordevelopment/meteorclient/mixin/ItemStackMixin.java b/src/main/java/meteordevelopment/meteorclient/mixin/ItemStackMixin.java index cc139b4f24..fac7631650 100644 --- a/src/main/java/meteordevelopment/meteorclient/mixin/ItemStackMixin.java +++ b/src/main/java/meteordevelopment/meteorclient/mixin/ItemStackMixin.java @@ -33,8 +33,8 @@ public abstract class ItemStackMixin { @ModifyReturnValue(method = "getTooltip", at = @At("RETURN")) private List onGetTooltip(List original) { if (Utils.canUpdate()) { - ItemStackTooltipEvent event = MeteorClient.EVENT_BUS.post(ItemStackTooltipEvent.get((ItemStack) (Object) this, original)); - return event.list; + ItemStackTooltipEvent event = MeteorClient.EVENT_BUS.post(new ItemStackTooltipEvent((ItemStack) (Object) this, original)); + return event.list(); } return original; diff --git a/src/main/java/meteordevelopment/meteorclient/systems/modules/render/BetterTooltips.java b/src/main/java/meteordevelopment/meteorclient/systems/modules/render/BetterTooltips.java index 610e7952c4..9336b51568 100644 --- a/src/main/java/meteordevelopment/meteorclient/systems/modules/render/BetterTooltips.java +++ b/src/main/java/meteordevelopment/meteorclient/systems/modules/render/BetterTooltips.java @@ -58,6 +58,7 @@ public class BetterTooltips extends Module { .name("display-when") .description("When to display previews.") .defaultValue(DisplayWhen.Keybind) + .onChanged(value -> updateTooltips = true) .build() ); @@ -66,6 +67,7 @@ public class BetterTooltips extends Module { .description("The bind for keybind mode.") .defaultValue(Keybind.fromKey(GLFW_KEY_LEFT_ALT)) .visible(() -> displayWhen.get() == DisplayWhen.Keybind) + .onChanged(value -> updateTooltips = true) .build() ); @@ -90,6 +92,7 @@ public class BetterTooltips extends Module { .name("containers") .description("Shows a preview of a containers when hovering over it in an inventory.") .defaultValue(true) + .onChanged(value -> updateTooltips = true) .build() ); @@ -105,6 +108,7 @@ public class BetterTooltips extends Module { .name("echests") .description("Shows a preview of your echest when hovering over it in an inventory.") .defaultValue(true) + .onChanged(value -> updateTooltips = true) .build() ); @@ -112,6 +116,7 @@ public class BetterTooltips extends Module { .name("maps") .description("Shows a preview of a map when hovering over it in an inventory.") .defaultValue(true) + .onChanged(value -> updateTooltips = true) .build() ); @@ -129,6 +134,7 @@ public class BetterTooltips extends Module { .name("books") .description("Shows contents of a book when hovering over it in an inventory.") .defaultValue(true) + .onChanged(value -> updateTooltips = true) .build() ); @@ -136,6 +142,7 @@ public class BetterTooltips extends Module { .name("banners") .description("Shows banners' patterns when hovering over it in an inventory. Also works with shields.") .defaultValue(true) + .onChanged(value -> updateTooltips = true) .build() ); @@ -143,6 +150,7 @@ public class BetterTooltips extends Module { .name("entities-in-buckets") .description("Shows entities in buckets when hovering over it in an inventory.") .defaultValue(true) + .onChanged(value -> updateTooltips = true) .build() ); @@ -152,6 +160,7 @@ public class BetterTooltips extends Module { .name("byte-size") .description("Displays an item's size in bytes in the tooltip.") .defaultValue(true) + .onChanged(value -> updateTooltips = true) .build() ); @@ -159,6 +168,7 @@ public class BetterTooltips extends Module { .name("status-effects") .description("Adds list of status effects to tooltips of food items.") .defaultValue(true) + .onChanged(value -> updateTooltips = true) .build() ); @@ -166,6 +176,7 @@ public class BetterTooltips extends Module { .name("beehive") .description("Displays information about a beehive or bee nest.") .defaultValue(true) + .onChanged(value -> updateTooltips = true) .build() ); @@ -234,6 +245,8 @@ public class BetterTooltips extends Module { .build() ); + private boolean updateTooltips = false; + public BetterTooltips() { super(Categories.Render, "better-tooltips", "Displays more useful tooltips for certain items."); } @@ -242,34 +255,34 @@ public BetterTooltips() { private void appendTooltip(ItemStackTooltipEvent event) { // Status effects if (statusEffects.get()) { - if (event.itemStack.getItem() == Items.SUSPICIOUS_STEW) { - SuspiciousStewEffectsComponent stewEffectsComponent = event.itemStack.get(DataComponentTypes.SUSPICIOUS_STEW_EFFECTS); + if (event.itemStack().getItem() == Items.SUSPICIOUS_STEW) { + SuspiciousStewEffectsComponent stewEffectsComponent = event.itemStack().get(DataComponentTypes.SUSPICIOUS_STEW_EFFECTS); if (stewEffectsComponent != null) { for (StewEffect effectTag : stewEffectsComponent.effects()) { StatusEffectInstance effect = new StatusEffectInstance(effectTag.effect(), effectTag.duration(), 0); - event.list.add(1, getStatusText(effect)); + event.list().add(1, getStatusText(effect)); } } } else { - FoodComponent food = event.itemStack.get(DataComponentTypes.FOOD); + FoodComponent food = event.itemStack().get(DataComponentTypes.FOOD); if (food != null) { - food.effects().forEach(e -> event.list.add(1, getStatusText(e.effect()))); + food.effects().forEach(e -> event.list().add(1, getStatusText(e.effect()))); } } } //Beehive if (beehive.get()) { - if (event.itemStack.getItem() == Items.BEEHIVE || event.itemStack.getItem() == Items.BEE_NEST) { - BlockStateComponent blockStateComponent = event.itemStack.get(DataComponentTypes.BLOCK_STATE); + if (event.itemStack().getItem() == Items.BEEHIVE || event.itemStack().getItem() == Items.BEE_NEST) { + BlockStateComponent blockStateComponent = event.itemStack().get(DataComponentTypes.BLOCK_STATE); if (blockStateComponent != null) { String level = blockStateComponent.properties().get("honey_level"); - event.list.add(1, Text.literal(String.format("%sHoney level: %s%s%s.", Formatting.GRAY, Formatting.YELLOW, level, Formatting.GRAY))); + event.list().add(1, Text.literal(String.format("%sHoney level: %s%s%s.", Formatting.GRAY, Formatting.YELLOW, level, Formatting.GRAY))); } - List bees = event.itemStack.get(DataComponentTypes.BEES); + List bees = event.itemStack().get(DataComponentTypes.BEES); if (bees != null) { - event.list.add(1, Text.literal(String.format("%sBees: %s%d%s.", Formatting.GRAY, Formatting.YELLOW, bees.size(), Formatting.GRAY))); + event.list().add(1, Text.literal(String.format("%sBees: %s%d%s.", Formatting.GRAY, Formatting.YELLOW, bees.size(), Formatting.GRAY))); } } } @@ -277,7 +290,7 @@ private void appendTooltip(ItemStackTooltipEvent event) { // Item size tooltip if (byteSize.get()) { try { - event.itemStack.encode(mc.player.getRegistryManager()).write(ByteCountDataOutput.INSTANCE); + event.itemStack().encode(mc.player.getRegistryManager()).write(ByteCountDataOutput.INSTANCE); int byteCount = ByteCountDataOutput.INSTANCE.getCount(); String count; @@ -287,24 +300,24 @@ private void appendTooltip(ItemStackTooltipEvent event) { if (byteCount >= 1024) count = String.format("%.2f kb", byteCount / (float) 1024); else count = String.format("%d bytes", byteCount); - event.list.add(Text.literal(count).formatted(Formatting.GRAY)); + event.list().add(Text.literal(count).formatted(Formatting.GRAY)); } catch (Exception e) { - event.list.add(Text.literal("Error getting bytes.").formatted(Formatting.RED)); + event.list().add(Text.literal("Error getting bytes.").formatted(Formatting.RED)); } } // Hold to preview tooltip - if ((shulkers.get() && !previewShulkers() && Utils.hasItems(event.itemStack)) - || (event.itemStack.getItem() == Items.ENDER_CHEST && echest.get() && !previewEChest()) - || (event.itemStack.getItem() == Items.FILLED_MAP && maps.get() && !previewMaps()) - || (event.itemStack.getItem() == Items.WRITABLE_BOOK && books.get() && !previewBooks()) - || (event.itemStack.getItem() == Items.WRITTEN_BOOK && books.get() && !previewBooks()) - || (event.itemStack.getItem() instanceof EntityBucketItem && entitiesInBuckets.get() && !previewEntities()) - || (event.itemStack.getItem() instanceof BannerItem && banners.get() && !previewBanners()) - || (event.itemStack.getItem() instanceof BannerPatternItem && banners.get() && !previewBanners()) - || (event.itemStack.getItem() == Items.SHIELD && banners.get() && !previewBanners())) { - event.list.add(Text.literal("")); - event.list.add(Text.literal("Hold " + Formatting.YELLOW + keybind + Formatting.RESET + " to preview")); + if ((shulkers.get() && !previewShulkers() && Utils.hasItems(event.itemStack())) + || (event.itemStack().getItem() == Items.ENDER_CHEST && echest.get() && !previewEChest()) + || (event.itemStack().getItem() == Items.FILLED_MAP && maps.get() && !previewMaps()) + || (event.itemStack().getItem() == Items.WRITABLE_BOOK && books.get() && !previewBooks()) + || (event.itemStack().getItem() == Items.WRITTEN_BOOK && books.get() && !previewBooks()) + || (event.itemStack().getItem() instanceof EntityBucketItem && entitiesInBuckets.get() && !previewEntities()) + || (event.itemStack().getItem() instanceof BannerItem && banners.get() && !previewBanners()) + || (event.itemStack().getItem() instanceof BannerPatternItem && banners.get() && !previewBanners()) + || (event.itemStack().getItem() == Items.SHIELD && banners.get() && !previewBanners())) { + event.list().add(Text.literal("")); + event.list().add(Text.literal("Hold " + Formatting.YELLOW + keybind + Formatting.RESET + " to preview")); } } @@ -482,6 +495,15 @@ private boolean isPressed() { return (keybind.get().isPressed() && displayWhen.get() == DisplayWhen.Keybind) || displayWhen.get() == DisplayWhen.Always; } + public boolean updateTooltips() { + if (updateTooltips && isActive()) { + updateTooltips = false; + return true; + } + + return false; + } + public enum DisplayWhen { Keybind, Always diff --git a/src/main/resources/meteor-client.mixins.json b/src/main/resources/meteor-client.mixins.json index f1d012c785..02345053b9 100644 --- a/src/main/resources/meteor-client.mixins.json +++ b/src/main/resources/meteor-client.mixins.json @@ -108,6 +108,7 @@ "ItemEnchantmentsComponentMixin", "ItemEntityMixin", "ItemEntityRendererMixin", + "ItemGroupsMixin", "ItemMixin", "ItemRendererMixin", "ItemStackAccessor",