diff --git a/src/main/java/foundationgames/enhancedblockentities/client/model/DynamicBakedModel.java b/src/main/java/foundationgames/enhancedblockentities/client/model/DynamicBakedModel.java index f82109e..c0a7e3e 100644 --- a/src/main/java/foundationgames/enhancedblockentities/client/model/DynamicBakedModel.java +++ b/src/main/java/foundationgames/enhancedblockentities/client/model/DynamicBakedModel.java @@ -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 activeModelIndices; + private final ThreadLocal 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 @@ -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; } } @@ -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();