aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/lodmod/mixin/MixinWorldRenderer.java
blob: eba0a5e26c287cf7137e4fe6828824eb3fc50c87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package makamys.lodmod.mixin;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.opengl.GL11;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import makamys.lodmod.LODMod;
import makamys.lodmod.ducks.ITessellator;
import makamys.lodmod.ducks.IWorldRenderer;
import makamys.lodmod.renderer.ChunkMesh;
import makamys.lodmod.renderer.FarChunkCache;
import makamys.lodmod.renderer.FarWorldRenderer;
import makamys.lodmod.renderer.LODRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.World;

@Mixin(WorldRenderer.class)
abstract class MixinWorldRenderer implements IWorldRenderer {
    
    @Shadow
    public int posX;
    @Shadow
    public int posY;
    @Shadow
    public int posZ;
    
    @Shadow
    private boolean isInFrustum;
    @Shadow
    public boolean[] skipRenderPass;
    @Shadow
    private int glRenderList;
    
    boolean lastDrawn;
    
    @Shadow
    public boolean needsUpdate;
    
    public List<ChunkMesh> chunkMeshes;
    
    @Redirect(method = "setPosition", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/entity/RenderItem;renderAABB(Lnet/minecraft/util/AxisAlignedBB;)V"))
    private void redirectRenderAABB(AxisAlignedBB p1) {
        if(!FarWorldRenderer.class.isInstance(this.getClass())) {
            RenderItem.renderAABB(p1);
        }
    }
    
    @Redirect(method = "updateRenderer", at = @At(value = "NEW", target = "Lnet/minecraft/world/ChunkCache;"))
    private ChunkCache redirectConstructChunkCache(World p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8) {
        if(!FarWorldRenderer.class.isInstance(this.getClass())) {
            return new ChunkCache(p1, p2, p3, p4, p5, p6, p7, p8);
        } else {
            return new FarChunkCache(p1, p2, p3, p4, p5, p6, p7, p8);
        }
    }
    
    @Inject(method = "updateRenderer", at = @At(value = "HEAD"))
    private void preUpdateRenderer(CallbackInfo ci) {
        if(LODMod.isActive()) {
            if(needsUpdate) {
                if(chunkMeshes != null) {
                    chunkMeshes.clear();
                } else {
                    chunkMeshes = new ArrayList<>();
                }
            } else {
                chunkMeshes = null;
            }
        }
    }
    
    @Inject(method = "updateRenderer", at = @At(value = "TAIL"))
    private void postUpdateRenderer(CallbackInfo ci) {
        if(LODMod.isActive()) {
            if(chunkMeshes != null) {
                LODMod.renderer.onWorldRendererPost(WorldRenderer.class.cast(this));
                chunkMeshes.clear();
            }
        }
    }
    
    @Inject(method = "postRenderBlocks", at = @At(value = "HEAD"))
    private void prePostRenderBlocks(int pass, EntityLivingBase entity, CallbackInfo ci) {
        if(LODMod.isActive() && !LODMod.disableChunkMeshes) {
            if(chunkMeshes != null) {
                chunkMeshes.add(((ITessellator)Tessellator.instance).toChunkMesh(pass, WorldRenderer.class.cast(this)));
            }
        }
    }
    
    @Redirect(method = "postRenderBlocks", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/Tessellator;draw()I"))
    private int redirectPostRenderBlocksDraw() {
        if(!FarWorldRenderer.class.isInstance(this.getClass())) {
            return Tessellator.instance.draw();
        } else {
            Tessellator.instance.reset();
            return 0;
        }
    }
    
    // There's probably a nicer way to do this
    
    @Redirect(method = "postRenderBlocks", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glPopMatrix()V"))
    private void redirectPostRenderBlocksGL1() {
        if(!FarWorldRenderer.class.isInstance(this.getClass())) {
            GL11.glPopMatrix();
        }
    }
    
    @Redirect(method = "postRenderBlocks", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glEndList()V"))
    private void redirectPostRenderBlocksGL2() {
        if(!FarWorldRenderer.class.isInstance(this.getClass())) {
            GL11.glEndList();
        }
    }
    
    // XXX this is inconsistent, Forge callbacks are preserved in postRenderBlocks but not preRenderBlocks
    
    @Inject(method = "preRenderBlocks", at = @At(value = "HEAD"))
    private void preRenderBlocksInjector(CallbackInfo ci) {
        if(FarWorldRenderer.class.isInstance(this.getClass())) {
            Tessellator.instance.setTranslation((double)(-this.posX), (double)(-this.posY), (double)(-this.posZ));
            ci.cancel();
        }
    }
    
    @Inject(method = "setDontDraw", at = @At(value = "HEAD"))
    private void preSetDontDraw(CallbackInfo ci) {
        if(LODMod.isActive()) {
            LODMod.renderer.onWorldRendererChanged(WorldRenderer.class.cast(this), false);
        }
    }
    
    @Override
    public List<ChunkMesh> getChunkMeshes() {
        return chunkMeshes;
    }
    
    public void myTick() {
        boolean drawn = isInFrustum && (!skipRenderPass[0] || !skipRenderPass[1]);
        if(drawn != lastDrawn) {
            LODMod.renderer.onWorldRendererChanged(WorldRenderer.class.cast(this), drawn);
        }
        lastDrawn = drawn;
    }
}