Skip to content

Commit

Permalink
Fix crash on world save with broken multithreading
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Feb 28, 2024
1 parent 8aff697 commit 8246741
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 7 additions & 10 deletions common/src/main/java/dev/sweetberry/wwizardry/api/Badges.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -27,7 +24,7 @@ public class Badges {

private static final Gson GSON = new Gson();

private static final Map<UUID, Component> BADGES_CACHE = new ConcurrentHashMap<>();
private static final Map<UUID, Optional<Component>> BADGES_CACHE = new ConcurrentHashMap<>();

private static final String BASE_URL = "https://badges.wwizardry.sweetberry.dev";

Expand All @@ -51,10 +48,10 @@ public class Badges {

@Nullable
public static Component getBadgeFor(UUID player) {
if (!WORKER.isAlive())
if (!WORKER.isAlive() && !WORKER.isInterrupted())
WORKER.start();
if (BADGES_CACHE.containsKey(player))
return BADGES_CACHE.get(player);
return BADGES_CACHE.get(player).orElse(null);
if (!RESOLUTION_QUEUE.contains(player))
RESOLUTION_QUEUE.add(player);
return null;
Expand All @@ -73,14 +70,14 @@ private static void resolveThread() {
var name = makeRequest(player);

if (!MAP.containsKey(name))
BADGES_CACHE.put(player, null);
BADGES_CACHE.put(player, Optional.empty());

WanderingWizardry.LOGGER.info(player + " -> " + name);

var badge = MAP.get(name);
BADGES_CACHE.put(player, badge);
BADGES_CACHE.put(player, Optional.ofNullable(badge));
} catch (IOException | InterruptedException | JsonSyntaxException e) {
BADGES_CACHE.put(player, null);
BADGES_CACHE.put(player, Optional.empty());
}
RESOLUTION_QUEUE.poll();
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.jvmargs = -Xmx4G
org.gradle.parallel = true

# Mod Properties
version = 1.0.13
version = 1.0.14
classification =
maven_group = dev.sweetberry
archives_base_name = wwizardry
Expand Down

0 comments on commit 8246741

Please sign in to comment.