Add NEI overlay functionality so recipe on anvil can be selected via NEI.

This will allow to click plus-sign in NEI and NEI will select recipe on Anvil automatically.
This commit is contained in:
Mikhail Semenov 2025-10-21 21:21:18 +02:00
parent 907d389b43
commit e16be434f5
4 changed files with 185 additions and 101 deletions

View File

@ -0,0 +1,37 @@
package com.hbm.handler.nei;
import com.hbm.inventory.gui.GUIAnvil;
import com.hbm.inventory.recipes.anvil.AnvilRecipes.AnvilConstructionRecipe;
import codechicken.nei.api.IOverlayHandler;
import codechicken.nei.recipe.IRecipeHandler;
import net.minecraft.client.gui.inventory.GuiContainer;
/**
* Links NEI overlay interactions with the {@link GUIAnvil} so selecting a recipe focuses the anvil view.
*/
public class AnvilOverlayHandler implements IOverlayHandler {
/**
* {@inheritDoc}
*
* <p>When the NEI overlay is opened for the Nuclear Tech anvil, the GUI is instructed to focus the selected recipe.</p>
* <p>It will not craft the recepie. Only select it.</p>
*
* @param firstGui the GUI currently displaying the overlay
* @param recipe the NEI recipe handler backing the overlay
* @param recipeIndex index of the recipe within the handler
* @param maxTransfer whether NEI is performing a maximal transfer (ignored)
*/
@Override
public void overlayRecipe(GuiContainer firstGui, IRecipeHandler recipe, int recipeIndex, boolean maxTransfer) {
if(!(firstGui instanceof GUIAnvil) || !(recipe instanceof AnvilRecipeHandler)) {
return;
}
AnvilConstructionRecipe construction = ((AnvilRecipeHandler) recipe).getConstructionRecipe(recipeIndex);
if(construction != null) {
((GUIAnvil) firstGui).focusRecipe(construction);
}
}
}

View File

@ -50,20 +50,25 @@ public class AnvilRecipeHandler extends TemplateRecipeHandler implements ICompat
return "ntmAnvil";
}
@Override
public String getOverlayIdentifier() {
return "ntmAnvil";
}
public LinkedList<RecipeTransferRect> transferRectsRec = new LinkedList<RecipeTransferRect>();
public LinkedList<RecipeTransferRect> transferRectsGui = new LinkedList<RecipeTransferRect>();
public LinkedList<Class<? extends GuiContainer>> guiRec = new LinkedList<Class<? extends GuiContainer>>();
public LinkedList<Class<? extends GuiContainer>> guiGui = new LinkedList<Class<? extends GuiContainer>>();
public class RecipeSet extends TemplateRecipeHandler.CachedRecipe {
List<PositionedStack> input = new ArrayList();
List<PositionedStack> output = new ArrayList();
PositionedStack anvil;
int tier;
final AnvilConstructionRecipe recipe;
OverlayType shape;
public RecipeSet(List<Object> in, List<Object> out, int tier) {
public RecipeSet(List<Object> in, List<Object> out, int tier, AnvilConstructionRecipe recipe) {
//not the prettiest of solutions but certainly the most pleasant to work with
int inLine = 1;
@ -120,6 +125,7 @@ public class AnvilRecipeHandler extends TemplateRecipeHandler implements ICompat
this.anvil = new PositionedStack(NTMAnvil.getAnvilsFromTier(tier), anvX, anvY);
this.tier = tier;
this.recipe = recipe;
}
@Override
@ -225,7 +231,7 @@ public class AnvilRecipeHandler extends TemplateRecipeHandler implements ICompat
outs.add(stack);
}
this.arecipes.add(new RecipeSet(ins, outs, recipe.tierLower));
this.arecipes.add(new RecipeSet(ins, outs, recipe.tierLower, recipe));
}
@Override
@ -247,6 +253,20 @@ public class AnvilRecipeHandler extends TemplateRecipeHandler implements ICompat
return RefStrings.MODID + ":textures/gui/nei/gui_nei_anvil.png";
}
/**
* Resolves the underlying anvil recipe for the NEI recipe index.
*
* @param index recipe index supplied by NEI
* @return the construction recipe or {@code null} if the index is out of bounds
*/
public AnvilConstructionRecipe getConstructionRecipe(int index) {
if(index < 0 || index >= this.arecipes.size()) {
return null;
}
return ((RecipeSet) this.arecipes.get(index)).recipe;
}
@Override
public void drawBackground(int recipe) {
super.drawBackground(recipe);

View File

@ -79,6 +79,30 @@ public class GUIAnvil extends GuiContainer {
this.search.setMaxStringLength(25);
}
/**
* Requests the GUI to focus on the supplied recipe. Used by the NEI overlay handler when a recipe is selected externally.
*
* @param target the recipe instance to focus, may be {@code null}
*/
public void focusRecipe(AnvilConstructionRecipe target) {
if(target == null) {
return;
}
if (!this.originList.contains(target)) {
return;
}
search.setText("");
regenerateRecipes();
int pos = this.recipes.indexOf(target);
if(pos < 0) {
return;
}
this.selection = pos;
this.index = MathHelper.clamp_int(pos / 2, 0, this.size);
}
private void regenerateRecipes() {
this.recipes.clear();

View File

@ -9,8 +9,10 @@ import com.hbm.blocks.ModBlocks;
import com.hbm.blocks.generic.BlockPlushie.TileEntityPlushie;
import com.hbm.config.ClientConfig;
import com.hbm.config.CustomMachineConfigJSON;
import com.hbm.handler.nei.AnvilOverlayHandler;
import com.hbm.handler.nei.CustomMachineHandler;
import com.hbm.items.ItemEnums.EnumIngotMetal;
import com.hbm.inventory.gui.GUIAnvil;
import com.hbm.items.ItemEnums.EnumSecretType;
import com.hbm.items.ModItems;
import com.hbm.items.machine.ItemBattery;
@ -33,6 +35,7 @@ public class NEIConfig implements IConfigureNEI {
for (TemplateRecipeHandler handler: NEIRegistry.listAllHandlers()) {
registerHandler(handler);
}
API.registerGuiOverlayHandler(GUIAnvil.class, new AnvilOverlayHandler(), "ntmAnvil");
for(CustomMachineConfigJSON.MachineConfiguration conf : CustomMachineConfigJSON.niceList) {
registerHandlerBypass(new CustomMachineHandler(conf));