Use regex for finding colour codes
Merge styles rather than replacing and undoing our work sometimes.
This commit is contained in:
dordsor21 2020-07-04 14:15:49 +01:00
parent d92cded832
commit 704e76eb6d
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B

View File

@ -5,6 +5,7 @@ import com.sk89q.worldedit.util.formatting.WorldEditText;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.util.formatting.text.format.Style;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
import com.sk89q.worldedit.util.formatting.text.serializer.legacy.LegacyComponentSerializer;
import org.jetbrains.annotations.Nullable;
@ -16,9 +17,13 @@ import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Caption {
private static final Pattern colorCodes = Pattern.compile("&([0-9a-o])");
public static String toString(Component component) {
return toString(component, WorldEdit.getInstance().getTranslationManager().getDefaultLocale());
}
@ -39,9 +44,9 @@ public class Caption {
private static Component color(TextComponent text) {
String content = text.content();
if (content.indexOf('&') != -1) {
if (colorCodes.matcher(content).find()) {
TextComponent legacy = LegacyComponentSerializer.INSTANCE.deserialize(content, '&');
legacy = (TextComponent) legacy.style(text.style());
legacy.style().merge(text.style(), Style.Merge.Strategy.IF_ABSENT_ON_TARGET);
if (!text.children().isEmpty()) {
text = TextComponent.builder().append(legacy).append(text.children()).build();
} else {