Skip to content

Commit

Permalink
Add Portable Cell Workbench (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 authored Nov 17, 2024
1 parent 2c32ea2 commit 97a5905
Show file tree
Hide file tree
Showing 19 changed files with 895 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.data.models.blockstates.PropertyDispatch;
import net.minecraft.data.models.blockstates.Variant;
import net.minecraft.data.models.blockstates.VariantProperties;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.neoforged.neoforge.client.model.generators.ConfiguredModel;
import net.neoforged.neoforge.client.model.generators.ModelFile;
Expand Down Expand Up @@ -58,6 +59,7 @@ protected void registerStatesAndModels() {

basicItem(MEGAItems.GREATER_ENERGY_CARD);
basicItem(MEGAItems.COMPRESSION_CARD);
basicItem(MEGAItems.PORTABLE_CELL_WORKBENCH);

for (var cell : MEGAItems.getTieredCells()) {
if (cell.portable()) {
Expand Down Expand Up @@ -182,7 +184,7 @@ protected void registerStatesAndModels() {
}));
}

private void basicItem(ItemDefinition<?> item) {
private void basicItem(ItemLike item) {
itemModels().basicItem(item.asItem());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21.1 2024-11-15T19:31:29.757213749 Language
01c5e0e544a51a97c1494a283b0c34a0e73b3f14 assets/megacells/lang/en_us.json
// 1.21.1 2024-11-17T14:07:31.916554252 Language
79f2bdffe2659a931e47c3e340144ada20c70b08 assets/megacells/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.21.1 2024-11-15T19:31:29.758210643 Block States / Models
// 1.21.1 2024-11-15T20:14:53.378945918 Block States / Models
8d69a3c391bcb75b5aac370510d15fa9c367b200 assets/megacells/blockstates/16m_crafting_storage.json
6b8ebbdecbd556e59c1c0d9abb4222d5de046e9a assets/megacells/blockstates/1m_crafting_storage.json
c716f5bd44bda4274f6e5f2c2f2b09f6223a8de7 assets/megacells/blockstates/256m_crafting_storage.json
Expand Down Expand Up @@ -105,6 +105,7 @@ a31fadf8962f679c8fc76b36b705c49d64a494e4 assets/megacells/models/item/mega_inter
43bb96e2391a03b8d1782ce0765563c8cc722cad assets/megacells/models/item/mega_item_cell_housing.json
a4708e37ca1954b67e57a24d60c5955a437b0ef5 assets/megacells/models/item/mega_pattern_provider.json
6762bae761c01dfae6e5a358e1da5ec25b4b3c93 assets/megacells/models/item/mega_source_cell_housing.json
f9f81f7f70a276807ae69104dca94146e9f709b6 assets/megacells/models/item/portable_cell_workbench.json
05c359b2007ed45af3da1911571f48b6f2b84182 assets/megacells/models/item/portable_chemical_cell_16m.json
a5430292a6845f49fb8fc6e60611815b617323f8 assets/megacells/models/item/portable_chemical_cell_1m.json
3c2fd4a6757406e90eb49b972618a50255fa1e67 assets/megacells/models/item/portable_chemical_cell_256m.json
Expand Down
3 changes: 3 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 Expand Up @@ -62,6 +64,7 @@
"item.megacells.mega_fluid_cell_housing": "MEGA Fluid Cell Housing",
"item.megacells.mega_item_cell_housing": "MEGA Item Cell Housing",
"item.megacells.mega_source_cell_housing": "MEGA Source Cell Housing",
"item.megacells.portable_cell_workbench": "Portable Cell Workbench",
"item.megacells.portable_chemical_cell_16m": "16M Portable Chemical Cell",
"item.megacells.portable_chemical_cell_1m": "1M Portable Chemical Cell",
"item.megacells.portable_chemical_cell_256m": "256M Portable Chemical Cell",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "megacells:item/portable_cell_workbench"
}
}
14 changes: 14 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,11 +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 @@ -44,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 All @@ -58,6 +63,11 @@ private static void initScreens(RegisterMenuScreensEvent event) {
PatternProviderScreen<MEGAPatternProviderMenu>::new,
"/screens/megacells/mega_pattern_provider.json");
InitScreens.register(event, MEGAMenus.CELL_DOCK, CellDockScreen::new, "/screens/megacells/cell_dock.json");
InitScreens.register(
event,
MEGAMenus.PORTABLE_CELL_WORKBENCH,
PortableCellWorkbenchScreen::new,
"/screens/megacells/portable_cell_workbench.json");
}

private static void initCraftingUnitModels(FMLClientSetupEvent event) {
Expand Down Expand Up @@ -127,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
@@ -0,0 +1,111 @@
package gripe._90.megacells.client.screen;

import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.NotNull;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;

import appeng.api.config.ActionItems;
import appeng.api.config.CopyMode;
import appeng.api.config.FuzzyMode;
import appeng.api.config.Settings;
import appeng.api.stacks.AEItemKey;
import appeng.api.stacks.GenericStack;
import appeng.client.gui.Icon;
import appeng.client.gui.implementations.UpgradeableScreen;
import appeng.client.gui.style.ScreenStyle;
import appeng.client.gui.widgets.ActionButton;
import appeng.client.gui.widgets.SettingToggleButton;
import appeng.client.gui.widgets.ToggleButton;
import appeng.core.definitions.AEItems;
import appeng.core.localization.GuiText;

import gripe._90.megacells.menu.PortableCellWorkbenchMenu;

/**
* See {@link appeng.client.gui.implementations.CellWorkbenchScreen}
*/
public class PortableCellWorkbenchScreen extends UpgradeableScreen<PortableCellWorkbenchMenu> {
private final ToggleButton copyMode;
private final SettingToggleButton<FuzzyMode> fuzzyMode;

public PortableCellWorkbenchScreen(
PortableCellWorkbenchMenu menu, Inventory playerInventory, Component title, ScreenStyle style) {
super(menu, playerInventory, title, style);
fuzzyMode = addToLeftToolbar(new SettingToggleButton<>(
Settings.FUZZY_MODE,
FuzzyMode.IGNORE_ALL,
(button, backwards) -> menu.setCellFuzzyMode(button.getNextValue(backwards))));
addToLeftToolbar(new ActionButton(ActionItems.COG, act -> menu.partition()));
addToLeftToolbar(new ActionButton(ActionItems.CLOSE, act -> menu.clear()));
copyMode = addToLeftToolbar(new ToggleButton(
Icon.COPY_MODE_ON,
Icon.COPY_MODE_OFF,
GuiText.CopyMode.text(),
GuiText.CopyModeDesc.text(),
act -> menu.nextWorkBenchCopyMode()));
}

@Override
protected void updateBeforeRender() {
super.updateBeforeRender();
copyMode.setState(menu.getCopyMode() == CopyMode.CLEAR_ON_REMOVE);
fuzzyMode.set(menu.getFuzzyMode());
fuzzyMode.setVisibility(menu.getUpgrades().isInstalled(AEItems.FUZZY_CARD));
}

@NotNull
@Override
protected List<Component> getTooltipFromContainerItem(@NotNull ItemStack stack) {
var cell = getMenu().getWorkbenchItem();

if (cell.isEmpty()) {
return super.getTooltipFromContainerItem(stack);
}

if (cell == stack) {
return super.getTooltipFromContainerItem(stack);
}

var genericStack = GenericStack.unwrapItemStack(stack);
var what = genericStack != null ? genericStack.what() : AEItemKey.of(stack);

if (what == null) {
return super.getTooltipFromContainerItem(stack);
}

var configInventory = getMenu().getHost().getCell().getConfigInventory(cell);

if (!configInventory.isSupportedType(what.getType())) {
var lines = new ArrayList<>(super.getTooltipFromContainerItem(stack));
lines.add(GuiText.IncompatibleWithCell.text().withStyle(ChatFormatting.RED));
return lines;
}

var filter = configInventory.getFilter();

if (filter != null) {
var anySlotMatches = false;

for (var i = 0; i < configInventory.size(); i++) {
if (configInventory.isAllowedIn(i, what)) {
anySlotMatches = true;
break;
}
}

if (!anySlotMatches) {
var lines = new ArrayList<>(super.getTooltipFromContainerItem(stack));
lines.add(GuiText.IncompatibleWithCell.text().withStyle(ChatFormatting.RED));
return lines;
}
}

return super.getTooltipFromContainerItem(stack);
}
}
Loading

0 comments on commit 97a5905

Please sign in to comment.