| 1 | package net.minecraft.network.chat;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import it.unimi.dsi.fastutil.ints.Int2IntFunction;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.function.UnaryOperator;
|
| 9 | import net.minecraft.util.FormattedCharSequence;
|
| 10 | import net.minecraft.util.StringDecomposer;
|
| 11 |
|
| 12 | public class SubStringSource {
|
| 13 | private final String plainText;
|
| 14 | private final List<Style> charStyles;
|
| 15 | private final Int2IntFunction reverseCharModifier;
|
| 16 |
|
| 17 | private SubStringSource(final String plainText, final List<Style> charStyles, final Int2IntFunction reverseCharModifier) {
|
| 18 | this.plainText = plainText;
|
| 19 | this.charStyles = ImmutableList.copyOf(charStyles);
|
| 20 | this.reverseCharModifier = reverseCharModifier;
|
| 21 | }
|
| 22 |
|
| 23 | public String getPlainText() {
|
| 24 | return this.plainText;
|
| 25 | }
|
| 26 |
|
| 27 | public List<FormattedCharSequence> substring(final int start, final int length, final boolean reverse) {
|
| 28 | if (length == 0) {
|
| 29 | return ImmutableList.of();
|
| 30 | }
|
| 31 |
|
| 32 | List<FormattedCharSequence> parts = Lists.newArrayList();
|
| 33 | Style currentRunStyle = this.charStyles.get(start);
|
| 34 | int currentRunStart = start;
|
| 35 |
|
| 36 | for (int i = 1; i < length; i++) {
|
| 37 | int actualIndex = start + i;
|
| 38 | Style charStyle = this.charStyles.get(actualIndex);
|
| 39 | if (!charStyle.equals(currentRunStyle)) {
|
| 40 | String currentRunText = this.plainText.substring(currentRunStart, actualIndex);
|
| 41 | parts.add(
|
| 42 | reverse
|
| 43 | ? FormattedCharSequence.backward(currentRunText, currentRunStyle, this.reverseCharModifier)
|
| 44 | : FormattedCharSequence.forward(currentRunText, currentRunStyle)
|
| 45 | );
|
| 46 | currentRunStyle = charStyle;
|
| 47 | currentRunStart = actualIndex;
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | if (currentRunStart < start + length) {
|
| 52 | String lastRunText = this.plainText.substring(currentRunStart, start + length);
|
| 53 | parts.add(
|
| 54 | reverse
|
| 55 | ? FormattedCharSequence.backward(lastRunText, currentRunStyle, this.reverseCharModifier)
|
| 56 | : FormattedCharSequence.forward(lastRunText, currentRunStyle)
|
| 57 | );
|
| 58 | }
|
| 59 |
|
| 60 | return reverse ? Lists.reverse(parts) : parts;
|
| 61 | }
|
| 62 |
|
| 63 | public static SubStringSource create(final FormattedText text) {
|
| 64 | return create(text, ch -> ch, s -> s);
|
| 65 | }
|
| 66 |
|
| 67 | public static SubStringSource create(final FormattedText text, final Int2IntFunction reverseCharModifier, final UnaryOperator<String> shaper) {
|
| 68 | StringBuilder plainText = new StringBuilder();
|
| 69 | List<Style> charStyles = Lists.newArrayList();
|
| 70 | text.visit((style, contents) -> {
|
| 71 | StringDecomposer.iterateFormatted(contents, style, (position, charStyle, codepoint) -> {
|
| 72 | plainText.appendCodePoint(codepoint);
|
| 73 | int charCount = Character.charCount(codepoint);
|
| 74 |
|
| 75 | for (int i = 0; i < charCount; i++) {
|
| 76 | charStyles.add(charStyle);
|
| 77 | }
|
| 78 |
|
| 79 | return true;
|
| 80 | });
|
| 81 | return Optional.empty();
|
| 82 | }, Style.EMPTY);
|
| 83 | return new SubStringSource(shaper.apply(plainText.toString()), charStyles, reverseCharModifier);
|
| 84 | }
|
| 85 | }
|
| 86 |
|