Skip to content

Commit 5b2e4f3

Browse files
committed
Clean up structure member and member test internals to do proper loaded and null checks
1 parent 701d04c commit 5b2e4f3

File tree

7 files changed

+65
-40
lines changed

7 files changed

+65
-40
lines changed

src/main/java/aztech/modern_industrialization/machines/multiblocks/structure/member/HatchStructureMember.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import aztech.modern_industrialization.machines.multiblocks.structure.member.test.StructureMemberTest;
3535
import com.mojang.datafixers.util.Pair;
3636
import java.util.List;
37+
import java.util.Objects;
3738
import java.util.Optional;
3839
import java.util.function.Supplier;
3940
import net.minecraft.core.BlockPos;
@@ -48,6 +49,8 @@ public class HatchStructureMember extends SimpleStructureMember {
4849

4950
public HatchStructureMember(Supplier<BlockState> previewSupplier, List<StructureMemberTest> tests, MachineCasing casing, HatchFlags hatchFlags) {
5051
super(previewSupplier, tests);
52+
Objects.requireNonNull(casing);
53+
Objects.requireNonNull(hatchFlags);
5154
this.casing = casing;
5255
this.hatchFlags = hatchFlags;
5356
}

src/main/java/aztech/modern_industrialization/machines/multiblocks/structure/member/SimpleStructureMember.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import net.minecraft.world.level.block.Blocks;
4646
import net.minecraft.world.level.block.state.BlockState;
4747

48-
public class SimpleStructureMember implements StructureMember {
48+
public class SimpleStructureMember extends StructureMember {
4949
protected Supplier<BlockState> previewSupplier;
5050
protected List<StructureMemberTest> tests;
5151

@@ -78,7 +78,7 @@ public boolean isLoaded() {
7878

7979
@Override
8080
public void load(CompoundTag tag) {
81-
Objects.requireNonNull(tag);
81+
super.load(tag);
8282
if (!tag.contains("preview", CompoundTag.TAG_COMPOUND) ||
8383
!tag.contains("tests", CompoundTag.TAG_LIST)) {
8484
throw new IllegalArgumentException("Invalid structure member format for type \"" + typeId() + "\": " + tag);
@@ -102,8 +102,7 @@ public void load(CompoundTag tag) {
102102

103103
@Override
104104
public void save(CompoundTag tag) {
105-
Objects.requireNonNull(tag);
106-
StructureMember.super.save(tag);
105+
super.save(tag);
107106

108107
tag.put("preview", NbtUtils.writeBlockState(getPreviewState()));
109108

@@ -138,6 +137,7 @@ public Optional<Pair<BlockState, FastBlockEntity>> asStructureBlock(BlockPos pos
138137

139138
@Override
140139
public boolean matchesState(BlockState state) {
140+
assertLoaded();
141141
for (StructureMemberTest test : tests) {
142142
if (test.matchesState(state)) {
143143
return true;
@@ -148,6 +148,7 @@ public boolean matchesState(BlockState state) {
148148

149149
@Override
150150
public BlockState getPreviewState() {
151+
assertLoaded();
151152
if (preview == null) {
152153
preview = previewSupplier.get();
153154
if (preview == null) {

src/main/java/aztech/modern_industrialization/machines/multiblocks/structure/member/StructureMember.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,54 @@
3131
import aztech.modern_industrialization.machines.multiblocks.structure.member.test.StructureMemberTest;
3232
import com.mojang.datafixers.util.Pair;
3333
import java.util.List;
34+
import java.util.Objects;
3435
import java.util.Optional;
3536
import java.util.function.Supplier;
3637
import net.minecraft.core.BlockPos;
3738
import net.minecraft.nbt.CompoundTag;
3839
import net.minecraft.world.level.block.state.BlockState;
3940

40-
public interface StructureMember extends SimpleMember {
41-
String typeId();
41+
public abstract class StructureMember implements SimpleMember {
42+
public abstract String typeId();
4243

43-
boolean isLoaded();
44+
public abstract boolean isLoaded();
4445

45-
default void assertLoaded() {
46+
protected final void assertLoaded() {
4647
if (!isLoaded()) {
4748
throw new IllegalStateException("Member is not loaded");
4849
}
4950
}
5051

51-
void load(CompoundTag tag);
52+
public void load(CompoundTag tag) {
53+
Objects.requireNonNull(tag);
54+
}
5255

53-
default void save(CompoundTag tag) {
56+
public void save(CompoundTag tag) {
57+
Objects.requireNonNull(tag);
5458
assertLoaded();
5559
}
5660

57-
Optional<Pair<BlockState, FastBlockEntity>> asStructureBlock(BlockPos pos, boolean attempt);
61+
public abstract Optional<Pair<BlockState, FastBlockEntity>> asStructureBlock(BlockPos pos, boolean attempt);
5862

59-
static SimpleStructureMember simple(Supplier<BlockState> preview, List<StructureMemberTest> tests) {
63+
public static SimpleStructureMember simple(Supplier<BlockState> preview, List<StructureMemberTest> tests) {
6064
return new SimpleStructureMember(preview, tests);
6165
}
6266

63-
static SimpleStructureMember literal(Supplier<BlockState> blockState) {
67+
public static SimpleStructureMember literal(Supplier<BlockState> blockState) {
6468
return simple(blockState, List.of(new StateStructureMemberTest(blockState)));
6569
}
6670

67-
static HatchStructureMember hatch(Supplier<BlockState> preview, List<StructureMemberTest> tests, MachineCasing casing, HatchFlags hatchFlags) {
71+
public static HatchStructureMember hatch(Supplier<BlockState> preview, List<StructureMemberTest> tests, MachineCasing casing,
72+
HatchFlags hatchFlags) {
6873
return new HatchStructureMember(preview, tests, casing, hatchFlags);
6974
}
7075

71-
static VariableStructureMember variable(String name) {
76+
public static VariableStructureMember variable(String name) {
7277
return new VariableStructureMember(name);
7378
}
7479

75-
static StructureMember from(CompoundTag tag) {
80+
public static StructureMember from(CompoundTag tag) {
81+
Objects.requireNonNull(tag);
7682
if (!tag.contains("type", CompoundTag.TAG_STRING)) {
7783
throw new IllegalArgumentException("Invalid structure member format: " + tag);
7884
}
@@ -81,7 +87,7 @@ static StructureMember from(CompoundTag tag) {
8187
case "simple" -> new SimpleStructureMember();
8288
case "hatch" -> new HatchStructureMember();
8389
case "variable" -> new VariableStructureMember();
84-
default -> throw new IllegalStateException("Unexpected value: " + type);
90+
default -> throw new IllegalStateException("Unexpected type: " + type);
8591
};
8692
member.load(tag);
8793
return member;

src/main/java/aztech/modern_industrialization/machines/multiblocks/structure/member/VariableStructureMember.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import net.minecraft.nbt.Tag;
3636
import net.minecraft.world.level.block.state.BlockState;
3737

38-
public class VariableStructureMember implements StructureMember {
38+
public class VariableStructureMember extends StructureMember {
3939
protected String name;
4040

4141
public VariableStructureMember(String name) {
@@ -63,7 +63,7 @@ public boolean isLoaded() {
6363

6464
@Override
6565
public void load(CompoundTag tag) {
66-
Objects.requireNonNull(tag);
66+
super.load(tag);
6767
if (!tag.contains("name", Tag.TAG_STRING)) {
6868
throw new IllegalArgumentException("Invalid structure member format for type \"" + typeId() + "\": " + tag);
6969
}
@@ -76,8 +76,7 @@ public void load(CompoundTag tag) {
7676

7777
@Override
7878
public void save(CompoundTag tag) {
79-
Objects.requireNonNull(tag);
80-
StructureMember.super.save(tag);
79+
super.save(tag);
8180

8281
tag.putString("name", name);
8382
}

src/main/java/aztech/modern_industrialization/machines/multiblocks/structure/member/test/StateStructureMemberTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@
2929
import net.minecraft.nbt.CompoundTag;
3030
import net.minecraft.nbt.NbtUtils;
3131
import net.minecraft.nbt.Tag;
32+
import net.minecraft.world.level.block.Blocks;
3233
import net.minecraft.world.level.block.state.BlockState;
3334

34-
public class StateStructureMemberTest implements StructureMemberTest {
35+
public class StateStructureMemberTest extends StructureMemberTest {
3536
private Supplier<BlockState> blockStateSupplier;
3637

3738
private BlockState blockState;
3839

3940
public StateStructureMemberTest(Supplier<BlockState> blockState) {
41+
Objects.requireNonNull(blockState);
4042
this.blockStateSupplier = blockState;
4143
}
4244

@@ -45,8 +47,12 @@ public StateStructureMemberTest() {
4547
}
4648

4749
public BlockState blockState() {
50+
assertLoaded();
4851
if (blockState == null) {
4952
blockState = blockStateSupplier.get();
53+
if (blockState == null) {
54+
blockState = Blocks.AIR.defaultBlockState();
55+
}
5056
}
5157
return blockState;
5258
}
@@ -58,6 +64,7 @@ public String typeId() {
5864

5965
@Override
6066
public boolean matchesState(BlockState state) {
67+
assertLoaded();
6168
return this.blockState() == state;
6269
}
6370

@@ -68,7 +75,7 @@ public boolean isLoaded() {
6875

6976
@Override
7077
public void load(CompoundTag tag) {
71-
Objects.requireNonNull(tag);
78+
super.load(tag);
7279
if (!tag.contains("state", Tag.TAG_COMPOUND)) {
7380
throw new IllegalArgumentException("Invalid structure member test format for type \"" + typeId() + "\": " + tag);
7481
}
@@ -79,15 +86,16 @@ public void load(CompoundTag tag) {
7986

8087
@Override
8188
public void save(CompoundTag tag) {
82-
Objects.requireNonNull(tag);
83-
StructureMemberTest.super.save(tag);
89+
super.save(tag);
8490

8591
tag.put("state", NbtUtils.writeBlockState(this.blockState()));
8692
}
8793

8894
@Override
8995
public boolean equals(Object o) {
96+
assertLoaded();
9097
if (o instanceof StateStructureMemberTest other) {
98+
other.assertLoaded();
9199
return this.blockState() == other.blockState();
92100
}
93101
return false;

src/main/java/aztech/modern_industrialization/machines/multiblocks/structure/member/test/StructureMemberTest.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,43 @@
2323
*/
2424
package aztech.modern_industrialization.machines.multiblocks.structure.member.test;
2525

26+
import java.util.Objects;
2627
import net.minecraft.nbt.CompoundTag;
2728
import net.minecraft.nbt.Tag;
2829
import net.minecraft.world.level.block.state.BlockState;
2930

30-
public interface StructureMemberTest {
31-
String typeId();
31+
public abstract class StructureMemberTest {
32+
public abstract String typeId();
3233

33-
boolean matchesState(BlockState state);
34+
public abstract boolean matchesState(BlockState state);
3435

35-
boolean isLoaded();
36+
public abstract boolean isLoaded();
3637

37-
default void assertLoaded() {
38+
protected final void assertLoaded() {
3839
if (!isLoaded()) {
3940
throw new IllegalStateException("Member test is not loaded");
4041
}
4142
}
4243

43-
void load(CompoundTag tag);
44+
public void load(CompoundTag tag) {
45+
Objects.requireNonNull(tag);
46+
}
4447

45-
default void save(CompoundTag tag) {
48+
public void save(CompoundTag tag) {
49+
Objects.requireNonNull(tag);
4650
assertLoaded();
4751
}
4852

49-
static StructureMemberTest from(CompoundTag tag) {
53+
public static StructureMemberTest from(CompoundTag tag) {
54+
Objects.requireNonNull(tag);
5055
if (!tag.contains("type", Tag.TAG_STRING)) {
5156
throw new IllegalArgumentException("Invalid structure member test format: " + tag);
5257
}
5358
String type = tag.getString("type");
5459
StructureMemberTest test = switch (type) {
5560
case "tag" -> new TagStructureMemberTest();
5661
case "state" -> new StateStructureMemberTest();
57-
default -> throw new IllegalStateException("Unexpected value: " + type);
62+
default -> throw new IllegalStateException("Unexpected type: " + type);
5863
};
5964
test.load(tag);
6065
return test;

src/main/java/aztech/modern_industrialization/machines/multiblocks/structure/member/test/TagStructureMemberTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
import net.minecraft.world.level.block.Block;
3333
import net.minecraft.world.level.block.state.BlockState;
3434

35-
public class TagStructureMemberTest implements StructureMemberTest {
35+
public class TagStructureMemberTest extends StructureMemberTest {
3636
private TagKey<Block> blockTag;
3737

3838
public TagStructureMemberTest(TagKey<Block> blockTag) {
39+
Objects.requireNonNull(blockTag);
3940
this.blockTag = blockTag;
4041
}
4142

@@ -54,7 +55,8 @@ public String typeId() {
5455

5556
@Override
5657
public boolean matchesState(BlockState state) {
57-
return blockTag != null && state.is(blockTag);
58+
assertLoaded();
59+
return state.is(blockTag);
5860
}
5961

6062
@Override
@@ -64,7 +66,7 @@ public boolean isLoaded() {
6466

6567
@Override
6668
public void load(CompoundTag tag) {
67-
Objects.requireNonNull(tag);
69+
super.load(tag);
6870
if (!tag.contains("tag", Tag.TAG_STRING)) {
6971
throw new IllegalArgumentException("Invalid structure member test format for type \"" + typeId() + "\": " + tag);
7072
}
@@ -74,17 +76,18 @@ public void load(CompoundTag tag) {
7476

7577
@Override
7678
public void save(CompoundTag tag) {
77-
Objects.requireNonNull(tag);
78-
StructureMemberTest.super.save(tag);
79+
super.save(tag);
7980

8081
tag.putString("tag", blockTag.location().toString());
8182
}
8283

8384
@Override
8485
public boolean equals(Object o) {
86+
assertLoaded();
8587
if (o instanceof TagStructureMemberTest other) {
88+
other.assertLoaded();
8689
return blockTag == other.blockTag ||
87-
(blockTag != null && other.blockTag != null && blockTag.location().equals(other.blockTag.location()));
90+
blockTag.location().equals(other.blockTag.location());
8891
}
8992
return false;
9093
}

0 commit comments

Comments
 (0)