Skip to content

Commit d0eb42c

Browse files
committed
Add bungeecord support, Improve IridiumColorAPI#apply
1 parent d5f6965 commit d0eb42c

2 files changed

Lines changed: 40 additions & 44 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 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

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

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
import com.iridium.iridiumcolorapi.patterns.RainbowPattern;
77
import com.iridium.iridiumcolorapi.patterns.SolidPattern;
88
import net.md_5.bungee.api.ChatColor;
9-
import org.apache.commons.lang.Validate;
109
import org.bukkit.Bukkit;
1110

1211
import javax.annotation.Nonnull;
1312
import java.awt.*;
14-
import java.util.ArrayList;
1513
import java.util.Arrays;
1614
import java.util.List;
1715
import java.util.Map;
@@ -34,7 +32,7 @@ public class IridiumColorAPI {
3432
*
3533
* @since 1.0.0
3634
*/
37-
private static final boolean SUPPORTS_RGB = VERSION >= 16;
35+
private static final boolean SUPPORTS_RGB = VERSION >= 16 || VERSION == -1;
3836

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

@@ -81,8 +79,7 @@ public static String process(@Nonnull String string) {
8179
string = pattern.process(string);
8280
}
8381

84-
string = ChatColor.translateAlternateColorCodes('&', string);
85-
return string;
82+
return ChatColor.translateAlternateColorCodes('&', string);
8683
}
8784

8885
/**
@@ -121,10 +118,8 @@ public static String color(@Nonnull String string, @Nonnull Color color) {
121118
*/
122119
@Nonnull
123120
public static String color(@Nonnull String string, @Nonnull Color start, @Nonnull Color end) {
124-
String originalString = string;
125-
126121
ChatColor[] colors = createGradient(start, end, withoutSpecialChar(string).length());
127-
return apply(originalString, colors);
122+
return apply(string, colors);
128123
}
129124

130125
/**
@@ -136,10 +131,8 @@ public static String color(@Nonnull String string, @Nonnull Color start, @Nonnul
136131
*/
137132
@Nonnull
138133
public static String rainbow(@Nonnull String string, float saturation) {
139-
String originalString = string;
140-
141134
ChatColor[] colors = createRainbow(withoutSpecialChar(string).length(), saturation);
142-
return apply(originalString, colors);
135+
return apply(string, colors);
143136
}
144137

145138
/**
@@ -171,22 +164,22 @@ public static String stripColorFormatting(@Nonnull String string) {
171164
private static String apply(@Nonnull String source, ChatColor[] colors) {
172165
StringBuilder specialColors = new StringBuilder();
173166
StringBuilder stringBuilder = new StringBuilder();
174-
String[] characters = source.split("");
175167
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]);
168+
169+
for (int i = 0; i < source.length(); i++) {
170+
char currentChar = source.charAt(i);
171+
if (('&' != currentChar && '§' != currentChar) || i + 1 >= source.length()) {
172+
stringBuilder.append(colors[outIndex++]).append(specialColors).append(currentChar);
173+
continue;
174+
}
175+
176+
char nextChar = source.charAt(i + 1);
177+
if ('r' == nextChar) {
178+
specialColors.setLength(0);
179+
} else {
180+
specialColors.append(currentChar).append(nextChar);
181+
}
182+
i++;
190183
}
191184
return stringBuilder.toString();
192185
}
@@ -285,28 +278,31 @@ private static ChatColor getClosestColor(Color color) {
285278
* Gets a simplified major version (..., 9, 10, ..., 14).
286279
* In most cases, you shouldn't be using this method.
287280
*
288-
* @return the simplified major version.
281+
* @return the simplified major version, or -1 for bungeecord
289282
* @since 1.0.0
290283
*/
291284
private static int getVersion() {
292-
String version = Bukkit.getVersion();
293-
Validate.notEmpty(version, "Cannot get major Minecraft version from null or empty string");
294-
295-
// getVersion()
296-
int index = version.lastIndexOf("MC:");
297-
if (index != -1) {
298-
version = version.substring(index + 4, version.length() - 1);
299-
} else if (version.endsWith("SNAPSHOT")) {
300-
// getBukkitVersion()
301-
index = version.indexOf('-');
302-
version = version.substring(0, index);
285+
if (!classExists("org.bukkit.Bukkit") && classExists("net.md_5.bungee.api.ChatColor")) {
286+
return -1;
303287
}
288+
String version = Bukkit.getServer().getClass().getPackage().getName().substring(24);
289+
return Integer.parseInt(version.split("_")[1]);
290+
}
304291

305-
// 1.13.2, 1.14.4, etc...
306-
int lastDot = version.lastIndexOf('.');
307-
if (version.indexOf('.') != lastDot) version = version.substring(0, lastDot);
308-
309-
return Integer.parseInt(version.substring(2));
292+
/**
293+
* Checks if a class exists in the current server
294+
*
295+
* @param path The path of that class
296+
* @return true if the class exists, false if it doesn't
297+
* @since 1.0.7
298+
*/
299+
private static boolean classExists(final String path) {
300+
try {
301+
Class.forName(path);
302+
return true;
303+
} catch (ClassNotFoundException e) {
304+
return false;
305+
}
310306
}
311307

312308
}

0 commit comments

Comments
 (0)