-
Notifications
You must be signed in to change notification settings - Fork 2
Iteration
FlameyosFlow edited this page Sep 26, 2023
·
1 revision
MenuIterator is the default Iterator of BaseMenu+ and BaseMenu is an Iterable
Let's see how we can iterate through a Menu for example:
for (MenuItem item : menu) {
...
}
Oh.. yeah let's see a more Interesting reason why MenuIterator exists instead of being a ListIterator
MenuIterator iterator = menu.iterator();
// other iterator variations
iterator = menu.iterator(IterationDirection.VERTICAL);
iterator = menu.iterator(6, 9, IterationDirection.BACKWARDS_HORIZONTAL); // goes 53, 52, 51, 50, 49, etc.
while (iterator.hasNext()) {
Slot slot = iterator.nextSlot();
MenuItem item = menu.getItem(slot);
...
}
// or just normal iteration
while (iterator.hasNext()) {
MenuItem item = iterator.next();
...
}
now THIS is a more fun example of iteration in Woody.
We use a custom iterator so that we can add much more flexibility.
It's the same way, but iterating through certain pages can be a bit challenging.
I'm here to tell you; it's not.
it works the EXACT same way as MenuIterator but it's renamed to PageIterator and internally PageIterator uses Page instead of BaseMenu
PageIterator iterator = menu.getPage(1 /* second */).iterator();
while (iterator.hasNext()) {
MenuItem item = iterator.next();
...
}
// or just, you know
for (MenuItem item : menu.getPage(1)) {
...
}