Skip to content

Commit

Permalink
Merge pull request #41 from byteful/v1.3.8
Browse files Browse the repository at this point in the history
v1.3.8
  • Loading branch information
byteful authored Jun 17, 2024
2 parents 6ed8356 + 1fb07ab commit 7de81c7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {
implementation 'de.tr7zw:item-nbt-api:2.12.4'
implementation 'com.github.Redempt:RedLib:6.6.1'
implementation 'com.github.Redempt:Crunch:2.0.3'
implementation 'com.github.cryptomorin:XSeries:10.0.0'
implementation 'com.github.cryptomorin:XSeries:11.0.0'
implementation 'com.github.Revxrsal.Lamp:common:3.2.1'
implementation 'com.github.Revxrsal.Lamp:bukkit:3.2.1'
implementation 'com.github.Sven65:Item-Names:1.0.2'
Expand Down
53 changes: 45 additions & 8 deletions src/main/java/me/byteful/plugin/leveltools/util/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,55 @@
import org.bukkit.ChatColor;
import org.jetbrains.annotations.NotNull;

/**
* Pulled utility out of lucko's helper library. Since this is pretty much all that was used from
* helper, I removed the lib to minimize the size of LevelTools' jar.
*/
import java.util.Arrays;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;

// Borrowed from RedLib (https://github.com/boxbeam/RedCommands/blob/master/src/redempt/redlib/misc/FormatUtils.java)
public final class Text {
@NotNull
public static String colorize(@NotNull String string) {
return ChatColor.translateAlternateColorCodes('&', string);
public static String decolorize(@NotNull String string) {
return colorize(string).replace("" + ChatColor.COLOR_CHAR, "&");
}

private static Set<Character> colorChars = "4c6e2ab319d5f780rlonmk".chars().mapToObj(i -> (char) i).collect(Collectors.toSet());

@NotNull
public static String decolorize(@NotNull String string) {
return colorize(string).replace("" + ChatColor.COLOR_CHAR, "&");
// Author: boxbeam
public static String colorize(String input) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (i + 1 >= input.length()) {
builder.append(c);
continue;
}
char n = input.charAt(i + 1);
if (c == '\\' && (n == '&' || n == '\\')) {
i++;
builder.append(n);
continue;
}
if (c != '&') {
builder.append(c);
continue;
}
if (colorChars.contains(n)) {
builder.append(ChatColor.COLOR_CHAR);
continue;
}
if (n == '#' && i + 7 <= input.length()) {
String hexCode = input.substring(i + 2, i + 8).toUpperCase(Locale.ROOT);
if (hexCode.chars().allMatch(ch -> (ch <= '9' && ch >= '0') || (ch <= 'F' && ch >= 'A'))) {
hexCode = Arrays.stream(hexCode.split("")).map(s -> ChatColor.COLOR_CHAR + s).collect(Collectors.joining());
builder.append(ChatColor.COLOR_CHAR).append("x").append(hexCode);
i += 7;
continue;
}
}
builder.append(c);
}
return builder.toString();
}
}

0 comments on commit 7de81c7

Please sign in to comment.