Skip to content

Commit e8c80df

Browse files
Try to fix weird race condition that caused model shuffling
- Unknown if this fix works, as I was not able to reproduce the bug in the first place despite receiving many reports of it
1 parent 5fdb423 commit e8c80df

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

src/main/java/foundationgames/enhancedblockentities/client/model/DynamicBakedModel.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ public class DynamicBakedModel implements BakedModel, FabricBakedModel {
2727
private final ModelSelector selector;
2828
private final DynamicModelEffects effects;
2929

30-
private final int[] activeModelIndices;
31-
private final BakedModel[] displayedModels;
30+
private final ThreadLocal<int[]> activeModelIndices;
31+
private final ThreadLocal<BakedModel[]> displayedModels;
3232

3333
public DynamicBakedModel(BakedModel[] models, ModelSelector selector, DynamicModelEffects effects) {
3434
this.models = models;
3535
this.selector = selector;
3636
this.effects = effects;
3737

38-
this.activeModelIndices = new int[selector.displayedModelCount];
39-
this.displayedModels = new BakedModel[selector.displayedModelCount];
38+
this.activeModelIndices = ThreadLocal.withInitial(() -> new int[selector.displayedModelCount]);
39+
this.displayedModels = ThreadLocal.withInitial(() -> new BakedModel[selector.displayedModelCount]);
4040
}
4141

4242
@Override
@@ -49,14 +49,17 @@ public void emitBlockQuads(BlockRenderView view, BlockState state, BlockPos bloc
4949
QuadEmitter emitter = context.getEmitter();
5050
RenderMaterial mat = null;
5151

52-
getSelector().writeModelIndices(view, state, blockPos, rng, context, this.activeModelIndices);
53-
for (int i = 0; i < this.activeModelIndices.length; i++) {
54-
int modelIndex = this.activeModelIndices[i];
52+
var indices = this.activeModelIndices.get();
53+
var models = this.displayedModels.get();
54+
55+
getSelector().writeModelIndices(view, state, blockPos, rng, context, indices);
56+
for (int i = 0; i < indices.length; i++) {
57+
int modelIndex = indices[i];
5558

5659
if (modelIndex >= 0) {
57-
this.displayedModels[i] = this.models[modelIndex];
60+
models[i] = this.models[modelIndex];
5861
} else {
59-
this.displayedModels[i] = null;
62+
models[i] = null;
6063
}
6164
}
6265

@@ -67,7 +70,7 @@ public void emitBlockQuads(BlockRenderView view, BlockState state, BlockPos bloc
6770

6871
for (int i = 0; i <= 6; i++) {
6972
Direction dir = ModelHelper.faceFromIndex(i);
70-
for (BakedModel model : this.displayedModels) if (model != null) {
73+
for (BakedModel model : models) if (model != null) {
7174
for (BakedQuad quad : model.getQuads(state, dir, rng.get())) {
7275
emitter.fromVanilla(quad, mat, dir);
7376
emitter.emit();

0 commit comments

Comments
 (0)