Fix nullpointer when contraptions stop

This commit is contained in:
Jozufozu 2021-08-02 00:08:26 -07:00
parent c674b5df8d
commit 468435e1a0
2 changed files with 19 additions and 8 deletions

View file

@ -50,13 +50,17 @@ public abstract class ContraptionRenderManager<C extends ContraptionRenderInfo>
} }
public void beginFrame(BeginFrameEvent event) { public void beginFrame(BeginFrameEvent event) {
visible.clear();
renderInfos.int2ObjectEntrySet() renderInfos.int2ObjectEntrySet()
.stream() .stream()
.map(Map.Entry::getValue) .map(Map.Entry::getValue)
.forEach(renderInfo -> renderInfo.beginFrame(event)); .forEach(renderInfo -> renderInfo.beginFrame(event));
collectVisible();
}
protected void collectVisible() {
visible.clear();
renderInfos.int2ObjectEntrySet() renderInfos.int2ObjectEntrySet()
.stream() .stream()
.map(Map.Entry::getValue) .map(Map.Entry::getValue)
@ -80,6 +84,9 @@ public abstract class ContraptionRenderManager<C extends ContraptionRenderInfo>
renderInfos.clear(); renderInfos.clear();
} }
/**
* Remove all render infos associated with dead/removed contraptions.
*/
public void removeDeadRenderers() { public void removeDeadRenderers() {
renderInfos.values().removeIf(ContraptionRenderInfo::isDead); renderInfos.values().removeIf(ContraptionRenderInfo::isDead);
} }

View file

@ -80,13 +80,17 @@ public class FlwContraptionManager extends ContraptionRenderManager<RenderedCont
@Override @Override
public void removeDeadRenderers() { public void removeDeadRenderers() {
renderInfos.values().removeIf(renderer -> { boolean removed = renderInfos.values()
if (renderer.isDead()) { .removeIf(renderer -> {
renderer.invalidate(); if (renderer.isDead()) {
return true; renderer.invalidate();
} return true;
return false; }
}); return false;
});
// we use visible in #tick() so we have to re-evaluate it if any were removed
if (removed) collectVisible();
} }
@Override @Override