1717
1818package com .lambda .mixin .render ;
1919
20+ import com .google .common .base .Strings ;
2021import com .lambda .command .CommandManager ;
22+ import com .lambda .graphics .renderer .gui .font .core .LambdaAtlas ;
23+ import com .lambda .module .modules .client .LambdaMoji ;
24+ import com .lambda .module .modules .client .RenderSettings ;
2125import com .mojang .brigadier .CommandDispatcher ;
26+ import com .mojang .brigadier .suggestion .Suggestions ;
27+ import com .mojang .brigadier .suggestion .SuggestionsBuilder ;
2228import net .minecraft .client .gui .screen .ChatInputSuggestor ;
2329import net .minecraft .client .gui .widget .TextFieldWidget ;
2430import net .minecraft .client .network .ClientPlayNetworkHandler ;
2531import net .minecraft .command .CommandSource ;
32+ import org .jetbrains .annotations .Nullable ;
2633import org .spongepowered .asm .mixin .Final ;
2734import org .spongepowered .asm .mixin .Mixin ;
2835import org .spongepowered .asm .mixin .Shadow ;
36+ import org .spongepowered .asm .mixin .Unique ;
2937import org .spongepowered .asm .mixin .injection .At ;
38+ import org .spongepowered .asm .mixin .injection .Inject ;
3039import org .spongepowered .asm .mixin .injection .ModifyVariable ;
3140import org .spongepowered .asm .mixin .injection .Redirect ;
41+ import org .spongepowered .asm .mixin .injection .callback .CallbackInfo ;
42+
43+ import java .util .concurrent .CompletableFuture ;
44+ import java .util .regex .Matcher ;
45+ import java .util .regex .Pattern ;
46+ import java .util .stream .Stream ;
3247
3348@ Mixin (ChatInputSuggestor .class )
34- public class ChatInputSuggestorMixin {
49+ public abstract class ChatInputSuggestorMixin {
3550
3651 @ Shadow
3752 @ Final
3853 TextFieldWidget textField ;
3954
55+ @ Shadow
56+ private @ Nullable CompletableFuture <Suggestions > pendingSuggestions ;
57+
58+ @ Shadow
59+ public abstract void show (boolean narrateFirstSuggestion );
60+
4061 @ ModifyVariable (method = "refresh" , at = @ At (value = "STORE" ), index = 3 )
4162 private boolean refreshModify (boolean showCompletions ) {
4263 return CommandManager .INSTANCE .isCommand (textField .getText ());
@@ -46,4 +67,55 @@ private boolean refreshModify(boolean showCompletions) {
4667 private CommandDispatcher <CommandSource > refreshRedirect (ClientPlayNetworkHandler instance ) {
4768 return CommandManager .INSTANCE .currentDispatcher (textField .getText ());
4869 }
70+
71+ @ Inject (method = "refresh" , at = @ At ("TAIL" ))
72+ private void refreshEmojiSuggestion (CallbackInfo ci ) {
73+ if (!LambdaMoji .INSTANCE .isEnabled () ||
74+ !LambdaMoji .INSTANCE .getSuggestions ()) return ;
75+
76+ String typing = textField .getText ();
77+
78+ // Don't suggest emojis in commands
79+ if (CommandManager .INSTANCE .isCommand (typing ) ||
80+ CommandManager .INSTANCE .isLambdaCommand (typing )) return ;
81+
82+ int cursor = textField .getCursor ();
83+ String textToCursor = typing .substring (0 , cursor );
84+ if (textToCursor .isEmpty ()) return ;
85+
86+ // Most right index at the left of the regex expression
87+ int start = neoLambda$getLastColon (textToCursor );
88+ if (start == -1 ) return ;
89+
90+ String emojiString = typing .substring (start + 1 );
91+
92+ Stream <String > results = LambdaAtlas .INSTANCE .getKeys (RenderSettings .INSTANCE .getEmojiFont ())
93+ .keySet ().stream ()
94+ .filter (s -> s .startsWith (emojiString ))
95+ .map (s -> s + ":" );
96+
97+ pendingSuggestions = CommandSource .suggestMatching (results , new SuggestionsBuilder (textToCursor , start + 1 ));
98+ pendingSuggestions .thenRun (() -> {
99+ if (!pendingSuggestions .isDone ()) return ;
100+
101+ show (false );
102+ });
103+ }
104+
105+ @ Unique
106+ private static final Pattern COLON_PATTERN = Pattern .compile ("(:[a-zA-Z0-9_]+)" );
107+
108+ @ Unique
109+ private int neoLambda$getLastColon (String input ) {
110+ if (Strings .isNullOrEmpty (input )) return -1 ;
111+
112+ int i = -1 ;
113+ Matcher matcher = COLON_PATTERN .matcher (input );
114+
115+ while (matcher .find ()) {
116+ i = matcher .start ();
117+ }
118+
119+ return i ;
120+ }
49121}
0 commit comments