From e9d838cc78c5e2fbb0c908952532a8003b0b231b Mon Sep 17 00:00:00 2001 From: Damian <34820479+mt1006@users.noreply.github.com> Date: Sun, 15 Sep 2024 22:33:34 +0200 Subject: [PATCH] Fix parent map issues (#775) --- .../com/terraformersmc/modmenu/ModMenu.java | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/terraformersmc/modmenu/ModMenu.java b/src/main/java/com/terraformersmc/modmenu/ModMenu.java index 46df1cbf..018faeb8 100644 --- a/src/main/java/com/terraformersmc/modmenu/ModMenu.java +++ b/src/main/java/com/terraformersmc/modmenu/ModMenu.java @@ -127,21 +127,46 @@ public void onInitializeClient() { Map dummyParents = new HashMap<>(); // Initialize parent map + HashSet modParentSet = new HashSet<>(); for (Mod mod : MODS.values()) { String parentId = mod.getParent(); - if (parentId != null) { - Mod parent = MODS.getOrDefault(parentId, dummyParents.get(parentId)); + if (parentId == null) { + ROOT_MODS.put(mod.getId(), mod); + continue; + } + + Mod parent; + modParentSet.clear(); + while (true) { + parent = MODS.getOrDefault(parentId, dummyParents.get(parentId)); if (parent == null) { if (mod instanceof FabricMod) { parent = new FabricDummyParentMod((FabricMod) mod, parentId); dummyParents.put(parentId, parent); } } - PARENT_MAP.put(parent, mod); - } else { + + parentId = parent != null ? parent.getParent() : null; + if (parentId == null) { + // It will most likely end here in the first iteration + break; + } + + if (modParentSet.contains(parentId)) { + LOGGER.warn("Mods contain each other as parents: {}", modParentSet); + parent = null; + break; + } + modParentSet.add(parentId); + } + + if (parent == null) { ROOT_MODS.put(mod.getId(), mod); + continue; } + PARENT_MAP.put(parent, mod); } + MODS.putAll(dummyParents); ModMenuEventHandler.register(); }