Skip to content

Commit 5f0a9a8

Browse files
committed
clear currentIndexMap when stopping in ListButton and ListMask
1 parent da71387 commit 5f0a9a8

File tree

2 files changed

+51
-3
lines changed
  • button/src/main/java/io/github/projectunified/craftux/button
  • mask/src/main/java/io/github/projectunified/craftux/mask

2 files changed

+51
-3
lines changed

button/src/main/java/io/github/projectunified/craftux/button/ListButton.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public boolean apply(@NotNull UUID uuid, @NotNull ActionItem actionItem) {
4848
return buttons.get(currentIndexMap.get(uuid)).apply(uuid, actionItem);
4949
}
5050

51-
for (int i = 0; i < getButtons().size(); i++) {
51+
for (int i = 0; i < buttons.size(); i++) {
5252
Button button = buttons.get(i);
5353
if (button.apply(uuid, actionItem)) {
5454
currentIndexMap.put(uuid, i);
@@ -58,4 +58,10 @@ public boolean apply(@NotNull UUID uuid, @NotNull ActionItem actionItem) {
5858

5959
return false;
6060
}
61+
62+
@Override
63+
public void stop() {
64+
super.stop();
65+
currentIndexMap.clear();
66+
}
6167
}

mask/src/main/java/io/github/projectunified/craftux/mask/ListMask.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,62 @@
88

99
import java.util.Map;
1010
import java.util.UUID;
11+
import java.util.concurrent.ConcurrentHashMap;
1112
import java.util.function.Consumer;
1213

1314
/**
1415
* The mask with a list of child masks
1516
*/
1617
public class ListMask extends MultiMask<Mask> {
18+
private final Map<UUID, Integer> currentIndexMap = new ConcurrentHashMap<>();
19+
private boolean keepCurrentIndex = false;
20+
21+
/**
22+
* Should the mask keep the current index for the unique id?
23+
*
24+
* @return true if it should
25+
*/
26+
public boolean isKeepCurrentIndex() {
27+
return keepCurrentIndex;
28+
}
29+
30+
/**
31+
* Should the mask keep the current index for the unique id?
32+
*
33+
* @param keepCurrentIndex true if it should
34+
*/
35+
public void setKeepCurrentIndex(boolean keepCurrentIndex) {
36+
this.keepCurrentIndex = keepCurrentIndex;
37+
}
38+
39+
/**
40+
* Remove the current index for the unique id
41+
*
42+
* @param uuid the unique id
43+
*/
44+
public void removeCurrentIndex(UUID uuid) {
45+
this.currentIndexMap.remove(uuid);
46+
}
47+
1748
@Override
1849
public @Nullable Map<Position, Consumer<ActionItem>> apply(@NotNull UUID uuid) {
19-
for (Mask mask : elements) {
20-
Map<Position, Consumer<ActionItem>> itemMap = mask.apply(uuid);
50+
if (keepCurrentIndex && currentIndexMap.containsKey(uuid)) {
51+
return elements.get(currentIndexMap.get(uuid)).apply(uuid);
52+
}
53+
54+
for (int i = 0; i < elements.size(); i++) {
55+
Map<Position, Consumer<ActionItem>> itemMap = elements.get(i).apply(uuid);
2156
if (itemMap != null) {
57+
currentIndexMap.put(uuid, i);
2258
return itemMap;
2359
}
2460
}
2561
return null;
2662
}
63+
64+
@Override
65+
public void stop() {
66+
super.stop();
67+
currentIndexMap.clear();
68+
}
2769
}

0 commit comments

Comments
 (0)