88package net .wurstclient .clickgui .screens ;
99
1010import java .util .ArrayList ;
11+ import java .util .HashMap ;
12+ import java .util .Map ;
1113
1214import net .minecraft .client .gui .DrawContext ;
1315import net .minecraft .client .gui .screen .Screen ;
@@ -47,6 +49,21 @@ private static final class RowWidgets
4749
4850 private final ArrayList <RowWidgets > rows = new ArrayList <>();
4951
52+ // Persisted scroll per-world+dimension so returning keeps the same
53+ // position even if a new screen instance was created.
54+ private static final Map <String , Integer > savedScrolls = new HashMap <>();
55+
56+ private void saveScrollState ()
57+ {
58+ try
59+ {
60+ String key = resolveWorldId () + ":"
61+ + (filterDim == null ? "ALL" : filterDim .name ());
62+ savedScrolls .put (key , scroll );
63+ }catch (Exception ignored )
64+ {}
65+ }
66+
5067 public WaypointsScreen (Screen prev , WaypointsManager manager )
5168 {
5269 super (Text .literal ("Waypoints" ));
@@ -61,7 +78,9 @@ protected void init()
6178 int x = this .width / 2 - 150 ;
6279
6380 rows .clear ();
64- scroll = 0 ;
81+ // preserve scroll so returning to this screen or refreshing doesn't
82+ // jump the list back to the top
83+ // scroll = 0;
6584
6685 // Initialize default filter to current world if not set
6786 if (filterDim == null )
@@ -149,6 +168,17 @@ protected void init()
149168 viewportTop = listStartY ;
150169 viewportBottom = this .height - 36 ; // a bit above back button
151170
171+ // Try to restore a previously saved scroll position for this world+dim
172+ try
173+ {
174+ String key = resolveWorldId () + ":"
175+ + (filterDim == null ? "ALL" : filterDim .name ());
176+ Integer s = savedScrolls .get (key );
177+ if (s != null )
178+ scroll = s ;
179+ }catch (Exception ignored )
180+ {}
181+
152182 for (int i = 0 ; i < cachedList .size (); i ++)
153183 {
154184 Waypoint w = cachedList .get (i );
@@ -193,6 +223,14 @@ protected void init()
193223 rows .add (rw );
194224 }
195225
226+ // Clamp scroll so it stays within valid bounds after rebuilding the
227+ // list
228+ int contentHeight = rows .size () * ROW_HEIGHT ;
229+ int maxScroll =
230+ Math .max (0 , contentHeight - (viewportBottom - viewportTop ));
231+ scroll = Math .max (0 , Math .min (scroll , maxScroll ));
232+ // Persist the (possibly adjusted) scroll position
233+ saveScrollState ();
196234 // Scroll buttons (▲ / ▼) positioned just to the right of the 300px list
197235 // area
198236 int arrowX = x + 305 ; // a little to the right of the list
@@ -225,6 +263,7 @@ private void scrollBy(int dy)
225263 int maxScroll =
226264 Math .max (0 , contentHeight - (viewportBottom - viewportTop ));
227265 scroll = Math .max (0 , Math .min (scroll + dy , maxScroll ));
266+ saveScrollState ();
228267 }
229268
230269 @ Override
@@ -420,6 +459,7 @@ void saveNow()
420459 private void scrollToTop ()
421460 {
422461 scroll = 0 ;
462+ saveScrollState ();
423463 }
424464
425465 private void scrollToBottom ()
@@ -428,5 +468,6 @@ private void scrollToBottom()
428468 int maxScroll =
429469 Math .max (0 , contentHeight - (viewportBottom - viewportTop ));
430470 scroll = maxScroll ;
471+ saveScrollState ();
431472 }
432473}
0 commit comments