Skip to content

Commit 7f84f39

Browse files
committed
make less crashy, accept "other" project types
1 parent fc8d3ea commit 7f84f39

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ authors=ModFest
1717
contributors=Prospector, Sisby folk, acikek
1818
license=MIT
1919
# Mod Version
20-
baseVersion=0.6.1
20+
baseVersion=0.6.2
2121
# Branch Metadata
2222
branch=1.21
2323
tagBranch=1.21

src/main/java/net/modfest/ballotbox/BallotBoxPlatformClient.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.collect.HashMultimap;
44
import com.google.gson.Gson;
55
import com.google.gson.JsonArray;
6+
import com.mojang.datafixers.util.Pair;
67
import com.mojang.serialization.JsonOps;
78
import net.minecraft.resource.ResourceManager;
89
import net.minecraft.util.Identifier;
@@ -11,9 +12,9 @@
1112
import net.modfest.ballotbox.data.VotingSelections;
1213

1314
import java.io.BufferedReader;
14-
import java.io.IOException;
1515
import java.io.InputStreamReader;
1616
import java.util.Map;
17+
import java.util.Objects;
1718
import java.util.UUID;
1819
import java.util.concurrent.CompletableFuture;
1920
import java.util.concurrent.ConcurrentHashMap;
@@ -28,11 +29,11 @@ public class BallotBoxPlatformClient {
2829
public static void init(ResourceManager resourceManager) {
2930
try {
3031
categories.clear();
31-
GSON.fromJson(new BufferedReader(new InputStreamReader(resourceManager.getResourceOrThrow(CATEGORIES_DATA).getInputStream())), JsonArray.class).asList().stream().map(e -> VotingCategory.CODEC.decode(JsonOps.INSTANCE, e).getOrThrow().getFirst()).forEach(category -> categories.put(category.id(), category));
3232
options.clear();
33-
GSON.fromJson(new BufferedReader(new InputStreamReader(resourceManager.getResourceOrThrow(OPTIONS_DATA).getInputStream())), JsonArray.class).asList().stream().map(e -> VotingOption.CODEC.decode(JsonOps.INSTANCE, e).getOrThrow().getFirst()).forEach(option -> options.put(option.id(), option));
34-
} catch (IOException e) {
35-
throw new RuntimeException(e);
33+
GSON.fromJson(new BufferedReader(new InputStreamReader(resourceManager.getResourceOrThrow(CATEGORIES_DATA).getInputStream())), JsonArray.class).asList().stream().map(e -> VotingCategory.CODEC.decode(JsonOps.INSTANCE, e).mapOrElse(Pair::getFirst, a -> null)).filter(Objects::nonNull).forEach(category -> categories.put(category.id(), category));
34+
GSON.fromJson(new BufferedReader(new InputStreamReader(resourceManager.getResourceOrThrow(OPTIONS_DATA).getInputStream())), JsonArray.class).asList().stream().map(e -> VotingOption.CODEC.decode(JsonOps.INSTANCE, e).mapOrElse(Pair::getFirst, a -> null)).filter(Objects::nonNull).forEach(option -> options.put(option.id(), option));
35+
} catch (Exception e) {
36+
BallotBox.LOGGER.info("[BallotBox] Failed to load ballotbox data!", e);
3637
}
3738
}
3839

src/main/java/net/modfest/ballotbox/client/VotingScreen.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected void initSidePanel() {
9494
Map<String, List<VotingCategory>> typedCategories = categories.stream().collect(Collectors.groupingBy(VotingCategory::type));
9595
typedCategories.entrySet().stream().sorted(Comparator.comparing(e -> CATEGORY_TYPES.contains(e.getKey()) ? CATEGORY_TYPES.indexOf(e.getKey()) : 99)).forEach(e -> {
9696
e.getValue().forEach(category -> addCategoryTab(tabs, category));
97-
if (tabs.getList().children().size() < categories.size() + typedCategories.keySet().size() - 1) tabs.addSeparatorEntry(null);
97+
if (tabs.getList().children().size() < categories.size() + typedCategories.size() - 1) tabs.addSeparatorEntry(null);
9898
});
9999
tabs.getList().setBackground(EmptyBackground.EMPTY_BACKGROUND);
100100
addSelectableChild(tabs);
@@ -188,7 +188,8 @@ public VotingOptionButtonWidget(Position position, int width, int height, Voting
188188
this.client.getTextureManager().registerTexture(modIconCache.get(option.id()), icon);
189189
}
190190
texture = modIconCache.get(option.id());
191-
if (option.platform().type().equals("modrinth")) url = "https://modrinth.com/mod/%s".formatted(option.platform().project_id()); // Use project ID later
191+
if (option.platform().type().equals("modrinth")) url = "https://modrinth.com/mod/%s".formatted(option.platform().project_id());
192+
if (option.platform().type().equals("other")) url = option.platform().homepage_url().orElse(null);
192193
setTooltip(url == null ? Text.literal(option.description()).formatted(Formatting.GRAY) : Text.literal(option.description()).formatted(Formatting.GRAY).append(Text.literal("\n")).append(Text.literal("Right-Click").formatted(Formatting.GOLD)).append(Text.literal(" to open the mod page.").formatted(Formatting.WHITE)));
193194
}
194195

src/main/java/net/modfest/ballotbox/data/VotingOption.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public record VotingOption(String id, Optional<String> mod_id, String name, Stri
1414
Platform.CODEC.fieldOf("platform").forGetter(VotingOption::platform)
1515
).apply(instance, VotingOption::new));
1616

17-
public record Platform(String type, String project_id, String version_id) {
17+
public record Platform(String type, Optional<String> project_id, Optional<String> homepage_url) {
1818
public static final Codec<Platform> CODEC = RecordCodecBuilder.create(instance -> instance.group(
1919
Codec.STRING.fieldOf("type").forGetter(Platform::type),
20-
Codec.STRING.fieldOf("project_id").forGetter(Platform::project_id),
21-
Codec.STRING.fieldOf("version_id").forGetter(Platform::version_id)
20+
Codec.STRING.optionalFieldOf("project_id").forGetter(Platform::project_id),
21+
Codec.STRING.optionalFieldOf("homepage_url").forGetter(Platform::homepage_url)
2222
).apply(instance, Platform::new));
2323
}
2424
}

0 commit comments

Comments
 (0)