diff --git a/1.7.10/src/main/java/net/teamfruit/serverobserver/CommonProxy.java b/1.7.10/src/main/java/net/teamfruit/serverobserver/CommonProxy.java index 1850072..1784f0b 100644 --- a/1.7.10/src/main/java/net/teamfruit/serverobserver/CommonProxy.java +++ b/1.7.10/src/main/java/net/teamfruit/serverobserver/CommonProxy.java @@ -4,7 +4,7 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; -public class CommonProxy implements IProxy { +public class CommonProxy implements IProxyL { @Override public void preInit(final FMLPreInitializationEvent event) { } diff --git a/1.7.10/src/main/java/net/teamfruit/serverobserver/IProxy.java b/1.7.10/src/main/java/net/teamfruit/serverobserver/IProxyL.java similarity index 94% rename from 1.7.10/src/main/java/net/teamfruit/serverobserver/IProxy.java rename to 1.7.10/src/main/java/net/teamfruit/serverobserver/IProxyL.java index fdbbf60..921e07a 100644 --- a/1.7.10/src/main/java/net/teamfruit/serverobserver/IProxy.java +++ b/1.7.10/src/main/java/net/teamfruit/serverobserver/IProxyL.java @@ -6,7 +6,7 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; -public interface IProxy { +public interface IProxyL { void preInit(@Nonnull FMLPreInitializationEvent event); diff --git a/1.7.10/src/main/java/net/teamfruit/serverobserver/ServerObserver.java b/1.7.10/src/main/java/net/teamfruit/serverobserver/ServerObserver.java index f558c0d..7b03719 100644 --- a/1.7.10/src/main/java/net/teamfruit/serverobserver/ServerObserver.java +++ b/1.7.10/src/main/java/net/teamfruit/serverobserver/ServerObserver.java @@ -17,7 +17,7 @@ @Mod(modid = Reference.MODID, name = Reference.NAME, version = Reference.VERSION, guiFactory = Reference.GUI_FACTORY) public class ServerObserver { @SidedProxy(serverSide = Reference.PROXY_SERVER, clientSide = Reference.PROXY_CLIENT) - private static @Nullable IProxy proxy; + private static @Nullable IProxyL proxy; @NetworkCheckHandler public boolean checkModList(final @Nonnull Map versions, final @Nonnull Side side) { diff --git a/common.gradle b/common.gradle index d97c0ef..4bf119d 100644 --- a/common.gradle +++ b/common.gradle @@ -34,6 +34,21 @@ sourceSets { apply plugin: project.forgegradle_plugin def fg_plugin = plugins.findPlugin project.forgegradle_plugin +sourceSets { + [[common: common_api, src: api], [common: common_main, src: main], [common: common_test, src: test]].each { s -> + s.common.java { + srcDirs = ["../src/${s.src.name}/java"] + } + s.common.resources { + srcDirs = ["../src/${s.src.name}/resources"] + } + s.common.compileClasspath += s.src.compileClasspath + s.common.runtimeClasspath += s.src.runtimeClasspath + s.src.compileClasspath += s.common.output + s.src.runtimeClasspath += s.common.output + } +} + // Fix compatibility issue between Gradle 4 and ForgeGradle 2.1 if (project.forgegradle.contains('1.2') || project.forgegradle.contains('2.1')) if (gradle.startParameter.logLevel.compareTo(org.gradle.api.logging.LogLevel.LIFECYCLE) >= 0) @@ -93,22 +108,22 @@ task setupSourceSets { processResources { // this will ensure that this task is redone when the versions change. inputs.property 'version', mod_version - inputs.property 'mcversion', project.minecraft.version + inputs.property 'mcversion', project.version_minecraft - from(sourceSets.main.resources.srcDirs) { + from(sourceSets.common_main.resources.srcDirs) { include 'mcmod.info' - expand([ + /*expand([ 'modid' : project.modid, 'modname' : project.modname, 'version' : mod_version, 'mcversion' : project.version_minecraft, 'forgeversion' : project.version_forge, 'minforgeversion': project.isProperty('version_minforge') ? project.version_minforge : project.version_forge, - ]) + ])*/ } - from(sourceSets.main.resources.srcDirs) { + from(sourceSets.common_main.resources.srcDirs) { exclude 'mcmod.info' } } @@ -119,6 +134,10 @@ repositories { name = 'CurseForge' url = 'https://minecraft.curseforge.com/api/maven/' } + maven { + name = 'Bintray TeamFruit' + url = "https://dl.bintray.com/team-fruit/mods/" + } } dependencies { @@ -126,21 +145,9 @@ dependencies { shade fileTree(dir: 'libs/shade', include: '*.jar') compile fileTree(dir: '../libs', include: '*.jar') shade fileTree(dir: '../libs/shade', include: '*.jar') -} -sourceSets { - [[common: common_api, src: api], [common: common_main, src: main], [common: common_test, src: test]].each { s -> - s.common.java { - srcDirs = ["../src/${s.src.name}/java"] - } - s.common.resources { - srcDirs = ["../src/${s.src.name}/resources"] - } - s.common.compileClasspath += s.src.compileClasspath - s.common.runtimeClasspath += s.src.runtimeClasspath - s.src.compileClasspath += s.common.output - s.src.runtimeClasspath += s.common.output - } + if (project.name==mod_commonname) + compile 'net.teamfruit:fmllegacydependency:1.0.2' } ext.commonManifest = { @@ -173,6 +180,7 @@ jar { classifier 'diff' manifest commonManifest } +tasks.jar.dependsOn 'shadowJar' // println !reobf.hasProperty('dependsOn') if (!reobf.hasProperty('dependsOn')) { diff --git a/common/src/main/java/net/teamfruit/serverobserver/IProxyL.java b/common/src/main/java/net/teamfruit/serverobserver/IProxyL.java new file mode 100644 index 0000000..921e07a --- /dev/null +++ b/common/src/main/java/net/teamfruit/serverobserver/IProxyL.java @@ -0,0 +1,17 @@ +package net.teamfruit.serverobserver; + +import javax.annotation.Nonnull; + +import cpw.mods.fml.common.event.FMLInitializationEvent; +import cpw.mods.fml.common.event.FMLPostInitializationEvent; +import cpw.mods.fml.common.event.FMLPreInitializationEvent; + +public interface IProxyL { + + void preInit(@Nonnull FMLPreInitializationEvent event); + + void init(@Nonnull FMLInitializationEvent event); + + void postInit(@Nonnull FMLPostInitializationEvent event); + +} \ No newline at end of file diff --git a/common/src/main/java/net/teamfruit/serverobserver/ServerObserver.java b/common/src/main/java/net/teamfruit/serverobserver/ServerObserver.java index 7b5c447..3f47829 100644 --- a/common/src/main/java/net/teamfruit/serverobserver/ServerObserver.java +++ b/common/src/main/java/net/teamfruit/serverobserver/ServerObserver.java @@ -1,34 +1,18 @@ package net.teamfruit.serverobserver; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; import java.util.Map; -import java.util.Map.Entry; -import java.util.zip.ZipFile; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; - -import com.google.common.collect.Maps; - -import net.minecraft.launchwrapper.Launch; -import net.minecraftforge.fml.common.Loader; -import net.minecraftforge.fml.common.LoaderException; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.Mod.Instance; -import net.minecraftforge.fml.common.ModContainer; 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.network.NetworkCheckHandler; -import net.minecraftforge.fml.relauncher.FMLInjectionData; import net.minecraftforge.fml.relauncher.Side; @Mod(modid = Reference.MODID, name = Reference.NAME, version = Reference.VERSION, guiFactory = Reference.GUI_FACTORY) @@ -37,64 +21,12 @@ public class ServerObserver { public static @Nullable ServerObserver instance; static { - final ModContainer container = Loader.instance().activeModContainer(); - Log.log.info(container); - final Object[] data = FMLInjectionData.data(); - String mccversion = (String) data[4]; - - final Map versions = Maps.newHashMap(); - versions.put("1.8", "1.8.9"); - versions.put("1.9", "1.9.4"); - versions.put("1.10", "1.10.2"); - versions.put("1.11", "1.11.2"); - versions.put("1.12", "1.12.2"); - for (final Entry entry : versions.entrySet()) { - final String key = entry.getKey(); - if (StringUtils.startsWith(mccversion, key)) { - final String value = entry.getValue(); - mccversion = value; - break; - } - } - - final File minecraftDir = (File) data[6]; - final File modsDir = new File(minecraftDir, "mods"); - if (container!=null) { - final File modFile = container.getSource(); - if (modFile!=null) { - Log.log.info(container.getSource()); - ZipFile file = null; - InputStream stream = null; - try { - final File canonicalModsDir = modsDir.getCanonicalFile(); - final File versionSpecificModsDir = new File(canonicalModsDir, mccversion); - final File modVersionSpecific = new File(versionSpecificModsDir, Reference.MODID); - - final String jarname = String.format("%s.jar", mccversion); - final File destMod = new File(modVersionSpecific, jarname); - - file = new ZipFile(modFile); - stream = file.getInputStream(file.getEntry(jarname)); - - FileUtils.copyInputStreamToFile(stream, destMod); - Launch.classLoader.addURL(destMod.toURI().toURL()); - } catch (final IOException e) { - new LoaderException("Could not load version-specific file.", e); - } finally { - IOUtils.closeQuietly(file); - IOUtils.closeQuietly(stream); - } - - } - } - Log.log.info("init"); + UniversalVersioner.loadVersion(); } @SidedProxy(serverSide = Reference.PROXY_SERVER, clientSide = Reference.PROXY_CLIENT) public static @Nullable IProxy proxy; - // public static final @Nonnull ICompat compat = new Compat(); - @NetworkCheckHandler public boolean checkModList(final @Nonnull Map versions, final @Nonnull Side side) { return true; diff --git a/common/src/main/java/net/teamfruit/serverobserver/ServerObserverL.java b/common/src/main/java/net/teamfruit/serverobserver/ServerObserverL.java new file mode 100644 index 0000000..bce5948 --- /dev/null +++ b/common/src/main/java/net/teamfruit/serverobserver/ServerObserverL.java @@ -0,0 +1,52 @@ +package net.teamfruit.serverobserver; + +import java.util.Map; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import cpw.mods.fml.common.Mod; +import cpw.mods.fml.common.Mod.EventHandler; +import cpw.mods.fml.common.Mod.Instance; +import cpw.mods.fml.common.SidedProxy; +import cpw.mods.fml.common.event.FMLInitializationEvent; +import cpw.mods.fml.common.event.FMLPostInitializationEvent; +import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.network.NetworkCheckHandler; +import cpw.mods.fml.relauncher.Side; + +@Mod(modid = Reference.MODID, name = Reference.NAME, version = Reference.VERSION, guiFactory = Reference.GUI_FACTORY) +public class ServerObserverL { + @Instance(Reference.MODID) + public static @Nullable ServerObserverL instance; + + static { + UniversalVersioner.loadVersion(); + } + + @SidedProxy(serverSide = Reference.PROXY_SERVER, clientSide = Reference.PROXY_CLIENT) + public static @Nullable IProxyL proxy; + + @NetworkCheckHandler + public boolean checkModList(final @Nonnull Map versions, final @Nonnull Side side) { + return true; + } + + @EventHandler + public void preInit(final @Nonnull FMLPreInitializationEvent event) { + if (proxy!=null) + proxy.preInit(event); + } + + @EventHandler + public void init(final @Nonnull FMLInitializationEvent event) { + if (proxy!=null) + proxy.init(event); + } + + @EventHandler + public void postInit(final @Nonnull FMLPostInitializationEvent event) { + if (proxy!=null) + proxy.postInit(event); + } +} diff --git a/common/src/main/java/net/teamfruit/serverobserver/UniversalVersioner.java b/common/src/main/java/net/teamfruit/serverobserver/UniversalVersioner.java new file mode 100644 index 0000000..a93fdf2 --- /dev/null +++ b/common/src/main/java/net/teamfruit/serverobserver/UniversalVersioner.java @@ -0,0 +1,109 @@ +package net.teamfruit.serverobserver; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map.Entry; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import com.google.common.collect.ImmutableMap; + +import net.minecraft.launchwrapper.Launch; + +public class UniversalVersioner { + public static final ImmutableMap versions = ImmutableMap. builder() + .put("1.7", "1.7.10") + .put("1.8", "1.8.9") + .put("1.9", "1.9.4") + .put("1.10", "1.10.2") + .put("1.11", "1.11.2") + .put("1.12", "1.12.2") + .build(); + + public static String getVersion(final String mccversion) { + for (final Entry entry : UniversalVersioner.versions.entrySet()) { + final String key = entry.getKey(); + if (StringUtils.startsWith(mccversion, key)) + return entry.getValue(); + } + return null; + } + + private static T invoke(final String afterFmlPath, final String name, final Class[] types, final Object instance, final Object[] params) { + Class $class; + try { + $class = Class.forName("net.minecraftforge.fml."+afterFmlPath); + } catch (final ClassNotFoundException e1) { + try { + $class = Class.forName("cpw.mods.fml."+afterFmlPath); + } catch (final ClassNotFoundException e2) { + throw new RuntimeException("Could not load fml class"); + } + } + if ($class==null) + throw new RuntimeException("Could not find fml class"); + Method $method; + try { + $method = $class.getMethod(name, types); + } catch (final Exception e) { + throw new RuntimeException("Could not find or access fml method"); + } + try { + @SuppressWarnings("unchecked") + final T res = (T) $method.invoke(instance, params); + return res; + } catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException e) { + throw new RuntimeException("Could not invoke fml method"); + } + } + + public static void loadVersion() { + final Object loader = invoke("common.Loader", "instance", new Class[0], null, new Object[0]); + final Object container = invoke("common.Loader", "activeModContainer", new Class[0], loader, new Object[0]); + final Object[] data = invoke("relauncher.FMLInjectionData", "data", new Class[0], null, new Object[0]); + final String mccversion0 = (String) data[4]; + final String mccversion = getVersion(mccversion0); + + if (mccversion==null) + throw new RuntimeException(String.format("Version %s is not supported! Supported version is %s.", mccversion0, versions)); + + final File minecraftDir = (File) data[6]; + final File modsDir = new File(minecraftDir, "mods"); + if (container!=null) { + final File modFile = invoke("common.ModContainer", "getSource", new Class[0], container, new Object[0]); + if (modFile!=null) { + ZipFile file = null; + InputStream stream = null; + try { + final File canonicalModsDir = modsDir.getCanonicalFile(); + final File versionSpecificModsDir = new File(canonicalModsDir, mccversion); + final File modVersionSpecific = new File(versionSpecificModsDir, Reference.MODID); + + final String jarname = String.format("%s.jar", mccversion); + final File destMod = new File(modVersionSpecific, jarname); + + file = new ZipFile(modFile); + final ZipEntry entry = file.getEntry(jarname); + if (entry==null) + throw new RuntimeException("Could not find version-specific file: "+jarname); + stream = file.getInputStream(entry); + + FileUtils.copyInputStreamToFile(stream, destMod); + Launch.classLoader.addURL(destMod.toURI().toURL()); + } catch (final IOException e) { + new RuntimeException("Could not load version-specific file.", e); + } finally { + IOUtils.closeQuietly(file); + IOUtils.closeQuietly(stream); + } + } + } + } +} diff --git a/common/src/test/java/net/teamfruit/serverobserver/UniversalVersionerTest.java b/common/src/test/java/net/teamfruit/serverobserver/UniversalVersionerTest.java new file mode 100644 index 0000000..70cd527 --- /dev/null +++ b/common/src/test/java/net/teamfruit/serverobserver/UniversalVersionerTest.java @@ -0,0 +1,57 @@ +package net.teamfruit.serverobserver; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class UniversalVersionerTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + assertEquals("1.7.10", UniversalVersioner.getVersion("1.7")); + assertEquals("1.7.10", UniversalVersioner.getVersion("1.7.4")); + assertEquals("1.7.10", UniversalVersioner.getVersion("1.7.10")); + assertEquals("1.7.10", UniversalVersioner.getVersion("1.70.2")); + assertEquals("1.8.9", UniversalVersioner.getVersion("1.8")); + assertEquals("1.8.9", UniversalVersioner.getVersion("1.8.1")); + assertEquals("1.8.9", UniversalVersioner.getVersion("1.8.9")); + assertEquals("1.8.9", UniversalVersioner.getVersion("1.82.3")); + assertEquals("1.9.4", UniversalVersioner.getVersion("1.9")); + assertEquals("1.9.4", UniversalVersioner.getVersion("1.9.2")); + assertEquals("1.9.4", UniversalVersioner.getVersion("1.9.4")); + assertEquals("1.9.4", UniversalVersioner.getVersion("1.9.80")); + assertEquals("1.10.2", UniversalVersioner.getVersion("1.10")); + assertEquals("1.10.2", UniversalVersioner.getVersion("1.10.1")); + assertEquals("1.10.2", UniversalVersioner.getVersion("1.10.2")); + assertEquals("1.10.2", UniversalVersioner.getVersion("1.10.50")); + assertEquals("1.11.2", UniversalVersioner.getVersion("1.11")); + assertEquals("1.11.2", UniversalVersioner.getVersion("1.11.0")); + assertEquals("1.11.2", UniversalVersioner.getVersion("1.11.2")); + assertEquals("1.11.2", UniversalVersioner.getVersion("1.11.93")); + assertEquals("1.12.2", UniversalVersioner.getVersion("1.12")); + assertEquals("1.12.2", UniversalVersioner.getVersion("1.12.4")); + assertEquals("1.12.2", UniversalVersioner.getVersion("1.12.2")); + assertEquals("1.12.2", UniversalVersioner.getVersion("1.12.18")); + } + +} diff --git a/src/main/java/net/teamfruit/serverobserver/Reference.java b/src/main/java/net/teamfruit/serverobserver/Reference.java index 163d7e6..b4335b2 100644 --- a/src/main/java/net/teamfruit/serverobserver/Reference.java +++ b/src/main/java/net/teamfruit/serverobserver/Reference.java @@ -7,7 +7,7 @@ public class Reference { public static final @Nonnull String NAME = "ServerObserver"; public static final @Nonnull String VERSION = "${version}"; public static final @Nonnull String FORGE = "${forgeversion}"; - public static final @Nonnull String MINECRAFT = "${mcversion}"; + // public static final @Nonnull String MINECRAFT = "${mcversion}"; public static final @Nonnull String PROXY_SERVER = "net.teamfruit.serverobserver.CommonProxy"; public static final @Nonnull String PROXY_CLIENT = "net.teamfruit.serverobserver.ClientProxy"; public static final @Nonnull String GUI_FACTORY = "net.teamfruit.serverobserver.ConfigGuiFactory"; diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info index a3f7aaa..7adc7db 100644 --- a/src/main/resources/mcmod.info +++ b/src/main/resources/mcmod.info @@ -5,8 +5,8 @@ "modid": "serverobserver", "name": "ServerObserver", "description": "Observe Minecraft Server Status", - "mcversion": "${mcversion}", - "version": "${version}", + "mcversion": "", + "version": "", "authorList": [ "Kamesuta" ], @@ -18,7 +18,7 @@ "logoFile": "assets/serverobserver/textures/banner.png", "screenshots": [], "updateUrl": "https://github.com/Team-Fruit/ServerObserver/releases", - "url": "https://github.com/Team-Fruit/ServerObserver/releases", + "url": "https://minecraft.curseforge.com/projects/serverobserver", "useDependencyInformation": false } ]