StringUtil .java

net.minecraft.util.StringUtil (class, 96 lines): members: truncateStringIfNecessary, STRIP_COLOR_PATTERN, formatTickDuration, isValidPlayerName, lineCount.

net.minecraft.utilclass96 linescommon2.8 KB

API Surface (13)

String formatTickDuration(final int ticks, final float tickrate)
String stripColor(final String input)
boolean isNullOrEmpty(final @Nullable String s)
String truncateStringIfNecessary(final String s, final int maxLength, final boolean addDotDotDotIfTruncated)
int lineCount(final String s)
boolean endsWithNewLine(final String s)
String trimChatMessage(final String message)
boolean isAllowedChatCharacter(final int ch)
boolean isValidPlayerName(final String name)
String filterText(final String input)
String filterText(final String input, final boolean multiline)
boolean isWhitespace(final int codepoint)
boolean isBlank(final @Nullable String string)

Fields & Constants (3)

STRIP_COLOR_PATTERN
LINE_PATTERN
LINE_END_PATTERN

Code Graph: Dependencies (1)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

StringUtil.java96 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import java.util.Locale;
4
import java.util.regex.Matcher;
5
import java.util.regex.Pattern;
6
import org.apache.commons.lang3.StringUtils;
7
import org.jspecify.annotations.Nullable;
8
9
public class StringUtil {
10
	private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)\\u00A7[0-9A-FK-OR]");
11
	private static final Pattern LINE_PATTERN = Pattern.compile("\\r\\n|\\v");
12
	private static final Pattern LINE_END_PATTERN = Pattern.compile("(?:\\r\\n|\\v)$");
13
14
	public static String formatTickDuration(final int ticks, final float tickrate) {
15
		int seconds = Mth.floor(ticks / tickrate);
16
		int minutes = seconds / 60;
17
		seconds %= 60;
18
		int hours = minutes / 60;
19
		minutes %= 60;
20
		return hours > 0 ? String.format(Locale.ROOT, "%02d:%02d:%02d", hours, minutes, seconds) : String.format(Locale.ROOT, "%02d:%02d", minutes, seconds);
21
	}
22
23
	public static String stripColor(final String input) {
24
		return STRIP_COLOR_PATTERN.matcher(input).replaceAll("");
25
	}
26
27
	public static boolean isNullOrEmpty(final @Nullable String s) {
28
		return StringUtils.isEmpty(s);
29
	}
30
31
	public static String truncateStringIfNecessary(final String s, final int maxLength, final boolean addDotDotDotIfTruncated) {
32
		if (s.length() <= maxLength) {
33
			return s;
34
		} else {
35
			return addDotDotDotIfTruncated && maxLength > 3 ? s.substring(0, maxLength - 3) + "..." : s.substring(0, maxLength);
36
		}
37
	}
38
39
	public static int lineCount(final String s) {
40
		if (s.isEmpty()) {
41
			return 0;
42
		}
43
44
		Matcher matcher = LINE_PATTERN.matcher(s);
45
		int count = 1;
46
47
		while (matcher.find()) {
48
			count++;
49
		}
50
51
		return count;
52
	}
53
54
	public static boolean endsWithNewLine(final String s) {
55
		return LINE_END_PATTERN.matcher(s).find();
56
	}
57
58
	public static String trimChatMessage(final String message) {
59
		return truncateStringIfNecessary(message, 256, false);
60
	}
61
62
	public static boolean isAllowedChatCharacter(final int ch) {
63
		return ch != 167 && ch >= 32 && ch != 127;
64
	}
65
66
	public static boolean isValidPlayerName(final String name) {
67
		return name.length() > 16 ? false : name.chars().filter(c -> c <= 32 || c >= 127).findAny().isEmpty();
68
	}
69
70
	public static String filterText(final String input) {
71
		return filterText(input, false);
72
	}
73
74
	public static String filterText(final String input, final boolean multiline) {
75
		StringBuilder builder = new StringBuilder();
76
77
		for (char c : input.toCharArray()) {
78
			if (isAllowedChatCharacter(c)) {
79
				builder.append(c);
80
			} else if (multiline && c == '\n') {
81
				builder.append(c);
82
			}
83
		}
84
85
		return builder.toString();
86
	}
87
88
	public static boolean isWhitespace(final int codepoint) {
89
		return Character.isWhitespace(codepoint) || Character.isSpaceChar(codepoint);
90
	}
91
92
	public static boolean isBlank(final @Nullable String string) {
93
		return string != null && !string.isEmpty() ? string.chars().allMatch(StringUtil::isWhitespace) : true;
94
	}
95
}
96

Decompiled Minecraft 26.3 source of StringUtil (net/minecraft/util/StringUtil.java) with Mojang official mappings, stored verbatim on the site.