Add tooltip for ore generation depths.

Will only work in SP for now, will need to create a packet to send the data from the server.
This commit is contained in:
modmuss50 2021-11-28 14:14:35 +00:00
parent 5ce0881f56
commit 27c684d376
4 changed files with 87 additions and 5 deletions

View file

@ -32,12 +32,22 @@ import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.server.world.ServerWorld;
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 net.minecraft.world.World;
import net.minecraft.world.gen.HeightContext;
import org.jetbrains.annotations.Nullable;
import reborncore.common.BaseBlockEntityProvider;
import techreborn.init.TRContent;
import techreborn.items.UpgradeItem;
import techreborn.utils.ToolTipAssistUtils;
import techreborn.world.OreDistribution;
import techreborn.world.TargetDimension;
import java.util.List;
import java.util.Map;
@ -45,9 +55,28 @@ import java.util.Map;
public class StackToolTipHandler implements ItemTooltipCallback {
public static final Map<Item, Boolean> ITEM_ID = Maps.newHashMap();
private static final Map<Block, OreDistribution> ORE_DISTRIBUTION_MAP = Maps.newHashMap();
public static void setup() {
ItemTooltipCallback.EVENT.register(new StackToolTipHandler());
for (TRContent.Ores ore : TRContent.Ores.values()) {
if (ore.isDeepslate()) continue;
if (ore.distribution != null) {
ORE_DISTRIBUTION_MAP.put(ore.block, ore.distribution);
if (ore.distribution.dimension != TargetDimension.OVERWORLD) {
continue; // No Deepslate in other dims
}
TRContent.Ores deepslate = ore.getDeepslate();
if (deepslate != null) {
// Deepslate shares the same distribution as the stone version.
ORE_DISTRIBUTION_MAP.put(deepslate.block, ore.distribution);
}
}
}
}
@Override
@ -70,13 +99,59 @@ public class StackToolTipHandler implements ItemTooltipCallback {
}
if (item instanceof UpgradeItem upgrade) {
ToolTipAssistUtils.addInfo(item.getTranslationKey(), tooltipLines, false);
tooltipLines.addAll(ToolTipAssistUtils.getUpgradeStats(TRContent.Upgrades.valueOf(upgrade.name.toUpperCase()), stack.getCount(), Screen.hasShiftDown()));
}
OreDistribution oreDistribution = ORE_DISTRIBUTION_MAP.get(block);
if (oreDistribution != null) {
Text text = switch (oreDistribution.dimension) {
case OVERWORLD -> getOverworldOreText(oreDistribution);
case END -> new TranslatableText("techreborn.tooltip.ores.end");
case NETHER -> new TranslatableText("techreborn.tooltip.ores.nether");
};
if (text != null)
tooltipLines.add(text.copy().formatted(Formatting.AQUA));
}
}
private static boolean isTRItem(Item item) {
return Registry.ITEM.getId(item).getNamespace().equals("techreborn");
}
@Nullable
private static HeightContext getHeightContextSafely() {
final IntegratedServer server = MinecraftClient.getInstance().getServer();
if (server == null) {
return null;
}
final ServerWorld world = server.getWorld(World.OVERWORLD);
if (world == null) {
return null;
}
return new HeightContext(world.getChunkManager().getChunkGenerator(), world);
}
@Nullable
private static Text getOverworldOreText(OreDistribution oreDistribution) {
final HeightContext heightContext = getHeightContextSafely();
if (heightContext == null) {
return null;
}
final int minY = oreDistribution.minOffset.getY(heightContext);
final int maxY = oreDistribution.maxY;
return new TranslatableText("techreborn.tooltip.ores.overworld",
new LiteralText(String.valueOf(minY)).formatted(Formatting.YELLOW),
new LiteralText(String.valueOf(maxY)).formatted(Formatting.YELLOW)
);
}
}

View file

@ -432,10 +432,14 @@ public class TRContent {
return block.asItem();
}
public TRContent.Ores getDeeplate() {
Preconditions.checkArgument(!name.startsWith("deepslate_"));
public TRContent.Ores getDeepslate() {
Preconditions.checkArgument(!isDeepslate());
return deepslateMap.get(this);
}
public boolean isDeepslate() {
return name.startsWith("deepslate_");
}
}
public enum StorageBlocks implements ItemConvertible {

View file

@ -65,10 +65,10 @@ public class OreFeature {
}
private OreFeatureConfig createOverworldFeatureConfig() {
if (this.ore.getDeeplate() != null) {
if (this.ore.getDeepslate() != null) {
return new OreFeatureConfig(List.of(
OreFeatureConfig.createTarget(OreConfiguredFeatures.STONE_ORE_REPLACEABLES, this.ore.block.getDefaultState()),
OreFeatureConfig.createTarget(OreConfiguredFeatures.DEEPSLATE_ORE_REPLACEABLES, this.ore.getDeeplate().block.getDefaultState())
OreFeatureConfig.createTarget(OreConfiguredFeatures.DEEPSLATE_ORE_REPLACEABLES, this.ore.getDeepslate().block.getDefaultState())
), ore.distribution.veinSize);
}

View file

@ -867,6 +867,9 @@
"techreborn.tooltip.stack_info": "Hold Shift for stack info",
"techreborn.tooltip.omnitool_motto" : "Swiss Army Knife",
"techreborn.tooltip.fusion_coil" : "Right click Fusion Control computer to auto place",
"techreborn.tooltip.ores.overworld" : "Can be found at y levels %s < %s",
"techreborn.tooltip.ores.nether" : "Can be found in the nether",
"techreborn.tooltip.ores.end" : "Can be found in the end",
"_comment23": "ManualUI",
"techreborn.manual.wiki": "Online wiki",