Skip to content

Commit

Permalink
Optimize tile entity optimizations
Browse files Browse the repository at this point in the history
Fixes #73
  • Loading branch information
Wyvest committed Aug 5, 2024
1 parent b27402b commit eb28e7f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(RenderChunk.class)
public class RenderChunkMixin_OptimizeSpecialTileEntities {
//#if MC==10809

//#if MC==10809
@Unique private TileEntitySpecialRenderer<TileEntity> patcher$tileEntitySpecialRenderer;

@ModifyVariable(method = "rebuildChunk", at = @At("STORE"), ordinal = 0)
private TileEntitySpecialRenderer<TileEntity> patcher$renderSpecialTileEntitiesOnce(TileEntitySpecialRenderer<TileEntity> tileEntitySpecialRenderer) {
patcher$tileEntitySpecialRenderer = tileEntitySpecialRenderer;
return tileEntitySpecialRenderer;
}

/**
* Minecraft typically renders special tile entities twice per frame. This is noticeable by comparing the opacity
* of a beacon beam when the beacon is in view versus when it's out of view (fly 50 blocks above, for instance).
Expand All @@ -21,9 +32,8 @@ public class RenderChunkMixin_OptimizeSpecialTileEntities {
*/
@Redirect(method = "rebuildChunk", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/chunk/CompiledChunk;addTileEntity(Lnet/minecraft/tileentity/TileEntity;)V"))
private void patcher$renderSpecialTileEntitiesOnce(CompiledChunk instance, TileEntity tileEntityIn) {
TileEntitySpecialRenderer<TileEntity> tileentityspecialrenderer = TileEntityRendererDispatcher.instance.getSpecialRenderer(tileEntityIn);
if (!tileentityspecialrenderer.forceTileEntityRender()) instance.addTileEntity(tileEntityIn);
if (!patcher$tileEntitySpecialRenderer.forceTileEntityRender()) instance.addTileEntity(tileEntityIn);
}
//#endif
//#endif
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(value = TileEntityRendererDispatcher.class, priority = 100)
public abstract class TileEntityRendererDispatcherMixin_RemoveInvalidEntities {

@Mixin(TileEntityRendererDispatcher.class)
public class TileEntityRendererDispatcherMixin_RemoveInvalidEntities {
//#if MC==10809
@Inject(method = "getSpecialRenderer", at = @At("HEAD"), cancellable = true)
private <T extends TileEntity> void patcher$returnNullIfInvalid(TileEntity tileEntityIn, CallbackInfoReturnable<TileEntitySpecialRenderer<T>> cir) {
if (tileEntityIn == null || tileEntityIn.isInvalid()) {
cir.setReturnValue(null);
}
@Shadow
public abstract <T extends TileEntity> TileEntitySpecialRenderer<T> getSpecialRendererByClass(Class<? extends TileEntity> teClass);

/**
* @author MicrocontrollersDev and Wyvest
* @reason Remove invalid tile entities from being rendered
*/
@Overwrite
private <T extends TileEntity> TileEntitySpecialRenderer<T> getSpecialRenderer(TileEntity tileEntityIn) {
return tileEntityIn != null && !tileEntityIn.isInvalid() ? this.getSpecialRendererByClass(tileEntityIn.getClass()) : null;
}
//#endif
}

0 comments on commit eb28e7f

Please sign in to comment.