diff --git a/src/main/java/dev/ithundxr/railwaystweaks/mixin/client/compat/create/ContraptionRenderingWorldMixin.java b/src/main/java/dev/ithundxr/railwaystweaks/mixin/client/compat/create/ContraptionRenderingWorldMixin.java new file mode 100644 index 0000000..03e32a3 --- /dev/null +++ b/src/main/java/dev/ithundxr/railwaystweaks/mixin/client/compat/create/ContraptionRenderingWorldMixin.java @@ -0,0 +1,81 @@ +package dev.ithundxr.railwaystweaks.mixin.client.compat.create; + +import com.jozufozu.flywheel.event.BeginFrameEvent; +import com.simibubi.create.content.contraptions.AbstractContraptionEntity; +import com.simibubi.create.content.contraptions.Contraption; +import com.simibubi.create.content.contraptions.ContraptionHandler; +import com.simibubi.create.content.contraptions.render.ContraptionRenderInfo; +import com.simibubi.create.content.contraptions.render.ContraptionRenderingWorld; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import net.minecraft.world.level.Level; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Shadow; + +import java.lang.ref.WeakReference; +import java.util.List; + +@Mixin(ContraptionRenderingWorld.class) +public abstract class ContraptionRenderingWorldMixin { + @Shadow @Final protected Level world; + + @Shadow private int removalTimer; + + @Shadow @Final protected Int2ObjectMap renderInfos; + @Shadow @Final protected List visible; + + @Shadow public abstract void removeDeadRenderers(); + @Shadow public abstract C getRenderInfo(Contraption c); + + + /** + * @author IThundxr + * @reason Replace stream with for loops to help with performance + */ + @Overwrite + public void tick() { + removalTimer++; + if (removalTimer >= 20) { + removeDeadRenderers(); + removalTimer = 0; + } + + for (WeakReference ref : ContraptionHandler.loadedContraptions.get(world).values()) { + AbstractContraptionEntity entity = ref.get(); + + // contraptions that are too large will not be synced, and un-synced contraptions will be null + if (entity != null && entity.getContraption() != null) { + getRenderInfo(entity.getContraption()); + } + } + } + + /** + * @author IThundxr + * @reason Replace stream with for loops to help with performance + */ + @Overwrite + public void beginFrame(BeginFrameEvent event) { + renderInfos.forEach((key, renderInfo) -> + renderInfo.beginFrame(event) + ); + + collectVisible(); + } + + /** + * @author IThundxr + * @reason Replace stream with for loops to help with performance + */ + @Overwrite + protected void collectVisible() { + visible.clear(); + + renderInfos.forEach((key, renderInfo) -> { + if (renderInfo.isVisible()) { + visible.add(renderInfo); + } + }); + } +} diff --git a/src/main/resources/railwaystweaks.mixins.json b/src/main/resources/railwaystweaks.mixins.json index a673798..b15f881 100644 --- a/src/main/resources/railwaystweaks.mixins.json +++ b/src/main/resources/railwaystweaks.mixins.json @@ -19,7 +19,8 @@ "client": [ "client.HttpTextureMixin", "client.PlayerRendererMixin", - "client.compat.copycatsplus.MultiStateCopycatModelMixin" + "client.compat.copycatsplus.MultiStateCopycatModelMixin", + "client.compat.create.ContraptionRenderingWorldMixin" ], "injectors": { "defaultRequire": 1