112 lines
3.4 KiB
Java
112 lines
3.4 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* org.apache.commons.lang3.StringUtils
|
|
* org.jspecify.annotations.Nullable
|
|
*/
|
|
package net.minecraft.util;
|
|
|
|
import java.util.Locale;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
import net.minecraft.util.Mth;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.jspecify.annotations.Nullable;
|
|
|
|
public class StringUtil {
|
|
private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)\\u00A7[0-9A-FK-OR]");
|
|
private static final Pattern LINE_PATTERN = Pattern.compile("\\r\\n|\\v");
|
|
private static final Pattern LINE_END_PATTERN = Pattern.compile("(?:\\r\\n|\\v)$");
|
|
|
|
public static String formatTickDuration(int ticks, float tickrate) {
|
|
int seconds = Mth.floor((float)ticks / tickrate);
|
|
int minutes = seconds / 60;
|
|
seconds %= 60;
|
|
int hours = minutes / 60;
|
|
minutes %= 60;
|
|
if (hours > 0) {
|
|
return String.format(Locale.ROOT, "%02d:%02d:%02d", hours, minutes, seconds);
|
|
}
|
|
return String.format(Locale.ROOT, "%02d:%02d", minutes, seconds);
|
|
}
|
|
|
|
public static String stripColor(String input) {
|
|
return STRIP_COLOR_PATTERN.matcher(input).replaceAll("");
|
|
}
|
|
|
|
public static boolean isNullOrEmpty(@Nullable String s) {
|
|
return StringUtils.isEmpty((CharSequence)s);
|
|
}
|
|
|
|
public static String truncateStringIfNecessary(String s, int maxLength, boolean addDotDotDotIfTruncated) {
|
|
if (s.length() <= maxLength) {
|
|
return s;
|
|
}
|
|
if (addDotDotDotIfTruncated && maxLength > 3) {
|
|
return s.substring(0, maxLength - 3) + "...";
|
|
}
|
|
return s.substring(0, maxLength);
|
|
}
|
|
|
|
public static int lineCount(String s) {
|
|
if (s.isEmpty()) {
|
|
return 0;
|
|
}
|
|
Matcher matcher = LINE_PATTERN.matcher(s);
|
|
int count = 1;
|
|
while (matcher.find()) {
|
|
++count;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
public static boolean endsWithNewLine(String s) {
|
|
return LINE_END_PATTERN.matcher(s).find();
|
|
}
|
|
|
|
public static String trimChatMessage(String message) {
|
|
return StringUtil.truncateStringIfNecessary(message, 256, false);
|
|
}
|
|
|
|
public static boolean isAllowedChatCharacter(int ch) {
|
|
return ch != 167 && ch >= 32 && ch != 127;
|
|
}
|
|
|
|
public static boolean isValidPlayerName(String name) {
|
|
if (name.length() > 16) {
|
|
return false;
|
|
}
|
|
return name.chars().filter(c -> c <= 32 || c >= 127).findAny().isEmpty();
|
|
}
|
|
|
|
public static String filterText(String input) {
|
|
return StringUtil.filterText(input, false);
|
|
}
|
|
|
|
public static String filterText(String input, boolean multiline) {
|
|
StringBuilder builder = new StringBuilder();
|
|
for (char c : input.toCharArray()) {
|
|
if (StringUtil.isAllowedChatCharacter(c)) {
|
|
builder.append(c);
|
|
continue;
|
|
}
|
|
if (!multiline || c != '\n') continue;
|
|
builder.append(c);
|
|
}
|
|
return builder.toString();
|
|
}
|
|
|
|
public static boolean isWhitespace(int codepoint) {
|
|
return Character.isWhitespace(codepoint) || Character.isSpaceChar(codepoint);
|
|
}
|
|
|
|
public static boolean isBlank(@Nullable String string) {
|
|
if (string == null || string.isEmpty()) {
|
|
return true;
|
|
}
|
|
return string.chars().allMatch(StringUtil::isWhitespace);
|
|
}
|
|
}
|
|
|