Skip to content

Commit

Permalink
use local supporter & config file if api call fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterpaul committed Jan 11, 2025
1 parent aa82ac7 commit 76b75da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
23 changes: 12 additions & 11 deletions src/main/java/de/teamlapen/vampirism/misc/SettingsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
Expand All @@ -38,7 +39,7 @@ public class SettingsProvider implements ISettingsProvider {

public SettingsProvider(String baseUrl) {
this.baseUrl = baseUrl;
this.client = HttpClient.newHttpClient();
this.client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).executor(Util.nonCriticalIoPool()).build();
}

@Override
Expand Down Expand Up @@ -98,7 +99,7 @@ private Optional<Map<String, String>> checkSettings(Map<String, String> settings
if (error != null) {
LOGGER.error("Failed to retrieve settings from server", error);
}
if (VampirismMod.inDev || settings != null) {
if (VampirismMod.inDev || settings == null) {
InputStream inputStream = VampirismMod.class.getResourceAsStream("/default_remote_config.json");
if (inputStream != null) {
try {
Expand All @@ -115,16 +116,16 @@ private Optional<Collection<Supporter>> checkSupporter(Collection<Supporter> fil
if (error != null) {
LOGGER.error("Failed to retrieve supporter from server", error);
}
if (VampirismMod.inDev || file != null) {
InputStream inputStream = VampirismMod.class.getResourceAsStream("/supporters.json");
if (inputStream != null) {
try {
List<Supporter> list = GSON.fromJson(new JsonReader(new InputStreamReader(inputStream)), TypeToken.getParameterized(List.class, Supporter.class).getType());
return Optional.of(list);
} catch (JsonSyntaxException ex) {
LOGGER.error("Failed to retrieve supporter from file", ex);
if (VampirismMod.inDev || file == null) {
InputStream inputStream = VampirismMod.class.getResourceAsStream("/supporters.json");
if (inputStream != null) {
try {
List<Supporter> list = GSON.fromJson(new JsonReader(new InputStreamReader(inputStream)), TypeToken.getParameterized(List.class, Supporter.class).getType());
return Optional.of(list);
} catch (JsonSyntaxException ex) {
LOGGER.error("Failed to retrieve supporter from file", ex);
}
}
}
}
return Optional.ofNullable(file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import net.minecraft.DetectedVersion;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.LogicalSide;
import net.neoforged.fml.ModList;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.fml.util.thread.EffectiveSide;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -35,7 +37,7 @@ public static void execute() {
}

private static void send() {
try(var http = HttpClient.newBuilder().executor(Util.nonCriticalIoPool()).build()) {
try(var http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).executor(Util.nonCriticalIoPool()).build()) {
StringBuilder builder = new StringBuilder();
builder.append(REFERENCE.SETTINGS_API);
builder.append("/telemetry/basic");
Expand All @@ -44,12 +46,12 @@ private static void send() {
params.put("mod_version", REFERENCE.VERSION.toString());
params.put("mc_version", DetectedVersion.BUILT_IN.getName());
params.put("mod_count", Integer.toString(ModList.get().size()));
params.put("side", (EffectiveSide.get() == LogicalSide.CLIENT ? "client" : "server"));
params.put("side", (FMLEnvironment.dist == Dist.CLIENT ? "client" : "server"));

builder.append("?");
builder.append(params.entrySet().stream().map(s -> s.getKey() + "=" + URLEncoder.encode(s.getValue(), StandardCharsets.UTF_8)).collect(Collectors.joining("&")));

http.sendAsync(HttpRequest.newBuilder().uri(new URI(builder.toString())).timeout(Duration.ofSeconds(5)).build(), HttpResponse.BodyHandlers.ofString());
http.sendAsync(HttpRequest.newBuilder().uri(new URI(builder.toString())).build(), HttpResponse.BodyHandlers.ofString());
} catch (URISyntaxException e) {
LOGGER.error("Failed to send telemetry data", e);
}
Expand Down

0 comments on commit 76b75da

Please sign in to comment.