2121import me .juancarloscp52 .entropy .Entropy ;
2222import me .juancarloscp52 .entropy .events .EventRegistry ;
2323import me .juancarloscp52 .entropy .events .EventType ;
24- import me .juancarloscp52 .entropy .mixin .AbstractScrollAreaAccessor ;
2524import net .fabricmc .api .EnvType ;
2625import net .fabricmc .api .Environment ;
2726import net .minecraft .client .Minecraft ;
4140import net .minecraft .resources .ResourceKey ;
4241import net .minecraft .resources .ResourceLocation ;
4342import net .minecraft .sounds .SoundEvents ;
44- import net .minecraft .util .Mth ;
4543
46- import java .util .ArrayList ;
4744import java .util .Comparator ;
4845import java .util .List ;
4946import java .util .Locale ;
47+ import java .util .stream .Stream ;
5048
5149public class EntropyEventListWidget extends ContainerObjectSelectionList <EntropyEventListWidget .ButtonEntry > {
52- public final List <ButtonEntry > visibleEntries = new ArrayList <>() ;
50+ private final List <EventInfo > eventInfos ;
5351 private final Font textRenderer ;
5452
5553 public EntropyEventListWidget (Minecraft minecraftClient , int width , int height , int x , int y , int itemHeight ) {
5654 super (minecraftClient , width , height , y , itemHeight );
5755 this .setX (x );
5856 this .centerListVertically = false ;
5957 this .textRenderer = minecraftClient .font ;
60- }
61-
62- public void addAllFromRegistry () {
63- EventRegistry .EVENTS
58+ eventInfos = EventRegistry .EVENTS
6459 .listElements ()
6560 .map (typeReference -> new EventInfo (Component .translatable (typeReference .value ().getLanguageKey ()).getString (), typeReference ))
6661 .sorted (Comparator .comparing (EventInfo ::name ))
67- .forEach (this ::addEvent );
62+ .toList ();
63+ getEntries ().forEach (this ::addEntry );
6864 }
6965
70- public int addEvent (EventInfo eventInfo ) {
71- return this .addEntry (EntropyEventListWidget .ButtonEntry .create (eventInfo , textRenderer ));
72- }
73-
74- @ Override
75- protected int addEntry (ButtonEntry entry ) {
76- this .visibleEntries .add (entry );
77- return super .addEntry (entry );
66+ public Stream <ButtonEntry > getEntries () {
67+ return eventInfos .stream ().map (eventInfo -> EntropyEventListWidget .ButtonEntry .create (eventInfo , textRenderer ));
7868 }
7969
8070 @ Override
@@ -87,86 +77,17 @@ protected int scrollBarX() {
8777 return super .scrollBarX () + 32 ;
8878 }
8979
90- @ Override
91- protected int getItemCount () {
92- return this .visibleEntries .size ();
93- }
94-
95- @ Override
96- public boolean mouseClicked (MouseButtonEvent event , boolean doubleClick ) {
97- final double mouseX = event .x ();
98- final double mouseY = event .y ();
99- final int button = event .button ();
100- this .updateScrolling (event );
101- if (!this .isMouseOver (mouseX , mouseY )) {
102- return false ;
103- } else {
104- ButtonEntry entry = this .getEntryAtPositionRespectingSearch (mouseX , mouseY );
105- if (entry != null ) {
106- if (entry .mouseClicked (event , doubleClick )) {
107- this .setFocused (entry );
108- this .setDragging (true );
109- return true ;
110- }
111- } else if (button == 0 ) {
112- super .mouseClicked (event , doubleClick );
113- return true ;
114- }
115-
116- return ((AbstractScrollAreaAccessor ) this ).getScrolling ();
117- }
118- }
119-
120- protected ButtonEntry getEntryAtPositionRespectingSearch (double x , double y ) {
121- int i = this .getRowWidth () / 2 ;
122- int j = this .getX () + this .width / 2 ;
123- int k = j - i ;
124- int l = j + i ;
125- int m = Mth .floor (y - (double )this .getY ()) + (int )this .scrollAmount () - 4 ;
126- int n = m / this .defaultEntryHeight ;
127- return x < (double )this .scrollBarX () && x >= (double )k && x <= (double )l && n >= 0 && m >= 0 && n < this .getItemCount () ? this .visibleEntries .get (n ) : null ;
128- }
129-
130- @ Override
131- protected void renderListItems (GuiGraphics drawContext , int mouseX , int mouseY , float delta ) {
132- int entryCount = this .children ().size ();
133- int drawIndex = 0 ;
134-
135- for (int index = 0 ; index < entryCount ; ++index ) {
136- int rowTop = this .getRowTop (drawIndex );
137- int rowBottom = rowTop + this .defaultEntryHeight ;
138-
139- EntropyEventListWidget .ButtonEntry entry = this .children ().get (index );
140- if (entry .checkbox .visible ) {
141- drawIndex ++;
142-
143- if (rowBottom >= this .getY () && rowTop <= this .getBottom ())
144- this .renderItem (drawContext , mouseX , mouseY , delta , entry );
145- }
146- }
147- }
148-
14980 public void updateVisibleEntries (String searchText , FilterMode filterMode ) {
15081 String lowerCasedNewText = searchText .toLowerCase (Locale .ROOT );
151- visibleEntries .clear ();
152-
153- if (searchText .isBlank ())
154- children ().stream ().forEach (buttonEntry -> {
155- buttonEntry .checkbox .visible = filterMode .allowsVisibility (buttonEntry );
156-
157- if (buttonEntry .checkbox .visible )
158- visibleEntries .add (buttonEntry );
159- });
160- else {
161- children ().stream ().forEach (buttonEntry -> {
162- buttonEntry .checkbox .visible = filterMode .allowsVisibility (buttonEntry ) && (buttonEntry .eventInfo .name .toLowerCase (Locale .ROOT ).contains (lowerCasedNewText ) || buttonEntry .eventInfo .typeReference .key ().location ().toString ().contains (lowerCasedNewText ));
82+ Stream <ButtonEntry > buttonEntries = getEntries ().filter (filterMode ::allowsVisibility );
16383
164- if (buttonEntry .checkbox .visible )
165- visibleEntries .add (buttonEntry );
166- });
84+ if (!searchText .isBlank ()) {
85+ buttonEntries = buttonEntries .filter (buttonEntry -> buttonEntry .eventInfo .name .toLowerCase (Locale .ROOT ).contains (lowerCasedNewText ) || buttonEntry .eventInfo .typeReference .key ().location ().toString ().contains (lowerCasedNewText ));
16786 }
16887
16988 setScrollAmount (0.0D );
89+ clearEntries ();
90+ buttonEntries .forEach (this ::addEntry );
17091 }
17192
17293 @ Environment (EnvType .CLIENT )
0 commit comments