-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
data driven voting options, configurable pause replacement, modmenu s…
…tyle icon getting
- Loading branch information
1 parent
b032d29
commit ae2f902
Showing
13 changed files
with
1,253 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package net.modfest.ballotbox.util; | ||
|
||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.fabricmc.loader.api.ModContainer; | ||
import net.fabricmc.loader.api.metadata.ModMetadata; | ||
import net.minecraft.client.texture.NativeImage; | ||
import net.minecraft.client.texture.NativeImageBackedTexture; | ||
import net.modfest.ballotbox.BallotBox; | ||
import org.apache.commons.lang3.Validate; | ||
|
||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Liberally stolen from ModMenu. Thanks ModMenu! | ||
*/ | ||
public class ModMetaUtil { | ||
private static final Map<Path, NativeImageBackedTexture> modIconCache = new HashMap<>(); | ||
|
||
public static NativeImageBackedTexture createIcon(ModContainer iconSource, String iconPath) { | ||
try { | ||
Path path = iconSource.getPath(iconPath); | ||
NativeImageBackedTexture cachedIcon = modIconCache.get(path); | ||
if (cachedIcon != null) { | ||
return cachedIcon; | ||
} | ||
cachedIcon = modIconCache.get(path); | ||
if (cachedIcon != null) { | ||
return cachedIcon; | ||
} | ||
try (InputStream inputStream = Files.newInputStream(path)) { | ||
NativeImage image = NativeImage.read(Objects.requireNonNull(inputStream)); | ||
Validate.validState(image.getHeight() == image.getWidth(), "Must be square icon"); | ||
NativeImageBackedTexture tex = new NativeImageBackedTexture(image); | ||
modIconCache.put(path, tex); | ||
return tex; | ||
} | ||
|
||
} catch (IllegalStateException e) { | ||
if (e.getMessage().equals("Must be square icon")) { | ||
BallotBox.LOGGER.error("Mod icon must be a square for icon source {}: {}", | ||
iconSource.getMetadata().getId(), | ||
iconPath, | ||
e | ||
); | ||
} | ||
|
||
return null; | ||
} catch (Throwable t) { | ||
if (!iconPath.equals("assets/" + iconSource.getMetadata().getId() + "/icon.png")) { | ||
BallotBox.LOGGER.error("Invalid mod icon for icon source {}: {}", iconSource.getMetadata().getId(), iconPath, t); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
public static NativeImageBackedTexture getMissingIcon() { | ||
return createIcon( | ||
FabricLoader.getInstance() | ||
.getModContainer(BallotBox.ID) | ||
.orElseThrow(() -> new RuntimeException("Cannot get ModContainer for Fabric mod with id " + BallotBox.ID)), | ||
"assets/" + BallotBox.ID + "/unknown_icon.png" | ||
); | ||
} | ||
|
||
public static NativeImageBackedTexture getIcon(ModContainer mod, int preferredSize) { | ||
ModMetadata meta = mod.getMetadata(); | ||
String modId = meta.getId(); | ||
String iconPath = meta.getIconPath(preferredSize).orElse("assets/" + modId + "/icon.png"); | ||
final String finalIconSourceId = modId; | ||
ModContainer iconSource = FabricLoader.getInstance() | ||
.getModContainer(modId) | ||
.orElseThrow(() -> new RuntimeException("Cannot get ModContainer for Fabric mod with id " + finalIconSourceId)); | ||
NativeImageBackedTexture icon = createIcon(iconSource, iconPath); | ||
if (icon == null) return getMissingIcon(); | ||
return icon; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-31.2 KB
(44%)
src/main/resources/assets/modfest/textures/art/graphics/lockup-transparent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.