8
8
9
9
import java .util .Iterator ;
10
10
import java .util .NoSuchElementException ;
11
+ import java .util .Objects ;
12
+ import java .util .Optional ;
11
13
12
14
/**
13
15
*
14
- * @author Mqzen
16
+ * @author Mqzen, FlameyosFlow (Mostly Mqzen)
15
17
* @date 28/8/2023
16
18
*
17
19
* A special class made to iterate over complex menus.
@@ -38,8 +40,7 @@ public MenuIterator(int startingRow, int startingCol,
38
40
public @ Nullable Slot nextSlot (boolean emptyOnly ) {
39
41
if (!emptyOnly ) {
40
42
Slot newPos = direction .shift (currentPosition , menu .getRows ());
41
- if (newPos .getRow () >= menu .getRows () || newPos .getRow () < 1
42
- || newPos .getColumn () > 9 || newPos .getColumn () < 1 ) {
43
+ if (!currentPosition .isSlot ()) {
43
44
return null ;
44
45
}
45
46
currentPosition = newPos ;
@@ -49,8 +50,7 @@ public MenuIterator(int startingRow, int startingCol,
49
50
while (menu .hasItem (currentPosition )) {
50
51
Slot shiftedPos = direction .shift (currentPosition , menu .getRows ());
51
52
52
- if (shiftedPos .getRow () >= menu .getRows () || shiftedPos .getRow () < 1
53
- || shiftedPos .getColumn () > 9 || shiftedPos .getColumn () < 1 ) {
53
+ if (!currentPosition .isSlot ()) {
54
54
return null ;
55
55
}
56
56
@@ -63,8 +63,7 @@ public MenuIterator(int startingRow, int startingCol,
63
63
64
64
public @ Nullable Slot nextSlot () {
65
65
Slot newPos = direction .shift (currentPosition , menu .getRows ());
66
- if (newPos .getRow () >= menu .getRows () || newPos .getRow () < 1
67
- || newPos .getColumn () > 9 || newPos .getColumn () < 1 ) {
66
+ if (!newPos .isSlot ()) {
68
67
return null ;
69
68
}
70
69
currentPosition = newPos ;
@@ -73,18 +72,25 @@ public MenuIterator(int startingRow, int startingCol,
73
72
74
73
@ Override
75
74
public boolean hasNext () {
76
- return currentPosition .getRow () >= menu .getRows () || currentPosition .getRow () < 1
77
- || currentPosition .getColumn () > 9 || currentPosition .getColumn () < 1 ;
75
+ return currentPosition .isSlot ();
78
76
}
79
77
80
78
@ Override
81
- public MenuItem next () {
82
- Slot slot = nextSlot (false );
79
+ public @ Nullable MenuItem next () {
80
+ Slot slot = nextSlot ();
83
81
if (slot == null )
84
82
throw new NoSuchElementException ("Used MenuIterator#next() but no more items to iterate over" );
85
83
return menu .getItem (slot );
86
84
}
87
85
86
+ public @ NotNull MenuItem nextNotNull () {
87
+ return Objects .requireNonNull (next ());
88
+ }
89
+
90
+ public Optional <MenuItem > nextOptional () {
91
+ return Optional .ofNullable (next ());
92
+ }
93
+
88
94
public enum IterationDirection {
89
95
HORIZONTAL () {
90
96
@ Override
@@ -93,12 +99,8 @@ Slot shift(Slot oldPos, int maxRows) {
93
99
int oldCol = oldPos .getColumn ();
94
100
int oldRow = oldPos .getRow ();
95
101
96
- if (oldCol == 9 && oldRow < maxRows ) {
97
- oldRow ++;
98
- oldCol = 1 ;
99
- } else {
100
- oldCol ++;
101
- }
102
+ oldCol = (oldCol == 9 && oldRow < maxRows ) ? 1 : oldCol + 1 ;
103
+ if (oldCol == 1 ) oldRow ++;
102
104
103
105
return new Slot (oldRow , oldCol );
104
106
}
@@ -142,13 +144,12 @@ Slot shift(Slot oldPos, int maxRows) {
142
144
}
143
145
return new Slot (row , oldPos .getColumn ());
144
146
}
145
-
146
147
},
147
148
148
149
RIGHT_ONLY {
149
150
@ Override
150
151
Slot shift (Slot oldPos , int maxRows ) {
151
- int col = oldPos .getColumn ()+ 1 ;
152
+ int col = oldPos .getColumn () + 1 ;
152
153
if (col > 9 ) {
153
154
col = oldPos .getColumn ();
154
155
}
@@ -157,10 +158,10 @@ Slot shift(Slot oldPos, int maxRows) {
157
158
},
158
159
159
160
160
- LEFT_ONLY {
161
+ LEFT_ONLY {
161
162
@ Override
162
163
Slot shift (Slot oldPos , int maxRows ) {
163
- int col = oldPos .getColumn ()- 1 ;
164
+ int col = oldPos .getColumn () - 1 ;
164
165
if (col < 1 ) {
165
166
col = oldPos .getColumn ();
166
167
}
0 commit comments