Skip to content

Commit 7305600

Browse files
committed
Added a menu iterator
1 parent d1be222 commit 7305600

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package me.flame.menus.menu.iterator;
2+
3+
import me.flame.menus.menu.BaseMenu;
4+
import me.flame.menus.menu.Slot;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
/**
8+
*
9+
* @author Mqzen
10+
* @date 28/8/2023
11+
*
12+
* A special class made to iterate over complex menus.
13+
*/
14+
public final class MenuIterator {
15+
16+
@NotNull
17+
private Slot currentPosition;
18+
19+
@NotNull
20+
private final IterationDirection direction;
21+
22+
@NotNull
23+
private final BaseMenu<?> menu;
24+
25+
public MenuIterator(int startingRow, int startingCol,
26+
@NotNull IterationDirection direction,
27+
@NotNull BaseMenu<?> menu) {
28+
this.menu = menu;
29+
this.currentPosition = new Slot(startingRow, startingCol);
30+
this.direction = direction;
31+
}
32+
33+
public Slot nextSlot(boolean emptyOnly) {
34+
35+
36+
if(!emptyOnly) {
37+
Slot newPos = direction.shift(currentPosition, menu.getRows());
38+
currentPosition = newPos;
39+
return newPos;
40+
}
41+
42+
while(menu.getOptionalItem(currentPosition).isPresent()) {
43+
currentPosition = direction.shift(currentPosition, menu.getRows());
44+
}
45+
46+
//when it becomes empty
47+
return currentPosition;
48+
}
49+
50+
public Slot nextSlot() {
51+
return nextSlot(false);
52+
}
53+
54+
public enum IterationDirection {
55+
56+
57+
HORIZONTAL() {
58+
@Override
59+
Slot shift(Slot oldPos, int maxRows) {
60+
61+
int oldCol = oldPos.getColumn();
62+
int oldRow = oldPos.getRow();
63+
64+
if(oldCol == 8 && oldRow < maxRows-1) {
65+
oldRow++;
66+
oldCol = 0;
67+
}else {
68+
oldCol++;
69+
}
70+
71+
return new Slot(oldRow, oldCol);
72+
}
73+
},
74+
75+
VERTICAL() {
76+
@Override
77+
Slot shift(Slot oldPos, int maxRows) {
78+
int oldCol = oldPos.getColumn();
79+
int oldRow = oldPos.getRow();
80+
81+
if(oldCol < 8 && oldRow == maxRows-1) {
82+
oldCol++;
83+
oldRow = 0;
84+
}else {
85+
oldRow++;
86+
}
87+
88+
return new Slot(oldRow, oldCol);
89+
}
90+
},
91+
92+
UPWARDS_ONLY {
93+
94+
@Override
95+
Slot shift(Slot oldPos, int maxRows) {
96+
int row = oldPos.getRow()-1;
97+
if(row < 0) {
98+
row = oldPos.getRow();
99+
}
100+
return new Slot(row, oldPos.getColumn());
101+
}
102+
},
103+
104+
DOWNWARDS_ONLY {
105+
@Override
106+
Slot shift(Slot oldPos, int maxRows) {
107+
int row = oldPos.getRow()+1;
108+
if(row > maxRows) {
109+
row = oldPos.getRow();
110+
}
111+
return new Slot(row, oldPos.getColumn());
112+
}
113+
114+
},
115+
116+
RIGHT_ONLY {
117+
@Override
118+
Slot shift(Slot oldPos, int maxRows) {
119+
int col = oldPos.getColumn()+1;
120+
if(col > 8) {
121+
col = oldPos.getColumn();
122+
}
123+
return new Slot(oldPos.getRow(), col);
124+
}
125+
},
126+
127+
128+
LEFT_ONLY{
129+
@Override
130+
Slot shift(Slot oldPos, int maxRows) {
131+
int col = oldPos.getColumn()-1;
132+
if(col < 0) {
133+
col = oldPos.getColumn();
134+
}
135+
return new Slot(oldPos.getRow(), col);
136+
}
137+
},
138+
139+
140+
RIGHT_UPWARDS_ONLY {
141+
@Override
142+
Slot shift(Slot oldPos, int maxRows) {
143+
Slot upwardSlot = UPWARDS_ONLY.shift(oldPos, maxRows);
144+
int row = upwardSlot.getRow();
145+
146+
Slot rightSlot = RIGHT_ONLY.shift(oldPos, maxRows);
147+
int col = rightSlot.getColumn();
148+
149+
return new Slot(row, col);
150+
}
151+
},
152+
153+
154+
RIGHT_DOWNWARDS_ONLY {
155+
@Override
156+
Slot shift(Slot oldPos, int maxRows) {
157+
Slot downwardSlot = DOWNWARDS_ONLY.shift(oldPos, maxRows);
158+
int row = downwardSlot.getRow();
159+
160+
Slot rightSlot = RIGHT_ONLY.shift(oldPos, maxRows);
161+
int col = rightSlot.getColumn();
162+
163+
return new Slot(row, col);
164+
}
165+
},
166+
167+
LEFT_UPWARDS {
168+
@Override
169+
Slot shift(Slot oldPos, int maxRows) {
170+
Slot upwardSlot = UPWARDS_ONLY.shift(oldPos, maxRows);
171+
int row = upwardSlot.getRow();
172+
173+
Slot leftSlot = LEFT_ONLY.shift(oldPos, maxRows);
174+
int col = leftSlot.getColumn();
175+
176+
return new Slot(row, col);
177+
}
178+
},
179+
180+
LEFT_DOWNWARDS {
181+
@Override
182+
Slot shift(Slot oldPos, int maxRows) {
183+
Slot downwardSlot = DOWNWARDS_ONLY.shift(oldPos, maxRows);
184+
int row = downwardSlot.getRow();
185+
186+
Slot leftSlot = LEFT_ONLY.shift(oldPos, maxRows);
187+
int col = leftSlot.getColumn();
188+
189+
return new Slot(row, col);
190+
}
191+
};
192+
193+
abstract Slot shift(Slot oldPos, int maxRows);
194+
}
195+
196+
197+
}

0 commit comments

Comments
 (0)