Skip to content

Commit b5eb1bf

Browse files
authored
Version Increment: Merge pull request #30 from iiAhmedYT/master
Add bungeecord support, rewrite IridiumColorAPI#apply, fix a potential bug
2 parents d5f6965 + 033b0f5 commit b5eb1bf

2 files changed

Lines changed: 45 additions & 28 deletions

File tree

build.gradle.kts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = "com.iridium"
7-
version = "1.0.6"
7+
version = "1.0.7"
88
description = "IridiumColorAPI"
99
java.sourceCompatibility = JavaVersion.VERSION_1_8
1010

@@ -26,6 +26,10 @@ publishing {
2626
}
2727
}
2828

29+
tasks.withType<JavaCompile>().configureEach {
30+
options.encoding = "UTF-8"
31+
}
32+
2933
tasks {
3034
build {
3135
dependsOn(test)

src/main/java/com/iridium/iridiumcolorapi/IridiumColorAPI.java

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import javax.annotation.Nonnull;
1313
import java.awt.*;
14-
import java.util.ArrayList;
1514
import java.util.Arrays;
1615
import java.util.List;
1716
import java.util.Map;
@@ -34,7 +33,7 @@ public class IridiumColorAPI {
3433
*
3534
* @since 1.0.0
3635
*/
37-
private static final boolean SUPPORTS_RGB = VERSION >= 16;
36+
private static final boolean SUPPORTS_RGB = VERSION >= 16 || VERSION == -1;
3837

3938
private static final List<String> SPECIAL_COLORS = Arrays.asList("&l", "&n", "&o", "&k", "&m", "§l", "§n", "§o", "§k", "§m");
4039

@@ -81,8 +80,7 @@ public static String process(@Nonnull String string) {
8180
string = pattern.process(string);
8281
}
8382

84-
string = ChatColor.translateAlternateColorCodes('&', string);
85-
return string;
83+
return ChatColor.translateAlternateColorCodes('&', string);
8684
}
8785

8886
/**
@@ -121,10 +119,8 @@ public static String color(@Nonnull String string, @Nonnull Color color) {
121119
*/
122120
@Nonnull
123121
public static String color(@Nonnull String string, @Nonnull Color start, @Nonnull Color end) {
124-
String originalString = string;
125-
126122
ChatColor[] colors = createGradient(start, end, withoutSpecialChar(string).length());
127-
return apply(originalString, colors);
123+
return apply(string, colors);
128124
}
129125

130126
/**
@@ -136,10 +132,8 @@ public static String color(@Nonnull String string, @Nonnull Color start, @Nonnul
136132
*/
137133
@Nonnull
138134
public static String rainbow(@Nonnull String string, float saturation) {
139-
String originalString = string;
140-
141135
ChatColor[] colors = createRainbow(withoutSpecialChar(string).length(), saturation);
142-
return apply(originalString, colors);
136+
return apply(string, colors);
143137
}
144138

145139
/**
@@ -171,22 +165,22 @@ public static String stripColorFormatting(@Nonnull String string) {
171165
private static String apply(@Nonnull String source, ChatColor[] colors) {
172166
StringBuilder specialColors = new StringBuilder();
173167
StringBuilder stringBuilder = new StringBuilder();
174-
String[] characters = source.split("");
175168
int outIndex = 0;
176-
for (int i = 0; i < characters.length; i++) {
177-
if (characters[i].equals("&") || characters[i].equals("§")) {
178-
if (i + 1 < characters.length) {
179-
if (characters[i + 1].equals("r")) {
180-
specialColors.setLength(0);
181-
} else {
182-
specialColors.append(characters[i]);
183-
specialColors.append(characters[i + 1]);
184-
}
185-
i++;
186-
} else
187-
stringBuilder.append(colors[outIndex++]).append(specialColors).append(characters[i]);
188-
} else
189-
stringBuilder.append(colors[outIndex++]).append(specialColors).append(characters[i]);
169+
170+
for (int i = 0; i < source.length(); i++) {
171+
char currentChar = source.charAt(i);
172+
if (('&' != currentChar && '§' != currentChar) || i + 1 >= source.length()) {
173+
stringBuilder.append(colors[outIndex++]).append(specialColors).append(currentChar);
174+
continue;
175+
}
176+
177+
char nextChar = source.charAt(i + 1);
178+
if ('r' == nextChar || 'R' == nextChar) {
179+
specialColors.setLength(0);
180+
} else {
181+
specialColors.append(currentChar).append(nextChar);
182+
}
183+
i++;
190184
}
191185
return stringBuilder.toString();
192186
}
@@ -285,10 +279,14 @@ private static ChatColor getClosestColor(Color color) {
285279
* Gets a simplified major version (..., 9, 10, ..., 14).
286280
* In most cases, you shouldn't be using this method.
287281
*
288-
* @return the simplified major version.
282+
* @return the simplified major version, or -1 for bungeecord
289283
* @since 1.0.0
290284
*/
291285
private static int getVersion() {
286+
if (!classExists("org.bukkit.Bukkit") && classExists("net.md_5.bungee.api.ChatColor")) {
287+
return -1;
288+
}
289+
292290
String version = Bukkit.getVersion();
293291
Validate.notEmpty(version, "Cannot get major Minecraft version from null or empty string");
294292

@@ -301,12 +299,27 @@ private static int getVersion() {
301299
index = version.indexOf('-');
302300
version = version.substring(0, index);
303301
}
304-
305302
// 1.13.2, 1.14.4, etc...
306303
int lastDot = version.lastIndexOf('.');
307304
if (version.indexOf('.') != lastDot) version = version.substring(0, lastDot);
308305

309306
return Integer.parseInt(version.substring(2));
310307
}
311308

309+
/**
310+
* Checks if a class exists in the current server
311+
*
312+
* @param path The path of that class
313+
* @return true if the class exists, false if it doesn't
314+
* @since 1.0.7
315+
*/
316+
private static boolean classExists(final String path) {
317+
try {
318+
Class.forName(path);
319+
return true;
320+
} catch (ClassNotFoundException e) {
321+
return false;
322+
}
323+
}
324+
312325
}

0 commit comments

Comments
 (0)