|
| 1 | +package top.mcfpp.mod.debugger.gui; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import com.google.common.collect.Lists; |
| 5 | +import com.mojang.brigadier.CommandDispatcher; |
| 6 | +import com.mojang.brigadier.ParseResults; |
| 7 | +import com.mojang.brigadier.StringReader; |
| 8 | +import com.mojang.brigadier.context.CommandContextBuilder; |
| 9 | +import com.mojang.brigadier.context.ParsedArgument; |
| 10 | +import net.minecraft.command.CommandSource; |
| 11 | +import net.minecraft.text.OrderedText; |
| 12 | +import net.minecraft.text.Style; |
| 13 | +import net.minecraft.util.Formatting; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import java.util.Objects; |
| 17 | +import java.util.stream.Stream; |
| 18 | + |
| 19 | +public class CommandHighLineUtils { |
| 20 | + |
| 21 | + public static OrderedText highlightOneLine(CommandDispatcher<CommandSource> commandDispatcher, CommandSource commandSource, String original){ |
| 22 | + StringReader stringReader = new StringReader(original); |
| 23 | + boolean bl = stringReader.canRead() && stringReader.peek() == '/'; |
| 24 | + if (bl) { |
| 25 | + stringReader.skip(); |
| 26 | + } |
| 27 | + ParseResults<CommandSource> parse = commandDispatcher.parse(stringReader, commandSource); |
| 28 | + return highlight(parse,original,bl?1:0); |
| 29 | + } |
| 30 | + |
| 31 | + private static final Style ERROR_STYLE; |
| 32 | + private static final Style INFO_STYLE; |
| 33 | + private static final List<Style> HIGHLIGHT_STYLES; |
| 34 | + static { |
| 35 | + ERROR_STYLE = Style.EMPTY.withColor(Formatting.RED); |
| 36 | + INFO_STYLE = Style.EMPTY.withColor(Formatting.GRAY); |
| 37 | + Stream<Formatting> formattings = Stream.of(Formatting.AQUA, Formatting.YELLOW, Formatting.GREEN, Formatting.LIGHT_PURPLE, Formatting.GOLD); |
| 38 | + Style emptyStyle = Style.EMPTY; |
| 39 | + Objects.requireNonNull(emptyStyle); |
| 40 | + HIGHLIGHT_STYLES = formattings.map(emptyStyle::withColor).collect(ImmutableList.toImmutableList()); |
| 41 | + } |
| 42 | + private static OrderedText highlight(ParseResults<CommandSource> parse, String original, int firstCharacterIndex) { |
| 43 | + List<OrderedText> list = Lists.newArrayList(); |
| 44 | + int i = 0; |
| 45 | + int j = -1; |
| 46 | + CommandContextBuilder<CommandSource> commandContextBuilder = parse.getContext().getLastChild(); |
| 47 | + |
| 48 | + for (ParsedArgument<CommandSource, ?> parsedArgument : commandContextBuilder.getArguments().values()) { |
| 49 | + ++j; |
| 50 | + if (j >= HIGHLIGHT_STYLES.size()) { |
| 51 | + j = 0; |
| 52 | + } |
| 53 | + |
| 54 | + int k = Math.max(parsedArgument.getRange().getStart() - firstCharacterIndex, 0); |
| 55 | + if (k >= original.length()) { |
| 56 | + break; |
| 57 | + } |
| 58 | + |
| 59 | + int l = Math.min(parsedArgument.getRange().getEnd() - firstCharacterIndex, original.length()); |
| 60 | + if (l > 0) { |
| 61 | + list.add(OrderedText.styledForwardsVisitedString(original.substring(i, k), INFO_STYLE)); |
| 62 | + list.add(OrderedText.styledForwardsVisitedString(original.substring(k, l), (Style) HIGHLIGHT_STYLES.get(j))); |
| 63 | + i = l; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (parse.getReader().canRead()) { |
| 68 | + int m = Math.max(parse.getReader().getCursor() - firstCharacterIndex, 0); |
| 69 | + if (m < original.length()) { |
| 70 | + int n = Math.min(m + parse.getReader().getRemainingLength(), original.length()); |
| 71 | + list.add(OrderedText.styledForwardsVisitedString(original.substring(i, m), INFO_STYLE)); |
| 72 | + list.add(OrderedText.styledForwardsVisitedString(original.substring(m, n), ERROR_STYLE)); |
| 73 | + i = n; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + list.add(OrderedText.styledForwardsVisitedString(original.substring(i), INFO_STYLE)); |
| 78 | + return OrderedText.concat(list); |
| 79 | + } |
| 80 | +} |
0 commit comments