Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
fix async stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Mar 30, 2023
1 parent 25ed2f1 commit 2aa3198
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ public static boolean isPrefixedWith(Icon icon, Text text) {
/**
* Updates {@link #data} based on the data given from the defined {@link #url}.
*/
public CompletableFuture<UnicodeIconsStore> retrieve() {
return CompletableFuture.supplyAsync(() -> {
JsonHelper.parseCodecUrl(this.url, Data.CODEC, data -> this.data = data);
return this;
});
public CompletableFuture<Void> retrieve() {
return CompletableFuture.supplyAsync(() -> JsonHelper.parseCodecUrl(this.url, Data.CODEC))
.thenAccept(maybeData -> maybeData.ifPresent(data -> this.data = data));
}

public URL getUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ public UpdateTracker(String url) {
/**
* Updates {@link #data} based on the data given from the defined {@link #url}.
*/
public CompletableFuture<UpdateTracker> retrieve() {
return CompletableFuture.supplyAsync(() -> {
JsonHelper.parseCodecUrl(this.url, Data.CODEC, data -> this.data = data);
return this;
});
public CompletableFuture<Void> retrieve() {
return CompletableFuture.supplyAsync(() -> JsonHelper.parseCodecUrl(this.url, Data.CODEC))
.thenAccept(maybeData -> maybeData.ifPresent(data -> this.data = data));
}

public URL getUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public EventApiHook(String url) {
/**
* Updates {@link #data} based on the data given from the defined {@link #url}.
*/
public CompletableFuture<EventApiHook> retrieve() {
return CompletableFuture.supplyAsync(() -> {
JsonHelper.parseCodecUrl(this.url, Data.CODEC, data -> this.data = data);
return this;
});
public CompletableFuture<Void> retrieve() {
return CompletableFuture.supplyAsync(() -> JsonHelper.parseCodecUrl(this.url, Data.CODEC,
element -> element.getAsJsonObject().getAsJsonObject("data")
))
.thenAccept(maybeData -> maybeData.ifPresent(data -> this.data = data));
}

public URL getUrl() {
Expand All @@ -63,16 +63,16 @@ public boolean isEventDateInFuture() {

public record Data(String date, String event, String updateVideo) {
public static final Codec<Data> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
Codec.STRING.fieldOf("date")
.xmap(Function.identity(), s -> s.substring(0, s.length() - 5) + "Z")
.forGetter(Data::date),
Codec.STRING.fieldOf("event")
.forGetter(Data::event),
Codec.STRING.fieldOf("updateVideo")
.orElse("")
.forGetter(Data::updateVideo)
).apply(instance, Data::new)
instance -> instance.group(
Codec.STRING.fieldOf("date")
.xmap(Function.identity(), s -> s.substring(0, s.length() - 5) + "Z")
.forGetter(Data::date),
Codec.STRING.fieldOf("event")
.forGetter(Data::event),
Codec.STRING.fieldOf("updateVideo")
.orElse("")
.forGetter(Data::updateVideo)
).apply(instance, Data::new)
);

public Optional<Date> createDate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import org.slf4j.Logger;

public interface JsonHelper {
Expand Down Expand Up @@ -53,18 +53,18 @@ static JsonElement parseJsonReader(JsonReader reader) throws IOException {
};
}

static <T> Optional<T> parseCodecUrl(URL url, Codec<T> codec) {
static <T> Optional<T> parseCodecUrl(URL url, Codec<T> codec, UnaryOperator<JsonElement> modifier) {
try {
JsonReader reader = new JsonReader(new InputStreamReader(url.openStream()));
return codec.parse(JsonOps.INSTANCE, JsonHelper.parseJsonReader(reader)).result();
return codec.parse(JsonOps.INSTANCE, modifier.apply(JsonHelper.parseJsonReader(reader))).result();
} catch (IOException | JsonSyntaxException | IllegalStateException exception) {
LOGGER.error("%s: Retrieval Error".formatted(MCCIC.MOD_NAME), exception);
}

return Optional.empty();
}

static <T> void parseCodecUrl(URL url, Codec<T> codec, Consumer<T> action) {
parseCodecUrl(url, codec).ifPresent(action);
static <T> Optional<T> parseCodecUrl(URL url, Codec<T> codec) {
return parseCodecUrl(url, codec, UnaryOperator.identity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ public static OptionalInt matchAndGrabIndex(Text message, String raw, String pat
private void onGameJoin(ClientPlayNetworkHandler handler, GameJoinS2CPacket packet) {
ToastsClientConfig config = ToastsClientConfig.getConfig();
if (config.eventAnnouncements()) {
EventApiHook.INSTANCE.retrieve().thenAccept(api -> {
if (api.isEventDateInFuture()) {
api.getData().ifPresent(data -> {
EventApiHook api = EventApiHook.INSTANCE;
api.retrieve().thenRun(() -> {
api.getData().ifPresent(data -> {
if (api.isEventDateInFuture()) {
data.createDate().ifPresent(date -> {
Calendar calendar = Calendar.getInstance();
TimeZone timeZone = calendar.getTimeZone();
Expand All @@ -174,18 +175,19 @@ private void onGameJoin(ClientPlayNetworkHandler handler, GameJoinS2CPacket pack
"%02d".formatted(calendar.get(Calendar.DAY_OF_MONTH)),
"%02d".formatted(calendar.get(Calendar.MONTH) + 1),
calendar.get(Calendar.HOUR),
calendar.get(Calendar.AM_PM) == Calendar.PM ? "pm": "am",
calendar.get(Calendar.AM_PM) == Calendar.PM ? "pm" : "am",
timeZone.getDisplayName(timeZone.inDaylightTime(date), TimeZone.SHORT)
)
).add();
});
});
}
}
});
});
}

if (config.updateNotifications()) {
UpdateTracker.INSTANCE.retrieve().thenAccept(tracker -> {
UpdateTracker tracker = UpdateTracker.INSTANCE;
tracker.retrieve().thenRun(() -> {
tracker.getData().ifPresent(data -> {
if (tracker.isUpdateAvailable()) {
data.createSemanticVersion().ifPresent(version -> {
Expand Down

0 comments on commit 2aa3198

Please sign in to comment.