File size: 26,626 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package net.minecraft.client.gui.components;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.context.SuggestionContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.Rect2i;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.Style;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.Vec2;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class CommandSuggestions {
    private static final Pattern WHITESPACE_PATTERN = Pattern.compile("(\\s+)");
    private static final Style UNPARSED_STYLE = Style.EMPTY.withColor(ChatFormatting.RED);
    private static final Style LITERAL_STYLE = Style.EMPTY.withColor(ChatFormatting.GRAY);
    private static final List<Style> ARGUMENT_STYLES = Stream.of(
            ChatFormatting.AQUA, ChatFormatting.YELLOW, ChatFormatting.GREEN, ChatFormatting.LIGHT_PURPLE, ChatFormatting.GOLD
        )
        .map(Style.EMPTY::withColor)
        .collect(ImmutableList.toImmutableList());
    final Minecraft minecraft;
    private final Screen screen;
    final EditBox input;
    final Font font;
    private final boolean commandsOnly;
    private final boolean onlyShowIfCursorPastError;
    final int lineStartOffset;
    final int suggestionLineLimit;
    final boolean anchorToBottom;
    final int fillColor;
    private final List<FormattedCharSequence> commandUsage = Lists.newArrayList();
    private int commandUsagePosition;
    private int commandUsageWidth;
    @Nullable
    private ParseResults<SharedSuggestionProvider> currentParse;
    @Nullable
    private CompletableFuture<Suggestions> pendingSuggestions;
    @Nullable
    private CommandSuggestions.SuggestionsList suggestions;
    private boolean allowSuggestions;
    boolean keepSuggestions;
    private boolean allowHiding = true;

    public CommandSuggestions(
        Minecraft p_93871_,
        Screen p_93872_,
        EditBox p_93873_,
        Font p_93874_,
        boolean p_93875_,
        boolean p_93876_,
        int p_93877_,
        int p_93878_,
        boolean p_93879_,
        int p_93880_
    ) {
        this.minecraft = p_93871_;
        this.screen = p_93872_;
        this.input = p_93873_;
        this.font = p_93874_;
        this.commandsOnly = p_93875_;
        this.onlyShowIfCursorPastError = p_93876_;
        this.lineStartOffset = p_93877_;
        this.suggestionLineLimit = p_93878_;
        this.anchorToBottom = p_93879_;
        this.fillColor = p_93880_;
        p_93873_.setFormatter(this::formatChat);
    }

    public void setAllowSuggestions(boolean p_93923_) {
        this.allowSuggestions = p_93923_;
        if (!p_93923_) {
            this.suggestions = null;
        }
    }

    public void setAllowHiding(boolean p_301612_) {
        this.allowHiding = p_301612_;
    }

    public boolean keyPressed(int p_93889_, int p_93890_, int p_93891_) {
        boolean flag = this.suggestions != null;
        if (flag && this.suggestions.keyPressed(p_93889_, p_93890_, p_93891_)) {
            return true;
        } else if (this.screen.getFocused() != this.input || p_93889_ != 258 || this.allowHiding && !flag) {
            return false;
        } else {
            this.showSuggestions(true);
            return true;
        }
    }

    public boolean mouseScrolled(double p_93883_) {
        return this.suggestions != null && this.suggestions.mouseScrolled(Mth.clamp(p_93883_, -1.0, 1.0));
    }

    public boolean mouseClicked(double p_93885_, double p_93886_, int p_93887_) {
        return this.suggestions != null && this.suggestions.mouseClicked((int)p_93885_, (int)p_93886_, p_93887_);
    }

    public void showSuggestions(boolean p_93931_) {
        if (this.pendingSuggestions != null && this.pendingSuggestions.isDone()) {
            Suggestions suggestions = this.pendingSuggestions.join();
            if (!suggestions.isEmpty()) {
                int i = 0;

                for (Suggestion suggestion : suggestions.getList()) {
                    i = Math.max(i, this.font.width(suggestion.getText()));
                }

                int j = Mth.clamp(this.input.getScreenX(suggestions.getRange().getStart()), 0, this.input.getScreenX(0) + this.input.getInnerWidth() - i);
                int k = this.anchorToBottom ? this.screen.height - 12 : 72;
                this.suggestions = new CommandSuggestions.SuggestionsList(j, k, i, this.sortSuggestions(suggestions), p_93931_);
            }
        }
    }

    public boolean isVisible() {
        return this.suggestions != null;
    }

    public Component getUsageNarration() {
        if (this.suggestions != null && this.suggestions.tabCycles) {
            return this.allowHiding
                ? Component.translatable("narration.suggestion.usage.cycle.hidable")
                : Component.translatable("narration.suggestion.usage.cycle.fixed");
        } else {
            return this.allowHiding
                ? Component.translatable("narration.suggestion.usage.fill.hidable")
                : Component.translatable("narration.suggestion.usage.fill.fixed");
        }
    }

    public void hide() {
        this.suggestions = null;
    }

    private List<Suggestion> sortSuggestions(Suggestions p_93899_) {
        String s = this.input.getValue().substring(0, this.input.getCursorPosition());
        int i = getLastWordIndex(s);
        String s1 = s.substring(i).toLowerCase(Locale.ROOT);
        List<Suggestion> list = Lists.newArrayList();
        List<Suggestion> list1 = Lists.newArrayList();

        for (Suggestion suggestion : p_93899_.getList()) {
            if (!suggestion.getText().startsWith(s1) && !suggestion.getText().startsWith("minecraft:" + s1)) {
                list1.add(suggestion);
            } else {
                list.add(suggestion);
            }
        }

        list.addAll(list1);
        return list;
    }

    public void updateCommandInfo() {
        String s = this.input.getValue();
        if (this.currentParse != null && !this.currentParse.getReader().getString().equals(s)) {
            this.currentParse = null;
        }

        if (!this.keepSuggestions) {
            this.input.setSuggestion(null);
            this.suggestions = null;
        }

        this.commandUsage.clear();
        StringReader stringreader = new StringReader(s);
        boolean flag = stringreader.canRead() && stringreader.peek() == '/';
        if (flag) {
            stringreader.skip();
        }

        boolean flag1 = this.commandsOnly || flag;
        int i = this.input.getCursorPosition();
        if (flag1) {
            CommandDispatcher<SharedSuggestionProvider> commanddispatcher = this.minecraft.player.connection.getCommands();
            if (this.currentParse == null) {
                this.currentParse = commanddispatcher.parse(stringreader, this.minecraft.player.connection.getSuggestionsProvider());
            }

            int j = this.onlyShowIfCursorPastError ? stringreader.getCursor() : 1;
            if (i >= j && (this.suggestions == null || !this.keepSuggestions)) {
                this.pendingSuggestions = commanddispatcher.getCompletionSuggestions(this.currentParse, i);
                this.pendingSuggestions.thenRun(() -> {
                    if (this.pendingSuggestions.isDone()) {
                        this.updateUsageInfo();
                    }
                });
            }
        } else {
            String s1 = s.substring(0, i);
            int k = getLastWordIndex(s1);
            Collection<String> collection = this.minecraft.player.connection.getSuggestionsProvider().getCustomTabSugggestions();
            this.pendingSuggestions = SharedSuggestionProvider.suggest(collection, new SuggestionsBuilder(s1, k));
        }
    }

    private static int getLastWordIndex(String p_93913_) {
        if (Strings.isNullOrEmpty(p_93913_)) {
            return 0;
        } else {
            int i = 0;
            Matcher matcher = WHITESPACE_PATTERN.matcher(p_93913_);

            while (matcher.find()) {
                i = matcher.end();
            }

            return i;
        }
    }

    private static FormattedCharSequence getExceptionMessage(CommandSyntaxException p_93897_) {
        Component component = ComponentUtils.fromMessage(p_93897_.getRawMessage());
        String s = p_93897_.getContext();
        return s == null ? component.getVisualOrderText() : Component.translatable("command.context.parse_error", component, p_93897_.getCursor(), s).getVisualOrderText();
    }

    private void updateUsageInfo() {
        boolean flag = false;
        if (this.input.getCursorPosition() == this.input.getValue().length()) {
            if (this.pendingSuggestions.join().isEmpty() && !this.currentParse.getExceptions().isEmpty()) {
                int i = 0;

                for (Entry<CommandNode<SharedSuggestionProvider>, CommandSyntaxException> entry : this.currentParse.getExceptions().entrySet()) {
                    CommandSyntaxException commandsyntaxexception = entry.getValue();
                    if (commandsyntaxexception.getType() == CommandSyntaxException.BUILT_IN_EXCEPTIONS.literalIncorrect()) {
                        i++;
                    } else {
                        this.commandUsage.add(getExceptionMessage(commandsyntaxexception));
                    }
                }

                if (i > 0) {
                    this.commandUsage.add(getExceptionMessage(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().create()));
                }
            } else if (this.currentParse.getReader().canRead()) {
                flag = true;
            }
        }

        this.commandUsagePosition = 0;
        this.commandUsageWidth = this.screen.width;
        if (this.commandUsage.isEmpty() && !this.fillNodeUsage(ChatFormatting.GRAY) && flag) {
            this.commandUsage.add(getExceptionMessage(Commands.getParseException(this.currentParse)));
        }

        this.suggestions = null;
        if (this.allowSuggestions && this.minecraft.options.autoSuggestions().get()) {
            this.showSuggestions(false);
        }
    }

    private boolean fillNodeUsage(ChatFormatting p_289002_) {
        CommandContextBuilder<SharedSuggestionProvider> commandcontextbuilder = this.currentParse.getContext();
        SuggestionContext<SharedSuggestionProvider> suggestioncontext = commandcontextbuilder.findSuggestionContext(this.input.getCursorPosition());
        Map<CommandNode<SharedSuggestionProvider>, String> map = this.minecraft
            .player
            .connection
            .getCommands()
            .getSmartUsage(suggestioncontext.parent, this.minecraft.player.connection.getSuggestionsProvider());
        List<FormattedCharSequence> list = Lists.newArrayList();
        int i = 0;
        Style style = Style.EMPTY.withColor(p_289002_);

        for (Entry<CommandNode<SharedSuggestionProvider>, String> entry : map.entrySet()) {
            if (!(entry.getKey() instanceof LiteralCommandNode)) {
                list.add(FormattedCharSequence.forward(entry.getValue(), style));
                i = Math.max(i, this.font.width(entry.getValue()));
            }
        }

        if (!list.isEmpty()) {
            this.commandUsage.addAll(list);
            this.commandUsagePosition = Mth.clamp(this.input.getScreenX(suggestioncontext.startPos), 0, this.input.getScreenX(0) + this.input.getInnerWidth() - i);
            this.commandUsageWidth = i;
            return true;
        } else {
            return false;
        }
    }

    private FormattedCharSequence formatChat(String p_93915_, int p_93916_) {
        return this.currentParse != null ? formatText(this.currentParse, p_93915_, p_93916_) : FormattedCharSequence.forward(p_93915_, Style.EMPTY);
    }

    @Nullable
    static String calculateSuggestionSuffix(String p_93928_, String p_93929_) {
        return p_93929_.startsWith(p_93928_) ? p_93929_.substring(p_93928_.length()) : null;
    }

    private static FormattedCharSequence formatText(ParseResults<SharedSuggestionProvider> p_93893_, String p_93894_, int p_93895_) {
        List<FormattedCharSequence> list = Lists.newArrayList();
        int i = 0;
        int j = -1;
        CommandContextBuilder<SharedSuggestionProvider> commandcontextbuilder = p_93893_.getContext().getLastChild();

        for (ParsedArgument<SharedSuggestionProvider, ?> parsedargument : commandcontextbuilder.getArguments().values()) {
            if (++j >= ARGUMENT_STYLES.size()) {
                j = 0;
            }

            int k = Math.max(parsedargument.getRange().getStart() - p_93895_, 0);
            if (k >= p_93894_.length()) {
                break;
            }

            int l = Math.min(parsedargument.getRange().getEnd() - p_93895_, p_93894_.length());
            if (l > 0) {
                list.add(FormattedCharSequence.forward(p_93894_.substring(i, k), LITERAL_STYLE));
                list.add(FormattedCharSequence.forward(p_93894_.substring(k, l), ARGUMENT_STYLES.get(j)));
                i = l;
            }
        }

        if (p_93893_.getReader().canRead()) {
            int i1 = Math.max(p_93893_.getReader().getCursor() - p_93895_, 0);
            if (i1 < p_93894_.length()) {
                int j1 = Math.min(i1 + p_93893_.getReader().getRemainingLength(), p_93894_.length());
                list.add(FormattedCharSequence.forward(p_93894_.substring(i, i1), LITERAL_STYLE));
                list.add(FormattedCharSequence.forward(p_93894_.substring(i1, j1), UNPARSED_STYLE));
                i = j1;
            }
        }

        list.add(FormattedCharSequence.forward(p_93894_.substring(i), LITERAL_STYLE));
        return FormattedCharSequence.composite(list);
    }

    public void render(GuiGraphics p_282650_, int p_282266_, int p_281963_) {
        if (!this.renderSuggestions(p_282650_, p_282266_, p_281963_)) {
            this.renderUsage(p_282650_);
        }
    }

    public boolean renderSuggestions(GuiGraphics p_283503_, int p_281628_, int p_282260_) {
        if (this.suggestions != null) {
            this.suggestions.render(p_283503_, p_281628_, p_282260_);
            return true;
        } else {
            return false;
        }
    }

    public void renderUsage(GuiGraphics p_282763_) {
        int i = 0;

        for (FormattedCharSequence formattedcharsequence : this.commandUsage) {
            int j = this.anchorToBottom ? this.screen.height - 14 - 13 - 12 * i : 72 + 12 * i;
            p_282763_.fill(this.commandUsagePosition - 1, j, this.commandUsagePosition + this.commandUsageWidth + 1, j + 12, this.fillColor);
            p_282763_.drawString(this.font, formattedcharsequence, this.commandUsagePosition, j + 2, -1);
            i++;
        }
    }

    public Component getNarrationMessage() {
        return (Component)(this.suggestions != null ? CommonComponents.NEW_LINE.copy().append(this.suggestions.getNarrationMessage()) : CommonComponents.EMPTY);
    }

    @OnlyIn(Dist.CLIENT)
    public class SuggestionsList {
        private final Rect2i rect;
        private final String originalContents;
        private final List<Suggestion> suggestionList;
        private int offset;
        private int current;
        private Vec2 lastMouse = Vec2.ZERO;
        boolean tabCycles;
        private int lastNarratedEntry;

        SuggestionsList(final int p_93957_, final int p_93958_, final int p_93959_, final List<Suggestion> p_93960_, final boolean p_93961_) {
            int i = p_93957_ - (CommandSuggestions.this.input.isBordered() ? 0 : 1);
            int j = CommandSuggestions.this.anchorToBottom
                ? p_93958_ - 3 - Math.min(p_93960_.size(), CommandSuggestions.this.suggestionLineLimit) * 12
                : p_93958_ - (CommandSuggestions.this.input.isBordered() ? 1 : 0);
            this.rect = new Rect2i(i, j, p_93959_ + 1, Math.min(p_93960_.size(), CommandSuggestions.this.suggestionLineLimit) * 12);
            this.originalContents = CommandSuggestions.this.input.getValue();
            this.lastNarratedEntry = p_93961_ ? -1 : 0;
            this.suggestionList = p_93960_;
            this.select(0);
        }

        public void render(GuiGraphics p_282264_, int p_283591_, int p_283236_) {
            int i = Math.min(this.suggestionList.size(), CommandSuggestions.this.suggestionLineLimit);
            int j = -5592406;
            boolean flag = this.offset > 0;
            boolean flag1 = this.suggestionList.size() > this.offset + i;
            boolean flag2 = flag || flag1;
            boolean flag3 = this.lastMouse.x != (float)p_283591_ || this.lastMouse.y != (float)p_283236_;
            if (flag3) {
                this.lastMouse = new Vec2((float)p_283591_, (float)p_283236_);
            }

            if (flag2) {
                p_282264_.fill(
                    this.rect.getX(),
                    this.rect.getY() - 1,
                    this.rect.getX() + this.rect.getWidth(),
                    this.rect.getY(),
                    CommandSuggestions.this.fillColor
                );
                p_282264_.fill(
                    this.rect.getX(),
                    this.rect.getY() + this.rect.getHeight(),
                    this.rect.getX() + this.rect.getWidth(),
                    this.rect.getY() + this.rect.getHeight() + 1,
                    CommandSuggestions.this.fillColor
                );
                if (flag) {
                    for (int k = 0; k < this.rect.getWidth(); k++) {
                        if (k % 2 == 0) {
                            p_282264_.fill(
                                this.rect.getX() + k, this.rect.getY() - 1, this.rect.getX() + k + 1, this.rect.getY(), -1
                            );
                        }
                    }
                }

                if (flag1) {
                    for (int i1 = 0; i1 < this.rect.getWidth(); i1++) {
                        if (i1 % 2 == 0) {
                            p_282264_.fill(
                                this.rect.getX() + i1,
                                this.rect.getY() + this.rect.getHeight(),
                                this.rect.getX() + i1 + 1,
                                this.rect.getY() + this.rect.getHeight() + 1,
                                -1
                            );
                        }
                    }
                }
            }

            boolean flag4 = false;

            for (int l = 0; l < i; l++) {
                Suggestion suggestion = this.suggestionList.get(l + this.offset);
                p_282264_.fill(
                    this.rect.getX(),
                    this.rect.getY() + 12 * l,
                    this.rect.getX() + this.rect.getWidth(),
                    this.rect.getY() + 12 * l + 12,
                    CommandSuggestions.this.fillColor
                );
                if (p_283591_ > this.rect.getX()
                    && p_283591_ < this.rect.getX() + this.rect.getWidth()
                    && p_283236_ > this.rect.getY() + 12 * l
                    && p_283236_ < this.rect.getY() + 12 * l + 12) {
                    if (flag3) {
                        this.select(l + this.offset);
                    }

                    flag4 = true;
                }

                p_282264_.drawString(
                    CommandSuggestions.this.font,
                    suggestion.getText(),
                    this.rect.getX() + 1,
                    this.rect.getY() + 2 + 12 * l,
                    l + this.offset == this.current ? -256 : -5592406
                );
            }

            if (flag4) {
                Message message = this.suggestionList.get(this.current).getTooltip();
                if (message != null) {
                    p_282264_.renderTooltip(CommandSuggestions.this.font, ComponentUtils.fromMessage(message), p_283591_, p_283236_);
                }
            }
        }

        public boolean mouseClicked(int p_93976_, int p_93977_, int p_93978_) {
            if (!this.rect.contains(p_93976_, p_93977_)) {
                return false;
            } else {
                int i = (p_93977_ - this.rect.getY()) / 12 + this.offset;
                if (i >= 0 && i < this.suggestionList.size()) {
                    this.select(i);
                    this.useSuggestion();
                }

                return true;
            }
        }

        public boolean mouseScrolled(double p_93972_) {
            int i = (int)(
                CommandSuggestions.this.minecraft.mouseHandler.xpos()
                    * (double)CommandSuggestions.this.minecraft.getWindow().getGuiScaledWidth()
                    / (double)CommandSuggestions.this.minecraft.getWindow().getScreenWidth()
            );
            int j = (int)(
                CommandSuggestions.this.minecraft.mouseHandler.ypos()
                    * (double)CommandSuggestions.this.minecraft.getWindow().getGuiScaledHeight()
                    / (double)CommandSuggestions.this.minecraft.getWindow().getScreenHeight()
            );
            if (this.rect.contains(i, j)) {
                this.offset = Mth.clamp((int)((double)this.offset - p_93972_), 0, Math.max(this.suggestionList.size() - CommandSuggestions.this.suggestionLineLimit, 0));
                return true;
            } else {
                return false;
            }
        }

        public boolean keyPressed(int p_93989_, int p_93990_, int p_93991_) {
            if (p_93989_ == 265) {
                this.cycle(-1);
                this.tabCycles = false;
                return true;
            } else if (p_93989_ == 264) {
                this.cycle(1);
                this.tabCycles = false;
                return true;
            } else if (p_93989_ == 258) {
                if (this.tabCycles) {
                    this.cycle(Screen.hasShiftDown() ? -1 : 1);
                }

                this.useSuggestion();
                return true;
            } else if (p_93989_ == 256) {
                CommandSuggestions.this.hide();
                CommandSuggestions.this.input.setSuggestion(null);
                return true;
            } else {
                return false;
            }
        }

        public void cycle(int p_93974_) {
            this.select(this.current + p_93974_);
            int i = this.offset;
            int j = this.offset + CommandSuggestions.this.suggestionLineLimit - 1;
            if (this.current < i) {
                this.offset = Mth.clamp(this.current, 0, Math.max(this.suggestionList.size() - CommandSuggestions.this.suggestionLineLimit, 0));
            } else if (this.current > j) {
                this.offset = Mth.clamp(
                    this.current + CommandSuggestions.this.lineStartOffset - CommandSuggestions.this.suggestionLineLimit,
                    0,
                    Math.max(this.suggestionList.size() - CommandSuggestions.this.suggestionLineLimit, 0)
                );
            }
        }

        public void select(int p_93987_) {
            this.current = p_93987_;
            if (this.current < 0) {
                this.current = this.current + this.suggestionList.size();
            }

            if (this.current >= this.suggestionList.size()) {
                this.current = this.current - this.suggestionList.size();
            }

            Suggestion suggestion = this.suggestionList.get(this.current);
            CommandSuggestions.this.input
                .setSuggestion(CommandSuggestions.calculateSuggestionSuffix(CommandSuggestions.this.input.getValue(), suggestion.apply(this.originalContents)));
            if (this.lastNarratedEntry != this.current) {
                CommandSuggestions.this.minecraft.getNarrator().sayNow(this.getNarrationMessage());
            }
        }

        public void useSuggestion() {
            Suggestion suggestion = this.suggestionList.get(this.current);
            CommandSuggestions.this.keepSuggestions = true;
            CommandSuggestions.this.input.setValue(suggestion.apply(this.originalContents));
            int i = suggestion.getRange().getStart() + suggestion.getText().length();
            CommandSuggestions.this.input.setCursorPosition(i);
            CommandSuggestions.this.input.setHighlightPos(i);
            this.select(this.current);
            CommandSuggestions.this.keepSuggestions = false;
            this.tabCycles = true;
        }

        Component getNarrationMessage() {
            this.lastNarratedEntry = this.current;
            Suggestion suggestion = this.suggestionList.get(this.current);
            Message message = suggestion.getTooltip();
            return message != null
                ? Component.translatable(
                    "narration.suggestion.tooltip", this.current + 1, this.suggestionList.size(), suggestion.getText(), Component.translationArg(message)
                )
                : Component.translatable("narration.suggestion", this.current + 1, this.suggestionList.size(), suggestion.getText());
        }
    }
}