1
1
package de .siphalor .amecs .gui ;
2
2
3
- import java .lang .reflect .Constructor ;
4
- import java .lang .reflect .InvocationTargetException ;
5
3
import java .util .*;
6
4
7
5
import org .apache .logging .log4j .Level ;
8
6
7
+ import de .klotzi111 .fabricmultiversionhelper .api .text .IMutableText ;
8
+ import de .klotzi111 .fabricmultiversionhelper .api .text .TextWrapper ;
9
9
import de .siphalor .amecs .Amecs ;
10
10
import de .siphalor .amecs .KeyBindingEntryFilterSettings ;
11
11
import de .siphalor .amecs .compat .NMUKProxy ;
12
12
import de .siphalor .amecs .impl .duck .IKeyBindingEntry ;
13
13
import de .siphalor .amecs .impl .duck .IKeybindsScreen ;
14
+ import de .siphalor .amecs .version .KeyBindingEntryVersionHelper ;
15
+ import de .siphalor .amecs .version .ScreenVersionHelper ;
16
+ import de .siphalor .amecs .version .TextFieldWidgetVersionHelper ;
14
17
import net .minecraft .client .MinecraftClient ;
15
18
import net .minecraft .client .font .TextRenderer ;
16
19
import net .minecraft .client .gui .Element ;
17
20
import net .minecraft .client .gui .Selectable ;
18
21
import net .minecraft .client .gui .screen .option .ControlsListWidget ;
22
+ import net .minecraft .client .gui .screen .option .ControlsListWidget .KeyBindingEntry ;
19
23
import net .minecraft .client .gui .screen .option .GameOptionsScreen ;
20
24
import net .minecraft .client .gui .widget .TextFieldWidget ;
21
25
import net .minecraft .client .option .KeyBinding ;
22
26
import net .minecraft .client .resource .language .I18n ;
23
27
import net .minecraft .client .util .math .MatrixStack ;
24
- import net .minecraft .text .BaseText ;
25
- import net .minecraft .text .LiteralText ;
26
- import net .minecraft .text .Text ;
27
- import net .minecraft .text .TranslatableText ;
28
28
import net .minecraft .util .Formatting ;
29
29
30
30
public class SearchFieldControlsListWidget extends ControlsListWidget .Entry {
31
- private static final Constructor <ControlsListWidget .KeyBindingEntry > KeyBindingEntry_contructor ;
32
-
33
- static {
34
- Constructor <ControlsListWidget .KeyBindingEntry > local_KeyBindingEntry_contructor = null ;
35
- try {
36
- // noinspection JavaReflectionMemberAccess
37
- local_KeyBindingEntry_contructor = ControlsListWidget .KeyBindingEntry .class .getDeclaredConstructor (
38
- ControlsListWidget .class , KeyBinding .class , Text .class );
39
- local_KeyBindingEntry_contructor .setAccessible (true );
40
- } catch (NoSuchMethodException | SecurityException e ) {
41
- Amecs .log (Level .ERROR , "Failed to load constructor from class \" KeyBindingEntry\" with reflection" );
42
- e .printStackTrace ();
43
- }
44
-
45
- KeyBindingEntry_contructor = local_KeyBindingEntry_contructor ;
46
- }
47
31
48
32
public final TextFieldWidget textFieldWidget ;
49
33
@@ -63,17 +47,17 @@ private void copyKeyBindingEntrysFromChildren(List<ControlsListWidget.Entry> chi
63
47
}
64
48
65
49
private void recompileChildrenList (ControlsListWidget listWidget , MinecraftClient client ) {
66
- try {
67
- entries .clear ();
68
- KeyBinding [] keyBindings = client .options .allKeys .clone ();
69
- Arrays .sort (keyBindings );
70
- for (KeyBinding keyBinding : keyBindings ) {
71
- ControlsListWidget .KeyBindingEntry entry = KeyBindingEntry_contructor .newInstance (listWidget , keyBinding , new TranslatableText (keyBinding .getTranslationKey ()));
72
- entries .add (entry );
50
+ entries .clear ();
51
+ KeyBinding [] keyBindings = client .options .allKeys .clone ();
52
+ Arrays .sort (keyBindings );
53
+ for (KeyBinding keyBinding : keyBindings ) {
54
+ ControlsListWidget .KeyBindingEntry entry = (KeyBindingEntry ) KeyBindingEntryVersionHelper .createKeyBindingEntry (listWidget , keyBinding , TextWrapper .translatable (keyBinding .getTranslationKey ()));
55
+ if (entry == null ) {
56
+ Amecs .log (Level .ERROR , "An unexpected error occured during recompilation of controls list!" );
57
+ entries .clear ();
58
+ return ;
73
59
}
74
- } catch (InstantiationException | IllegalAccessException | InvocationTargetException e ) {
75
- Amecs .log (Level .ERROR , "An unexpected exception occured during recompilation of controls list!" );
76
- e .printStackTrace ();
60
+ entries .add (entry );
77
61
}
78
62
isFirstCompile = false ;
79
63
}
@@ -95,7 +79,7 @@ private void filterChildrenList(ControlsListWidget listWidget, List<ControlsList
95
79
lastMatched = Amecs .entryMatches (entry , filterSettings );
96
80
if (lastMatched ) {
97
81
if (!Objects .equals (cat , lastCat )) {
98
- children .add (listWidget .new CategoryEntry (new TranslatableText (cat )));
82
+ children .add (listWidget .new CategoryEntry (TextWrapper . translatable (cat )));
99
83
lastCat = cat ;
100
84
}
101
85
@@ -137,7 +121,7 @@ public SearchFieldControlsListWidget(GameOptionsScreen parent, MinecraftClient c
137
121
TextRenderer textRenderer = client .textRenderer ;
138
122
assert parent != null ;
139
123
140
- textFieldWidget = new TextFieldWidget (textRenderer , parent . width / 2 - 125 , 0 , 250 , 20 , new LiteralText ( "" ) );
124
+ textFieldWidget = TextFieldWidgetVersionHelper . createTextFieldWidget (textRenderer , ScreenVersionHelper . getWidth ( parent ) / 2 - 125 , 0 , 250 , 20 , "" );
141
125
textFieldWidget .setSuggestion (I18n .translate ("amecs.search.placeholder" ));
142
126
textFieldWidget .setChangedListener (inputText -> {
143
127
ControlsListWidget listWidget = ((IKeybindsScreen ) parent ).amecs$getControlsList ();
@@ -154,6 +138,7 @@ public SearchFieldControlsListWidget(GameOptionsScreen parent, MinecraftClient c
154
138
155
139
// controls list has more or less children than we had last time
156
140
// TODO: this is not ideal. We might NOT update if for example some external source remove one and adds one entry
141
+ // but external changes SHOULD NOT happen anyways
157
142
if (children .size () != lastChildrenCount ) {
158
143
if (!isFirstCompile ) {
159
144
Amecs .log (Level .INFO , "Controls search results changed externally - recompiling the list!" );
@@ -171,8 +156,8 @@ public SearchFieldControlsListWidget(GameOptionsScreen parent, MinecraftClient c
171
156
lastChildrenCount = children .size ();
172
157
173
158
if (lastChildrenCount <= 1 ) {
174
- BaseText noResultsText = new TranslatableText (Amecs .MOD_ID + ".search.no_results" );
175
- noResultsText .setStyle (noResultsText .getStyle ().withColor (Formatting .GRAY ));
159
+ IMutableText noResultsText = ( IMutableText ) TextWrapper . translatable (Amecs .MOD_ID + ".search.no_results" );
160
+ noResultsText .fmvh$ setStyle (noResultsText .fmvh$ getStyle ().withColor (Formatting .GRAY ));
176
161
children .add (listWidget .new CategoryEntry (noResultsText ));
177
162
lastChildrenCount ++;
178
163
}
0 commit comments