Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.eterniamc.extramixinstuffs;
- import com.google.gson.GsonBuilder;
- import com.google.gson.stream.JsonReader;
- import net.minecraft.launchwrapper.LaunchClassLoader;
- import net.minecraftforge.fml.common.FMLLog;
- import net.minecraftforge.fml.relauncher.CoreModManager;
- import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
- import org.apache.commons.io.FileUtils;
- import org.spongepowered.asm.launch.MixinBootstrap;
- import org.spongepowered.asm.mixin.MixinEnvironment;
- import org.spongepowered.asm.mixin.Mixins;
- import javax.annotation.Nullable;
- import java.io.*;
- import java.util.Collection;
- import java.util.Map;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- @IFMLLoadingPlugin.MCVersion("1.12.2")
- @IFMLLoadingPlugin.DependsOn("pixelmon")
- public class MixinsCoreMod implements IFMLLoadingPlugin {
- static File modFile;
- public MixinsCoreMod() {
- //TODO nasty hack to get pixelmon up at the same time as the mixins
- try {
- findAndLoadPixelmon();
- } catch (Exception e) {
- e.printStackTrace();
- }
- MixinBootstrap.init();
- //TODO debug options
- MixinEnvironment.getDefaultEnvironment().setOption(MixinEnvironment.Option.DEBUG_EXPORT_DECOMPILE, true);
- MixinEnvironment.getDefaultEnvironment().setOption(MixinEnvironment.Option.DEBUG_EXPORT, true);
- Mixins.addConfiguration("mixins.mymixins.json");
- try {
- File file = new File("./config/mixins.json");
- if (!file.exists()) {
- ExtraMixinStuffs.config = new Config();
- file.createNewFile();
- PrintWriter pw = new PrintWriter(file);
- String json = new GsonBuilder().setPrettyPrinting().create().toJson(ExtraMixinStuffs.config);
- pw.print(json);
- pw.flush();
- pw.close();
- } else
- ExtraMixinStuffs.config = new GsonBuilder().setPrettyPrinting().create().fromJson(new JsonReader(new FileReader(file)), Config.class);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void loadModJar(File jar) throws Exception{
- ((LaunchClassLoader) this.getClass().getClassLoader()).addURL(jar.toURI().toURL());
- CoreModManager.getReparseableCoremods().add(jar.getName());
- }
- @Override
- public String[] getASMTransformerClass() {
- return new String[] {};
- }
- @Override
- public String getModContainerClass() {
- return null;
- }
- @Nullable
- @Override
- public String getSetupClass() {
- return null;
- }
- @Override
- public void injectData(Map<String, Object> data) {
- modFile = (File) data.get("coremodLocation");
- if (modFile == null) {
- modFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
- }
- }
- @Override
- public String getAccessTransformerClass() {
- return null;
- }
- /**
- * NOTE: this can be used for any other mod as well.
- *
- * @author happyzleaf (finding the jar)
- * @author clienthax (loading the jar)
- */
- public void findAndLoadPixelmon() {
- try {
- FMLLog.log.info("Trying to load pixelmon...");
- File modsFolder = new File(System.getProperty("user.dir"), "mods");
- if (!modsFolder.exists()) {
- FMLLog.log.error("The mods folder couldn't be found.\nFolder: ", modsFolder.toString());
- }
- //Scanning through /mods/ searching for jars
- Collection<File> jars = FileUtils.listFiles(modsFolder, new String[]{"jar"}, false);
- File pixelmon = null;
- for (File jar : jars) {
- ZipInputStream zip = new ZipInputStream(new FileInputStream(jar));
- ZipEntry entry;
- while ((entry = zip.getNextEntry()) != null) {
- zip.closeEntry();
- //Checking if the jar is the pixelmon
- if (entry.getName().equals("com/pixelmonmod/pixelmon/Pixelmon.class")) {
- pixelmon = jar;
- break;
- }
- }
- zip.close();
- if (pixelmon != null) break;
- }
- //Opsie dopsie, no pixelmon jars found :(
- if (pixelmon == null) {
- FMLLog.log.error("Pixelmon's jar cannot be found, the program will not continue.");
- return;
- }
- //Once we know the file that contains the pixelmon, it's better to check if another coremod already loaded it
- if (!CoreModManager.getReparseableCoremods().contains(pixelmon.getName())) {
- //Now we're safe to load the jar (thanks to clienthax)
- ((LaunchClassLoader) this.getClass().getClassLoader()).addURL(pixelmon.toURI().toURL());
- CoreModManager.getReparseableCoremods().add(pixelmon.getName());
- }
- FMLLog.log.info("Loaded!");
- } catch (Exception e) {
- FMLLog.log.error("There was a problem trying to find Pixelmon's jar, the program will not continue.", e);
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement