77 lines
		
	
	
	
		
			2.6 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			77 lines
		
	
	
	
		
			2.6 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package net.minecraft.network.chat;
 | |
| 
 | |
| import com.google.common.collect.ImmutableList;
 | |
| import com.google.common.collect.Lists;
 | |
| import it.unimi.dsi.fastutil.ints.Int2IntFunction;
 | |
| import java.util.List;
 | |
| import java.util.Optional;
 | |
| import java.util.function.UnaryOperator;
 | |
| import net.minecraft.util.FormattedCharSequence;
 | |
| import net.minecraft.util.StringDecomposer;
 | |
| 
 | |
| public class SubStringSource {
 | |
| 	private final String plainText;
 | |
| 	private final List<Style> charStyles;
 | |
| 	private final Int2IntFunction reverseCharModifier;
 | |
| 
 | |
| 	private SubStringSource(String plainText, List<Style> charStyles, Int2IntFunction reverseCharModifier) {
 | |
| 		this.plainText = plainText;
 | |
| 		this.charStyles = ImmutableList.copyOf(charStyles);
 | |
| 		this.reverseCharModifier = reverseCharModifier;
 | |
| 	}
 | |
| 
 | |
| 	public String getPlainText() {
 | |
| 		return this.plainText;
 | |
| 	}
 | |
| 
 | |
| 	public List<FormattedCharSequence> substring(int fromIndex, int toIndex, boolean reversed) {
 | |
| 		if (toIndex == 0) {
 | |
| 			return ImmutableList.of();
 | |
| 		} else {
 | |
| 			List<FormattedCharSequence> list = Lists.<FormattedCharSequence>newArrayList();
 | |
| 			Style style = (Style)this.charStyles.get(fromIndex);
 | |
| 			int i = fromIndex;
 | |
| 
 | |
| 			for (int j = 1; j < toIndex; j++) {
 | |
| 				int k = fromIndex + j;
 | |
| 				Style style2 = (Style)this.charStyles.get(k);
 | |
| 				if (!style2.equals(style)) {
 | |
| 					String string = this.plainText.substring(i, k);
 | |
| 					list.add(reversed ? FormattedCharSequence.backward(string, style, this.reverseCharModifier) : FormattedCharSequence.forward(string, style));
 | |
| 					style = style2;
 | |
| 					i = k;
 | |
| 				}
 | |
| 			}
 | |
| 
 | |
| 			if (i < fromIndex + toIndex) {
 | |
| 				String string2 = this.plainText.substring(i, fromIndex + toIndex);
 | |
| 				list.add(reversed ? FormattedCharSequence.backward(string2, style, this.reverseCharModifier) : FormattedCharSequence.forward(string2, style));
 | |
| 			}
 | |
| 
 | |
| 			return reversed ? Lists.reverse(list) : list;
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	public static SubStringSource create(FormattedText formattedText) {
 | |
| 		return create(formattedText, i -> i, string -> string);
 | |
| 	}
 | |
| 
 | |
| 	public static SubStringSource create(FormattedText formattedText, Int2IntFunction reverseCharModifier, UnaryOperator<String> textTransformer) {
 | |
| 		StringBuilder stringBuilder = new StringBuilder();
 | |
| 		List<Style> list = Lists.<Style>newArrayList();
 | |
| 		formattedText.visit((style, string) -> {
 | |
| 			StringDecomposer.iterateFormatted(string, style, (i, stylex, j) -> {
 | |
| 				stringBuilder.appendCodePoint(j);
 | |
| 				int k = Character.charCount(j);
 | |
| 
 | |
| 				for (int l = 0; l < k; l++) {
 | |
| 					list.add(stylex);
 | |
| 				}
 | |
| 
 | |
| 				return true;
 | |
| 			});
 | |
| 			return Optional.empty();
 | |
| 		}, Style.EMPTY);
 | |
| 		return new SubStringSource((String)textTransformer.apply(stringBuilder.toString()), list, reverseCharModifier);
 | |
| 	}
 | |
| }
 |