Skip to content

Commit

Permalink
Expose entire network inventory to EMI for craftable/favorite checks …
Browse files Browse the repository at this point in the history
…(configurable, off by default) (#8215)

Also fixes a bug where items in the crafting terminal grid would
disappear from the tree

Closes #8214

---------

Co-authored-by: Sebastian Hartte <[email protected]>
  • Loading branch information
ramidzkh and shartte authored Dec 8, 2024
1 parent 5cdddc3 commit 38c8ff3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/appeng/core/AEConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ public boolean isEnableFacadeRecipesInRecipeViewer() {
return client.enableFacadeRecipesInRecipeViewer.getAsBoolean();
}

public boolean isExposeNetworkInventoryToEmi() {
return client.exposeNetworkInventoryToEmi.getAsBoolean();
}

public int getCraftingCalculationTimePerTick() {
return common.craftingCalculationTimePerTick.get();
}
Expand Down Expand Up @@ -424,6 +428,7 @@ private static class ClientConfig {
public final BooleanValue disableColoredCableRecipesInRecipeViewer;
public final BooleanValue enableFacadesInRecipeViewer;
public final BooleanValue enableFacadeRecipesInRecipeViewer;
public final BooleanValue exposeNetworkInventoryToEmi;
public final EnumValue<PowerUnit> selectedPowerUnit;
public final BooleanValue debugGuiOverlays;
public final BooleanValue showPlacementPreview;
Expand Down Expand Up @@ -458,6 +463,8 @@ public ClientConfig() {
"Show facades in REI/JEI/EMI item list");
this.enableFacadeRecipesInRecipeViewer = define(builder, "enableFacadeRecipesInRecipeViewer", true,
"Show facade recipes in REI/JEI/EMI for supported blocks");
this.exposeNetworkInventoryToEmi = define(builder, "provideNetworkInventoryToEmi", false,
"Expose the full network inventory to EMI, which might cause performance problems.");
builder.pop();

builder.push("client");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,33 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.Level;

import dev.emi.emi.api.recipe.EmiPlayerInventory;
import dev.emi.emi.api.recipe.EmiRecipe;
import dev.emi.emi.api.recipe.VanillaEmiRecipeCategories;
import dev.emi.emi.api.recipe.handler.EmiCraftContext;
import dev.emi.emi.api.recipe.handler.StandardRecipeHandler;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.widget.Bounds;
import dev.emi.emi.api.widget.SlotWidget;
import dev.emi.emi.api.widget.Widget;

import appeng.api.stacks.AEKey;
import appeng.api.stacks.GenericStack;
import appeng.core.AEConfig;
import appeng.integration.modules.itemlists.EncodingHelper;
import appeng.integration.modules.itemlists.TransferHelper;
import appeng.menu.AEBaseMenu;
import appeng.menu.SlotSemantics;
import appeng.menu.me.common.MEStorageMenu;
import appeng.menu.me.items.CraftingTermMenu;

abstract class AbstractRecipeHandler<T extends AEBaseMenu> implements StandardRecipeHandler<T> {
Expand All @@ -51,6 +57,7 @@ public List<Slot> getInputSources(T menu) {
var slots = new ArrayList<Slot>();
slots.addAll(menu.getSlots(SlotSemantics.PLAYER_INVENTORY));
slots.addAll(menu.getSlots(SlotSemantics.PLAYER_HOTBAR));
slots.addAll(menu.getSlots(SlotSemantics.CRAFTING_GRID));
return slots;
}

Expand All @@ -67,6 +74,34 @@ public List<Slot> getCraftingSlots(T menu) {
return null;
}

@Override
public EmiPlayerInventory getInventory(AbstractContainerScreen<T> screen) {
if (!AEConfig.instance().isExposeNetworkInventoryToEmi()) {
return StandardRecipeHandler.super.getInventory(screen);
}

var list = new ArrayList<EmiStack>();

for (Slot slot : getInputSources(screen.getMenu())) {
list.add(EmiStack.of(slot.getItem()));
}

if (screen.getMenu() instanceof MEStorageMenu menu) {
var repo = menu.getClientRepo();

if (repo != null) {
for (var entry : repo.getAllEntries()) {
if (entry.getStoredAmount() <= 0) {
continue; // Skip items that are only craftable
}
list.add(EmiStackHelper.toEmiStack(new GenericStack(entry.getWhat(), entry.getStoredAmount())));
}
}
}

return new EmiPlayerInventory(list);
}

@Override
public boolean canCraft(EmiRecipe recipe, EmiCraftContext<T> context) {
if (context.getType() == EmiCraftContext.Type.FILL_BUTTON) {
Expand Down

0 comments on commit 38c8ff3

Please sign in to comment.