Skip to content

Commit

Permalink
Merge pull request #47 from Septillihedron/icon-fallback
Browse files Browse the repository at this point in the history
Adds fallback when creating a skull icon fails
  • Loading branch information
Xemorr authored Sep 17, 2024
2 parents 24b2b39 + 5c4badf commit a56f6b5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = "me.xemor"
version = "6.1.0"
version = "6.1.1"
description = "superheroes"
java.sourceCompatibility = JavaVersion.VERSION_17

Expand Down
31 changes: 25 additions & 6 deletions src/main/java/me/xemor/superheroes/data/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,12 @@ public void loadSuperheroes(HeroHandler heroHandler) {

private void calculateIcon(Superhero hero, ConfigurationSection heroSection) {
ItemStack icon;
ConfigurationSection section = heroSection.getConfigurationSection("icon");
Component colouredName = MiniMessage.miniMessage().deserialize(hero.getColouredName());
if (section != null) {
icon = new ItemStackData(heroSection.getConfigurationSection("icon")).getItem();
ConfigurationSection iconSection = heroSection.getConfigurationSection("icon");
if (iconSection != null) {
icon = new ItemStackData(iconSection).getItem();
} else {
TextColor color;
icon = hero.getBase64Skin().equals("") ? ((color = colouredName.color()) == null ? new ItemStack(Material.BLACK_WOOL) : new ItemStack(this.woolFromColor(color.red(), color.green(), color.blue()))) : SkullCreator.itemFromBase64(hero.getBase64Skin());
Component colouredName = MiniMessage.miniMessage().deserialize(hero.getColouredName());
icon = createSkullIcon(hero).orElseGet(() -> createWoolIcon(hero, colouredName));
ItemMeta meta = icon.getItemMeta();
meta.setDisplayName(legacySerializer.serialize(colouredName));
Component description = MiniMessage.miniMessage().deserialize(hero.getDescription());
Expand All @@ -196,6 +195,26 @@ private void calculateIcon(Superhero hero, ConfigurationSection heroSection) {
hero.setIcon(icon);
}

private ItemStack createWoolIcon(Superhero hero, Component colouredName) {
TextColor color = colouredName.color();
if (color == null) {
return new ItemStack(Material.BLACK_WOOL);
}
return new ItemStack(this.woolFromColor(color.red(), color.green(), color.blue()));
}

private Optional<ItemStack> createSkullIcon(Superhero hero) {
String base64Skin = hero.getBase64Skin();
if (base64Skin.isEmpty()) return Optional.empty();
try {
return Optional.of(SkullCreator.itemFromBase64(base64Skin));
} catch (IllegalArgumentException e) {
Superheroes.getInstance().getLogger().severe("Failed to make skull icons");
e.printStackTrace();
return Optional.empty();
}
}

public Material woolFromColor(int red, int green, int blue) {
int distance = Integer.MAX_VALUE;
DyeColor closest = DyeColor.BLACK;
Expand Down

0 comments on commit a56f6b5

Please sign in to comment.