aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/neodymium/mixin/MixinWorldRenderer.java
blob: 32b27a961fc36c558322a01b0699020030b8f500 (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
package makamys.neodymium.mixin;

import java.util.Collections;
import java.util.List;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.google.common.collect.Lists;

import makamys.neodymium.Neodymium;
import makamys.neodymium.ducks.IWorldRenderer;
import makamys.neodymium.renderer.ChunkMesh;
import makamys.neodymium.renderer.NeoRenderer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.entity.EntityLivingBase;

/** Inserts hooks in WorldRenderer to listen for changes, and to grab the tessellator data right before rendering. */
@Mixin(WorldRenderer.class)
abstract class MixinWorldRenderer implements IWorldRenderer {
    
    @Shadow
    private boolean isInFrustum;
    @Shadow
    public boolean[] skipRenderPass;
    
    @Shadow
    public boolean needsUpdate;
    
    private boolean nd$savedDrawnStatus;
    
    private List<ChunkMesh> nd$chunkMeshes;
    
    @Inject(method = {"updateRenderer"}, at = @At(value = "HEAD"))
    private void preUpdateRenderer(CallbackInfo ci) {
        preUpdateRenderer(false);
    }
    
    @Inject(method = {"updateRendererSort"}, at = @At(value = "HEAD"))
    private void preUpdateRendererSort(CallbackInfo ci) {
        preUpdateRenderer(true);
    }
    
    @Unique
    private void preUpdateRenderer(boolean sort) {
        saveDrawnStatus();
        
        if(Neodymium.isActive()) {
            if(nd$chunkMeshes != null) {
                Collections.fill(nd$chunkMeshes, null);
            } else {
                nd$chunkMeshes = Lists.newArrayList(null, null);
            }
        }
    }
    
    @Inject(method = {"updateRenderer"}, at = @At(value = "RETURN"))
    private void postUpdateRenderer(CallbackInfo ci) {
        postUpdateRenderer(false);
    }
    
    @Inject(method = {"updateRendererSort"}, at = @At(value = "RETURN"))
    private void postUpdateRendererSort(CallbackInfo ci) {
        postUpdateRenderer(true);
    }
    
    @Unique
    private void postUpdateRenderer(boolean sort) {
        notifyIfDrawnStatusChanged();
        
        if(Neodymium.isActive()) {
            if(nd$chunkMeshes != null) {
                Neodymium.renderer.onWorldRendererPost(WorldRenderer.class.cast(this), sort);
                Collections.fill(nd$chunkMeshes, null);
            }
        }
    }
    
    @Inject(method = "postRenderBlocks", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/Tessellator;draw()I"))
    private void prePostRenderBlocks(int pass, EntityLivingBase entity, CallbackInfo ci) {
        if(Neodymium.isActive()) {
            if(nd$chunkMeshes != null) {
                nd$chunkMeshes.set(pass, ChunkMesh.fromTessellator(pass, WorldRenderer.class.cast(this), Tessellator.instance));
            }
        }
    }
    
    @Inject(method = "setDontDraw", at = @At(value = "HEAD"))
    private void preSetDontDraw(CallbackInfo ci) {
        if(Neodymium.isActive()) {
            Neodymium.renderer.onWorldRendererChanged(WorldRenderer.class.cast(this), NeoRenderer.WorldRendererChange.DELETED);
        }
    }
    
    @Override
    public List<ChunkMesh> getChunkMeshes() {
        return nd$chunkMeshes;
    }
    
    @Inject(method = "updateInFrustum", at = @At(value = "HEAD"))
    private void preUpdateInFrustum(CallbackInfo ci) {
        saveDrawnStatus();
    }
    
    @Inject(method = "updateInFrustum", at = @At(value = "RETURN"))
    private void postUpdateInFrustum(CallbackInfo ci) {
        notifyIfDrawnStatusChanged();
    }
    
    @Unique
    private void saveDrawnStatus() {
        nd$savedDrawnStatus = isDrawn();
    }
    
    @Unique
    private void notifyIfDrawnStatusChanged() {
        boolean drawn = isDrawn();
        if(Neodymium.isActive() && drawn != nd$savedDrawnStatus) {
            Neodymium.renderer.onWorldRendererChanged(WorldRenderer.class.cast(this), drawn ? NeoRenderer.WorldRendererChange.VISIBLE : NeoRenderer.WorldRendererChange.INVISIBLE);
        }
    }
    
    @Override
    public boolean isDrawn() {
        return isInFrustum && (!skipRenderPass[0] || !skipRenderPass[1]);
    }
}