Created
July 12, 2017 05:56
-
-
Save Daomephsta/fb812ffe54aaf9d17cb7e6b44eb50144 to your computer and use it in GitHub Desktop.
Forge maven and MCPBot interfaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package leviathan143.fantasticchainsaw.forgemaven; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.Arrays; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import com.google.common.collect.Maps; | |
import com.google.common.reflect.TypeToken; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParser; | |
public class ForgeMavenInterface | |
{ | |
private static final String FORGE_VERSIONS_URL = "http://files.minecraftforge.net/maven/net/minecraftforge/forge/json"; | |
private static final Gson FORGE_VERSION_GSON = new GsonBuilder().create(); | |
private static Map<String, String[]> mcVersionToForgeVersions = Maps.newHashMap(); | |
@SuppressWarnings("unused") | |
private static class ForgeVersionArtifacts | |
{ | |
private String branch; | |
private int build; | |
private String[][] files; | |
private String mcversion; | |
private float modified; | |
private String version; | |
} | |
@SuppressWarnings("serial") | |
public static void updateForgeVersions() | |
{ | |
try | |
{ | |
URL versionJSONURL = new URL(FORGE_VERSIONS_URL); | |
URLConnection connection = versionJSONURL.openConnection(); | |
connection.connect(); | |
InputStream in = connection.getInputStream(); | |
long startTime = System.currentTimeMillis(); | |
JsonObject json = new JsonParser().parse(new InputStreamReader(in)).getAsJsonObject(); | |
Map<Integer, ForgeVersionArtifacts> artifacts = FORGE_VERSION_GSON.fromJson(json.get("number"), new TypeToken<Map<Integer, ForgeVersionArtifacts>>() {}.getType()); | |
Map<String, int[]> mcVersionToBuildNumber = FORGE_VERSION_GSON.fromJson(json.get("branches"), new TypeToken<Map<String, int[]>>() {}.getType()); | |
in.close(); | |
for(Entry<String, int[]> entry : mcVersionToBuildNumber.entrySet()) | |
{ | |
String[] forgeVersionStrings = Arrays.stream(entry.getValue()).mapToObj(value -> {return artifacts.get(value).version;}).toArray(String[]::new); | |
mcVersionToForgeVersions.put(entry.getKey(), forgeVersionStrings); | |
} | |
} | |
catch (MalformedURLException e) | |
{ | |
e.printStackTrace(); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package leviathan143.fantasticchainsaw.mcpbot; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.Map; | |
import com.google.common.collect.Maps; | |
import com.google.common.reflect.TypeToken; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import leviathan143.fantasticchainsaw.i18n.MCPBotInterfaceMessages; | |
public class MCPBotInterface | |
{ | |
private static final String MCP_VERSIONS_URL = "http://export.mcpbot.bspk.rs/versions.json"; | |
private static final Gson MCP_VERSION_GSON = new GsonBuilder().create(); | |
private static Map<String, VersionMappings> versionToMappings = Maps.newHashMap(); | |
private static final class VersionMappings | |
{ | |
private int[] snapshot; | |
private int[] stable; | |
} | |
@SuppressWarnings("serial") | |
public static void updateMCPVersions() | |
{ | |
try | |
{ | |
URL versionJSONURL = new URL(MCP_VERSIONS_URL); | |
URLConnection connection = versionJSONURL.openConnection(); | |
connection.connect(); | |
InputStream in = connection.getInputStream(); | |
System.out.println(MCPBotInterfaceMessages.retrievingMappings); | |
long startTime = System.currentTimeMillis(); | |
versionToMappings = MCP_VERSION_GSON.fromJson(new InputStreamReader(in), new TypeToken<Map<String, VersionMappings>>(){}.getType()); | |
System.out.println(String.format(MCPBotInterfaceMessages.mappingsRetrieved, System.currentTimeMillis() - startTime)); | |
in.close(); | |
} | |
catch (MalformedURLException e) | |
{ | |
e.printStackTrace(); | |
} | |
catch (IOException e) | |
{ | |
System.out.println(MCPBotInterfaceMessages.mappingsNotRetrieved); | |
e.printStackTrace(); | |
} | |
} | |
public static int[] getAvailableStableMappings(String mcVersion) | |
{ | |
if(!versionToMappings.containsKey(mcVersion)) | |
{ | |
System.out.println(String.format(MCPBotInterfaceMessages.noMappings, mcVersion)); | |
return null; | |
} | |
return versionToMappings.get(mcVersion).stable; | |
} | |
public static int[] getAvailableSnapshotMappings(String mcVersion) | |
{ | |
if(!versionToMappings.containsKey(mcVersion)) | |
{ | |
System.out.println(String.format(MCPBotInterfaceMessages.noMappings, mcVersion)); | |
return null; | |
} | |
return versionToMappings.get(mcVersion).snapshot; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment