Skip to content

Commit 73ed5c1

Browse files
committed
Add gradient name colours
1 parent afad569 commit 73ed5c1

File tree

3 files changed

+108
-10
lines changed

3 files changed

+108
-10
lines changed

src/client/java/golden/scnicknamer/DisplayMapping.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import java.util.UUID;
44
import org.jetbrains.annotations.NotNull;
55

6-
public record DisplayMapping(String mc_name, UUID mc_uuid, String nickname, String colour) {
6+
public record DisplayMapping(String mc_name, UUID mc_uuid,
7+
String nickname, String colour, String[] colours) {
78

89
@Override @NotNull
910
public String toString() {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package golden.scnicknamer;
2+
3+
import net.minecraft.text.MutableText;
4+
import net.minecraft.text.Style;
5+
import net.minecraft.text.Text;
6+
7+
import java.util.List;
8+
9+
/**
10+
* Utility class for applying gradient colors to text.
11+
*/
12+
public class GradientUtil {
13+
14+
/**
15+
* Applies a gradient to the given text using the specified colors.
16+
* Expects the number of colors to be less than the number of characters in the text.
17+
*
18+
* @param text The text to apply the gradient to.
19+
* @param colors An array of colors in integer format (0xRRGGBB).
20+
* @return A new Text object with the gradient applied.
21+
*/
22+
public static Text applyGradient(Text text, int[] colors) {
23+
String str = text.getString();
24+
Style style = text.getStyle();
25+
int length = str.length();
26+
27+
if (length < 1 || colors == null || colors.length < 1) { // No text or no colors
28+
return text;
29+
}
30+
if (length == 1 || colors.length == 1) { // Single character or single color
31+
return Text.literal(str).setStyle(style.withColor(colors[0]));
32+
}
33+
34+
MutableText result = Text.empty();
35+
int segments = colors.length - 1;
36+
37+
for (int i = 0; i < length; i++) {
38+
float pos = (float) i / (length - 1);
39+
int seg = Math.min((int) (pos * segments), segments - 1);
40+
float localRatio = pos * segments - seg;
41+
42+
int color = interpolateColours(colors[seg], colors[seg + 1], localRatio);
43+
result.append(Text.literal(String.valueOf(str.charAt(i))).setStyle(style.withColor(color)));
44+
}
45+
return result;
46+
}
47+
48+
/**
49+
* Applies a gradient to the given text using the specified hex color strings.
50+
*
51+
* @param text The text to apply the gradient to.
52+
* @param hexColors A list of colors in hex string format.
53+
* @return A new Text object with the gradient applied.
54+
* @see #applyGradient(Text, int[])
55+
*/
56+
public static Text applyGradient(Text text, List<String> hexColors) {
57+
int[] colorInts = hexColors.stream()
58+
.mapToInt(c -> Integer.parseInt(c, 16))
59+
.toArray();
60+
61+
return applyGradient(text, colorInts);
62+
}
63+
64+
/**
65+
* Helper method to interpolate between two colors.
66+
* @param start Start color in 0xRRGGBB format
67+
* @param end End color in 0xRRGGBB format
68+
* @param ratio Ratio between 0 and 1
69+
* @return Interpolated color in 0xRRGGBB format
70+
*/
71+
private static int interpolateColours(int start, int end, float ratio) {
72+
int r = interpolate(start >> 16 & 0xFF, end >> 16 & 0xFF, ratio);
73+
int g = interpolate(start >> 8 & 0xFF, end >> 8 & 0xFF, ratio);
74+
int b = interpolate(start & 0xFF, end & 0xFF, ratio);
75+
76+
return (r << 16) | (g << 8) | b;
77+
}
78+
79+
/**
80+
* Helper method to interpolate between two integer values.
81+
* @param start The start value
82+
* @param end The end value
83+
* @param ratio Ratio between 0 and 1
84+
* @return Interpolated integer value
85+
*/
86+
private static int interpolate(int start, int end, float ratio) {
87+
return (int) (start + (end - start) * ratio);
88+
}
89+
}

src/client/java/golden/scnicknamer/SCNicknamerClient.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import java.util.*;
1717

18+
import static golden.scnicknamer.GradientUtil.applyGradient;
19+
1820
/**
1921
* Client-side mod initializer for the mod "Spooncraft Name Link".
2022
*/
@@ -66,24 +68,30 @@ static MutableText applyMapping(Text message, DisplayMapping mapping,
6668

6769
MutableText outputMessage = Text.empty();
6870
message.visit((style, text) -> {
71+
if (!text.contains(mapping.mc_name())) {
72+
outputMessage.append(Text.literal(text).setStyle(style));
73+
return Optional.empty(); // Continue visiting
74+
}
75+
6976
String replacedText = text;
7077
Style replacedStyle = style;
7178

7279
// Apply the mapping
73-
if (replacedText.contains(mapping.mc_name())) {
74-
// Replace the string
75-
if (mapping.nickname() != null && replaceName) {
76-
replacedText = replacedText.replace(mapping.mc_name(), mapping.nickname());
77-
}
78-
// Apply color if specified
79-
if (mapping.colour() != null && replaceColour) {
80-
replacedStyle = replacedStyle.withColor(Integer.parseInt(mapping.colour(), 16));
81-
}
80+
// Replace the string
81+
if (mapping.nickname() != null && replaceName) {
82+
replacedText = replacedText.replace(mapping.mc_name(), mapping.nickname());
83+
}
84+
// Apply color if specified
85+
if (mapping.colour() != null && replaceColour) {
86+
replacedStyle = replacedStyle.withColor(Integer.parseInt(mapping.colour(), 16));
8287
}
8388

8489
// Create new MutableText with the display string and style
8590
MutableText newText = (MutableText) Text.of(replacedText);
8691
newText.setStyle(replacedStyle);
92+
if (mapping.colours() != null && mapping.colours().length > 1 && replaceColour) {
93+
newText = (MutableText) applyGradient(newText, List.of(mapping.colours()));
94+
}
8795
outputMessage.append(newText);
8896

8997
return Optional.empty(); // Continue visiting

0 commit comments

Comments
 (0)