Skip to content

Commit 36ad202

Browse files
committed
Clean up structure member internals
1 parent d7dd7ad commit 36ad202

File tree

13 files changed

+554
-243
lines changed

13 files changed

+554
-243
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import aztech.modern_industrialization.machines.models.MachineCasing;
3131
import aztech.modern_industrialization.machines.multiblocks.HatchFlags;
3232
import aztech.modern_industrialization.machines.multiblocks.structure.StructureMultiblockInputFormatters;
33-
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMemberTest;
33+
import aztech.modern_industrialization.machines.multiblocks.structure.member.test.StructureMemberTest;
3434
import aztech.modern_industrialization.network.structure.StructureUpdateMemberPacket;
3535
import com.google.common.collect.ImmutableList;
3636
import com.mojang.blaze3d.platform.InputConstants;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import aztech.modern_industrialization.machines.multiblocks.HatchFlags;
3232
import aztech.modern_industrialization.machines.multiblocks.structure.StructureMultiblockInputFormatters;
3333
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMember;
34-
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMemberTest;
34+
import aztech.modern_industrialization.machines.multiblocks.structure.member.test.StructureMemberTest;
3535
import java.util.List;
3636
import java.util.Locale;
3737
import java.util.Objects;

src/main/java/aztech/modern_industrialization/machines/multiblocks/ShapeMatcher.java

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323
*/
2424
package aztech.modern_industrialization.machines.multiblocks;
2525

26-
import aztech.modern_industrialization.MIBlock;
27-
import aztech.modern_industrialization.blocks.structure.member.StructureMemberMode;
28-
import aztech.modern_industrialization.blocks.structure.member.StructureMultiblockMemberBlock;
29-
import aztech.modern_industrialization.blocks.structure.member.StructureMultiblockMemberBlockEntity;
30-
import aztech.modern_industrialization.machines.multiblocks.structure.StructureMultiblockInputFormatters;
26+
import aztech.modern_industrialization.blocks.FastBlockEntity;
3127
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMember;
3228
import aztech.modern_industrialization.machines.multiblocks.world.ChunkEventListener;
3329
import aztech.modern_industrialization.machines.multiblocks.world.ChunkEventListeners;
@@ -256,40 +252,16 @@ public int buildMultiblock(Level level, boolean structureBlocks) {
256252
BlockPos pos = entry.getKey();
257253
var current = level.getBlockState(pos);
258254
if (entry.getValue() instanceof StructureMember member) {
259-
// TODO SWEDZ: there has gotta be a better way to do this
260-
boolean hasName = member.name() != null && !member.name().isEmpty();
261-
boolean hasMultipleTests = member.tests().size() > 1;
262-
boolean hasHatchFlags = member.casing() != null && member.hatchFlags() != null && member.hatchFlags().flags != 0;
263-
264-
if (structureBlocks || hasName) {
265-
if (hasName || hasMultipleTests || hasHatchFlags) {
266-
BlockState state = MIBlock.STRUCTURE_MULTIBLOCK_MEMBER.asBlock().defaultBlockState();
267-
StructureMultiblockMemberBlockEntity be = MIBlock.STRUCTURE_MULTIBLOCK_MEMBER.get().newBlockEntity(pos, state);
268-
if (hasName) {
269-
state = state.setValue(StructureMultiblockMemberBlock.MODE, StructureMemberMode.VARIABLE);
270-
be.setMode(StructureMemberMode.VARIABLE);
271-
be.setInputName(member.name());
272-
}
273-
if (hasMultipleTests || hasHatchFlags) {
274-
be.setInputPreview(StructureMultiblockInputFormatters.preview(member.preview()));
275-
be.setInputMembers(StructureMultiblockInputFormatters.members(member.tests()));
276-
if (hasHatchFlags) {
277-
state = state.setValue(StructureMultiblockMemberBlock.MODE, StructureMemberMode.HATCH);
278-
be.setMode(StructureMemberMode.HATCH);
279-
be.setInputCasing(StructureMultiblockInputFormatters.casing(member.casing()));
280-
be.setInputFlags(StructureMultiblockInputFormatters.hatchFlags(member.hatchFlags()));
281-
} else {
282-
state = state.setValue(StructureMultiblockMemberBlock.MODE, StructureMemberMode.SIMPLE);
283-
be.setMode(StructureMemberMode.SIMPLE);
284-
}
285-
}
286-
level.setBlockAndUpdate(pos, state);
287-
level.setBlockEntity(be);
288-
be.setChanged();
289-
be.sync();
290-
setBlocks++;
291-
continue;
292-
}
255+
var optionalStructureBlock = member.asStructureBlock(pos, structureBlocks);
256+
if (optionalStructureBlock.isPresent()) {
257+
BlockState state = optionalStructureBlock.get().getFirst();
258+
FastBlockEntity be = optionalStructureBlock.get().getSecond();
259+
level.setBlockAndUpdate(pos, state);
260+
level.setBlockEntity(be);
261+
be.setChanged();
262+
be.sync();
263+
setBlocks++;
264+
continue;
293265
}
294266
}
295267
if (!entry.getValue().matchesState(current)) {

src/main/java/aztech/modern_industrialization/machines/multiblocks/ShapeTemplate.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import aztech.modern_industrialization.machines.models.MachineCasing;
2727
import aztech.modern_industrialization.machines.multiblocks.structure.MIStructureTemplateManager;
28-
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMember;
28+
import aztech.modern_industrialization.machines.multiblocks.structure.member.VariableStructureMember;
2929
import java.util.ArrayList;
3030
import java.util.HashMap;
3131
import java.util.HashSet;
@@ -221,7 +221,7 @@ public Structure replace(String name, SimpleMember member, @Nullable HatchFlags
221221
List<BlockPos> positions = new ArrayList<>();
222222
for (var entry : template.simpleMembers.entrySet()) {
223223
SimpleMember other = entry.getValue();
224-
if (other instanceof StructureMember structureMember && Objects.equals(name, structureMember.name())) {
224+
if (other instanceof VariableStructureMember structureMember && Objects.equals(name, structureMember.name())) {
225225
positions.add(entry.getKey());
226226
}
227227
}
@@ -243,11 +243,9 @@ public Structure replace(String name, SimpleMember member) {
243243

244244
public ShapeTemplate build() {
245245
for (SimpleMember member : template.simpleMembers.values()) {
246-
if (member instanceof StructureMember structureMember) {
247-
if (structureMember.name() != null) {
248-
throw new IllegalArgumentException(
249-
"Tried to build structure template without replacing member with the name \"" + structureMember.name() + "\"");
250-
}
246+
if (member instanceof VariableStructureMember structureMember) {
247+
throw new IllegalArgumentException(
248+
"Tried to build structure template without replacing member with the name \"" + structureMember.name() + "\"");
251249
}
252250
}
253251
return template;

src/main/java/aztech/modern_industrialization/machines/multiblocks/structure/MIStructureTemplateManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import aztech.modern_industrialization.machines.models.MachineCasing;
3232
import aztech.modern_industrialization.machines.models.MachineCasings;
3333
import aztech.modern_industrialization.machines.multiblocks.ShapeTemplate;
34+
import aztech.modern_industrialization.machines.multiblocks.structure.member.HatchStructureMember;
3435
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMember;
3536
import java.io.IOException;
3637
import java.io.InputStream;
@@ -153,10 +154,11 @@ public static StructureResult fromWorld(ResourceLocation id, Level level,
153154
}
154155

155156
if (member != null) {
156-
if (member.hatchFlags() != null) {
157+
if (member instanceof HatchStructureMember hatch && hatch.hatchFlags() != null) {
157158
hatchBlocks.add(pos.immutable());
158159
}
159160
CompoundTag memberTag = new CompoundTag();
161+
memberTag.putString("type", member.typeId());
160162
member.save(memberTag);
161163
if (!members.contains(member)) {
162164
members.add(member);
@@ -210,7 +212,7 @@ private static ShapeTemplate deserialize(CompoundTag tag) {
210212
if (blockTag.contains("member_index", Tag.TAG_INT)) {
211213
int memberIndex = blockTag.getInt("member_index");
212214
StructureMember member = StructureMember.from(members.getCompound(memberIndex));
213-
builder.add(pos.getX(), pos.getY(), pos.getZ(), member, member.hatchFlags());
215+
builder.add(pos.getX(), pos.getY(), pos.getZ(), member, member instanceof HatchStructureMember hatch ? hatch.hatchFlags() : null);
214216
}
215217
}
216218

src/main/java/aztech/modern_industrialization/machines/multiblocks/structure/StructureMultiblockInputFormatters.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
import aztech.modern_industrialization.machines.models.MachineCasings;
2929
import aztech.modern_industrialization.machines.multiblocks.HatchFlags;
3030
import aztech.modern_industrialization.machines.multiblocks.HatchType;
31-
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMemberTest;
32-
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMemberTestState;
33-
import aztech.modern_industrialization.machines.multiblocks.structure.member.StructureMemberTestTag;
31+
import aztech.modern_industrialization.machines.multiblocks.structure.member.test.StateStructureMemberTest;
32+
import aztech.modern_industrialization.machines.multiblocks.structure.member.test.StructureMemberTest;
33+
import aztech.modern_industrialization.machines.multiblocks.structure.member.test.TagStructureMemberTest;
3434
import com.mojang.brigadier.exceptions.CommandSyntaxException;
3535
import java.util.ArrayList;
3636
import java.util.List;
@@ -110,11 +110,11 @@ public static List<StructureMemberTest> members(String input) {
110110
return null;
111111
}
112112
var tag = BlockTags.create(tagId);
113-
members.add(new StructureMemberTestTag(tag));
113+
members.add(new TagStructureMemberTest(tag));
114114
} else {
115115
try {
116116
var blockResult = BlockStateParser.parseForBlock(registry, part, true);
117-
members.add(new StructureMemberTestState(blockResult::blockState));
117+
members.add(new StateStructureMemberTest(blockResult::blockState));
118118
} catch (CommandSyntaxException ignored) {
119119
return null;
120120
}
@@ -131,12 +131,12 @@ public static String members(List<StructureMemberTest> members) {
131131

132132
StringBuilder string = new StringBuilder();
133133
for (StructureMemberTest test : members) {
134-
if (test instanceof StructureMemberTestTag testTag) {
134+
if (test instanceof TagStructureMemberTest testTag) {
135135
if (!string.isEmpty()) {
136136
string.append(";");
137137
}
138138
string.append("#").append(testTag.blockTag().location());
139-
} else if (test instanceof StructureMemberTestState stateTest) {
139+
} else if (test instanceof StateStructureMemberTest stateTest) {
140140
if (!string.isEmpty()) {
141141
string.append(";");
142142
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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.machines.multiblocks.structure.member;
25+
26+
import aztech.modern_industrialization.MIBlock;
27+
import aztech.modern_industrialization.blocks.FastBlockEntity;
28+
import aztech.modern_industrialization.blocks.structure.member.StructureMemberMode;
29+
import aztech.modern_industrialization.blocks.structure.member.StructureMultiblockMemberBlock;
30+
import aztech.modern_industrialization.machines.models.MachineCasing;
31+
import aztech.modern_industrialization.machines.models.MachineCasings;
32+
import aztech.modern_industrialization.machines.multiblocks.HatchFlags;
33+
import aztech.modern_industrialization.machines.multiblocks.structure.StructureMultiblockInputFormatters;
34+
import aztech.modern_industrialization.machines.multiblocks.structure.member.test.StructureMemberTest;
35+
import com.mojang.datafixers.util.Pair;
36+
import java.util.List;
37+
import java.util.Optional;
38+
import java.util.function.Supplier;
39+
import net.minecraft.core.BlockPos;
40+
import net.minecraft.nbt.CompoundTag;
41+
import net.minecraft.nbt.Tag;
42+
import net.minecraft.resources.ResourceLocation;
43+
import net.minecraft.world.level.block.state.BlockState;
44+
45+
public class HatchStructureMember extends SimpleStructureMember {
46+
protected MachineCasing casing;
47+
protected HatchFlags hatchFlags;
48+
49+
public HatchStructureMember(Supplier<BlockState> previewSupplier, List<StructureMemberTest> tests, MachineCasing casing, HatchFlags hatchFlags) {
50+
super(previewSupplier, tests);
51+
this.casing = casing;
52+
this.hatchFlags = hatchFlags;
53+
}
54+
55+
public HatchStructureMember() {
56+
}
57+
58+
public MachineCasing casing() {
59+
assertLoaded();
60+
return casing;
61+
}
62+
63+
public HatchFlags hatchFlags() {
64+
assertLoaded();
65+
return hatchFlags;
66+
}
67+
68+
@Override
69+
public String typeId() {
70+
return "hatch";
71+
}
72+
73+
@Override
74+
public boolean isLoaded() {
75+
return super.isLoaded() && casing != null && hatchFlags != null;
76+
}
77+
78+
@Override
79+
public void load(CompoundTag tag) {
80+
super.load(tag);
81+
82+
if (!tag.contains("casing", Tag.TAG_STRING) ||
83+
!tag.contains("hatch_flags", Tag.TAG_INT)) {
84+
throw new IllegalArgumentException("Invalid structure member format for type \"" + typeId() + "\": " + tag);
85+
}
86+
87+
ResourceLocation casingId = ResourceLocation.tryParse(tag.getString("casing"));
88+
if (casingId == null || !MachineCasings.registeredCasings.containsKey(casingId)) {
89+
throw new IllegalArgumentException("Invalid structure member format for type \"" + typeId() + "\": " + tag);
90+
}
91+
casing = MachineCasings.get(casingId);
92+
93+
int hatchFlagsValue = tag.getInt("hatch_flags");
94+
hatchFlags = hatchFlagsValue == 0 ? HatchFlags.NO_HATCH : new HatchFlags(hatchFlagsValue);
95+
}
96+
97+
@Override
98+
public void save(CompoundTag tag) {
99+
super.save(tag);
100+
101+
tag.putString("casing", casing.key.toString());
102+
103+
tag.putInt("hatch_flags", hatchFlags.flags);
104+
}
105+
106+
@Override
107+
public Optional<Pair<BlockState, FastBlockEntity>> asStructureBlock(BlockPos pos, boolean attempt) {
108+
assertLoaded();
109+
110+
if (attempt) {
111+
var state = MIBlock.STRUCTURE_MULTIBLOCK_MEMBER.asBlock().defaultBlockState();
112+
state = state.setValue(StructureMultiblockMemberBlock.MODE, StructureMemberMode.HATCH);
113+
var be = MIBlock.STRUCTURE_MULTIBLOCK_MEMBER.get().newBlockEntity(pos, state);
114+
be.setInputPreview(StructureMultiblockInputFormatters.preview(getPreviewState()));
115+
be.setInputMembers(StructureMultiblockInputFormatters.members(tests));
116+
be.setInputCasing(StructureMultiblockInputFormatters.casing(casing));
117+
be.setInputFlags(StructureMultiblockInputFormatters.hatchFlags(hatchFlags));
118+
return Optional.of(Pair.of(state, be));
119+
}
120+
121+
return Optional.empty();
122+
}
123+
124+
@Override
125+
public boolean equals(Object o) {
126+
assertLoaded();
127+
if (o instanceof HatchStructureMember other && this.getClass() == other.getClass()) {
128+
other.assertLoaded();
129+
return this.getPreviewState() == other.getPreviewState() &&
130+
tests.containsAll(other.tests) && other.tests.containsAll(tests) &&
131+
casing.equals(other.casing) &&
132+
hatchFlags.equals(other.hatchFlags);
133+
}
134+
return false;
135+
}
136+
}

0 commit comments

Comments
 (0)