Skip to content

Commit

Permalink
Prepare variant tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
Vazkii committed Oct 13, 2023
1 parent 35babe9 commit aa9ad53
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -136,6 +139,10 @@ public Block getBlockForVariant(Block block, String variant) {
return block;
}

public Collection<Block> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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(
Expand Down Expand Up @@ -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<Either<FormattedText, TooltipComponent>> 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) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/quark/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,",
Expand Down

0 comments on commit aa9ad53

Please sign in to comment.