diff --git a/src/generated/resources/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 b/src/generated/resources/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 index 3f7e21a6..daf08e0a 100644 --- a/src/generated/resources/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 +++ b/src/generated/resources/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 @@ -1,2 +1,2 @@ -// 1.21.1 2024-11-15T20:14:53.378044013 Language -a77898c0ee75796bccabca2cf157f495702102d1 assets/megacells/lang/en_us.json +// 1.21.1 2024-11-17T14:07:31.916554252 Language +79f2bdffe2659a931e47c3e340144ada20c70b08 assets/megacells/lang/en_us.json diff --git a/src/generated/resources/assets/megacells/lang/en_us.json b/src/generated/resources/assets/megacells/lang/en_us.json index 072f9ef5..c92b2973 100644 --- a/src/generated/resources/assets/megacells/lang/en_us.json +++ b/src/generated/resources/assets/megacells/lang/en_us.json @@ -28,6 +28,8 @@ "gui.tooltips.megacells.PartitionedFor": "Partitioned for: %s", "gui.tooltips.megacells.ProcessingOnly": "Supports processing patterns only.", "gui.tooltips.megacells.Quantity": "Quantity: %s", + "gui.tooltips.megacells.WorkbenchCell": "Cell:", + "gui.tooltips.megacells.WorkbenchConfig": "Config:", "item.megacells.accumulation_processor": "Accumulation Processor", "item.megacells.accumulation_processor_press": "Inscriber Accumulation Press", "item.megacells.bulk_cell_component": "MEGA Bulk Storage Component", diff --git a/src/main/java/gripe/_90/megacells/client/MEGACellsClient.java b/src/main/java/gripe/_90/megacells/client/MEGACellsClient.java index 2f2421d4..e20254f5 100644 --- a/src/main/java/gripe/_90/megacells/client/MEGACellsClient.java +++ b/src/main/java/gripe/_90/megacells/client/MEGACellsClient.java @@ -10,6 +10,7 @@ import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.common.Mod; import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.neoforge.client.event.RegisterClientTooltipComponentFactoriesEvent; import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent; import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent; @@ -28,12 +29,14 @@ import gripe._90.megacells.MEGACells; import gripe._90.megacells.block.MEGACraftingUnitType; import gripe._90.megacells.client.render.MEGACraftingUnitModelProvider; +import gripe._90.megacells.client.render.PortableCellWorkbenchClientTooltipComponent; import gripe._90.megacells.client.screen.CellDockScreen; import gripe._90.megacells.client.screen.PortableCellWorkbenchScreen; import gripe._90.megacells.definition.MEGABlockEntities; import gripe._90.megacells.definition.MEGABlocks; import gripe._90.megacells.definition.MEGAItems; import gripe._90.megacells.definition.MEGAMenus; +import gripe._90.megacells.item.cell.PortableCellWorkbenchTooltipComponent; import gripe._90.megacells.menu.MEGAInterfaceMenu; import gripe._90.megacells.menu.MEGAPatternProviderMenu; @@ -45,6 +48,7 @@ public MEGACellsClient(IEventBus eventBus) { eventBus.addListener(MEGACellsClient::initEnergyCellProps); eventBus.addListener(MEGACellsClient::initStorageCellModels); eventBus.addListener(MEGACellsClient::initItemColours); + eventBus.addListener(MEGACellsClient::initTooltipComponents); } private static void initScreens(RegisterMenuScreensEvent event) { @@ -133,4 +137,8 @@ private static void initItemColours(RegisterColorHandlersEvent.Item event) { (stack, tintIndex) -> FastColor.ARGB32.opaque(PortableCellItem.getColor(stack, tintIndex)), portableCells.toArray(new ItemLike[0])); } + + private static void initTooltipComponents(RegisterClientTooltipComponentFactoriesEvent event) { + event.register(PortableCellWorkbenchTooltipComponent.class, PortableCellWorkbenchClientTooltipComponent::new); + } } diff --git a/src/main/java/gripe/_90/megacells/client/render/PortableCellWorkbenchClientTooltipComponent.java b/src/main/java/gripe/_90/megacells/client/render/PortableCellWorkbenchClientTooltipComponent.java new file mode 100644 index 00000000..c31c4294 --- /dev/null +++ b/src/main/java/gripe/_90/megacells/client/render/PortableCellWorkbenchClientTooltipComponent.java @@ -0,0 +1,161 @@ +package gripe._90.megacells.client.render; + +import org.jetbrains.annotations.NotNull; +import org.joml.Matrix4f; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; +import net.minecraft.client.renderer.LightTexture; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.network.chat.Component; + +import appeng.api.client.AEKeyRendering; +import appeng.items.storage.StorageCellTooltipComponent; + +import gripe._90.megacells.definition.MEGATranslations; +import gripe._90.megacells.item.cell.PortableCellWorkbenchTooltipComponent; + +public class PortableCellWorkbenchClientTooltipComponent implements ClientTooltipComponent { + private static final Component CELL_LABEL = MEGATranslations.WorkbenchCell.text(); + private static final Component CONFIG_LABEL = MEGATranslations.WorkbenchConfig.text(); + + private final PortableCellWorkbenchTooltipComponent tooltipComponent; + + public PortableCellWorkbenchClientTooltipComponent(PortableCellWorkbenchTooltipComponent tooltipComponent) { + this.tooltipComponent = tooltipComponent; + } + + @Override + public int getHeight() { + var height = 0; + + if (!tooltipComponent.config().isEmpty()) { + height += 17; + } + + var cellOpt = tooltipComponent.cell().getTooltipImage(); + + if (cellOpt.isPresent() && cellOpt.get() instanceof StorageCellTooltipComponent) { + height += 17; + } + return height; + } + + @Override + public int getWidth(@NotNull Font font) { + var width = 0; + + if (!tooltipComponent.config().isEmpty()) { + var configWidth = tooltipComponent.config().size() * 17; + + if (tooltipComponent.hasMoreConfig()) { + configWidth += 10; + } + + width = font.width(CONFIG_LABEL) + 2 + Math.max(width, configWidth); + } + + var cellOpt = tooltipComponent.cell().getTooltipImage(); + + if (cellOpt.isPresent() && cellOpt.get() instanceof StorageCellTooltipComponent cellComponent) { + width = Math.max( + width, + font.width(CELL_LABEL) + 2 + 17 * (cellComponent.upgrades().size() + 1)); + } + + return width; + } + + @Override + public void renderText( + @NotNull Font font, + int x, + int y, + @NotNull Matrix4f matrix, + @NotNull MultiBufferSource.BufferSource bufferSource) { + y += (16 - font.lineHeight) / 2; + + if (!tooltipComponent.config().isEmpty()) { + font.drawInBatch( + CONFIG_LABEL, + x, + y, + 0x7E7E7E, + false, + matrix, + bufferSource, + Font.DisplayMode.NORMAL, + 0, + LightTexture.FULL_BRIGHT); + + if (tooltipComponent.hasMoreConfig()) { + font.drawInBatch( + "…", + x + + font.width(CONFIG_LABEL) + + 4 + + tooltipComponent.config().size() * 17, + y + 2, + -1, + false, + matrix, + bufferSource, + Font.DisplayMode.NORMAL, + 0, + LightTexture.FULL_BRIGHT); + } + + y += 17; + } + + if (!tooltipComponent.cell().isEmpty()) { + font.drawInBatch( + CELL_LABEL, + x, + y, + 0x7E7E7E, + false, + matrix, + bufferSource, + Font.DisplayMode.NORMAL, + 0, + LightTexture.FULL_BRIGHT); + } + } + + @Override + public void renderImage(@NotNull Font font, int x, int y, @NotNull GuiGraphics guiGraphics) { + var config = tooltipComponent.config(); + + if (!config.isEmpty()) { + var xOff = font.width(CONFIG_LABEL) + 2; + + for (var stack : config) { + AEKeyRendering.drawInGui(Minecraft.getInstance(), guiGraphics, x + xOff, y, stack.what()); + xOff += 17; + } + + y += 17; + } + + var cellOpt = tooltipComponent.cell().getTooltipImage(); + + if (cellOpt.isPresent() && cellOpt.get() instanceof StorageCellTooltipComponent cellComponent) { + var xOff = font.width(CELL_LABEL) + 2; + guiGraphics.renderItem(tooltipComponent.cell(), x + xOff, y); + + var upgrades = cellComponent.upgrades(); + + if (!upgrades.isEmpty()) { + xOff += 17; + + for (var upgrade : upgrades) { + guiGraphics.renderItem(upgrade, x + xOff, y); + xOff += 17; + } + } + } + } +} diff --git a/src/main/java/gripe/_90/megacells/definition/MEGATranslations.java b/src/main/java/gripe/_90/megacells/definition/MEGATranslations.java index edf5fd4e..53b23f59 100644 --- a/src/main/java/gripe/_90/megacells/definition/MEGATranslations.java +++ b/src/main/java/gripe/_90/megacells/definition/MEGATranslations.java @@ -19,7 +19,9 @@ public enum MEGATranslations implements LocalizationEnum { PartitionedFor("Partitioned for: %s", Type.TOOLTIP), ProcessingOnly("Supports processing patterns only.", Type.TOOLTIP), Quantity("Quantity: %s", Type.TOOLTIP), - NotPartitioned("Not Partitioned", Type.TOOLTIP); + NotPartitioned("Not Partitioned", Type.TOOLTIP), + WorkbenchCell("Cell:", Type.TOOLTIP), + WorkbenchConfig("Config:", Type.TOOLTIP); private final String englishText; private final Type type; diff --git a/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchItem.java b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchItem.java index cdb1aa3e..39513b3b 100644 --- a/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchItem.java +++ b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchItem.java @@ -1,5 +1,7 @@ package gripe._90.megacells.item.cell; +import java.util.ArrayList; +import java.util.Optional; import javax.annotation.ParametersAreNonnullByDefault; import org.jetbrains.annotations.NotNull; @@ -8,12 +10,15 @@ import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.phys.BlockHitResult; import appeng.api.implementations.menuobjects.IMenuItem; import appeng.api.implementations.menuobjects.ItemMenuHost; +import appeng.api.stacks.GenericStack; +import appeng.core.AEConfig; import appeng.items.AEBaseItem; import appeng.menu.MenuOpener; import appeng.menu.locator.ItemMenuHostLocator; @@ -42,4 +47,27 @@ public InteractionResultHolder use(Level level, Player player, Intera return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); } + + @NotNull + @Override + public Optional getTooltipImage(@NotNull ItemStack stack) { + var host = new PortableCellWorkbenchMenuHost(this, null, MenuLocators.forStack(stack)); + var config = host.getConfig().toList(); + + var shownConfig = new ArrayList(); + var hasMore = false; + + for (var c : config) { + if (c != null) { + shownConfig.add(c); + + if (shownConfig.size() == AEConfig.instance().getTooltipMaxCellContentShown()) { + hasMore = true; + break; + } + } + } + + return Optional.of(new PortableCellWorkbenchTooltipComponent(shownConfig, host.getContainedStack(), hasMore)); + } } diff --git a/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java index 6e348d69..22a81dfc 100644 --- a/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java +++ b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java @@ -47,12 +47,16 @@ public PortableCellWorkbenchMenuHost(PortableCellWorkbenchItem item, Player play config.readFromList(getItemStack().getOrDefault(AEComponents.EXPORTED_CONFIG_INV, Collections.emptyList())); } + public ItemStack getContainedStack() { + return cellInv.getStackInSlot(0); + } + public ICellWorkbenchItem getCell() { - if (cellInv.getStackInSlot(0).isEmpty()) { + if (getContainedStack().isEmpty()) { return null; } - return cellInv.getStackInSlot(0).getItem() instanceof ICellWorkbenchItem cell ? cell : null; + return getContainedStack().getItem() instanceof ICellWorkbenchItem cell ? cell : null; } @Override @@ -132,13 +136,11 @@ public IUpgradeInventory getCellUpgrades() { return UpgradeInventories.empty(); } - var is = cellInv.getStackInSlot(0); - - if (is.isEmpty()) { + if (getContainedStack().isEmpty()) { return UpgradeInventories.empty(); } - var inv = cell.getUpgrades(is); + var inv = cell.getUpgrades(getContainedStack()); return inv == null ? UpgradeInventories.empty() : new ProxiedUpgradeInventory(inv, this); } diff --git a/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchTooltipComponent.java b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchTooltipComponent.java new file mode 100644 index 00000000..23892d40 --- /dev/null +++ b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchTooltipComponent.java @@ -0,0 +1,11 @@ +package gripe._90.megacells.item.cell; + +import java.util.List; + +import net.minecraft.world.inventory.tooltip.TooltipComponent; +import net.minecraft.world.item.ItemStack; + +import appeng.api.stacks.GenericStack; + +public record PortableCellWorkbenchTooltipComponent(List config, ItemStack cell, boolean hasMoreConfig) + implements TooltipComponent {}