2727import net .roboxgamer .modernutils .util .RedstoneManager ;
2828import org .jetbrains .annotations .NotNull ;
2929
30- import java .util .ArrayList ;
31- import java .util .Objects ;
30+ import java .util .HashMap ;
31+ import java .util .Map ;
3232
3333import static net .roboxgamer .modernutils .util .RedstoneManager .REDSTONE_MODE_MAP ;
3434
@@ -73,7 +73,7 @@ public class MechanicalCrafterScreen extends AbstractContainerScreen<MechanicalC
7373 private ExtendedButton rightSideBtn ;
7474 private ExtendedButton backSideBtn ;
7575 private ExtendedButton frontSideBtn ;
76- private ArrayList < ExtendedButton > sideBtns ;
76+ private Map < Constants . Sides , SideConfigButton > sideButtons = new HashMap <>() ;
7777
7878
7979 public MechanicalCrafterScreen (MechanicalCrafterMenu menu , Inventory playerInv , Component title ) {
@@ -172,7 +172,6 @@ public void renderIcon(GuiGraphics guiGraphics, int mouseX, int mouseY, float pa
172172 guiGraphics .pose ().popPose ();
173173 }
174174 };
175-
176175 addRenderableWidget (this .sideConfigBtn );
177176
178177
@@ -199,24 +198,6 @@ public void renderIcon(GuiGraphics guiGraphics, int mouseX, int mouseY, float pa
199198 }
200199 };
201200 updateAutoImportButtonTooltip ();
202- SideConfigTab .addChild (this .autoImportBtn );
203-
204- this .upSideBtn = new ExtendedButton (
205- "UpSideBtn" ,
206- 24 , 24 ,
207- Component .literal ("Up Side" ),
208- true , // Optional icon
209- ExtendedButton .WidgetPosition .NONE ,
210- (button , clickAction , mouseX , mouseY ) -> this .blockEntity .handleSideBtnClick (Constants .Sides .UP , button ,clickAction ),
211- this .player
212- ){
213- @ Override
214- public void renderIcon (GuiGraphics guiGraphics , int mouseX , int mouseY , float partialTick , ExtendedButton extendedButton ) {
215- renderSideBtnBackgroundAndIcon (guiGraphics , extendedButton , Constants .Sides .UP );
216- }
217- };
218- SideConfigTab .addChild (this .upSideBtn );
219-
220201
221202 this .autoExportBtn = new ExtendedButton (
222203 "AutoExportBtn" ,
@@ -241,97 +222,39 @@ public void renderIcon(GuiGraphics guiGraphics, int mouseX, int mouseY, float pa
241222 }
242223 };
243224 updateAutoExportButtonTooltip ();
244- SideConfigTab .addChild (this .autoExportBtn );
245225
246- this .leftSideBtn = new ExtendedButton (
247- "LeftSideBtn" ,
248- 24 , 24 ,
249- Component .literal ("Left Side" ),
250- true , // Optional icon
251- ExtendedButton .WidgetPosition .NONE ,
252- (button , clickAction , mouseX , mouseY ) -> this .blockEntity .handleSideBtnClick (Constants .Sides .LEFT , button ,clickAction ),
253- this .player
254- ){
255- @ Override
256- public void renderIcon (GuiGraphics guiGraphics , int mouseX , int mouseY , float partialTick , ExtendedButton extendedButton ) {
257- renderSideBtnBackgroundAndIcon (guiGraphics , extendedButton , Constants .Sides .LEFT );
258- }
259- };
260- SideConfigTab .addChild (this .leftSideBtn );
226+ initSideButtons ();
261227
262- this .frontSideBtn = new ExtendedButton (
263- "FrontSideBtn" ,
264- 24 , 24 ,
265- Component .literal ("Front Side" ),
266- true , // Optional icon
267- ExtendedButton .WidgetPosition .NONE ,
268- (button , clickAction , mouseX , mouseY ) -> this .blockEntity .handleSideBtnClick (Constants .Sides .FRONT , button ,clickAction ),
269- this .player
270- ) {
271- @ Override
272- public void renderIcon (GuiGraphics guiGraphics , int mouseX , int mouseY , float partialTick , ExtendedButton extendedButton ) {
273- renderSideBtnBackgroundAndIcon (guiGraphics , extendedButton , Constants .Sides .FRONT );
274- }
275- };
228+ this .upSideBtn = sideButtons .get (Constants .Sides .UP );
229+ this .downSideBtn = sideButtons .get (Constants .Sides .DOWN );
230+ this .leftSideBtn = sideButtons .get (Constants .Sides .LEFT );
231+ this .rightSideBtn = sideButtons .get (Constants .Sides .RIGHT );
232+ this .frontSideBtn = sideButtons .get (Constants .Sides .FRONT );
233+ this .backSideBtn = sideButtons .get (Constants .Sides .BACK );
234+ SideConfigTab .addChild (this .autoImportBtn );
235+ SideConfigTab .addChild (this .upSideBtn );
236+ SideConfigTab .addChild (this .autoExportBtn );
237+ SideConfigTab .addChild (this .leftSideBtn );
276238 SideConfigTab .addChild (this .frontSideBtn );
277-
278- this .rightSideBtn = new ExtendedButton (
279- "RightSideBtn" ,
280- 24 , 24 ,
281- Component .literal ("Right Side" ),
282- true , // Optional icon
283- ExtendedButton .WidgetPosition .NONE ,
284- (button , clickAction , mouseX , mouseY ) -> this .blockEntity .handleSideBtnClick (Constants .Sides .RIGHT , button ,clickAction ),
285- this .player
286- ){
287- @ Override
288- public void renderIcon (GuiGraphics guiGraphics , int mouseX , int mouseY , float partialTick , ExtendedButton extendedButton ) {
289- renderSideBtnBackgroundAndIcon (guiGraphics , extendedButton , Constants .Sides .RIGHT );
290- }
291- };
292239 SideConfigTab .addChild (this .rightSideBtn );
293-
294- this .backSideBtn = new ExtendedButton (
295- "BackSideBtn" ,
296- 24 , 24 ,
297- Component .literal ("Back Side" ),
298- true , // Optional icon
299- ExtendedButton .WidgetPosition .NONE ,
300- (button , clickAction , mouseX , mouseY ) -> this .blockEntity .handleSideBtnClick (Constants .Sides .BACK , button ,clickAction ),
301- this .player
302- ){
303- @ Override
304- public void renderIcon (GuiGraphics guiGraphics , int mouseX , int mouseY , float partialTick , ExtendedButton extendedButton ) {
305- renderSideBtnBackgroundAndIcon (guiGraphics , extendedButton , Constants .Sides .BACK );
306- }
307- };
308240 SideConfigTab .addChild (this .backSideBtn );
309-
310- this .downSideBtn = new ExtendedButton (
311- "DownSideBtn" ,
312- 24 , 24 ,
313- Component .literal ("Down Side" ),
314- true , // Optional icon
315- ExtendedButton .WidgetPosition .NONE ,
316- (button , clickAction , mouseX , mouseY ) -> this .blockEntity .handleSideBtnClick (Constants .Sides .DOWN , button ,clickAction ),
317- this .player
318- ){
319- @ Override
320- public void renderIcon (GuiGraphics guiGraphics , int mouseX , int mouseY , float partialTick , ExtendedButton extendedButton ) {
321- renderSideBtnBackgroundAndIcon (guiGraphics , extendedButton , Constants .Sides .DOWN );
322- }
323- };
324241 SideConfigTab .addChild (this .downSideBtn );
325-
326- this .sideBtns = new ArrayList <>();
327- this .sideBtns .add (this .upSideBtn );
328- this .sideBtns .add (this .downSideBtn );
329- this .sideBtns .add (this .leftSideBtn );
330- this .sideBtns .add (this .rightSideBtn );
331- this .sideBtns .add (this .backSideBtn );
332- this .sideBtns .add (this .frontSideBtn );
333-
334- updateSideBtnsTooltip ();
242+ }
243+
244+ void initSideButtons () {
245+ // Create buttons for all sides
246+ for (Constants .Sides side : Constants .Sides .values ()) {
247+ String btnId = side .toString () + "SideBtn" ;
248+ SideConfigButton button = new SideConfigButton (
249+ btnId ,
250+ side ,
251+ this ,
252+ this .blockEntity ,
253+ this .player
254+ );
255+ // Store in our map for easy access
256+ sideButtons .put (side , button );
257+ }
335258 }
336259
337260 private boolean getAutoImportState () {
@@ -342,51 +265,6 @@ private boolean getAutoExportState() {
342265 return this .blockEntity .isAutoExportEnabled ();
343266 }
344267
345- private void updateSideBtnsTooltip () {
346- for (Constants .Sides side : Constants .Sides .values ()) {
347- var sideState = this .blockEntity .getSideState (side );
348- this .sideBtns .get (side .ordinal ()).setMessage (Component .literal (String .format ("%s Side,State: %s" ,side ,sideState )));
349- }
350- }
351-
352- private void renderSideBtnBackgroundAndIcon (GuiGraphics guiGraphics , ExtendedButton button , Constants .Sides side ) {
353- // Get the current mode of the side
354- Constants .SideState sideMode = this .blockEntity .getSideState (side );
355-
356- // Determine the background color based on the mode
357- int backgroundColor = Constants .getColorForMode (sideMode );
358- int margin = 2 ;
359- // Render the background color
360- guiGraphics .fill (
361- button .getX () + margin ,
362- button .getY () + margin ,
363- button .getX () + button .getWidth () - margin ,
364- button .getY () + button .getHeight () - margin ,
365- backgroundColor
366- );
367-
368- // Render the icon with scaling and centering
369- float scale = 1.15f ;
370- float offset = (button .getWidth () - (16 * scale )) / 2 ;
371-
372- guiGraphics .pose ().pushPose ();
373- guiGraphics .pose ().translate (button .getX () + offset , button .getY () + offset , 0 );
374- guiGraphics .pose ().scale (scale , scale , 1 );
375- ItemStack item = ItemStack .EMPTY ;
376- var pos = this .blockEntity .getBlockPos ().relative (this .blockEntity .getRelativeDirection (side ));
377- var state = Objects .requireNonNull (this .blockEntity .getLevel ()).getBlockState (pos );
378- if (state .hasBlockEntity () && this .blockEntity .getLevel ().isLoaded (pos )){
379- item = state .getBlock ().asItem ().getDefaultInstance ();
380- }
381- guiGraphics .renderFakeItem (
382- item ,
383- 0 ,
384- 0
385- );
386- guiGraphics .pose ().popPose ();
387- }
388-
389-
390268 private void handleAutoExportButtonClick (Button button ) {
391269 this .blockEntity .autoExportBtnHandler ();
392270 updateAutoExportButtonTooltip ();
0 commit comments