Skip to content

Commit

Permalink
fix issues with better tooltips when searching in the creative menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Wide-Cat committed Jul 6, 2024
1 parent e3df815 commit 2374b60
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,4 @@

import java.util.List;

public class ItemStackTooltipEvent {
private static final ItemStackTooltipEvent INSTANCE = new ItemStackTooltipEvent();

public ItemStack itemStack;
public List<Text> list;

public static ItemStackTooltipEvent get(ItemStack itemStack, List<Text> list) {
INSTANCE.itemStack = itemStack;
INSTANCE.list = list;
return INSTANCE;
}
}
public record ItemStackTooltipEvent(ItemStack itemStack, List<Text> list) {}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public abstract class ItemStackMixin {
@ModifyReturnValue(method = "getTooltip", at = @At("RETURN"))
private List<Text> onGetTooltip(List<Text> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);

Expand All @@ -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()
);

Expand All @@ -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()
);

Expand All @@ -105,13 +108,15 @@ 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()
);

private final Setting<Boolean> maps = sgPreviews.add(new BoolSetting.Builder()
.name("maps")
.description("Shows a preview of a map when hovering over it in an inventory.")
.defaultValue(true)
.onChanged(value -> updateTooltips = true)
.build()
);

Expand All @@ -129,20 +134,23 @@ 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()
);

private final Setting<Boolean> banners = sgPreviews.add(new BoolSetting.Builder()
.name("banners")
.description("Shows banners' patterns when hovering over it in an inventory. Also works with shields.")
.defaultValue(true)
.onChanged(value -> updateTooltips = true)
.build()
);

private final Setting<Boolean> entitiesInBuckets = sgPreviews.add(new BoolSetting.Builder()
.name("entities-in-buckets")
.description("Shows entities in buckets when hovering over it in an inventory.")
.defaultValue(true)
.onChanged(value -> updateTooltips = true)
.build()
);

Expand All @@ -152,20 +160,23 @@ 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()
);

private final Setting<Boolean> statusEffects = sgOther.add(new BoolSetting.Builder()
.name("status-effects")
.description("Adds list of status effects to tooltips of food items.")
.defaultValue(true)
.onChanged(value -> updateTooltips = true)
.build()
);

private final Setting<Boolean> beehive = sgOther.add(new BoolSetting.Builder()
.name("beehive")
.description("Displays information about a beehive or bee nest.")
.defaultValue(true)
.onChanged(value -> updateTooltips = true)
.build()
);

Expand Down Expand Up @@ -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.");
}
Expand All @@ -242,42 +255,42 @@ 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<BeehiveBlockEntity.BeeData> bees = event.itemStack.get(DataComponentTypes.BEES);
List<BeehiveBlockEntity.BeeData> 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)));
}
}
}

// 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;
Expand All @@ -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"));
}
}

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/meteor-client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"ItemEnchantmentsComponentMixin",
"ItemEntityMixin",
"ItemEntityRendererMixin",
"ItemGroupsMixin",
"ItemMixin",
"ItemRendererMixin",
"ItemStackAccessor",
Expand Down

0 comments on commit 2374b60

Please sign in to comment.