Skip to content

Commit 24615a7

Browse files
committed
Add member mode to explicitly require hatch fields when hatch mode is selected
1 parent 64025da commit 24615a7

File tree

7 files changed

+134
-30
lines changed

7 files changed

+134
-30
lines changed

src/client/java/aztech/modern_industrialization/gui/structure/StructureMultiblockMemberEditScreen.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,21 @@
2525

2626
import aztech.modern_industrialization.MIBlock;
2727
import aztech.modern_industrialization.MIText;
28+
import aztech.modern_industrialization.blocks.structure.StructureMemberMode;
2829
import aztech.modern_industrialization.blocks.structure.StructureMultiblockMemberBlockEntity;
2930
import aztech.modern_industrialization.machines.models.MachineCasing;
3031
import aztech.modern_industrialization.machines.multiblocks.HatchFlags;
3132
import aztech.modern_industrialization.machines.multiblocks.structure.StructureMultiblockInputFormatters;
3233
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMemberTest;
3334
import aztech.modern_industrialization.network.structure.StructureUpdateMemberPacket;
35+
import com.google.common.collect.ImmutableList;
3436
import com.mojang.blaze3d.platform.InputConstants;
3537
import java.util.List;
3638
import java.util.Optional;
3739
import net.minecraft.client.Minecraft;
3840
import net.minecraft.client.gui.GuiGraphics;
3941
import net.minecraft.client.gui.components.Button;
42+
import net.minecraft.client.gui.components.CycleButton;
4043
import net.minecraft.client.gui.components.EditBox;
4144
import net.minecraft.client.gui.screens.Screen;
4245
import net.minecraft.network.chat.CommonComponents;
@@ -48,10 +51,13 @@ public class StructureMultiblockMemberEditScreen extends Screen {
4851
private static final int VALID_TEXT_COLOR = 0xE0E0E0;
4952
private static final int INVALID_TEXT_COLOR = 0xE07272;
5053

54+
private static final ImmutableList<StructureMemberMode> ALL_MODES = ImmutableList.copyOf(StructureMemberMode.values());
55+
5156
private final StructureMultiblockMemberBlockEntity member;
5257

5358
private Button doneButton;
5459
private Button cancelButton;
60+
private CycleButton<StructureMemberMode> modeButton;
5561

5662
private EditBox previewBox;
5763
private EditBox membersBox;
@@ -80,6 +86,22 @@ private Optional<HatchFlags> getHatchFlags() {
8086
return Optional.ofNullable(StructureMultiblockInputFormatters.hatchFlags(flagsBox.getValue()));
8187
}
8288

89+
private void updateMode(StructureMemberMode mode) {
90+
this.sendToServer();
91+
92+
casingBox.visible = false;
93+
flagsBox.visible = false;
94+
95+
switch (mode) {
96+
case HATCH -> {
97+
casingBox.visible = true;
98+
flagsBox.visible = true;
99+
}
100+
case SIMPLE -> {
101+
}
102+
}
103+
}
104+
83105
private void updatePreview() {
84106
boolean validPreview = previewBox.getValue().isEmpty() || this.getPreview().isPresent();
85107
previewBox.setTextColor(validPreview ? VALID_TEXT_COLOR : INVALID_TEXT_COLOR);
@@ -101,6 +123,7 @@ private void updateFlags() {
101123
}
102124

103125
private void updateAll() {
126+
this.updateMode(member.getMode());
104127
this.updatePreview();
105128
this.updateMembers();
106129
this.updateCasing();
@@ -115,6 +138,7 @@ private void done() {
115138
private void sendToServer() {
116139
new StructureUpdateMemberPacket(
117140
member.getBlockPos(),
141+
modeButton.getValue(),
118142
previewBox.getValue(),
119143
membersBox.getValue(),
120144
casingBox.getValue(),
@@ -131,20 +155,25 @@ protected void init() {
131155
doneButton = Button.builder(CommonComponents.GUI_DONE, button -> this.done()).bounds(width / 2 - 4 - 150, 210, 150, 20).build());
132156
this.addRenderableWidget(
133157
cancelButton = Button.builder(CommonComponents.GUI_CANCEL, button -> this.cancel()).bounds(width / 2 + 4, 210, 150, 20).build());
158+
this.addRenderableWidget(modeButton = CycleButton.builder(StructureMemberMode::text)
159+
.withValues(ALL_MODES, ALL_MODES)
160+
.displayOnlyValue()
161+
.withInitialValue(member.getMode())
162+
.create(width / 2 - 4 - 150, 185, 50, 20, Component.literal("MODE"), (button, mode) -> this.updateMode(mode)));
134163

135-
previewBox = new EditBox(font, width / 2 - 152, 50, 304, 20, MIText.StructureMultiblockMemberPreview.text());
164+
previewBox = new EditBox(font, width / 2 - 152, 20, 304, 20, MIText.StructureMultiblockMemberPreview.text());
136165
previewBox.setMaxLength(Short.MAX_VALUE);
137166
previewBox.setValue(member.getInputPreview());
138167
previewBox.setResponder(text -> this.updatePreview());
139168
this.addRenderableWidget(previewBox);
140169

141-
membersBox = new EditBox(font, width / 2 - 152, 90, 304, 20, MIText.StructureMultiblockMemberMembers.text());
170+
membersBox = new EditBox(font, width / 2 - 152, 60, 304, 20, MIText.StructureMultiblockMemberMembers.text());
142171
membersBox.setMaxLength(Short.MAX_VALUE);
143172
membersBox.setValue(member.getInputMembers());
144173
membersBox.setResponder(text -> this.updateMembers());
145174
this.addRenderableWidget(membersBox);
146175

147-
casingBox = new EditBox(font, width / 2 - 152, 130, 304, 20, MIText.StructureMultiblockHatchCasing.text()) {
176+
casingBox = new EditBox(font, width / 2 - 152, 100, 304, 20, MIText.StructureMultiblockHatchCasing.text()) {
148177
@Override
149178
public boolean charTyped(char codePoint, int modifiers) {
150179
return ResourceLocation.isAllowedInResourceLocation(codePoint) && super.charTyped(codePoint, modifiers);
@@ -155,7 +184,7 @@ public boolean charTyped(char codePoint, int modifiers) {
155184
casingBox.setResponder(text -> this.updateCasing());
156185
this.addRenderableWidget(casingBox);
157186

158-
flagsBox = new EditBox(font, width / 2 - 152, 170, 304, 20, MIText.StructureMultiblockHatchFlags.text()) {
187+
flagsBox = new EditBox(font, width / 2 - 152, 140, 304, 20, MIText.StructureMultiblockHatchFlags.text()) {
159188
@Override
160189
public boolean charTyped(char codePoint, int modifiers) {
161190
return (Character.isDigit(codePoint) || Character.isAlphabetic(codePoint) || codePoint == ';' || codePoint == '_')
@@ -174,15 +203,17 @@ public boolean charTyped(char codePoint, int modifiers) {
174203
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
175204
super.render(graphics, mouseX, mouseY, partialTick);
176205

177-
graphics.drawCenteredString(font, title, width / 2, 20, 0xFFFFFF);
206+
graphics.drawString(font, MIText.StructureMultiblockMemberPreview.text(), width / 2 - 152, 10, 0xA0A0A0);
178207

179-
graphics.drawString(font, MIText.StructureMultiblockMemberPreview.text(), width / 2 - 152, 40, 0xA0A0A0);
208+
graphics.drawString(font, MIText.StructureMultiblockMemberMembers.text(), width / 2 - 152, 50, 0xA0A0A0);
180209

181-
graphics.drawString(font, MIText.StructureMultiblockMemberMembers.text(), width / 2 - 152, 80, 0xA0A0A0);
210+
if (casingBox.visible)
211+
graphics.drawString(font, MIText.StructureMultiblockHatchCasing.text(), width / 2 - 152, 90, 0xA0A0A0);
182212

183-
graphics.drawString(font, MIText.StructureMultiblockHatchCasing.text(), width / 2 - 152, 120, 0xA0A0A0);
213+
if (flagsBox.visible)
214+
graphics.drawString(font, MIText.StructureMultiblockHatchFlags.text(), width / 2 - 152, 130, 0xA0A0A0);
184215

185-
graphics.drawString(font, MIText.StructureMultiblockHatchFlags.text(), width / 2 - 152, 160, 0xA0A0A0);
216+
graphics.drawString(font, modeButton.getValue().textInfo(), width / 2 - 4 - 150, 175, 0xA0A0A0);
186217
}
187218

188219
@Override

src/generated/resources/assets/modern_industrialization/lang/en_us.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,10 @@
17001700
"text.modern_industrialization.StructureMultiblockLoadSuccess": "Successfully placed the structure %s.",
17011701
"text.modern_industrialization.StructureMultiblockLoadTooltip": "Hold SHIFT to place actual blocks instead of structure blocks.",
17021702
"text.modern_industrialization.StructureMultiblockMemberMembers": "Members",
1703+
"text.modern_industrialization.StructureMultiblockMemberModeHatch": "Hatch",
1704+
"text.modern_industrialization.StructureMultiblockMemberModeInfoHatch": "Hatch Mode - Include hatches",
1705+
"text.modern_industrialization.StructureMultiblockMemberModeInfoSimple": "Simple Mode - Just a block",
1706+
"text.modern_industrialization.StructureMultiblockMemberModeSimple": "Simple",
17031707
"text.modern_industrialization.StructureMultiblockMemberPreview": "Preview",
17041708
"text.modern_industrialization.StructureMultiblockSaveFailInvalidBounds": "Failed to save structure. The bounds provided are not valid.",
17051709
"text.modern_industrialization.StructureMultiblockSaveFailMisconfiguredBlock": "Failed to save structure. Some blocks are not properly configured.",

src/main/java/aztech/modern_industrialization/MIText.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ public enum MIText {
251251
StructureMultiblockHatchFlags("Hatch Flags"),
252252
StructureMultiblockMemberMembers("Members"),
253253
StructureMultiblockMemberPreview("Preview"),
254+
StructureMultiblockMemberModeSimple("Simple"),
255+
StructureMultiblockMemberModeHatch("Hatch"),
256+
StructureMultiblockMemberModeInfoSimple("Simple Mode - Just a block"),
257+
StructureMultiblockMemberModeInfoHatch("Hatch Mode - Include hatches"),
254258
StructureMultiblockStructureName("Structure Name"),
255259
StructureMultiblockLoadTooltip("Hold SHIFT to place actual blocks instead of structure blocks."),
256260
StructureMultiblockLoadFailInvalidId("Failed to place the structure. No id was provided."),
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020 Azercoco & Technici4n
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package aztech.modern_industrialization.blocks.structure;
25+
26+
import aztech.modern_industrialization.MIText;
27+
import net.minecraft.network.chat.Component;
28+
29+
public enum StructureMemberMode {
30+
HATCH(MIText.StructureMultiblockMemberModeInfoHatch, MIText.StructureMultiblockMemberModeHatch),
31+
SIMPLE(MIText.StructureMultiblockMemberModeInfoSimple, MIText.StructureMultiblockMemberModeSimple);
32+
33+
private final MIText textInfo, text;
34+
35+
StructureMemberMode(MIText textInfo, MIText text) {
36+
this.textInfo = textInfo;
37+
this.text = text;
38+
}
39+
40+
public Component textInfo() {
41+
return textInfo.text();
42+
}
43+
44+
public Component text() {
45+
return text.text();
46+
}
47+
}

src/main/java/aztech/modern_industrialization/blocks/structure/StructureMultiblockControllerBlockEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
import org.jetbrains.annotations.Nullable;
4444

4545
public class StructureMultiblockControllerBlockEntity extends FastBlockEntity implements StructureMemberOverride {
46+
private StructureControllerMode mode = StructureControllerMode.SAVE;
47+
4648
private String inputId;
4749
private String inputCasing;
4850

49-
private StructureControllerMode mode = StructureControllerMode.SAVE;
50-
5151
private ResourceLocation id;
5252
private MachineCasing casing;
5353
private StructureControllerBounds bounds = new StructureControllerBounds(0, 0, 0, 1, 1, 1);

src/main/java/aztech/modern_industrialization/blocks/structure/StructureMultiblockMemberBlockEntity.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMember;
3232
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMemberTest;
3333
import java.util.List;
34+
import java.util.Locale;
35+
import java.util.Objects;
3436
import net.minecraft.core.BlockPos;
3537
import net.minecraft.core.HolderLookup;
3638
import net.minecraft.nbt.CompoundTag;
@@ -41,6 +43,8 @@
4143
import org.jetbrains.annotations.Nullable;
4244

4345
public class StructureMultiblockMemberBlockEntity extends FastBlockEntity implements StructureMemberOverride {
46+
private StructureMemberMode mode = StructureMemberMode.HATCH;
47+
4448
private String inputPreview;
4549
private String inputMembers;
4650
private String inputCasing;
@@ -55,6 +59,14 @@ public StructureMultiblockMemberBlockEntity(BlockPos pos, BlockState state) {
5559
super(MIRegistries.STRUCTURE_MULTIBLOCK_MEMBER_BE.get(), pos, state);
5660
}
5761

62+
public StructureMemberMode getMode() {
63+
return mode;
64+
}
65+
66+
public void setMode(StructureMemberMode mode) {
67+
this.mode = Objects.requireNonNull(mode);
68+
}
69+
5870
@Nullable
5971
public String getInputPreview() {
6072
return inputPreview;
@@ -117,20 +129,19 @@ public HatchFlags getFlags() {
117129

118130
@Override
119131
public StructureMember getMemberOverride() {
120-
if (casing == null || (flags == null || flags.flags == 0)) {
121-
return new StructureMember(() -> preview, members, null, null);
122-
}
123-
return new StructureMember(() -> preview, members, casing, flags);
132+
return switch (mode) {
133+
case HATCH -> new StructureMember(() -> preview, members, casing, flags);
134+
case SIMPLE -> new StructureMember(() -> preview, members, null, null);
135+
};
124136
}
125137

126138
@Override
127139
public boolean isConfigurationValid() {
128140
if (preview != null && members != null && !members.isEmpty()) {
129-
boolean hasCasing = casing != null;
130-
boolean hasHatchFlags = flags != null && flags.flags != 0;
131-
boolean noCasing = (inputCasing == null || inputCasing.isEmpty()) && !hasCasing;
132-
boolean noFlags = (inputFlags == null || inputFlags.isEmpty()) && !hasHatchFlags;
133-
return (hasCasing && hasHatchFlags) || (noCasing && noFlags);
141+
if (mode == StructureMemberMode.HATCH) {
142+
return casing != null && flags != null && !inputFlags.isEmpty();
143+
}
144+
return true;
134145
}
135146
return false;
136147
}
@@ -150,6 +161,7 @@ public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
150161
@Override
151162
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
152163
super.saveAdditional(tag, registries);
164+
tag.putString("mode", mode.toString().toLowerCase(Locale.ROOT));
153165
if (inputPreview != null) {
154166
tag.putString("preview", inputPreview);
155167
}
@@ -167,6 +179,7 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries)
167179
@Override
168180
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
169181
super.loadAdditional(tag, registries);
182+
mode = StructureMemberMode.valueOf(tag.getString("mode").toUpperCase(Locale.ROOT));
170183
this.setInputPreview(tag.getString("preview"));
171184
this.setInputMembers(tag.getString("members"));
172185
this.setInputCasing(tag.getString("casing"));

src/main/java/aztech/modern_industrialization/network/structure/StructureUpdateMemberPacket.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package aztech.modern_industrialization.network.structure;
2525

26+
import aztech.modern_industrialization.blocks.structure.StructureMemberMode;
2627
import aztech.modern_industrialization.blocks.structure.StructureMultiblockMemberBlockEntity;
2728
import aztech.modern_industrialization.network.BasePacket;
2829
import io.netty.buffer.ByteBuf;
@@ -34,12 +35,15 @@
3435
import net.minecraft.world.level.Level;
3536
import net.minecraft.world.level.block.entity.BlockEntity;
3637

37-
public record StructureUpdateMemberPacket(BlockPos pos, String inputPreview, String inputMembers, String inputCasing, String inputFlags)
38+
public record StructureUpdateMemberPacket(BlockPos pos, StructureMemberMode mode, String inputPreview, String inputMembers, String inputCasing,
39+
String inputFlags)
3840
implements BasePacket {
3941

4042
public static final StreamCodec<ByteBuf, StructureUpdateMemberPacket> STREAM_CODEC = StreamCodec.composite(
4143
BlockPos.STREAM_CODEC,
4244
StructureUpdateMemberPacket::pos,
45+
ByteBufCodecs.idMapper((i) -> StructureMemberMode.values()[i], Enum::ordinal),
46+
StructureUpdateMemberPacket::mode,
4347
ByteBufCodecs.STRING_UTF8,
4448
StructureUpdateMemberPacket::inputPreview,
4549
ByteBufCodecs.STRING_UTF8,
@@ -62,17 +66,18 @@ public void handle(Context ctx) {
6266
}
6367

6468
BlockEntity blockEntity = level.getBlockEntity(pos);
65-
if (blockEntity instanceof StructureMultiblockMemberBlockEntity hatch) {
66-
hatch.setInputPreview(inputPreview);
67-
hatch.setInputMembers(inputMembers);
68-
hatch.setInputCasing(inputCasing);
69-
hatch.setInputFlags(inputFlags);
69+
if (blockEntity instanceof StructureMultiblockMemberBlockEntity member) {
70+
member.setMode(mode);
71+
member.setInputPreview(inputPreview);
72+
member.setInputMembers(inputMembers);
73+
member.setInputCasing(inputCasing);
74+
member.setInputFlags(inputFlags);
7075

71-
hatch.sync();
72-
hatch.setChanged();
76+
member.sync();
77+
member.setChanged();
7378

74-
if (player instanceof ServerPlayer serverPlayer && hatch.isConfigurationValid()) {
75-
StructureMisconfiguredBlocksPacket.forget(hatch.getBlockPos()).sendToClient(serverPlayer);
79+
if (player instanceof ServerPlayer serverPlayer && member.isConfigurationValid()) {
80+
StructureMisconfiguredBlocksPacket.forget(member.getBlockPos()).sendToClient(serverPlayer);
7681
}
7782
}
7883
}

0 commit comments

Comments
 (0)