Skip to content

Commit

Permalink
See ya later streamagator
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Nov 3, 2024
1 parent 4503482 commit 2486573
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<C extends ContraptionRenderInfo> {
@Shadow @Final protected Level world;

@Shadow private int removalTimer;

@Shadow @Final protected Int2ObjectMap<C> renderInfos;
@Shadow @Final protected List<C> 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<AbstractContraptionEntity> 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);
}
});
}
}
3 changes: 2 additions & 1 deletion src/main/resources/railwaystweaks.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"client": [
"client.HttpTextureMixin",
"client.PlayerRendererMixin",
"client.compat.copycatsplus.MultiStateCopycatModelMixin"
"client.compat.copycatsplus.MultiStateCopycatModelMixin",
"client.compat.create.ContraptionRenderingWorldMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 2486573

Please sign in to comment.