Skip to content

Commit

Permalink
Add tooltip information to Portable Cell Workbench
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Nov 17, 2024
1 parent f71b422 commit ed9b1dc
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/generated/resources/assets/megacells/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/gripe/_90/megacells/client/MEGACellsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -42,4 +47,27 @@ public InteractionResultHolder<ItemStack> use(Level level, Player player, Intera

return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}

@NotNull
@Override
public Optional<TooltipComponent> getTooltipImage(@NotNull ItemStack stack) {
var host = new PortableCellWorkbenchMenuHost(this, null, MenuLocators.forStack(stack));
var config = host.getConfig().toList();

var shownConfig = new ArrayList<GenericStack>();
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<GenericStack> config, ItemStack cell, boolean hasMoreConfig)
implements TooltipComponent {}

0 comments on commit ed9b1dc

Please sign in to comment.