Skip to content

Commit

Permalink
Add special handling for Java (#190)
Browse files Browse the repository at this point in the history
- Added special handling to give Java additional metadata
  • Loading branch information
williambl authored Jan 23, 2021
1 parent 139bf71 commit 8a5aefc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ private NativeImageBackedTexture createIcon() {
}
if (!Files.exists(path)) {
ModContainer modMenu = FabricLoader.getInstance().getModContainer(ModMenu.MOD_ID).orElseThrow(IllegalAccessError::new);
if (HardcodedUtil.getFabricMods().contains(metadata.getId())) {
path = modMenu.getPath("assets/" + ModMenu.MOD_ID + "/fabric_icon.png");
} else if (metadata.getId().equals("minecraft")) {
path = modMenu.getPath("assets/" + ModMenu.MOD_ID + "/mc_icon.png");
if (HardcodedUtil.modHasHardcodedIcon(metadata)) {
path = HardcodedUtil.getHardcodedIcon(metadata);
} else {
path = modMenu.getPath("assets/" + ModMenu.MOD_ID + "/grey_fabric_icon.png");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public enum BadgeType {
DEPRECATED(new TranslatableText("modmenu.deprecated"), 0xffff3333, 0xffb30000),
PATCHWORK_FORGE(new TranslatableText("modmenu.forge"), 0xff1f2d42, 0xff101721),
MINECRAFT(new TranslatableText("modmenu.minecraft"), 0xff6f6c6a, 0xff31302f);

private Text text;
private int outlineColor;
private int fillColor;
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/io/github/prospector/modmenu/util/HardcodedUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@
import net.minecraft.text.Text;
import org.apache.commons.lang3.text.WordUtils;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HardcodedUtil {
private static final Pattern FABRIC_PATTERN = Pattern.compile("^fabric-.*(-v\\d+)$");
private static final Set<String> FABRIC_MODS = new HashSet<>();
private static final HashMap<String, String> HARDCODED_DESCRIPTIONS = new HashMap<>();
private static final HashMap<Predicate<ModMetadata>, Path> HARDCODED_ICONS = new HashMap<>();

public static void initializeHardcodings() {
FABRIC_MODS.add("fabric");
FABRIC_MODS.add("fabricloader");
HARDCODED_DESCRIPTIONS.put("minecraft", "The base game.");
HARDCODED_DESCRIPTIONS.put("java", "The Java Runtime Environment");
ModContainer modMenu = FabricLoader.getInstance().getModContainer(ModMenu.MOD_ID).orElseThrow(IllegalAccessError::new);
HARDCODED_ICONS.put(mod -> getFabricMods().contains(mod.getId()), modMenu.getPath("assets/" + ModMenu.MOD_ID + "/fabric_icon.png"));
HARDCODED_ICONS.put(mod -> mod.getId().equals("minecraft"), modMenu.getPath("assets/" + ModMenu.MOD_ID + "/mc_icon.png"));
HARDCODED_ICONS.put(mod -> mod.getId().equals("java"), modMenu.getPath("assets/" + ModMenu.MOD_ID + "/duke_icon.png"));
}

public static void hardcodeModuleMetadata(ModContainer mod, ModMetadata metadata, String id) {
Expand All @@ -42,6 +51,9 @@ public static void hardcodeModuleMetadata(ModContainer mod, ModMetadata metadata
if (id.equals("fabricloader") || id.equals("fabric") || metadata.getName().endsWith(" API")) {
ModMenu.addLibraryMod(id);
}
if (id.equals("java")) {
ModMenu.addLibraryMod(id);
}
}

public static Text formatFabricModuleName(String name) {
Expand All @@ -60,6 +72,18 @@ public static Text formatFabricModuleName(String name) {
return new LiteralText(name);
}

public static boolean modHasHardcodedIcon(ModMetadata mod) {
return HARDCODED_ICONS.keySet().stream().anyMatch(predicate -> predicate.test(mod));
}

public static Path getHardcodedIcon(ModMetadata mod) {
AtomicReference<Path> result = new AtomicReference<>();
HARDCODED_ICONS.forEach((predicate, path) -> {
if (predicate.test(mod)) result.set(path);
});
return result.get();
}

public static String getHardcodedDescription(String id) {
return HARDCODED_DESCRIPTIONS.getOrDefault(id, "");
}
Expand Down
Binary file added src/main/resources/assets/modmenu/duke_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8a5aefc

Please sign in to comment.