|
| 1 | +package dev.ithundxr.railwaystweaks.mixin.client.compat.create; |
| 2 | + |
| 3 | +import com.jozufozu.flywheel.event.BeginFrameEvent; |
| 4 | +import com.simibubi.create.content.contraptions.AbstractContraptionEntity; |
| 5 | +import com.simibubi.create.content.contraptions.Contraption; |
| 6 | +import com.simibubi.create.content.contraptions.ContraptionHandler; |
| 7 | +import com.simibubi.create.content.contraptions.render.ContraptionRenderInfo; |
| 8 | +import com.simibubi.create.content.contraptions.render.ContraptionRenderingWorld; |
| 9 | +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; |
| 10 | +import net.minecraft.world.level.Level; |
| 11 | +import org.spongepowered.asm.mixin.Final; |
| 12 | +import org.spongepowered.asm.mixin.Mixin; |
| 13 | +import org.spongepowered.asm.mixin.Overwrite; |
| 14 | +import org.spongepowered.asm.mixin.Shadow; |
| 15 | + |
| 16 | +import java.lang.ref.WeakReference; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +@Mixin(ContraptionRenderingWorld.class) |
| 20 | +public abstract class ContraptionRenderingWorldMixin<C extends ContraptionRenderInfo> { |
| 21 | + @Shadow @Final protected Level world; |
| 22 | + |
| 23 | + @Shadow private int removalTimer; |
| 24 | + |
| 25 | + @Shadow @Final protected Int2ObjectMap<C> renderInfos; |
| 26 | + @Shadow @Final protected List<C> visible; |
| 27 | + |
| 28 | + @Shadow public abstract void removeDeadRenderers(); |
| 29 | + @Shadow public abstract C getRenderInfo(Contraption c); |
| 30 | + |
| 31 | + |
| 32 | + /** |
| 33 | + * @author IThundxr |
| 34 | + * @reason Replace stream with for loops to help with performance |
| 35 | + */ |
| 36 | + @Overwrite |
| 37 | + public void tick() { |
| 38 | + removalTimer++; |
| 39 | + if (removalTimer >= 20) { |
| 40 | + removeDeadRenderers(); |
| 41 | + removalTimer = 0; |
| 42 | + } |
| 43 | + |
| 44 | + for (WeakReference<AbstractContraptionEntity> ref : ContraptionHandler.loadedContraptions.get(world).values()) { |
| 45 | + AbstractContraptionEntity entity = ref.get(); |
| 46 | + |
| 47 | + // contraptions that are too large will not be synced, and un-synced contraptions will be null |
| 48 | + if (entity != null && entity.getContraption() != null) { |
| 49 | + getRenderInfo(entity.getContraption()); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @author IThundxr |
| 56 | + * @reason Replace stream with for loops to help with performance |
| 57 | + */ |
| 58 | + @Overwrite |
| 59 | + public void beginFrame(BeginFrameEvent event) { |
| 60 | + renderInfos.forEach((key, renderInfo) -> |
| 61 | + renderInfo.beginFrame(event) |
| 62 | + ); |
| 63 | + |
| 64 | + collectVisible(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @author IThundxr |
| 69 | + * @reason Replace stream with for loops to help with performance |
| 70 | + */ |
| 71 | + @Overwrite |
| 72 | + protected void collectVisible() { |
| 73 | + visible.clear(); |
| 74 | + |
| 75 | + renderInfos.forEach((key, renderInfo) -> { |
| 76 | + if (renderInfo.isVisible()) { |
| 77 | + visible.add(renderInfo); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | +} |
0 commit comments