aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/neodymium/Neodymium.java
blob: 1fa71e2af1d83dcdc4bc72cf4e260f118f492616 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package makamys.neodymium;

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

import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLConstructionEvent;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerAboutToStartEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import makamys.mclib.core.MCLib;
import makamys.mclib.core.MCLibModules;
import makamys.neodymium.config.Config;
import makamys.neodymium.renderer.NeoRenderer;
import makamys.neodymium.util.ChatUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import net.minecraftforge.client.event.EntityViewRenderEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;

@Mod(modid = Neodymium.MODID, version = Neodymium.VERSION, guiFactory = "makamys.neodymium.config.GuiFactory")
public class Neodymium
{
    public static final String MODID = "neodymium";
    public static final String VERSION = "@VERSION@";
    
    public static final Logger LOGGER = LogManager.getLogger(MODID);
    
    private static final Config.ReloadInfo CONFIG_RELOAD_INFO = new Config.ReloadInfo();

    private boolean renderDebugText = false;
    
    public static NeoRenderer renderer;
    
    private static World rendererWorld;
    
    @EventHandler
    public void preInit(FMLConstructionEvent event) {
        MCLib.init();
        Compat.applyCompatibilityTweaks();
    }
    
    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        MCLibModules.updateCheckAPI.submitModTask(MODID, "@UPDATE_URL@");
        
        if(VERSION.equals("@VERSION@")) {
            // Allow using config GUI in dev env
            event.getModMetadata().autogenerated = false;
        }
    }
    
    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        FMLCommonHandler.instance().bus().register(this);
        MinecraftForge.EVENT_BUS.register(this);
    }
    
    @SubscribeEvent
    public void onConfigChanged(ConfigChangedEvent event) {
        if(event.modID.equals(MODID)) {
            Config.flush();
        }
    }
    
    @EventHandler
    public void onServerAboutToStart(FMLServerAboutToStartEvent event)
    {
        Config.reloadConfig();
        ChatUtil.resetShownChatMessages();
    }
    
    private void onPlayerWorldChanged(World newWorld) {
    	if(getRendererWorld() == null && newWorld != null) {
    		Config.reloadConfig();
    	}
    	if(renderer != null) {
            destroyRenderer();
        }
    	if(Config.enabled && newWorld != null) {
    	    Pair<List<String>, List<String>> warnsAndCriticalWarns = checkCompat();
    	    List<String> warns = warnsAndCriticalWarns.getLeft();
    	    List<String> criticalWarns = warnsAndCriticalWarns.getRight();
    	    
    	    if(criticalWarns.isEmpty() || Config.ignoreIncompatibilities) {
    	        renderer = new NeoRenderer(newWorld);
    	        renderer.hasIncompatibilities = !warns.isEmpty() || !criticalWarns.isEmpty();
    	    }
        }
    	rendererWorld = newWorld;
    }
    
    @SubscribeEvent
    @SideOnly(Side.CLIENT)
    public void onWorldUnload(WorldEvent.Unload event) {
        if(!Config.enabled) return;
        
        if(event.world == getRendererWorld()) {
        	onPlayerWorldChanged(null);
        }
    }
    
    public static boolean isActive() {
        return renderer != null && renderer.hasInited && !renderer.destroyPending;
    }
    
    private World getRendererWorld() {
    	return rendererWorld;
    }
    
    @SubscribeEvent
    public void onClientTick(TickEvent.ClientTickEvent event) {
        if(!Config.enabled) return;
        
    	if(event.phase == TickEvent.Phase.START && isActive()) {
    	    if(Config.hotswap) {
    	        if(Config.reloadIfChanged(CONFIG_RELOAD_INFO)) {
    	            if(CONFIG_RELOAD_INFO.needReload) {
    	                Minecraft.getMinecraft().renderGlobal.loadRenderers();
    	            } else if(renderer != null) {
    	                renderer.reloadShader();
    	            }
    	        }
    	    }
    	}
    	
    	if(event.phase == TickEvent.Phase.START) {
    	    if(Compat.hasChanged()) {
    	        Pair<List<String>, List<String>> warns = checkCompat();
    	        if(renderer != null) {
    	            renderer.hasIncompatibilities = !warns.getLeft().isEmpty() || !warns.getRight().isEmpty();
    	        }
    	    }
    	}
    }
    
    @SubscribeEvent
    public void onRenderTick(TickEvent.RenderTickEvent event) {
        if(!Config.enabled) return;
        
        if(event.phase == TickEvent.Phase.START) {
            EntityPlayer player = Minecraft.getMinecraft().thePlayer;
            World world = player != null ? player.worldObj : null;
            if(world != getRendererWorld()) {
                onPlayerWorldChanged(world);
            }
            
            if(isActive()) {
                renderer.forceRenderFog = true;
            }
        } else if(event.phase == TickEvent.Phase.END) {
            if(isActive()) {
                renderer.onRenderTickEnd();
            }
        }
    }
    
    @SubscribeEvent
    public void onRenderOverlay(RenderGameOverlayEvent.Pre event) {
        if (Config.showDebugInfo && isActive()) {
            if (event.type.equals(RenderGameOverlayEvent.ElementType.DEBUG)) {
                renderDebugText = true;
            } else if (renderDebugText && (event instanceof RenderGameOverlayEvent.Text) && event.type.equals(RenderGameOverlayEvent.ElementType.TEXT)) {
                renderDebugText = false;
                RenderGameOverlayEvent.Text text = (RenderGameOverlayEvent.Text) event;
                text.right.add(null);
                text.right.addAll(renderer.getDebugText());
            }
        }
    }
    
    @SubscribeEvent
    public void onRenderFog(EntityViewRenderEvent.RenderFogEvent event) {
        if(isActive()) {
            renderer.forceRenderFog = false;
        }
    }

    public static boolean shouldRenderVanillaWorld() {
        return !isActive() || (isActive() && renderer.renderWorld && !renderer.rendererActive);
    }

    public static String modifySplash(String splash) {
        if(splash.equals("OpenGL 1.2!")) {
            return "OpenGL 3.3 (if supported)!";
        } else {
            return splash;
        }
    }

    public static void destroyRenderer() {
        if(renderer != null) {
            renderer.destroy();
            renderer = null;
        }
        rendererWorld = null;
    }
    
    private static Pair<List<String>, List<String>> checkCompat() {
        List<String> warns = new ArrayList<>();
        List<String> criticalWarns = new ArrayList<>();
        
        Compat.getCompatibilityWarnings(warns, criticalWarns);
        
        if(!criticalWarns.isEmpty()) {
            criticalWarns.add("Neodymium has been disabled due to a critical incompatibility.");
        }
        
        if(!Config.ignoreIncompatibilities) {
            for(String warn : warns) {
                ChatUtil.showNeoChatMessage(warn, ChatUtil.MessageVerbosity.WARNING, true);
            }
            for(String fatalWarn : criticalWarns) {
                ChatUtil.showNeoChatMessage(fatalWarn, ChatUtil.MessageVerbosity.ERROR, true);
            }
        }
        
        for(String warn : warns) {
            LOGGER.warn(warn);
        }
        for(String criticalWarn : criticalWarns) {
            LOGGER.warn("Critical: " + criticalWarn);
        }
        
        return Pair.of(warns, criticalWarns);
    }

}