44
55import com .tcm .MineTale .MineTale ;
66import com .tcm .MineTale .block .workbenches .menu .WorkbenchWorkbenchMenu ;
7+ import com .tcm .MineTale .mixin .client .RecipeBookComponentAccessor ;
8+ import com .tcm .MineTale .network .CraftRequestPayload ;
79import com .tcm .MineTale .recipe .MineTaleRecipeBookComponent ;
810import com .tcm .MineTale .registry .ModBlocks ;
911import com .tcm .MineTale .registry .ModRecipeDisplay ;
1012import com .tcm .MineTale .registry .ModRecipes ;
1113
14+ import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayNetworking ;
1215import net .minecraft .client .gui .GuiGraphics ;
16+ import net .minecraft .client .gui .components .Button ;
1317import net .minecraft .client .gui .navigation .ScreenPosition ;
1418import net .minecraft .client .gui .screens .inventory .AbstractRecipeBookScreen ;
1519import net .minecraft .client .gui .screens .recipebook .RecipeBookComponent ;
20+ import net .minecraft .client .gui .screens .recipebook .RecipeBookPage ;
21+ import net .minecraft .client .gui .screens .recipebook .RecipeCollection ;
1622import net .minecraft .client .renderer .RenderPipelines ;
1723import net .minecraft .resources .Identifier ;
1824import net .minecraft .world .entity .player .Inventory ;
1925import net .minecraft .world .item .ItemStack ;
26+ import net .minecraft .world .item .crafting .display .RecipeDisplayEntry ;
27+ import net .minecraft .world .item .crafting .display .RecipeDisplayId ;
28+ import net .minecraft .world .item .crafting .display .SlotDisplayContext ;
2029import net .minecraft .network .chat .Component ;
2130
2231public class WorkbenchWorkbenchScreen extends AbstractRecipeBookScreen <WorkbenchWorkbenchMenu > {
2332 private static final Identifier TEXTURE =
2433 Identifier .fromNamespaceAndPath (MineTale .MOD_ID , "textures/gui/container/furnace_workbench.png" );
2534
35+ private final MineTaleRecipeBookComponent mineTaleRecipeBook ;
36+
37+ private Button craftOneBtn ;
38+ private Button craftThirtyBtn ;
39+ private Button craftAllBtn ;
40+
2641 /**
2742 * Initialize a workbench GUI screen using the provided container menu, player inventory, and title.
2843 *
@@ -31,7 +46,12 @@ public class WorkbenchWorkbenchScreen extends AbstractRecipeBookScreen<Workbench
3146 * @param title the title component shown at the top of the screen
3247 */
3348 public WorkbenchWorkbenchScreen (WorkbenchWorkbenchMenu menu , Inventory inventory , Component title ) {
34- super (menu , createRecipeBookComponent (menu ), inventory , title );
49+ this (menu , inventory , title , createRecipeBookComponent (menu ));
50+ }
51+
52+ private WorkbenchWorkbenchScreen (WorkbenchWorkbenchMenu menu , Inventory inventory , Component title , MineTaleRecipeBookComponent recipeBook ) {
53+ super (menu , recipeBook , inventory , title );
54+ this .mineTaleRecipeBook = recipeBook ;
3555 }
3656
3757 /**
@@ -63,6 +83,109 @@ protected void init() {
6383 this .imageHeight = 166 ;
6484
6585 super .init ();
86+
87+ this .craftOneBtn = addRenderableWidget (Button .builder (Component .literal ("1" ), (button ) -> {
88+ handleCraftRequest (1 );
89+ }).bounds (this .leftPos + 80 , this .topPos + 20 , 30 , 20 ).build ());
90+
91+ this .craftThirtyBtn = addRenderableWidget (Button .builder (Component .literal ("30" ), (button ) -> {
92+ handleCraftRequest (30 );
93+ }).bounds (this .leftPos + 112 , this .topPos + 20 , 30 , 20 ).build ());
94+
95+ this .craftAllBtn = addRenderableWidget (Button .builder (Component .literal ("All" ), (button ) -> {
96+ handleCraftRequest (-1 ); // -1 represents "All" logic
97+ }).bounds (this .leftPos + 144 , this .topPos + 20 , 30 , 20 ).build ());
98+ }
99+
100+ // private void handleCraftRequest(int amount) {
101+ // RecipeBookPage page = ((RecipeBookComponentAccessor)this.mineTaleRecipeBook).getRecipeBookPage();
102+ // RecipeCollection collection = page.getLastClickedRecipeCollection();
103+ // RecipeDisplayId displayId = page.getLastClickedRecipe();
104+
105+ // if (collection != null && displayId != null) {
106+ // // 1. Find the specific entry that was clicked
107+ // for (RecipeDisplayEntry entry : collection.getSelectedRecipes(RecipeCollection.CraftableStatus.ANY)) {
108+ // if (entry.id().equals(displayId)) {
109+ // // 2. Resolve the visual result into an actual ItemStack
110+ // List<ItemStack> results = entry.resultItems(SlotDisplayContext.fromLevel(this.minecraft.level));
111+
112+ // if (!results.isEmpty()) {
113+ // ItemStack resultStack = results.get(0);
114+ // // 3. Send the item and amount to the server
115+ // // Note: Update your CraftRequestPayload to accept ItemStack instead of Identifier
116+ // ClientPlayNetworking.send(new CraftRequestPayload(resultStack, amount));
117+ // }
118+ // break;
119+ // }
120+ // }
121+ // }
122+ // }
123+
124+ // private void handleCraftRequest(int amount) {
125+ // // 1. Get the current page from the recipe book
126+ // // We use your mixin/accessor to get the internal page object
127+ // RecipeBookPage page = ((RecipeBookComponentAccessor)this.mineTaleRecipeBook).getRecipeBookPage();
128+
129+ // // 2. Identify WHAT was clicked
130+ // RecipeCollection collection = page.getLastClickedRecipeCollection();
131+ // RecipeDisplayId displayId = page.getLastClickedRecipe();
132+
133+ // if (collection != null && displayId != null) {
134+ // // 3. Find the display entry
135+ // for (RecipeDisplayEntry entry : collection.getSelectedRecipes(RecipeCollection.CraftableStatus.ANY)) {
136+ // if (entry.id().equals(displayId)) {
137+ // // 4. Get the result item (the Chest)
138+ // // 1.21.1 uses SlotDisplayContext to handle dynamic results
139+ // List<ItemStack> results = entry.resultItems(SlotDisplayContext.fromLevel(this.minecraft.level));
140+
141+ // if (!results.isEmpty()) {
142+ // ItemStack resultStack = results.get(0);
143+
144+ // // 5. Send the packet to the Server
145+ // // IMPORTANT: Ensure your CraftRequestPayload is registered to handle
146+ // // an ItemStack and an Int.
147+ // ClientPlayNetworking.send(new CraftRequestPayload(resultStack, amount));
148+
149+ // // Optional: Play a click sound so the player knows it worked
150+ // this.minecraft.getSoundManager().play(net.minecraft.client.resources.sounds.SimpleSoundInstance.forUI(
151+ // net.minecraft.sounds.SoundEvents.UI_BUTTON_CLICK, 1.0F));
152+ // }
153+ // break;
154+ // }
155+ // }
156+ // }
157+ // }
158+
159+ // RecipeBookComponent
160+
161+ private void handleCraftRequest (int amount ) {
162+ // 1. Cast the book component to the Accessor to get the selected data
163+ RecipeBookComponentAccessor accessor = (RecipeBookComponentAccessor ) this .mineTaleRecipeBook ;
164+
165+ RecipeCollection collection = accessor .getLastRecipeCollection ();
166+ RecipeDisplayId displayId = accessor .getLastRecipe ();
167+
168+ if (collection != null && displayId != null ) {
169+ // 2. Find the visual entry
170+ for (RecipeDisplayEntry entry : collection .getSelectedRecipes (RecipeCollection .CraftableStatus .ANY )) {
171+ if (entry .id ().equals (displayId )) {
172+ // 3. Resolve result for the packet
173+ List <ItemStack > results = entry .resultItems (SlotDisplayContext .fromLevel (this .minecraft .level ));
174+
175+ if (!results .isEmpty ()) {
176+ ItemStack resultStack = results .get (0 );
177+
178+ // 4. LOG FOR DEBUGGING: Does this print in your console?
179+ System .out .println ("Sending craft request for: " + resultStack + " amount: " + amount );
180+
181+ ClientPlayNetworking .send (new CraftRequestPayload (resultStack , amount ));
182+ }
183+ break ;
184+ }
185+ }
186+ } else {
187+ System .out .println ("Request failed: Collection or DisplayID is null!" );
188+ }
66189 }
67190
68191 /**
@@ -95,6 +218,11 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
95218 // 3. Call super (this draws your slots and items)
96219 super .render (graphics , mouseX , mouseY , delta );
97220
221+ boolean hasSelection = this .mineTaleRecipeBook .getSelectedRecipeId () != null ;
222+ this .craftOneBtn .active = hasSelection ;
223+ this .craftThirtyBtn .active = hasSelection ;
224+ this .craftAllBtn .active = hasSelection ;
225+
98226 renderTooltip (graphics , mouseX , mouseY );
99227 }
100228
0 commit comments