Skip to content

Commit d08d0f3

Browse files
committed
Major Refactor of the SideConfig Buttons in the MechanicalCrafterGUI
1 parent facb27e commit d08d0f3

File tree

10 files changed

+233
-187
lines changed

10 files changed

+233
-187
lines changed

src/main/java/net/roboxgamer/modernutils/ModernUtilsMod.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.roboxgamer.modernutils;
22

3-
import net.minecraft.client.Minecraft;
43
import net.minecraft.resources.ResourceLocation;
54
import net.neoforged.neoforge.capabilities.Capabilities;
65
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;

src/main/java/net/roboxgamer/modernutils/block/entity/custom/MechanicalCrafterBlockEntity.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import net.roboxgamer.modernutils.block.entity.ModBlockEntities;
4141
import net.roboxgamer.modernutils.menu.MechanicalCrafterMenu;
4242
import net.roboxgamer.modernutils.network.ItemStackPayload;
43-
import net.roboxgamer.modernutils.network.SideStatePayload;
4443
import net.roboxgamer.modernutils.network.SlotStatePayload;
4544
import net.roboxgamer.modernutils.util.Constants;
4645
import net.roboxgamer.modernutils.util.CustomRecipeExtender;
@@ -122,25 +121,24 @@ public Direction getRelativeDirection(Constants.Sides side) {
122121
return sideDir;
123122
}
124123

125-
public void handleSideBtnClick(@NotNull Constants.Sides side, Button button, ClickAction clickAction) {
124+
public void handleSideBtnClick(@NotNull Constants.Sides side, boolean isShiftPressed, ClickAction clickAction) {
126125
Direction sideDir = getRelativeDirection(side);
127-
// Cycle through the side states
128126
Constants.SideState sideState = getSideState(side);
129127
int currentState = sideState.ordinal();
130-
int nextState = (currentState + 1) % Constants.SideState.values().length;
131-
132-
// Check if the Shift key is held down
133-
long window = Minecraft.getInstance().getWindow().getWindow();
134-
boolean isShiftPressed = InputConstants.isKeyDown(window, InputConstants.KEY_LSHIFT)
135-
|| InputConstants.isKeyDown(window, InputConstants.KEY_RSHIFT);
128+
int totalStates = Constants.SideState.values().length;
136129

137130
if (isShiftPressed) {
138131
// Handle shift + left-click logic here
139132
sideState = Constants.SideState.NONE;
140133
this.sideBtnStates.put(side, sideState);
141-
}else{
134+
} else {
135+
int nextState;
142136
if (clickAction != null && clickAction.equals(ClickAction.SECONDARY)) {
143-
nextState = (currentState - 1) % Constants.SideState.values().length;
137+
// For reverse cycling, handle negative numbers
138+
nextState = (currentState - 1 + totalStates) % totalStates;
139+
} else {
140+
// Forward cycling
141+
nextState = (currentState + 1) % totalStates;
144142
}
145143
this.sideBtnStates.put(side, Constants.SideState.values()[nextState]);
146144
}
@@ -166,17 +164,9 @@ public void handleSideBtnClick(@NotNull Constants.Sides side, Button button, Cli
166164
this.exportDirections.put(sideDir,false);
167165
}
168166
}
169-
if (button != null) {
170-
button.setMessage(Component.literal(String.format("%s Side,State: %s",side,sideState)));
171-
}
172167

173168
ModernUtilsMod.LOGGER.debug("Side {} State Changed to {}", side, getSideState(side));
174169
setChanged();
175-
if (level != null && level.isClientSide()) {
176-
PacketDistributor.sendToServer(
177-
new SideStatePayload(side, clickAction, this.getBlockPos())
178-
);
179-
}
180170
}
181171

182172
public Constants.SideState getSideState(Constants.Sides side){
@@ -228,6 +218,10 @@ public boolean isAutoExportEnabled() {
228218
return this.autoExportEnabled;
229219
}
230220

221+
public void setSideBtnState(Constants.@NotNull Sides side,Constants.SideState state) {
222+
this.sideBtnStates.put(side, state);
223+
}
224+
231225
public class CustomItemStackHandler extends ItemStackHandler {
232226
public CustomItemStackHandler(int size) {
233227
super(size);
@@ -1111,8 +1105,14 @@ private void loadSidesConfig(CompoundTag tag) {
11111105
Direction sideDir = getRelativeDirection(side);
11121106

11131107
switch (sideState){
1114-
case INPUT -> this.importDirections.put(sideDir,true);
1115-
case OUTPUT -> this.exportDirections.put(sideDir,true);
1108+
case INPUT -> {
1109+
this.importDirections.put(sideDir,true);
1110+
this.exportDirections.put(sideDir,false);
1111+
}
1112+
case OUTPUT -> {
1113+
this.importDirections.put(sideDir,false);
1114+
this.exportDirections.put(sideDir,true);
1115+
}
11161116
case BOTH -> {
11171117
this.importDirections.put(sideDir,true);
11181118
this.exportDirections.put(sideDir,true);

src/main/java/net/roboxgamer/modernutils/client/screen/ExtendedButton.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected void renderWidget(@NotNull GuiGraphics guiGraphics, int mouseX, int mo
125125
}
126126
}
127127

128-
private void renderTooltip(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY) {
128+
public void renderTooltip(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY) {
129129
Component msg = this.getMessage();
130130
if (this.button != null){
131131
msg = this.button.getMessage();

src/main/java/net/roboxgamer/modernutils/client/screen/MechanicalCrafterScreen.java

Lines changed: 30 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import net.roboxgamer.modernutils.util.RedstoneManager;
2828
import 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

3333
import 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

Comments
 (0)