forked from OrnitheMC/modmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add provided update checkers, loader update checkers (#711)
- Add update checkers for Fabric Loader and Quilt Loader - Add API for mods to provide update checkers for other mods
- Loading branch information
Showing
10 changed files
with
454 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
47 changes: 47 additions & 0 deletions
47
src/main/java/com/terraformersmc/modmenu/util/HttpUtil.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.terraformersmc.modmenu.util; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.methods.RequestBuilder; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
|
||
import com.terraformersmc.modmenu.ModMenu; | ||
|
||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.fabricmc.loader.api.ModContainer; | ||
|
||
public class HttpUtil { | ||
private static final String USER_AGENT = buildUserAgent(); | ||
private static final CloseableHttpClient HTTP_CLIENT = HttpClientBuilder.create().build(); | ||
|
||
private HttpUtil() {} | ||
|
||
public static HttpResponse request(RequestBuilder builder) throws IOException { | ||
builder.setHeader("User-Agent", USER_AGENT); | ||
return HTTP_CLIENT.execute(builder.build()); | ||
} | ||
|
||
private static String buildUserAgent() { | ||
String env = ModMenu.devEnvironment ? "/development" : ""; | ||
String loader = ModMenu.runningQuilt ? "quilt" : "fabric"; | ||
|
||
String modMenuVersion = getModMenuVersion(ModMenu.MOD_ID); | ||
String minecraftVersion = getModMenuVersion("minecraft"); | ||
|
||
// -> TerraformersMC/ModMenu/9.1.0 (1.20.3/quilt/development) | ||
return String.format("%s/%s (%s/%s%s)", ModMenu.GITHUB_REF, modMenuVersion, minecraftVersion, loader, env); | ||
} | ||
|
||
private static String getModMenuVersion(String modId) { | ||
Optional<ModContainer> container = FabricLoader.getInstance().getModContainer(modId); | ||
|
||
if (!container.isPresent()) { | ||
throw new RuntimeException("Unable to find Modmenu's own mod container!"); | ||
} | ||
|
||
return VersionUtil.removeBuildMetadata(container.get().getMetadata().getVersion().getFriendlyString()); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/terraformersmc/modmenu/util/JsonUtil.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.terraformersmc.modmenu.util; | ||
|
||
import java.util.Optional; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonPrimitive; | ||
|
||
public class JsonUtil { | ||
private JsonUtil() {} | ||
|
||
public static Optional<String> getString(JsonObject parent, String field) { | ||
if (!parent.has(field)) { | ||
return Optional.empty(); | ||
} | ||
|
||
JsonElement value = parent.get(field); | ||
|
||
if (!value.isJsonPrimitive() || !((JsonPrimitive)value).isString()) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(value.getAsString()); | ||
} | ||
|
||
public static Optional<Boolean> getBoolean(JsonObject parent, String field) { | ||
if (!parent.has(field)) { | ||
return Optional.empty(); | ||
} | ||
|
||
JsonElement value = parent.get(field); | ||
|
||
if (!value.isJsonPrimitive() || !((JsonPrimitive)value).isBoolean()) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(value.getAsBoolean()); | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.