-
Notifications
You must be signed in to change notification settings - Fork 157
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
444 additions
and
28 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
17 changes: 14 additions & 3 deletions
17
src/main/java/com/terraformersmc/modmenu/ModMenuModMenuCompat.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 |
---|---|---|
@@ -1,23 +1,34 @@ | ||
package com.terraformersmc.modmenu; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.terraformersmc.modmenu.api.ConfigScreenFactory; | ||
import com.terraformersmc.modmenu.api.ModMenuApi; | ||
import com.terraformersmc.modmenu.api.UpdateChecker; | ||
import com.terraformersmc.modmenu.gui.ModMenuOptionsScreen; | ||
import com.terraformersmc.modmenu.util.mod.fabric.FabricLoaderUpdateChecker; | ||
import com.terraformersmc.modmenu.util.mod.quilt.QuiltLoaderUpdateChecker; | ||
|
||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.gui.screen.option.OptionsScreen; | ||
|
||
import java.util.Map; | ||
|
||
public class ModMenuModMenuCompat implements ModMenuApi { | ||
|
||
@Override | ||
public ConfigScreenFactory<?> getModConfigScreenFactory() { | ||
return ModMenuOptionsScreen::new; | ||
} | ||
|
||
@Override | ||
public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() { | ||
return ImmutableMap.of("minecraft", parent -> new OptionsScreen(parent, MinecraftClient.getInstance().options)); | ||
return Map.of("minecraft", parent -> new OptionsScreen(parent, MinecraftClient.getInstance().options)); | ||
} | ||
|
||
@Override | ||
public Map<String, UpdateChecker> getProvidedUpdateCheckers() { | ||
if (ModMenu.runningQuilt) { | ||
return Map.of("quilt_loader", new QuiltLoaderUpdateChecker()); | ||
} else { | ||
return Map.of("fabricloader", new FabricLoaderUpdateChecker()); | ||
} | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
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,44 @@ | ||
package com.terraformersmc.modmenu.util; | ||
|
||
import java.io.IOException; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
|
||
import com.terraformersmc.modmenu.ModMenu; | ||
|
||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.minecraft.SharedConstants; | ||
|
||
public class HttpUtil { | ||
private static final String USER_AGENT = buildUserAgent(); | ||
private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); | ||
|
||
private HttpUtil() {} | ||
|
||
public static <T> HttpResponse<T> request(HttpRequest.Builder builder, HttpResponse.BodyHandler<T> handler) throws IOException, InterruptedException { | ||
builder.setHeader("User-Agent", USER_AGENT); | ||
return HTTP_CLIENT.send(builder.build(), handler); | ||
} | ||
|
||
private static String buildUserAgent() { | ||
String env = ModMenu.devEnvironment ? "/development" : ""; | ||
String loader = ModMenu.runningQuilt ? "quilt" : "fabric"; | ||
|
||
var modMenuVersion = getModMenuVersion(); | ||
var minecraftVersion = SharedConstants.getGameVersion().getName(); | ||
|
||
// -> TerraformersMC/ModMenu/9.1.0 (1.20.3/quilt/development) | ||
return "%s/%s (%s/%s%s)".formatted(ModMenu.GITHUB_REF, modMenuVersion, minecraftVersion, loader, env); | ||
} | ||
|
||
private static String getModMenuVersion() { | ||
var container = FabricLoader.getInstance().getModContainer(ModMenu.MOD_ID); | ||
|
||
if (container.isEmpty()) { | ||
throw new RuntimeException("Unable to find Modmenu's own mod container!"); | ||
} | ||
|
||
return VersionUtil.removeBuildMetadata(container.get().getMetadata().getVersion().getFriendlyString()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
package com.terraformersmc.modmenu.util; | ||
|
||
import java.util.Optional; | ||
|
||
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(); | ||
} | ||
|
||
var 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(); | ||
} | ||
|
||
var 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.