99import com .tcm .MineTale .block .workbenches .menu .AbstractWorkbenchContainerMenu ;
1010import com .tcm .MineTale .block .workbenches .menu .ArmorersWorkbenchMenu ;
1111import com .tcm .MineTale .mixin .client .ClientRecipeBookAccessor ;
12- import com .tcm .MineTale .mixin .client .RecipeBookComponentAccessor ;
1312import com .tcm .MineTale .network .CraftRequestPayload ;
1413import com .tcm .MineTale .recipe .MineTaleRecipeBookComponent ;
1514import com .tcm .MineTale .registry .ModBlocks ;
2322import net .minecraft .client .gui .navigation .ScreenPosition ;
2423import net .minecraft .client .gui .screens .inventory .AbstractRecipeBookScreen ;
2524import net .minecraft .client .gui .screens .recipebook .RecipeBookComponent ;
26- import net .minecraft .client .gui .screens .recipebook .RecipeCollection ;
2725import net .minecraft .client .renderer .RenderPipelines ;
2826import net .minecraft .core .Holder ;
2927import net .minecraft .resources .Identifier ;
@@ -43,6 +41,8 @@ public class ArmorersWorkbenchScreen extends AbstractRecipeBookScreen<ArmorersWo
4341
4442 private final MineTaleRecipeBookComponent mineTaleRecipeBook ;
4543
44+ private RecipeDisplayId lastKnownSelectedId = null ;
45+
4646 private Button craftOneBtn ;
4747 private Button craftTenBtn ;
4848 private Button craftAllBtn ;
@@ -81,10 +81,10 @@ private static MineTaleRecipeBookComponent createRecipeBookComponent(ArmorersWor
8181 ItemStack tabIcon = new ItemStack (ModBlocks .ARMORERS_WORKBENCH_BLOCK .asItem ());
8282
8383 List <RecipeBookComponent .TabInfo > tabs = List .of (
84- new RecipeBookComponent .TabInfo (tabIcon .getItem (), ModRecipeDisplay .WORKBENCH_SEARCH )
84+ new RecipeBookComponent .TabInfo (tabIcon .getItem (), ModRecipeDisplay .ARMORERS_SEARCH )
8585 );
8686
87- return new MineTaleRecipeBookComponent (menu , tabs , ModRecipes .WORKBENCH_TYPE );
87+ return new MineTaleRecipeBookComponent (menu , tabs , ModRecipes .ARMORERS_TYPE );
8888 }
8989
9090 /**
@@ -128,34 +128,52 @@ protected void init() {
128128 * @param amount the quantity to craft; use -1 to request crafting of the full available stack ("All")
129129 */
130130
131- private void handleCraftRequest (int amount ) {
132- // 1. Cast the book component to the Accessor to get the selected data
133- RecipeBookComponentAccessor accessor = (RecipeBookComponentAccessor ) this .mineTaleRecipeBook ;
131+ // private void handleCraftRequest(int amount) {
132+ // // 1. Cast the book component to the Accessor to get the selected data
133+ // RecipeBookComponentAccessor accessor = (RecipeBookComponentAccessor) this.mineTaleRecipeBook;
134134
135- RecipeCollection collection = accessor .getLastRecipeCollection ();
136- RecipeDisplayId displayId = accessor .getLastRecipe ();
137-
138- if (collection != null && displayId != null ) {
139- // 2. Find the visual entry
140- for (RecipeDisplayEntry entry : collection .getSelectedRecipes (RecipeCollection .CraftableStatus .ANY )) {
141- if (entry .id ().equals (displayId )) {
142- // 3. Resolve result for the packet
143- List <ItemStack > results = entry .resultItems (SlotDisplayContext .fromLevel (this .minecraft .level ));
135+ // RecipeCollection collection = accessor.getLastRecipeCollection();
136+ // RecipeDisplayId displayId = accessor.getLastRecipe();
137+
138+ // if (collection != null && displayId != null) {
139+ // // 2. Find the visual entry
140+ // for (RecipeDisplayEntry entry : collection.getSelectedRecipes(RecipeCollection.CraftableStatus.ANY)) {
141+ // if (entry.id().equals(displayId)) {
142+ // // 3. Resolve result for the packet
143+ // List<ItemStack> results = entry.resultItems(SlotDisplayContext.fromLevel(this.minecraft.level));
144144
145- if (!results .isEmpty ()) {
146- ItemStack resultStack = results .get (0 );
145+ // if (!results.isEmpty()) {
146+ // ItemStack resultStack = results.get(0);
147147
148- // 4. LOG FOR DEBUGGING
149- System .out .println ("Sending craft request for: " + resultStack + " amount: " + amount );
148+ // // 4. LOG FOR DEBUGGING
149+ // System.out.println("Sending craft request for: " + resultStack + " amount: " + amount);
150150
151- ClientPlayNetworking .send (new CraftRequestPayload (resultStack , amount ));
152- }
153- break ;
151+ // ClientPlayNetworking.send(new CraftRequestPayload(resultStack, amount));
152+ // }
153+ // break;
154+ // }
155+ // }
156+ // } else {
157+ // System.out.println("Request failed: Collection or DisplayID is null!");
158+ // }
159+ // }
160+
161+ private void handleCraftRequest (int amount ) {
162+ // Look at our "Memory" instead of the component
163+ if (this .lastKnownSelectedId != null ) {
164+ ClientRecipeBook book = this .minecraft .player .getRecipeBook ();
165+ RecipeDisplayEntry entry = ((ClientRecipeBookAccessor ) book ).getKnown ().get (this .lastKnownSelectedId );
166+
167+ if (entry != null ) {
168+ List <ItemStack > results = entry .resultItems (SlotDisplayContext .fromLevel (this .minecraft .level ));
169+ if (!results .isEmpty ()) {
170+ System .out .println ("Persistent Selection Success: " + results .get (0 ));
171+ ClientPlayNetworking .send (new CraftRequestPayload (results .get (0 ), amount ));
172+ return ;
154173 }
155174 }
156- } else {
157- System .out .println ("Request failed: Collection or DisplayID is null!" );
158175 }
176+ System .out .println ("Request failed: No recipe was ever selected!" );
159177 }
160178
161179 /**
@@ -177,18 +195,22 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
177195 renderBackground (graphics , mouseX , mouseY , delta );
178196 super .render (graphics , mouseX , mouseY , delta );
179197
180- // Get the ID of the recipe clicked in the ghost-book
181- RecipeDisplayId displayId = this .mineTaleRecipeBook .getSelectedRecipeId ();
182- RecipeDisplayEntry selectedEntry = null ;
198+ // 1. Get the current selection from the book
199+ RecipeDisplayId currentId = this .mineTaleRecipeBook .getSelectedRecipeId ();
200+
201+ // 2. If it's NOT null, remember it!
202+ if (currentId != null ) {
203+ this .lastKnownSelectedId = currentId ;
204+ }
183205
184- if (displayId != null && this .minecraft .level != null ) {
206+ // 3. Use the remembered ID to find the entry for button activation
207+ RecipeDisplayEntry selectedEntry = null ;
208+ if (this .lastKnownSelectedId != null && this .minecraft .level != null ) {
185209 ClientRecipeBook book = this .minecraft .player .getRecipeBook ();
186- // Accessing the known recipes via your Accessor
187- Map <RecipeDisplayId , RecipeDisplayEntry > knownRecipes = ((ClientRecipeBookAccessor ) book ).getKnown ();
188- selectedEntry = knownRecipes .get (displayId );
210+ selectedEntry = ((ClientRecipeBookAccessor ) book ).getKnown ().get (this .lastKnownSelectedId );
189211 }
190212
191- // 2. Button Activation Logic
213+ // Logic for enabling/disabling buttons...
192214 if (selectedEntry != null ) {
193215 // We use the entry directly. It contains the 15 ingredients needed!
194216 boolean canCraftOne = canCraft (this .minecraft .player , selectedEntry , 1 );
0 commit comments