-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathModCore.java
More file actions
376 lines (325 loc) · 13.4 KB
/
ModCore.java
File metadata and controls
376 lines (325 loc) · 13.4 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package cam72cam.mod;
import cam72cam.mod.config.ConfigFile;
import cam72cam.mod.entity.ModdedEntity;
import cam72cam.mod.entity.sync.EntitySync;
import cam72cam.mod.event.ClientEvents;
import cam72cam.mod.gui.GuiRegistry;
import cam72cam.mod.input.Mouse;
import cam72cam.mod.net.Packet;
import cam72cam.mod.net.PacketDirection;
import cam72cam.mod.render.BlockRender;
import cam72cam.mod.render.Light;
import cam72cam.mod.resource.Identifier;
import cam72cam.mod.text.Command;
import cam72cam.mod.util.ModCoreCommand;
import cam72cam.mod.world.ChunkManager;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.FileResourcePack;
import net.minecraft.client.resources.FolderResourcePack;
import net.minecraft.client.resources.IResourcePack;
import net.minecraft.client.resources.SimpleReloadableResourceManager;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartedEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.logging.log4j.Logger;
import org.lwjgl.opengl.GL11;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/** UMC Mod, do not touch... */
@net.minecraftforge.fml.common.Mod(modid = ModCore.MODID, name = ModCore.NAME, version = ModCore.VERSION, acceptedMinecraftVersions = "[1.12,1.13)")
public class ModCore {
public static final String MODID = "universalmodcore";
public static final String NAME = "UniversalModCore";
public static final String VERSION = "1.3.0";
public static ModCore instance;
private List<Mod> mods = new ArrayList<>();
private Logger logger;
/** Register a mod, must happen before UMC is loaded! */
public static void register(Mod ctr) {
instance.mods.add(ctr);
proxy.event(ModEvent.CONSTRUCT, ctr);
}
/** Called during Mod Construction phase */
public ModCore() {
System.out.println("Welcome to UniversalModCore!");
instance = this;
}
/** INIT Phase (Forge) */
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
// LOCK MODS
mods = Collections.unmodifiableList(mods);
logger = event.getModLog();
proxy.event(ModEvent.INITIALIZE);
}
/** SETUP Phase (Forge) */
@EventHandler
public void init(FMLInitializationEvent event) {
proxy.event(ModEvent.SETUP);
}
/** FINALIZE Phase (Forge) */
@EventHandler
public void postInit(FMLPostInitializationEvent event) {
proxy.event(ModEvent.FINALIZE);
for (Mod mod : mods) {
File modDir = cacheFile(new Identifier(mod.modID(), "foo")).getParentFile();
if (modDir.exists() && modDir.isDirectory()) {
for (File file : modDir.listFiles()) {
if (!usedCacheFiles.contains(file)) {
ModCore.warn("Removing file cache entry: %s", file);
FileUtils.deleteQuietly(file);
}
}
}
}
}
/** START Phase (Forge) */
@EventHandler
public void serverStarting(FMLServerStartedEvent event) {
proxy.event(ModEvent.START);
}
/** Implement this to create a UMC mod */
public static abstract class Mod {
public abstract String modID();
/** Called both server and client side with a given event */
public abstract void commonEvent(ModEvent event);
/** Called client side with a given event */
public abstract void clientEvent(ModEvent event);
/** Called server side with a given event */
public abstract void serverEvent(ModEvent event);
/** Get config file for filename */
public final Path getConfig(String fname) {
return Paths.get(Loader.instance().getConfigDir().toString(), fname);
}
/* Standard logging functions */
public static void debug(String msg, Object...params) {
ModCore.debug(msg, params);
}
public static void info(String msg, Object...params) {
ModCore.info(msg, params);
}
public static void warn(String msg, Object...params) {
ModCore.warn(msg, params);
}
public static void error(String msg, Object...params) {
ModCore.error(msg, params);
}
public static void catching(Throwable ex) {
ModCore.catching(ex);
}
}
/** Returns -1 if server side */
public int getGPUTextureSize() {
return proxy.getGPUTextureSize();
}
@SidedProxy(serverSide = "cam72cam.mod.ModCore$ServerProxy", clientSide = "cam72cam.mod.ModCore$ClientProxy", modId = ModCore.MODID)
private static Proxy proxy;
/** Hooked into forge's proxy system and fires off corresponding events */
public static class Proxy {
public Proxy() {
proxy = this;
ModCore.register(new Internal());
}
public void event(ModEvent event) {
instance.mods.forEach(m -> event(event, m));
}
public void event(ModEvent event, Mod m) {
m.commonEvent(event);
}
public int getGPUTextureSize() {
return -1;
}
}
public static class ClientProxy extends Proxy {
public void event(ModEvent event, Mod m) {
if (event == ModEvent.CONSTRUCT) {
Config.getMaxTextureSize(); //populate
List<IResourcePack> packs = Minecraft.getMinecraft().defaultResourcePacks;
String configDir = Loader.instance().getConfigDir().toString();
new File(configDir).mkdirs();
File folder = new File(configDir + File.separator + m.modID());
if (folder.exists()) {
if (folder.isDirectory()) {
File[] files = folder.listFiles((dir, name) -> name.endsWith(".zip"));
for (File file : files) {
packs.add(createPack(file));
}
File[] folders = folder.listFiles((dir, name) -> dir.isDirectory());
for (File dir : folders) {
packs.add(createPack(dir));
}
}
} else {
folder.mkdirs();
}
IResourcePack modPack = createPack(Loader.instance().activeModContainer().getSource());
// Force first and last (and inject mod time) BUG: sounds can still be overridden by resource packs
packs.add(1, modPack);
packs.add(modPack);
}
super.event(event, m);
m.clientEvent(event);
}
@SideOnly(Side.CLIENT)
private static IResourcePack createPack(File path) {
if (path.isDirectory()) {
return new FolderResourcePack(path) {
@Override
protected InputStream getInputStreamByName(String name) throws IOException {
InputStream stream = super.getInputStreamByName(name);
File file = this.getFile(name);
return new Identifier.InputStreamMod(stream, file.lastModified());
}
};
} else {
return new FileResourcePack(path) {
@Override
protected InputStream getInputStreamByName(String name) throws IOException {
return new Identifier.InputStreamMod(super.getInputStreamByName(name), resourcePackFile.lastModified());
}
};
}
}
@Override
public int getGPUTextureSize() {
return Math.min(GL11.glGetInteger(GL11.GL_MAX_TEXTURE_SIZE), 8196);
}
}
public static class ServerProxy extends Proxy {
public void event(ModEvent event, Mod m) {
super.event(event, m);
m.serverEvent(event);
}
}
public static class Internal extends Mod {
public int skipN = 1;
@Override
public String modID() {
return "universalmodcoreinternal";
}
@Override
public void commonEvent(ModEvent event) {
switch (event) {
case CONSTRUCT:
Packet.register(EntitySync.EntitySyncPacket::new, PacketDirection.ServerToClient);
Packet.register(ModdedEntity.PassengerPositionsPacket::new, PacketDirection.ServerToClient);
Packet.register(ModdedEntity.PassengerSeatPacket::new, PacketDirection.ServerToClient);
Packet.register(Mouse.MousePressPacket::new, PacketDirection.ClientToServer);
Command.register(new ModCoreCommand());
Light.register();
ConfigFile.sync(Config.class);
break;
case INITIALIZE:
ChunkManager.setup();
break;
case SETUP:
World.MAX_ENTITY_RADIUS = Math.max(World.MAX_ENTITY_RADIUS, 32);
GuiRegistry.registration();
break;
case FINALIZE:
break;
case START:
Command.registration();
break;
}
}
@Override
public void clientEvent(ModEvent event) {
switch (event) {
case SETUP:
((SimpleReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(resourceManager -> {
if (skipN > 0) {
skipN--;
return;
}
ModCore.instance.mods.forEach(mod -> mod.clientEvent(ModEvent.RELOAD));
ClientEvents.fireReload();
});
BlockRender.onPostColorSetup();
ClientEvents.fireReload();
break;
}
}
@Override
public void serverEvent(ModEvent event) {
}
}
public static void debug(String msg, Object... params) {
if (Config.DebugLogging) {
if (instance == null || instance.logger == null) {
System.out.println("DEBUG: " + String.format(msg, params));
return;
}
instance.logger.info(String.format(msg, params));
}
}
public static void info(String msg, Object... params) {
if (instance == null || instance.logger == null) {
System.out.println("INFO: " + String.format(msg, params));
return;
}
instance.logger.info(String.format(msg, params));
}
public static void warn(String msg, Object... params) {
if (instance == null || instance.logger == null) {
System.out.println("WARN: " + String.format(msg, params));
return;
}
instance.logger.warn(String.format(msg, params));
}
public static void error(String msg, Object... params) {
if (instance == null || instance.logger == null) {
System.out.println("ERROR: " + String.format(msg, params));
return;
}
instance.logger.error(String.format(msg, params));
}
public static void catching(Throwable ex, String msg, Object... params) {
error(msg, params);
catching(ex);
}
public static void catching(Throwable ex) {
if (instance == null || instance.logger == null) {
ex.printStackTrace();
return;
}
instance.logger.error("Exception", ex);
}
private static final List<File> usedCacheFiles = new ArrayList<>();
/** Get a file for name in the UMC cache dir */
public static synchronized File cacheFile(Identifier id) {
File configDir = Loader.instance().getConfigDir();
if (configDir == null) {
configDir = new File(System.getProperty("java.io.tmpdir"), "minecraft");
}
File cacheDir = Paths.get(configDir.getParentFile().getPath(), "cache", id.getDomain()).toFile();
cacheDir.mkdirs();
// https://stackoverflow.com/questions/1155107/is-there-a-cross-platform-java-method-to-remove-filename-special-chars#comment96425990_17745189
String path = id.getPath().replaceAll("(?U)[^\\w\\._]+", ".");
if (SystemUtils.IS_OS_WINDOWS) {
// In a world with linux, who needs windows or gates?
path = StringUtils.right(path, 250 - cacheDir.getAbsolutePath().length()); // Windows default max *Path* len is 256
} else {
path = StringUtils.right(path, 250); // Most FS's allow up to 255
}
File f = new File(cacheDir, path);
usedCacheFiles.add(f);
return f;
}
}