Skip to content

Commit

Permalink
Merge pull request #100 from anatawa12/mcpbot-down
Browse files Browse the repository at this point in the history
use maven instead of mcpbot json
  • Loading branch information
anatawa12 authored Nov 21, 2021
2 parents e6886ec + 9316c36 commit 0a234af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 50 deletions.
72 changes: 32 additions & 40 deletions src/main/java/net/minecraftforge/gradle/common/BaseExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import net.minecraftforge.gradle.GradleConfigurationException;
import org.gradle.api.Project;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Map;
Expand All @@ -16,7 +19,6 @@ public class BaseExtension {
protected String runDir = "run";
private LinkedList<String> srgExtra = new LinkedList<String>();

protected Map<String, Map<String, int[]>> mcpJson;
protected boolean mappingsSet = false;
protected String mappingsChannel = null;
protected int mappingsVersion = -1;
Expand Down Expand Up @@ -142,49 +144,39 @@ protected void checkMappings() {
// mappings or mc version are null
if (!mappingsSet || "null".equals(version) || Strings.isNullOrEmpty(version) || customVersion != null)
return;
// mcp version mapping is not available
if (mcpJson == null) {
project.getLogger().warn("we couldn't check mcp version json.");
return;
}

// check if it exists
Map<String, int[]> versionMap = mcpJson.get(version);
if (versionMap == null)
throw new GradleConfigurationException("There are no mappings for MC " + version);

String channel = getMappingsChannelNoSubtype();
int[] channelList = versionMap.get(channel);
if (channelList == null)
throw new GradleConfigurationException("There is no such MCP mapping channel named " + channel);

// all is well with the world
if (searchArray(channelList, mappingsVersion))
return;

// if it gets here.. it wasnt found. Now we try to actually find it..
for (Entry<String, Map<String, int[]>> mcEntry : mcpJson.entrySet()) {
for (Entry<String, int[]> channelEntry : mcEntry.getValue().entrySet()) {
// found it!
if (searchArray(channelEntry.getValue(), mappingsVersion)) {
boolean rightMc = mcEntry.getKey().equals(version);
boolean rightChannel = channelEntry.getKey().equals(channel);

// right channel, but wrong mc
if (rightChannel && !rightMc) {
throw new GradleConfigurationException("This mapping '" + getMappings() + "' exists only for MC " + mcEntry.getKey() + "!");
}

// right MC , but wrong channel
else if (rightMc && !rightChannel) {
throw new GradleConfigurationException("This mapping '" + getMappings() + "' doesnt exist! perhaps you meant '" + channelEntry.getKey() + "_" + mappingsVersion + "'");
}
}
}
if (!checkMappingsVersion(channel, version, mappingsVersion, "HEAD")
&& !checkMappingsVersion(channel, version, mappingsVersion, "GET")) {
throw new GradleConfigurationException(
"There is no such MCP version " + mappingsVersion + " in channel " + channel + " for " + version + "."
);
}
}

// wasnt found
throw new GradleConfigurationException("The specified mapping '" + getMappings() + "' does not exist!");
private boolean checkMappingsVersion(String channel, String version, int mappingsVersion, String method) {
HttpURLConnection con = null;
try {
URL url = new URL(
"https://maven.minecraftforge.net/de/oceanlabs/mcp" +
"/mcp_" + channel +
"/" + mappingsVersion + "-" + version +
"/mcp_" + channel + "-" + mappingsVersion + "-" + version + ".zip"
);

con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(method);
con.setInstanceFollowRedirects(true);
con.setRequestProperty("User-Agent", Constants.USER_AGENT);

con.connect();

return con.getResponseCode() == 200;
} catch (IOException e) {
return false;
} finally {
if (con != null) con.disconnect();
}
}

private static boolean searchArray(int[] array, int key) {
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/net/minecraftforge/gradle/common/BasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public void execute(Project proj) {
});

// do Mcp Snapshots Stuff
setVersionInfoJson();
project.getConfigurations().create(Constants.CONFIG_MCP_DATA);

// Separated module
Expand Down Expand Up @@ -168,15 +167,6 @@ public void execute(Project project) {

private static boolean displayBanner = true;

private void setVersionInfoJson() {
File jsonCache = Constants.cacheFile(project, "caches", "minecraft", "McpMappings.json");
File etagFile = new File(jsonCache.getAbsolutePath() + ".etag");

getExtension().mcpJson = JsonFactory.GSON.fromJson(
getWithEtag(Constants.MCP_JSON_URL, jsonCache, etagFile),
new TypeToken<Map<String, Map<String, int[]>>>() {}.getType());
}

public void afterEvaluate() {
if (getExtension().mappingsSet()) {
project.getDependencies().add(Constants.CONFIG_MCP_DATA, ImmutableMap.of(
Expand Down

0 comments on commit 0a234af

Please sign in to comment.