Skip to content

Commit 9c22c0f

Browse files
committed
Force load BlockStates on startup
1 parent ff4580c commit 9c22c0f

File tree

10 files changed

+53
-6
lines changed

10 files changed

+53
-6
lines changed

src/client/java/aztech/modern_industrialization/compat/viewer/usage/MultiblockCategory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public Recipe(ResourceLocation controller, ShapeTemplate shapeTemplate, @Nullabl
9595
for (var entry : shapeTemplate.members().entrySet()) {
9696
// TODO SWEDZ MULTIBLOCKS: account for nbt
9797
MultiblockMemberState state = entry.getValue().getPreviewState();
98-
Item item = state.blockState().getBlock().asItem();
98+
Item item = state.state().getBlock().asItem();
9999
if (item != Items.AIR) {
100100
materials.put(item, 1 + materials.getOrDefault(item, 0));
101101
}

src/client/java/aztech/modern_industrialization/machines/multiblocks/MultiblockErrorHighlight.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static void end(RenderLevelStageEvent event) {
7474
RenderHelper.drawCube(poseStack, immediate, 1, 50f / 256, 50f / 256, 15728880, OverlayTexture.NO_OVERLAY);
7575
} else {
7676
// TODO SWEDZ MULTIBLOCKS: account for nbt + rotation
77-
Minecraft.getInstance().getBlockRenderer().renderSingleBlock(state.blockState(), poseStack, immediate, 15728880,
77+
Minecraft.getInstance().getBlockRenderer().renderSingleBlock(state.state(), poseStack, immediate, 15728880,
7878
OverlayTexture.NO_OVERLAY);
7979
}
8080

src/main/java/aztech/modern_industrialization/guidebook/MultiblockShapeCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void compile(GuidebookScene scene, PageCompiler compiler, LytErrorSink er
8080

8181
List<Component> tooltipLines = new ArrayList<>();
8282
// Add name of the block because the annotation overrides the usual tooltip
83-
tooltipLines.add(member.getPreviewState().blockState().getBlock().getName());
83+
tooltipLines.add(member.getPreviewState().state().getBlock().getName());
8484

8585
tooltipLines.add(MIText.AcceptsHatches.text());
8686
var flags = hatchMember.hatchFlags();

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

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

26+
import aztech.modern_industrialization.MI;
2627
import aztech.modern_industrialization.machines.models.MachineCasing;
2728
import aztech.modern_industrialization.machines.multiblocks.shape.member.MultiblockMember;
2829
import aztech.modern_industrialization.machines.multiblocks.shape.member.SimpleMultiblockMember;
2930
import java.util.*;
3031
import net.minecraft.core.BlockPos;
32+
import net.neoforged.bus.api.SubscribeEvent;
33+
import net.neoforged.fml.common.EventBusSubscriber;
34+
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
3135
import org.jetbrains.annotations.Nullable;
3236

3337
/**
3438
* An immutable description of a multiblock shape.
3539
*/
40+
@EventBusSubscriber(modid = MI.ID, bus = EventBusSubscriber.Bus.MOD)
3641
public final class ShapeTemplate {
42+
private static final List<ShapeTemplate> templates = new ArrayList<>();
43+
3744
private final Map<BlockPos, MultiblockMember> members = new HashMap<>();
3845

3946
private ShapeTemplate() {
47+
templates.add(this);
4048
}
4149

4250
public Map<BlockPos, MultiblockMember> members() {
4351
return Collections.unmodifiableMap(members);
4452
}
4553

54+
/**
55+
* Forces all lazy block states in structures to get loaded during startup as soon as blocks are in the registry.
56+
* This prevents any delay later where we otherwise would need to be parsing json elements during play. It also
57+
* ensures that we get consistent results (e.g. if a file changes during play).
58+
*/
59+
@SubscribeEvent
60+
private static void onSetup(FMLCommonSetupEvent event) {
61+
for (ShapeTemplate template : templates) {
62+
for (var member : template.members().values()) {
63+
member.forceLoad();
64+
}
65+
}
66+
MI.LOGGER.info("Initialized {} multiblock shapes", templates.size());
67+
}
68+
4669
public static class Builder {
4770
private final ShapeTemplate template;
4871
private final MachineCasing hatchCasing;

src/main/java/aztech/modern_industrialization/machines/multiblocks/shape/member/MultiblockMember.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public static HatchMultiblockMember hatch(SimpleMultiblockMember parent, Machine
7878
return new HatchMultiblockMember(parent.preview(), parent.tests(), casing, hatchFlags);
7979
}
8080

81+
public abstract void forceLoad();
82+
8183
// TODO SWEDZ MULTIBLOCKS: account for nbt
8284
public abstract boolean matchesState(BlockState state);
8385

src/main/java/aztech/modern_industrialization/machines/multiblocks/shape/member/MultiblockMemberState.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public MultiblockMemberState(BlockStateParser.BlockResult result) {
4949
this(Lazy.of(result::blockState));
5050
}
5151

52-
public BlockState blockState() {
52+
public BlockState state() {
5353
return lazyState.get();
5454
}
5555

5656
// TODO SWEDZ MULTIBLOCKS: account for nbt + rotation
5757
// TODO SWEDZ MULTIBLOCKS: boolean field for if we want the structure block
5858
public void setBlock(Level level, BlockPos pos) {
59-
level.setBlockAndUpdate(pos, this.blockState());
59+
level.setBlockAndUpdate(pos, this.state());
6060
}
6161
}

src/main/java/aztech/modern_industrialization/machines/multiblocks/shape/member/SimpleMultiblockMember.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ public List<MultiblockMemberTest> tests() {
4848
return tests;
4949
}
5050

51+
@Override
52+
public void forceLoad() {
53+
preview.state();
54+
for (var test : tests) {
55+
test.forceLoad();
56+
}
57+
}
58+
5159
@Override
5260
public boolean matchesState(BlockState state) {
5361
for (var test : tests) {

src/main/java/aztech/modern_industrialization/machines/multiblocks/shape/test/BlockMultiblockMemberTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public BlockMultiblockMemberTest(Lazy<? extends Block> block) {
3434
this.block = block;
3535
}
3636

37+
@Override
38+
public void forceLoad() {
39+
block.get();
40+
}
41+
3742
@Override
3843
public boolean matchesState(BlockState state) {
3944
return state.is(block.get());

src/main/java/aztech/modern_industrialization/machines/multiblocks/shape/test/MultiblockMemberTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import net.minecraft.world.level.block.state.BlockState;
2727

2828
public abstract class MultiblockMemberTest {
29+
public abstract void forceLoad();
30+
2931
public abstract boolean matchesState(BlockState state);
3032

3133
/**

src/main/java/aztech/modern_industrialization/machines/multiblocks/shape/test/StateMultiblockMemberTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@ public StateMultiblockMemberTest(MultiblockMemberState state) {
3939
this(List.of(state));
4040
}
4141

42+
@Override
43+
public void forceLoad() {
44+
for (var state : states) {
45+
state.state();
46+
}
47+
}
48+
4249
@Override
4350
public boolean matchesState(BlockState state) {
4451
for (var other : states) {
45-
if (state == other.blockState()) {
52+
if (state == other.state()) {
4653
return true;
4754
}
4855
}

0 commit comments

Comments
 (0)