2019-06-12 22:27:09 +01:00
|
|
|
/*
|
|
|
|
* This file is part of TechReborn, licensed under the MIT License (MIT).
|
|
|
|
*
|
2020-01-03 22:46:07 +00:00
|
|
|
* Copyright (c) 2020 TechReborn
|
2019-06-12 22:27:09 +01:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2021-12-30 20:42:51 +01:00
|
|
|
import com.google.common.collect.Lists;
|
2020-05-26 19:52:58 +08:00
|
|
|
import com.google.common.collect.Maps;
|
2020-10-29 17:24:38 +03:00
|
|
|
import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;
|
2019-06-12 22:27:09 +01:00
|
|
|
import net.minecraft.block.Block;
|
2021-04-18 19:47:29 +02:00
|
|
|
import net.minecraft.client.MinecraftClient;
|
2019-06-12 22:27:09 +01:00
|
|
|
import net.minecraft.client.gui.screen.Screen;
|
|
|
|
import net.minecraft.client.item.TooltipContext;
|
2022-01-02 10:12:27 +01:00
|
|
|
import net.minecraft.fluid.FlowableFluid;
|
|
|
|
import net.minecraft.fluid.Fluid;
|
|
|
|
import net.minecraft.fluid.Fluids;
|
2019-06-12 22:27:09 +01:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2021-11-28 14:14:35 +00:00
|
|
|
import net.minecraft.text.LiteralText;
|
2019-08-08 23:35:27 +01:00
|
|
|
import net.minecraft.text.Text;
|
2021-11-28 14:14:35 +00:00
|
|
|
import net.minecraft.text.TranslatableText;
|
|
|
|
import net.minecraft.util.Formatting;
|
2019-06-12 22:27:09 +01:00
|
|
|
import net.minecraft.util.registry.Registry;
|
2019-12-23 09:53:01 +11:00
|
|
|
import reborncore.common.BaseBlockEntityProvider;
|
|
|
|
import techreborn.init.TRContent;
|
2022-01-02 10:12:27 +01:00
|
|
|
import techreborn.items.DynamicCellItem;
|
2020-01-12 19:17:35 -08:00
|
|
|
import techreborn.items.UpgradeItem;
|
2020-01-04 14:20:34 +11:00
|
|
|
import techreborn.utils.ToolTipAssistUtils;
|
2022-01-28 20:14:35 +00:00
|
|
|
import techreborn.world.OreDepth;
|
2021-11-28 14:14:35 +00:00
|
|
|
import techreborn.world.TargetDimension;
|
2019-06-12 22:27:09 +01:00
|
|
|
|
|
|
|
import java.util.List;
|
2022-01-28 20:14:35 +00:00
|
|
|
import java.util.Locale;
|
2020-05-26 19:52:58 +08:00
|
|
|
import java.util.Map;
|
2019-06-12 22:27:09 +01:00
|
|
|
|
|
|
|
public class StackToolTipHandler implements ItemTooltipCallback {
|
|
|
|
|
2020-05-26 19:52:58 +08:00
|
|
|
public static final Map<Item, Boolean> ITEM_ID = Maps.newHashMap();
|
2021-12-30 20:42:51 +01:00
|
|
|
private static final List<Block> UNOBTAINABLE_ORES = Lists.newLinkedList();
|
2020-01-04 16:18:34 +11:00
|
|
|
|
2019-06-12 22:27:09 +01:00
|
|
|
public static void setup() {
|
|
|
|
ItemTooltipCallback.EVENT.register(new StackToolTipHandler());
|
2021-11-28 14:14:35 +00:00
|
|
|
|
|
|
|
for (TRContent.Ores ore : TRContent.Ores.values()) {
|
2021-12-30 20:42:51 +01:00
|
|
|
if (ore.isDeepslate()) {
|
|
|
|
TRContent.Ores normal = ore.getUnDeepslate();
|
|
|
|
if (normal.distribution != null && normal.distribution.dimension != TargetDimension.OVERWORLD)
|
|
|
|
UNOBTAINABLE_ORES.add(ore.block);
|
2021-11-28 14:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-12 22:27:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-29 17:24:38 +03:00
|
|
|
public void getTooltip(ItemStack stack, TooltipContext tooltipContext, List<Text> tooltipLines) {
|
2019-06-12 22:27:09 +01:00
|
|
|
Item item = stack.getItem();
|
2020-07-09 23:04:42 +10:00
|
|
|
|
2021-04-18 19:47:29 +02:00
|
|
|
// 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))
|
2020-05-26 19:52:58 +08:00
|
|
|
return;
|
2020-07-09 23:04:42 +10:00
|
|
|
|
2019-12-23 09:53:01 +11:00
|
|
|
// Machine info and upgrades helper section
|
|
|
|
Block block = Block.getBlockFromItem(item);
|
|
|
|
|
2020-07-09 23:04:42 +10:00
|
|
|
if (block instanceof BaseBlockEntityProvider) {
|
2020-10-29 17:24:38 +03:00
|
|
|
ToolTipAssistUtils.addInfo(item.getTranslationKey(), tooltipLines);
|
2019-12-23 09:53:01 +11:00
|
|
|
}
|
|
|
|
|
2021-05-29 20:32:05 +01:00
|
|
|
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()));
|
2019-12-23 09:53:01 +11:00
|
|
|
}
|
2021-11-28 14:14:35 +00:00
|
|
|
|
2022-01-02 10:12:27 +01:00
|
|
|
if (item instanceof DynamicCellItem cell) {
|
|
|
|
Fluid fluid = cell.getFluid(stack);
|
|
|
|
if (!(fluid instanceof FlowableFluid) && fluid != Fluids.EMPTY)
|
|
|
|
ToolTipAssistUtils.addInfo("unplaceable_fluid", tooltipLines, false);
|
|
|
|
}
|
|
|
|
|
2022-01-28 20:14:35 +00:00
|
|
|
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);
|
2021-12-30 20:42:51 +01:00
|
|
|
tooltipLines.add(text.copy().formatted(Formatting.AQUA));
|
2022-01-28 20:14:35 +00:00
|
|
|
}
|
2019-06-12 22:27:09 +01:00
|
|
|
}
|
2020-07-09 23:04:42 +10:00
|
|
|
|
2021-04-18 19:47:29 +02:00
|
|
|
private static boolean isTRItem(Item item) {
|
2020-05-26 19:52:58 +08:00
|
|
|
return Registry.ITEM.getId(item).getNamespace().equals("techreborn");
|
|
|
|
}
|
2021-11-28 14:14:35 +00:00
|
|
|
|
2022-01-28 20:14:35 +00:00
|
|
|
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)
|
2021-11-28 14:14:35 +00:00
|
|
|
);
|
|
|
|
}
|
2019-06-12 22:27:09 +01:00
|
|
|
}
|