Skip to content

Commit 245ccaf

Browse files
committed
Implement hardcoded gradients
1 parent 1ef0a01 commit 245ccaf

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ org.gradle.parallel=true
66
# check these on https://fabricmc.net/develop
77
minecraft_version=1.21.8
88
yarn_mappings=1.21.8+build.1
9-
loader_version=0.16.14
9+
loader_version=0.17.2
1010
loom_version=1.11-SNAPSHOT
1111

1212
# Mod Properties
13-
mod_version=1.4.0-a4
13+
mod_version=1.4.0-a-gradients5
1414
maven_group=golden.scnicknamer
1515
archives_base_name=scnicknamer
1616

1717
# Dependencies
18-
fabric_version=0.130.0+1.21.8
18+
fabric_version=0.133.4+1.21.8
1919
modmenu_version=15.0.0-beta.3
2020
clothconfig_version=19.0.147

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

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,31 @@ static MutableText applyMapping(Text message, DisplayMapping mapping,
6767

6868
MutableText outputMessage = Text.empty();
6969
message.visit((style, text) -> {
70+
if (!text.contains(mapping.mc_name)) {
71+
outputMessage.append(Text.literal(text).setStyle(style));
72+
return Optional.empty(); // Continue visiting
73+
}
74+
7075
String replacedText = text;
7176
Style replacedStyle = style;
7277

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

8588
// Create new MutableText with the display string and style
8689
MutableText newText = (MutableText) Text.of(replacedText);
8790
newText.setStyle(replacedStyle);
91+
if (true) {
92+
newText = (MutableText) getGradientText(newText, new String[]{mapping.colour, "a9c9ff", "ffbbec", "ffc3a0"});
93+
}
94+
8895
outputMessage.append(newText);
8996

9097
return Optional.empty(); // Continue visiting
@@ -168,6 +175,54 @@ public static Text getStyledChat(Text message, boolean replaceName, boolean repl
168175
return outputMessage;
169176
}
170177

178+
/**
179+
* Applies a gradient color effect to the given text using the specified colors.
180+
* @param text The text to which the gradient effect will be applied.
181+
* @param colors An array of color hex strings (without the '#' prefix) defining the gradient stops.
182+
* @return A new Text object with the gradient effect applied.
183+
*/
184+
private static Text getGradientText(Text text, String[] colors) {
185+
String str = text.getString();
186+
Style style = text.getStyle();
187+
int length = str.length();
188+
189+
if (length <= 1 || colors == null || colors.length < 2) {
190+
return text;
191+
}
192+
193+
int[] colorInts = Arrays.stream(colors)
194+
.mapToInt(c -> Integer.parseInt(c, 16))
195+
.toArray();
196+
197+
MutableText result = Text.empty();
198+
int segments = colorInts.length - 1;
199+
200+
for (int i = 0; i < length; i++) {
201+
float pos = (float) i / (length - 1);
202+
int seg = Math.min((int) (pos * segments), segments - 1);
203+
float localRatio = (pos * segments) - seg;
204+
205+
int start = colorInts[seg];
206+
int end = colorInts[seg + 1];
207+
208+
int r = interpolate((start >> 16) & 0xFF, (end >> 16) & 0xFF, localRatio);
209+
int g = interpolate((start >> 8) & 0xFF, (end >> 8) & 0xFF, localRatio);
210+
int b = interpolate(start & 0xFF, end & 0xFF, localRatio);
211+
212+
int color = (r << 16) | (g << 8) | b;
213+
214+
MutableText newChar = Text.literal(String.valueOf(str.charAt(i)))
215+
.setStyle(style.withColor(color));
216+
result.append(newChar);
217+
}
218+
219+
return result;
220+
}
221+
222+
private static int interpolate(int start, int end, float ratio) {
223+
return (int) (start + (end - start) * ratio);
224+
}
225+
171226
/**
172227
* Retrieves the mappings from the specified source URL or the default URL if none is provided.
173228
* If the mod is disabled in the configuration, it disables the mod and logs a warning.

0 commit comments

Comments
 (0)