Skip to content

Commit

Permalink
Users with Special badges can now set a color for each of them
Browse files Browse the repository at this point in the history
  • Loading branch information
UnlikePaladin committed Oct 2, 2023
1 parent df2a434 commit 7d468bf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
7 changes: 7 additions & 0 deletions common/src/main/java/org/figuramc/figura/avatar/Avatar.java
Expand Up @@ -98,6 +98,8 @@ public class Avatar {
public String id;
public int fileSize;
public String color;
public Map<String, String> badgeToColor = new HashMap<>();

public boolean minify;

// Runtime data
Expand Down Expand Up @@ -183,6 +185,11 @@ public void load(CompoundTag nbt) {
color = metadata.getString("color");
if (metadata.contains("minify"))
minify = metadata.getBoolean("minify");
for (String key : metadata.getAllKeys()) {
if (key.contains("badge_color_")) {
badgeToColor.put(key.replace("badge_color_", ""), metadata.getString(key));
}
}
fileSize = getFileSize();
versionStatus = getVersionStatus();
if (entityName.isBlank())
Expand Down
27 changes: 18 additions & 9 deletions common/src/main/java/org/figuramc/figura/avatar/Badges.java
Expand Up @@ -86,17 +86,24 @@ else if (avatar.nbt != null) {
}
}

// -- special -- //

// special badges
BitSet specialSet = pair.getSecond();
Special[] special = Special.values();
for (int i = special.length - 1; i >= 0; i--) {
if (specialSet.get(i))
badges.append(special[i].badge);
// -- special -- //
if (avatar != null) {
// special badges
BitSet specialSet = pair.getSecond();
Special[] specialValues = Special.values();
for (int i = specialValues.length - 1; i >= 0; i--) {
if (specialSet.get(i)) {
Special special = specialValues[i];
Integer color = special.color;
if (avatar.badgeToColor.containsKey(special.name().toLowerCase())) {
color = ColorUtils.rgbToInt(ColorUtils.userInputHex(avatar.badgeToColor.get(special.name().toLowerCase())));
}
Component badge = color != null ? special.badge.copy().withStyle(Style.EMPTY.withColor(color)) : special.badge;
badges.append(badge);
}
}
}


// -- extra -- //


Expand Down Expand Up @@ -204,6 +211,7 @@ public enum Special {

public final Component badge;
public final Component desc;
public final Integer color;

Special(String unicode) {
this(unicode, null);
Expand All @@ -213,6 +221,7 @@ public enum Special {
this.desc = FiguraText.of("badges.special." + this.name().toLowerCase());
Style style = Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, desc));
if (color != null) style = style.withColor(color);
this.color = color;
this.badge = Component.literal(unicode).withStyle(style);
}
}
Expand Down
Expand Up @@ -203,6 +203,9 @@ public static String fetchStatus(Avatar avatar) {
aMeta.addProperty("hasLuaRuntime", avatar.luaRuntime != null);
aMeta.addProperty("hasRenderer", avatar.renderer != null);
aMeta.addProperty("hasData", avatar.nbt != null);
for (Map.Entry<String, String> entry: avatar.badgeToColor.entrySet()) {
aMeta.addProperty(entry.getKey(), entry.getValue());
}

a.add("meta", aMeta);

Expand Down
Expand Up @@ -2,6 +2,8 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import net.minecraft.nbt.*;
import org.figuramc.figura.FiguraMod;
import org.figuramc.figura.config.Configs;
Expand Down Expand Up @@ -31,6 +33,14 @@ public static CompoundTag parse(String json, String filename) {

// nbt
CompoundTag nbt = new CompoundTag();
JsonElement jsonElement = JsonParser.parseString(json);
if (jsonElement != null && !jsonElement.isJsonNull() && !jsonElement.getAsJsonObject().asMap().isEmpty()) {
for (Map.Entry<String, JsonElement> jsonElementEntry : jsonElement.getAsJsonObject().entrySet()) {
if (jsonElementEntry.getKey() != null && !jsonElementEntry.getKey().isBlank() && jsonElementEntry.getKey().contains("badge_color_")) {
nbt.putString(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
}
}
}

// version
Version version = new Version(metadata.version);
Expand Down

0 comments on commit 7d468bf

Please sign in to comment.