From aa9ad53739e01edc6453c0ffbeb7d9903965df08 Mon Sep 17 00:00:00 2001 From: Vasco Lavos Date: Fri, 13 Oct 2023 12:55:07 +0100 Subject: [PATCH] Prepare variant tooltips --- .../config/BlockSuffixConfig.java | 7 ++++ .../module/VariantSelectorModule.java | 40 +++++++++++++++++++ .../resources/assets/quark/lang/en_us.json | 2 + 3 files changed, 49 insertions(+) diff --git a/src/main/java/vazkii/quark/content/experimental/config/BlockSuffixConfig.java b/src/main/java/vazkii/quark/content/experimental/config/BlockSuffixConfig.java index 497e43222c..0ba5f32c4d 100644 --- a/src/main/java/vazkii/quark/content/experimental/config/BlockSuffixConfig.java +++ b/src/main/java/vazkii/quark/content/experimental/config/BlockSuffixConfig.java @@ -2,11 +2,14 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.apache.commons.compress.utils.Lists; + import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; @@ -136,6 +139,10 @@ public Block getBlockForVariant(Block block, String variant) { return block; } + public Collection getAllVariants(Block block) { + return blockVariants.containsKey(block) ? blockVariants.get(block).variants.values() : Lists.newArrayList(); + } + public Block getOriginalBlock(Block block) { return originals.containsKey(block) ? originals.get(block) : block; } diff --git a/src/main/java/vazkii/quark/content/experimental/module/VariantSelectorModule.java b/src/main/java/vazkii/quark/content/experimental/module/VariantSelectorModule.java index a5d29f9b28..b5b638473d 100644 --- a/src/main/java/vazkii/quark/content/experimental/module/VariantSelectorModule.java +++ b/src/main/java/vazkii/quark/content/experimental/module/VariantSelectorModule.java @@ -1,18 +1,25 @@ package vazkii.quark.content.experimental.module; import java.util.Arrays; +import java.util.List; +import java.util.function.Function; import org.lwjgl.glfw.GLFW; import com.mojang.blaze3d.platform.Window; +import com.mojang.datafixers.util.Either; import net.minecraft.client.KeyMapping; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.Screen; import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.FormattedText; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; @@ -25,8 +32,10 @@ import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.client.event.InputEvent.Key; +import net.minecraftforge.client.event.RegisterClientTooltipComponentFactoriesEvent; import net.minecraftforge.client.event.RegisterKeyMappingsEvent; import net.minecraftforge.client.event.RenderGuiOverlayEvent; +import net.minecraftforge.client.event.RenderTooltipEvent; import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay; import net.minecraftforge.event.TickEvent.ClientTickEvent; import net.minecraftforge.event.TickEvent.Phase; @@ -41,6 +50,7 @@ import vazkii.quark.base.network.QuarkNetwork; import vazkii.quark.base.network.message.experimental.PlaceVariantUpdateMessage; import vazkii.quark.content.experimental.client.screen.VariantSelectorScreen; +import vazkii.quark.content.experimental.client.tooltip.VariantsComponent; import vazkii.quark.content.experimental.config.BlockSuffixConfig; import vazkii.quark.content.experimental.item.HammerItem; @@ -58,6 +68,9 @@ public class VariantSelectorModule extends QuarkModule { @Config(flag = "hammer", description = "Enable the hammer, allowing variants to be swapped between eachother, including the original block. Do this ONLY under the same circumstances as Convert Variant Items.") public static boolean enableHammer = false; + + @Config + public static boolean showTooltip = true; @Config public static BlockSuffixConfig variants = new BlockSuffixConfig( @@ -164,6 +177,33 @@ public void keystroke(Key event) { } } + @Override + @OnlyIn(Dist.CLIENT) + public void registerClientTooltipComponentFactories(RegisterClientTooltipComponentFactoriesEvent event) { + event.register(VariantsComponent.class, Function.identity()); + } + + @SubscribeEvent + @OnlyIn(Dist.CLIENT) + public void gatherComponents(RenderTooltipEvent.GatherComponents event) { + ItemStack stack = event.getItemStack(); + if(hasTooltip(stack)) { + List> elements = event.getTooltipElements(); + int index = 1; + + if(Screen.hasShiftDown()) { + elements.add(index, Either.left(Component.translatable("quark.misc.variant_tooltip_header", "TEST"))); + elements.add(index + 1, Either.right(new VariantsComponent(stack))); + } + else + elements.add(index, Either.left(Component.translatable("quark.misc.variant_tooltip_hold_shift"))); + } + } + + private boolean hasTooltip(ItemStack stack) { + return !stack.isEmpty() && stack.getItem() instanceof BlockItem bi && !variants.getAllVariants(bi.getBlock()).isEmpty(); + } + @SubscribeEvent @OnlyIn(Dist.CLIENT) public void clientTick(ClientTickEvent event) { diff --git a/src/main/resources/assets/quark/lang/en_us.json b/src/main/resources/assets/quark/lang/en_us.json index 907ba9e2e8..b17da0ea07 100644 --- a/src/main/resources/assets/quark/lang/en_us.json +++ b/src/main/resources/assets/quark/lang/en_us.json @@ -454,6 +454,8 @@ "quark.misc.quill_retry": "A Pathfinder's Quill could not find any biome nearby. Try somewhere else.", "quark.misc.quill_blank": "Blank", "quark.misc.quill_searching": "Searching", + "quark.misc.variant_tooltip_hold_shift": "Has Variants (Hold SHIFT)", + "quark.misc.variant_tooltip_header": "Variants (Place with [%s])", "quark.misc.you_came_to_the_wrong_neighborhood.0": "Six letter word just to get me along", "quark.misc.you_came_to_the_wrong_neighborhood.1": "It's a intricacy and I'm coding on my mod and I,",