Skip to content

Commit

Permalink
Try to fix weird race condition that caused model shuffling
Browse files Browse the repository at this point in the history
- Unknown if this fix works, as I was not able to reproduce the bug in the first place despite receiving many reports of it
  • Loading branch information
FoundationGames committed Sep 19, 2024
1 parent 5fdb423 commit e8c80df
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ public class DynamicBakedModel implements BakedModel, FabricBakedModel {
private final ModelSelector selector;
private final DynamicModelEffects effects;

private final int[] activeModelIndices;
private final BakedModel[] displayedModels;
private final ThreadLocal<int[]> activeModelIndices;
private final ThreadLocal<BakedModel[]> displayedModels;

public DynamicBakedModel(BakedModel[] models, ModelSelector selector, DynamicModelEffects effects) {
this.models = models;
this.selector = selector;
this.effects = effects;

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

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

getSelector().writeModelIndices(view, state, blockPos, rng, context, this.activeModelIndices);
for (int i = 0; i < this.activeModelIndices.length; i++) {
int modelIndex = this.activeModelIndices[i];
var indices = this.activeModelIndices.get();
var models = this.displayedModels.get();

getSelector().writeModelIndices(view, state, blockPos, rng, context, indices);
for (int i = 0; i < indices.length; i++) {
int modelIndex = indices[i];

if (modelIndex >= 0) {
this.displayedModels[i] = this.models[modelIndex];
models[i] = this.models[modelIndex];
} else {
this.displayedModels[i] = null;
models[i] = null;
}
}

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

for (int i = 0; i <= 6; i++) {
Direction dir = ModelHelper.faceFromIndex(i);
for (BakedModel model : this.displayedModels) if (model != null) {
for (BakedModel model : models) if (model != null) {
for (BakedQuad quad : model.getQuads(state, dir, rng.get())) {
emitter.fromVanilla(quad, mat, dir);
emitter.emit();
Expand Down

0 comments on commit e8c80df

Please sign in to comment.