Skip to content

Commit 27f8677

Browse files
committed
Fixed other formatting (bold, underline, etc)...
1 parent b371b85 commit 27f8677

File tree

3 files changed

+77
-46
lines changed

3 files changed

+77
-46
lines changed

dependency-reduced-pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>net.matrixcreations</groupId>
4+
<groupId>com.github.MatrixCreations</groupId>
55
<artifactId>MatrixColorAPI</artifactId>
66
<name>MatrixColorAPI</name>
7-
<version>1.0.5</version>
7+
<version>v1.0.6</version>
88
<build>
99
<defaultGoal>clean package</defaultGoal>
1010
<resources>

src/main/java/net/matrixcreations/libraries/utils/GradientUtils.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,49 @@ private static String applyGradient(String text, String startColor, String endCo
3636
TextColor end = TextColor.fromHexString("#" + endColor);
3737

3838
StringBuilder result = new StringBuilder();
39-
boolean isBold = text.contains("§l") || text.contains("&l");
40-
boolean isItalic = text.contains("§o") || text.contains("&o");
41-
boolean isUnderlined = text.contains("§n") || text.contains("&n");
42-
boolean isStrikethrough = text.contains("§m") || text.contains("&m");
43-
boolean isObfuscated = text.contains("§k") || text.contains("&k");
44-
45-
// Remove formatting markers from the actual text before processing
46-
text = text.replace("§l", "").replace("&l", "");
47-
text = text.replace("§o", "").replace("&o", "");
48-
text = text.replace("§n", "").replace("&n", "");
49-
text = text.replace("§m", "").replace("&m", "");
50-
text = text.replace("§k", "").replace("&k", "");
39+
40+
boolean bold = false;
41+
boolean italic = false;
42+
boolean underlined = false;
43+
boolean strikethrough = false;
44+
boolean obfuscated = false;
5145

5246
int length = text.length();
5347
for (int i = 0; i < length; i++) {
54-
float ratio = (float) i / length; // Changed ratio calculation
48+
char currentChar = text.charAt(i);
49+
50+
if (currentChar == '§') {
51+
i++;
52+
char formatCode = text.charAt(i);
53+
switch (formatCode) {
54+
case 'l': bold = true; break;
55+
case 'o': italic = true; break;
56+
case 'n': underlined = true; break;
57+
case 'm': strikethrough = true; break;
58+
case 'k': obfuscated = true; break;
59+
case 'r':
60+
bold = italic = underlined = strikethrough = obfuscated = false;
61+
break;
62+
}
63+
continue;
64+
}
65+
66+
float ratio = (float) i / length;
5567
TextColor color = ColorInterpolater.interpolateColor(start, end, ratio);
5668

5769
TextComponent.Builder component = Component.text()
58-
.content(String.valueOf(text.charAt(i)))
70+
.content(String.valueOf(currentChar))
5971
.color(color);
6072

61-
if (isBold) component.decorate(TextDecoration.BOLD);
62-
if (isItalic) component.decorate(TextDecoration.ITALIC);
63-
if (isUnderlined) component.decorate(TextDecoration.UNDERLINED);
64-
if (isStrikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
65-
if (isObfuscated) component.decorate(TextDecoration.OBFUSCATED);
73+
if (bold) component.decorate(TextDecoration.BOLD);
74+
if (italic) component.decorate(TextDecoration.ITALIC);
75+
if (underlined) component.decorate(TextDecoration.UNDERLINED);
76+
if (strikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
77+
if (obfuscated) component.decorate(TextDecoration.OBFUSCATED);
6678

6779
result.append(LegacyComponentSerializer.legacySection().serialize(component.build()));
6880
}
6981

7082
return result.toString();
7183
}
72-
}
84+
}

src/main/java/net/matrixcreations/libraries/utils/SolidColorUtils.java

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,47 @@ public static String processSolidColors(String text) {
3232
private static String applySolidColor(String text, String hexColor) {
3333
TextColor color = TextColor.fromHexString("#" + hexColor);
3434

35-
boolean isBold = text.contains("§l") || text.contains("&l");
36-
boolean isItalic = text.contains("§o") || text.contains("&o");
37-
boolean isUnderlined = text.contains("§n") || text.contains("&n");
38-
boolean isStrikethrough = text.contains("§m") || text.contains("&m");
39-
boolean isObfuscated = text.contains("§k") || text.contains("&k");
40-
41-
text = text.replace("§l", "").replace("&l", "");
42-
text = text.replace("§o", "").replace("&o", "");
43-
text = text.replace("§n", "").replace("&n", "");
44-
text = text.replace("§m", "").replace("&m", "");
45-
text = text.replace("§k", "").replace("&k", "");
46-
47-
TextComponent.Builder component = Component.text()
48-
.content(text)
49-
.color(color);
50-
51-
if (isBold) component.decorate(TextDecoration.BOLD);
52-
if (isItalic) component.decorate(TextDecoration.ITALIC);
53-
if (isUnderlined) component.decorate(TextDecoration.UNDERLINED);
54-
if (isStrikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
55-
if (isObfuscated) component.decorate(TextDecoration.OBFUSCATED);
56-
57-
return LegacyComponentSerializer.legacySection().serialize(component.build());
35+
StringBuilder result = new StringBuilder();
36+
37+
boolean bold = false;
38+
boolean italic = false;
39+
boolean underlined = false;
40+
boolean strikethrough = false;
41+
boolean obfuscated = false;
42+
43+
int length = text.length();
44+
for (int i = 0; i < length; i++) {
45+
char currentChar = text.charAt(i);
46+
47+
if (currentChar == '§') {
48+
i++;
49+
char formatCode = text.charAt(i);
50+
switch (formatCode) {
51+
case 'l': bold = true; break;
52+
case 'o': italic = true; break;
53+
case 'n': underlined = true; break;
54+
case 'm': strikethrough = true; break;
55+
case 'k': obfuscated = true; break;
56+
case 'r':
57+
bold = italic = underlined = strikethrough = obfuscated = false;
58+
break;
59+
}
60+
continue;
61+
}
62+
63+
TextComponent.Builder component = Component.text()
64+
.content(String.valueOf(currentChar))
65+
.color(color);
66+
67+
if (bold) component.decorate(TextDecoration.BOLD);
68+
if (italic) component.decorate(TextDecoration.ITALIC);
69+
if (underlined) component.decorate(TextDecoration.UNDERLINED);
70+
if (strikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
71+
if (obfuscated) component.decorate(TextDecoration.OBFUSCATED);
72+
73+
result.append(LegacyComponentSerializer.legacySection().serialize(component.build()));
74+
}
75+
76+
return result.toString();
5877
}
59-
}
78+
}

0 commit comments

Comments
 (0)