Skip to content

Commit

Permalink
Skip unknown badge keys (#720)
Browse files Browse the repository at this point in the history
Fixes #693

- Fixed crash with unknown badges
  • Loading branch information
haykam821 authored Jun 15, 2024
1 parent 8798223 commit a1f017f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
15 changes: 13 additions & 2 deletions src/main/java/com/terraformersmc/modmenu/util/mod/Mod.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.terraformersmc.modmenu.util.mod;

import com.google.common.base.Predicates;
import com.terraformersmc.modmenu.ModMenu;
import com.terraformersmc.modmenu.api.UpdateChecker;
import com.terraformersmc.modmenu.api.UpdateInfo;
import com.terraformersmc.modmenu.config.ModMenuConfig;
Expand Down Expand Up @@ -166,8 +168,17 @@ public int getFillColor() {
return this.fillColor;
}

public static Set<Badge> convert(Set<String> badgeKeys) {
return badgeKeys.stream().map(KEY_MAP::get).collect(Collectors.toSet());
public static Set<Badge> convert(Set<String> badgeKeys, String modId) {
return badgeKeys.stream()
.map(key -> {
if (!KEY_MAP.containsKey(key)) {
ModMenu.LOGGER.warn("Skipping unknown badge key '{}' specified by mod '{}'", key, modId);
}

return KEY_MAP.get(key);
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public FabricMod(ModContainer modContainer, Set<String> modpackMods) {
this.container = modContainer;
this.metadata = modContainer.getMetadata();

if ("minecraft".equals(metadata.getId()) || "java".equals(metadata.getId())) {
String id = metadata.getId();

if ("minecraft".equals(id) || "java".equals(id)) {
allowsUpdateChecks = false;
}

Expand All @@ -77,13 +79,13 @@ public FabricMod(ModContainer modContainer, Set<String> modpackMods) {
CustomValueUtil.getString("icon", parentObj),
CustomValueUtil.getStringSet("badges", parentObj).orElse(new HashSet<>())
);
if (parentId.orElse("").equals(this.metadata.getId())) {
if (parentId.orElse("").equals(id)) {
parentId = Optional.empty();
parentData = null;
throw new RuntimeException("Mod declared itself as its own parent");
}
} catch (Throwable t) {
LOGGER.error("Error loading parent data from mod: " + metadata.getId(), t);
LOGGER.error("Error loading parent data from mod: " + id, t);
}
}
}
Expand All @@ -94,11 +96,11 @@ public FabricMod(ModContainer modContainer, Set<String> modpackMods) {
this.modMenuData = new ModMenuData(
badgeNames,
parentId,
parentData
parentData,
id
);

/* Hardcode parents and badges for Fabric API & Fabric Loader */
String id = metadata.getId();
if (id.startsWith("fabric") && metadata.containsCustomValue("fabric-api:module-lifecycle")) {
if (FabricLoader.getInstance().isModLoaded("fabric-api") || !FabricLoader.getInstance().isModLoaded("fabric")) {
modMenuData.fillParentIfEmpty("fabric-api");
Expand Down Expand Up @@ -365,8 +367,8 @@ static class ModMenuData {
private @Nullable
final DummyParentData dummyParentData;

public ModMenuData(Set<String> badges, Optional<String> parent, DummyParentData dummyParentData) {
this.badges = Badge.convert(badges);
public ModMenuData(Set<String> badges, Optional<String> parent, DummyParentData dummyParentData, String id) {
this.badges = Badge.convert(badges, id);
this.parent = parent;
this.dummyParentData = dummyParentData;
}
Expand Down Expand Up @@ -413,7 +415,7 @@ public DummyParentData(String id, Optional<String> name, Optional<String> descri
this.name = name;
this.description = description;
this.icon = icon;
this.badges = Badge.convert(badges);
this.badges = Badge.convert(badges, id);
}

public String getId() {
Expand Down

0 comments on commit a1f017f

Please sign in to comment.