TechReborn/src/main/java/techreborn/events/StackToolTipHandler.java

123 lines
4.8 KiB
Java
Raw Normal View History

/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
2020-01-03 22:46:07 +00:00
* Copyright (c) 2020 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.events;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
2020-10-29 17:24:38 +03:00
import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;
import net.minecraft.block.Block;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.fluid.FlowableFluid;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.registry.Registry;
import reborncore.common.BaseBlockEntityProvider;
import techreborn.init.TRContent;
import techreborn.items.DynamicCellItem;
import techreborn.items.UpgradeItem;
import techreborn.utils.ToolTipAssistUtils;
import techreborn.world.OreDepth;
import techreborn.world.TargetDimension;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class StackToolTipHandler implements ItemTooltipCallback {
public static final Map<Item, Boolean> ITEM_ID = Maps.newHashMap();
private static final List<Block> UNOBTAINABLE_ORES = Lists.newLinkedList();
public static void setup() {
ItemTooltipCallback.EVENT.register(new StackToolTipHandler());
for (TRContent.Ores ore : TRContent.Ores.values()) {
if (ore.isDeepslate()) {
TRContent.Ores normal = ore.getUnDeepslate();
if (normal.distribution != null && normal.distribution.dimension != TargetDimension.OVERWORLD)
UNOBTAINABLE_ORES.add(ore.block);
}
}
}
@Override
2020-10-29 17:24:38 +03:00
public void getTooltip(ItemStack stack, TooltipContext tooltipContext, List<Text> tooltipLines) {
Item item = stack.getItem();
// Can currently be executed by a ForkJoinPool.commonPool-worker when REI is in async search mode
// We skip this method until a thread-safe solution is in place
if (!MinecraftClient.getInstance().isOnThread())
return;
if (!ITEM_ID.computeIfAbsent(item, StackToolTipHandler::isTRItem))
return;
// Machine info and upgrades helper section
Block block = Block.getBlockFromItem(item);
if (block instanceof BaseBlockEntityProvider) {
2020-10-29 17:24:38 +03:00
ToolTipAssistUtils.addInfo(item.getTranslationKey(), tooltipLines);
}
if (item instanceof UpgradeItem upgrade) {
2020-10-29 17:24:38 +03:00
ToolTipAssistUtils.addInfo(item.getTranslationKey(), tooltipLines, false);
tooltipLines.addAll(ToolTipAssistUtils.getUpgradeStats(TRContent.Upgrades.valueOf(upgrade.name.toUpperCase()), stack.getCount(), Screen.hasShiftDown()));
}
if (item instanceof DynamicCellItem cell) {
Fluid fluid = cell.getFluid(stack);
if (!(fluid instanceof FlowableFluid) && fluid != Fluids.EMPTY)
ToolTipAssistUtils.addInfo("unplaceable_fluid", tooltipLines, false);
}
if (UNOBTAINABLE_ORES.contains(block)) {
tooltipLines.add(new TranslatableText("techreborn.tooltip.unobtainable").formatted(Formatting.AQUA));
} else if (OreDepthSyncHandler.getOreDepthMap().containsKey(block)) {
OreDepth oreDepth = OreDepthSyncHandler.getOreDepthMap().get(block);
Text text = getOreDepthText(oreDepth);
tooltipLines.add(text.copy().formatted(Formatting.AQUA));
}
}
private static boolean isTRItem(Item item) {
return Registry.ITEM.getId(item).getNamespace().equals("techreborn");
}
private static TranslatableText getOreDepthText(OreDepth depth) {
return new TranslatableText("techreborn.tooltip.ores.%s".formatted(depth.dimension().name().toLowerCase(Locale.ROOT)),
new LiteralText(String.valueOf(depth.minY())).formatted(Formatting.YELLOW),
new LiteralText(String.valueOf(depth.maxY())).formatted(Formatting.YELLOW)
);
}
}