1
1
package ca .rttv .chatcalc ;
2
2
3
+ import com .mojang .datafixers .util .Either ;
3
4
import net .fabricmc .loader .api .FabricLoader ;
4
5
import net .minecraft .client .MinecraftClient ;
5
6
import net .minecraft .client .gui .widget .TextFieldWidget ;
7
+ import net .minecraft .text .ClickEvent ;
8
+ import net .minecraft .text .HoverEvent ;
9
+ import net .minecraft .text .MutableText ;
6
10
import net .minecraft .text .Text ;
7
11
import org .jetbrains .annotations .Contract ;
12
+ import org .jetbrains .annotations .NotNull ;
8
13
9
14
import java .util .Optional ;
10
15
import java .util .regex .Pattern ;
11
- import java .util .stream .Collectors ;
12
16
13
17
public class ChatCalc {
14
- public static final Pattern NUMBER = Pattern .compile ("[-+]?\\ d+(\\ .\\ d+)?" );
18
+ public static final Pattern NUMBER = Pattern .compile ("[-+]?(\\ d,?)+(\\ .\\ d+)?" );
19
+ public static final Pattern FUNCTION = Pattern .compile ("[a-zA-Z]+\\ (([a-zA-Z]+,)*?([a-zA-Z]+)\\ )" );
20
+ public static final Pattern CONSTANT = Pattern .compile ("[a-zA-Z]+" );
15
21
public static final String SEPARATOR = ";" ;
16
22
public static final char SEPARATOR_CHAR = ';' ;
17
23
18
24
@ Contract (value = "_->_" , mutates = "param1" )
19
- public static boolean tryParse (TextFieldWidget field ) {
25
+ public static boolean tryParse (@ NotNull TextFieldWidget field ) {
20
26
final MinecraftClient client = MinecraftClient .getInstance ();
21
27
String originalText = field .getText ();
22
28
int cursor = field .getCursor ();
23
- String text = ChatHelper .getWord (originalText , cursor );
29
+ String text = ChatHelper .getSection (originalText , cursor );
24
30
{
25
31
String [] split = text .split ("=" );
26
32
if (split .length == 2 ) {
27
33
if (Config .JSON .has (split [0 ])) {
28
34
Config .JSON .addProperty (split [0 ], split [1 ]);
29
35
Config .refreshJson ();
30
- return ChatHelper .replaceWord (field , "" );
36
+ return ChatHelper .replaceSection (field , "" );
31
37
} else {
32
- Optional <CallableFunction > func = CallableFunction .fromString (text );
33
- if (func .isPresent ()) {
34
- if (func .get ().rest ().isEmpty ()) {
35
- if (Config .FUNCTIONS .containsKey (func .get ().name ())) {
36
- Config .FUNCTIONS .remove (func .get ().name ());
37
- } else {
38
- return false ;
39
- }
40
- } else {
41
- Config .FUNCTIONS .put (func .get ().name (), func .get ());
38
+ Optional <Either <CustomFunction , CustomConstant >> either = parseDeclaration (text );
39
+ if (either .isPresent ()) {
40
+ Optional <CustomFunction > left = either .get ().left ();
41
+ Optional <CustomConstant > right = either .get ().right ();
42
+ if (left .isPresent ()) {
43
+ Config .FUNCTIONS .put (left .get ().name (), left .get ());
44
+ Config .refreshJson ();
45
+ return ChatHelper .replaceSection (field , "" );
46
+ } else if (right .isPresent ()) {
47
+ Config .CONSTANTS .put (right .get ().name (), right .get ());
48
+ Config .refreshJson ();
49
+ return ChatHelper .replaceSection (field , "" );
42
50
}
43
- Config .refreshJson ();
44
- return ChatHelper .replaceWord (field , "" );
45
51
}
46
52
}
47
53
} else if (split .length == 1 ) {
48
54
if (Config .JSON .has (split [0 ])) {
49
- return ChatHelper .replaceWord (field , Config .JSON .get (split [0 ]).getAsString ());
55
+ return ChatHelper .replaceSection (field , Config .JSON .get (split [0 ]).getAsString ());
50
56
} else if (!split [0 ].isEmpty () && Config .JSON .has (split [0 ].substring (0 , split [0 ].length () - 1 )) && split [0 ].endsWith ("?" ) && client .player != null ) {
51
57
client .player .sendMessage (Text .translatable ("chatcalc." + split [0 ].substring (0 , split [0 ].length () - 1 ) + ".description" ));
52
58
return false ;
59
+ } else {
60
+ Optional <Either <CustomFunction , CustomConstant >> either = parseDeclaration (text );
61
+ if (either .isPresent ()) {
62
+ Optional <CustomFunction > left = either .get ().left ();
63
+ Optional <CustomConstant > right = either .get ().right ();
64
+ if (left .isPresent ()) {
65
+ if (Config .FUNCTIONS .containsKey (left .get ().name ())) {
66
+ Config .FUNCTIONS .remove (left .get ().name ());
67
+ Config .refreshJson ();
68
+ return ChatHelper .replaceSection (field , "" );
69
+ }
70
+ } else if (right .isPresent ()) {
71
+ if (Config .CONSTANTS .containsKey (right .get ().name ())) {
72
+ Config .CONSTANTS .remove (right .get ().name ());
73
+ Config .refreshJson ();
74
+ return ChatHelper .replaceSection (field , "" );
75
+ }
76
+ }
77
+ }
53
78
}
54
79
}
55
80
}
@@ -61,7 +86,10 @@ public static boolean tryParse(TextFieldWidget field) {
61
86
Testcases .test (Testcases .TESTCASES );
62
87
return false ;
63
88
} else if (text .equals ("functions?" )) {
64
- client .player .sendMessage (Text .literal ("Currently defined custom functions are:\n " + Config .FUNCTIONS .values ().stream ().map (CallableFunction ::toString ).collect (Collectors .joining ("\n " ))));
89
+ client .player .sendMessage (Config .FUNCTIONS .values ().stream ().map (CustomFunction ::toString ).map (str -> Text .literal (str ).styled (style -> style .withClickEvent (new ClickEvent (ClickEvent .Action .COPY_TO_CLIPBOARD , str )).withHoverEvent (new HoverEvent (HoverEvent .Action .SHOW_TEXT , Text .literal ("Click to copy to clipboard" ))))).collect (() -> Text .literal ("Currently defined custom functions are:" ), (a , b ) -> a .append (Text .literal ("\n " ).append (b )), MutableText ::append ));
90
+ return false ;
91
+ } else if (text .equals ("constants?" )) {
92
+ client .player .sendMessage (Config .CONSTANTS .values ().stream ().map (CustomConstant ::toString ).map (str -> Text .literal (str ).styled (style -> style .withClickEvent (new ClickEvent (ClickEvent .Action .COPY_TO_CLIPBOARD , str )).withHoverEvent (new HoverEvent (HoverEvent .Action .SHOW_TEXT , Text .literal ("Click to copy to clipboard" ))))).collect (() -> Text .literal ("Currently defined custom constants are:" ), (a , b ) -> a .append (Text .literal ("\n " ).append (b )), MutableText ::append ));
65
93
return false ;
66
94
} else if (NUMBER .matcher (text ).matches ()) {
67
95
return false ;
@@ -85,10 +113,14 @@ public static boolean tryParse(TextFieldWidget field) {
85
113
}
86
114
Config .saveToChatHud (originalText );
87
115
Config .saveToClipboard (originalText );
88
- return add ? ChatHelper .addWordAfterIndex (field , solution ) : ChatHelper .replaceWord (field , solution );
116
+ return add ? ChatHelper .addSectionAfterIndex (field , solution ) : ChatHelper .replaceSection (field , solution );
89
117
} catch (Throwable t ) {
90
118
return false ;
91
119
}
92
120
}
93
121
}
122
+
123
+ private static Optional <Either <CustomFunction , CustomConstant >> parseDeclaration (String text ) {
124
+ return CustomFunction .fromString (text ).map (Either ::<CustomFunction , CustomConstant >left ).or (() -> CustomConstant .fromString (text ).map (Either ::<CustomFunction , CustomConstant >right ));
125
+ }
94
126
}
0 commit comments