Skip to content

Commit 3c7e690

Browse files
committed
Let controllers save to the current manager so controllers can load new/modified structures immediately
1 parent 2152731 commit 3c7e690

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ private static void assertLoaded() {
7878

7979
public static boolean exists(ResourceLocation id) {
8080
assertLoaded();
81+
Objects.requireNonNull(id);
8182
return STRUCTURE_TEMPLATES.containsKey(id);
8283
}
8384

8485
public static ShapeTemplate get(ResourceLocation id) {
8586
assertLoaded();
87+
Objects.requireNonNull(id);
8688
ShapeTemplate template = STRUCTURE_TEMPLATES.get(id);
8789
if (template != null) {
8890
return template;
8991
} else {
90-
throw new IllegalArgumentException("Structure shape template \"" + id.toString() + "\" does not exist.");
92+
throw new IllegalArgumentException("Structure shape template \"" + id + "\" does not exist.");
9193
}
9294
}
9395

96+
public static void register(ResourceLocation id, ShapeTemplate template) {
97+
assertLoaded();
98+
Objects.requireNonNull(id);
99+
Objects.requireNonNull(template);
100+
STRUCTURE_TEMPLATES.put(id, template);
101+
}
102+
94103
public static StructureResult fromWorld(ResourceLocation id, Level level,
95104
BlockPos controllerPos, Direction controllerDirection,
96105
MachineCasing hatchCasing, StructureControllerBounds bounds) {
@@ -186,7 +195,13 @@ public static StructureResult fromWorld(ResourceLocation id, Level level,
186195
tag.put("members", membersTag);
187196
tag.put("blocks", blocksTag);
188197

189-
return new StructureResult.Success(id, tag);
198+
ShapeTemplate template = deserialize(tag);
199+
if (template == null) {
200+
MI.LOGGER.error("Failed to deserialize structure tag immediately after it was created. Have you changed the deserialier or serializer?");
201+
return new StructureResult.Unknown();
202+
}
203+
204+
return new StructureResult.Success(id, template, tag);
190205
}
191206

192207
@Nullable
@@ -292,7 +307,7 @@ private static void register(ResourceLocation id, Path path) {
292307
if (structureTag != null) {
293308
ShapeTemplate structure = deserialize(structureTag);
294309
if (structure != null) {
295-
STRUCTURE_TEMPLATES.put(id, structure);
310+
register(id, structure);
296311
} else {
297312
MI.LOGGER.error("Failed to load structure with id \"{}\"", id);
298313
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package aztech.modern_industrialization.machines.multiblocks.structure;
2525

2626
import aztech.modern_industrialization.MIText;
27+
import aztech.modern_industrialization.machines.multiblocks.ShapeTemplate;
2728
import aztech.modern_industrialization.network.structure.StructureMisconfiguredBlocksPacket;
2829
import java.util.ArrayList;
2930
import java.util.List;
@@ -115,7 +116,7 @@ public Component text() {
115116
}
116117
}
117118

118-
record Success(ResourceLocation structureId, CompoundTag tag) implements StructureResult {
119+
record Success(ResourceLocation structureId, ShapeTemplate template, CompoundTag tag) implements StructureResult {
119120
@Override
120121
public boolean isSuccess() {
121122
return true;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ public void handle(Context ctx) {
6262
var result = MIStructureTemplateManager.fromWorld(id,
6363
level, pos, state.getValue(StructureMultiblockControllerBlock.FACING),
6464
controller.getCasing(), controller.getBounds());
65-
if (result instanceof StructureResult.Success success &&
66-
!MIStructureTemplateManager.save(id, success.tag())) {
67-
result = new StructureResult.Unknown();
65+
if (result instanceof StructureResult.Success success) {
66+
if (MIStructureTemplateManager.save(id, success.tag())) {
67+
MIStructureTemplateManager.register(id, success.template());
68+
} else {
69+
result = new StructureResult.Unknown();
70+
}
6871
}
6972
result.send(player);
7073
}

0 commit comments

Comments
 (0)