Skip to content

Commit 432c1a3

Browse files
authored
Merge pull request #202 from Maxlego08/codex/optimize-loops-in-zmenu
Replace stream usage with loops for performance
2 parents ee7a456 + 59251c0 commit 432c1a3

50 files changed

Lines changed: 897 additions & 277 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

API/src/main/java/fr/maxlego08/menu/api/button/Button.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,15 @@ public void onRender(Player player, InventoryEngine inventoryEngine) {
144144

145145
int inventorySize = this.isPlayerInventory() ? 36 : inventoryEngine.getInventory().getSize();
146146

147-
int[] slots = this.getSlots().stream().map(slot -> {
147+
List<Integer> slotList = new ArrayList<>(this.getSlots());
148+
int[] slots = new int[slotList.size()];
149+
for (int i = 0; i < slotList.size(); i++) {
150+
int slot = slotList.get(i);
148151
if (!this.isPermanent) {
149-
return slot - ((this.getPage() - 1) * inventorySize);
152+
slot -= ((this.getPage() - 1) * inventorySize);
150153
}
151-
return slot;
152-
}).mapToInt(Integer::intValue).toArray();
154+
slots[i] = slot;
155+
}
153156
inventoryEngine.displayFinalButton(this, slots);
154157
}
155158
}

API/src/main/java/fr/maxlego08/menu/api/button/PermissibleButton.java

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,153 @@ public abstract class PermissibleButton extends PerformButton {
1515
private Button elseButton;
1616
private Button parentButton;
1717

18+
/**
19+
* Retrieves the button displayed when the current button does not have the required permissions.
20+
*
21+
* @return the button displayed when the current button does not have the required permissions
22+
*/
1823
public Button getElseButton() {
1924
return this.elseButton;
2025
}
2126

27+
/**
28+
* Sets the button displayed when the current button does not have the required permissions.
29+
*
30+
* @param elseButton the button displayed when the current button does not have the required permissions
31+
*/
2232
public void setElseButton(Button elseButton) {
2333
this.elseButton = elseButton;
2434
}
2535

36+
/**
37+
* Returns true if this button has any permissions (either AND or OR).
38+
*
39+
* @return true if this button has any permissions, false otherwise
40+
*/
2641
public boolean hasPermission() {
2742
return !this.permissions.isEmpty() || !this.orPermissions.isEmpty();
2843
}
2944

45+
/**
46+
* Returns true if this button has an alternative button set using the "elseButton" method.
47+
*
48+
* @return true if this button has an alternative button set using the "elseButton" method, false otherwise
49+
*/
3050
public boolean hasElseButton() {
3151
return this.elseButton != null;
3252
}
3353

54+
/**
55+
* Checks if the player has permission to interact with this button.
56+
* Permissions are checked in the following order:
57+
* <ol>
58+
* <li>OR permissions (if any of the OR permissions are true, the method returns true)</li>
59+
* <li>AND permissions (if all of the AND permissions are true, the method returns true)</li>
60+
* </ol>
61+
* If none of the above conditions are true, the method returns false.
62+
*
63+
* @param player the player to check
64+
* @param inventoryEngine the inventory engine
65+
* @param placeholders the placeholders
66+
* @return true if the player has permission, false otherwise
67+
*/
3468
public boolean checkPermission(Player player, InventoryEngine inventoryEngine, Placeholders placeholders) {
3569

3670
if (!this.orPermissions.isEmpty()) {
37-
return this.orPermissions.stream().anyMatch(p -> p.hasPermission(player, null, inventoryEngine, placeholders));
71+
for (PermissionPermissible permission : this.orPermissions) {
72+
if (permission.hasPermission(player, null, inventoryEngine, placeholders)) {
73+
return true;
74+
}
75+
}
76+
return false;
3877
}
3978

4079
if (!this.permissions.isEmpty()) {
41-
return this.permissions.stream().allMatch(p -> p.hasPermission(player, null, inventoryEngine, placeholders));
80+
for (PermissionPermissible permission : this.permissions) {
81+
if (!permission.hasPermission(player, null, inventoryEngine, placeholders)) {
82+
return false;
83+
}
84+
}
85+
return true;
4286
}
4387

4488
return true;
4589
}
4690

91+
/**
92+
* Retrieves the parent button of this button.
93+
* The parent button is the button that is closest to this button in the
94+
* button hierarchy.
95+
*
96+
* @return the parent button of this button, or null if this button does not have a parent
97+
*/
4798
public Button getParentButton() {
4899
return this.parentButton;
49100
}
50101

102+
/**
103+
* Sets the parent button of this button.
104+
* The parent button is the button that is closest to this button in the
105+
* button hierarchy.
106+
*
107+
* @param parentButton the parent button of this button
108+
*/
51109
public void setParentButton(Button parentButton) {
52110
this.parentButton = parentButton;
53111
}
54112

113+
/**
114+
* Retrieves the master parent button of this button.
115+
* The master parent button is the highest button in the button hierarchy that is not null.
116+
* If this button does not have a parent button, then this button is returned as the master parent button.
117+
*
118+
* @return the master parent button of this button
119+
*/
55120
public Button getMasterParentButton() {
56121
Button button = this.getParentButton();
57122
return button == null ? (Button) this : button.getMasterParentButton();
58123
}
59124

125+
/**
126+
* Retrieves the list of permissions that must be met for this button to be visible.
127+
* This list is used in conjunction with the list of permissions returned by
128+
* {@link #getPermissions()}, and is used to specify alternative permissions
129+
* that can be used to satisfy the visibility requirement of this button.
130+
*
131+
* @return the list of alternative permissions that can be used to satisfy the visibility requirement of this button
132+
*/
60133
public List<PermissionPermissible> getOrPermission() {
61134
return this.orPermissions;
62135
}
63136

137+
/**
138+
* Retrieves the list of permissions that must be met for this button to be visible.
139+
*
140+
* @return the list of permissions that must be met for this button to be visible
141+
*/
64142
public List<PermissionPermissible> getPermissions() {
65143
return this.permissions;
66144
}
67145

146+
/**
147+
* Sets the list of permissions that must be met for this button to be visible.
148+
* This list is used to specify the permissions that must be met for this button to be visible.
149+
* If this list is empty, then this button is always visible.
150+
*
151+
* @param permissions the list of permissions that must be met for this button to be visible
152+
*/
68153
public void setPermissions(List<PermissionPermissible> permissions) {
69154
this.permissions = permissions;
70155
}
71156

157+
/**
158+
* Sets the list of permissions that can be used to satisfy the visibility requirement of this button.
159+
* This list is used in conjunction with the list of permissions returned by
160+
* {@link #getPermissions()}, and is used to specify alternative permissions that can be used to satisfy the visibility requirement of this button.
161+
* If this list is empty, then the list of permissions returned by {@link #getPermissions()} is used exclusively to satisfy the visibility requirement of this button.
162+
*
163+
* @param orPermissions the list of permissions that can be used to satisfy the visibility requirement of this button
164+
*/
72165
public void setOrPermissions(List<PermissionPermissible> orPermissions) {
73166
this.orPermissions = orPermissions;
74167
}

API/src/main/java/fr/maxlego08/menu/api/button/PlaceholderButton.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,78 @@ public abstract class PlaceholderButton extends PermissibleButton {
1212

1313
private List<PlaceholderPermissible> placeholders = new ArrayList<>();
1414

15+
/**
16+
* Retrieves the list of placeholders that must be met for this button to be visible.
17+
*
18+
* @return the list of placeholders that must be met for this button to be visible
19+
*/
1520
public List<PlaceholderPermissible> getPlaceholders() {
1621
return this.placeholders;
1722
}
1823

24+
/**
25+
* Sets the list of placeholders that must be met for this button to be visible.
26+
* This list is used in conjunction with the list of permissions returned by
27+
* {@link #getPermissions()}, and is used to specify alternative placeholders
28+
* that can be used to satisfy the visibility requirement of this button.
29+
*
30+
* @param placeholders the list of placeholders that must be met for this button to be visible
31+
*/
1932
public void setPlaceholders(List<PlaceholderPermissible> placeholders) {
2033
this.placeholders = placeholders;
2134
}
2235

36+
/**
37+
* Returns true if this button has any placeholders, false otherwise.
38+
*
39+
* @return true if this button has any placeholders, false otherwise
40+
*/
2341
public boolean hasPlaceHolder() {
2442
return this.placeholders != null && !this.placeholders.isEmpty();
2543
}
2644

45+
/**
46+
* Returns true if this button has any placeholders or permissions, false otherwise.
47+
* This method will first check if the button has any placeholders, and if so, it will return true.
48+
* If not, it will then check if the button has any permissions using the method from the superclass.
49+
*
50+
* @return true if this button has any placeholders or permissions, false otherwise
51+
*/
2752
@Override
2853
public boolean hasPermission() {
2954
return this.hasPlaceHolder() || super.hasPermission();
3055
}
3156

57+
/**
58+
* Checks if the player has permission to interact with this button.
59+
* Permissions are checked in the following order:
60+
* <ol>
61+
* <li>OR permissions (if any of the OR permissions are true, the method returns true)</li>
62+
* <li>AND permissions (if all of the AND permissions are true, the method returns true)</li>
63+
* <li>Placeholders (if all of the placeholders are true, the method returns true)</li>
64+
* </ol>
65+
* If none of the above conditions are true, the method returns false.
66+
*
67+
* @param player the player to check
68+
* @param inventoryEngine the inventory engine
69+
* @param placeholders the placeholders
70+
* @return true if the player has permission, false otherwise
71+
*/
3272
@Override
3373
public boolean checkPermission(Player player, InventoryEngine inventoryEngine, Placeholders placeholders) {
3474
// First check if player has permission
3575
if (!super.checkPermission(player, inventoryEngine, placeholders)) {
3676
return false;
3777
}
3878

79+
if (this.placeholders.isEmpty()) return true;
80+
3981
// Then we will check if the player to all valid placeholders
40-
return this.placeholders.stream().allMatch(placeholder -> placeholder.hasPermission(player, null, inventoryEngine, placeholders));
82+
for (PlaceholderPermissible placeholder : this.placeholders) {
83+
if (!placeholder.hasPermission(player, null, inventoryEngine, placeholders)) {
84+
return false;
85+
}
86+
}
87+
return true;
4188
}
4289
}

API/src/main/java/fr/maxlego08/menu/api/button/SlotButton.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,34 @@ public abstract class SlotButton {
88
protected List<Integer> slots;
99
protected int page;
1010

11+
/**
12+
* Returns the list of slots used by this button.
13+
*
14+
* @return The list of slots used by this button.
15+
*/
1116
public Collection<Integer> getSlots() {
1217
return this.slots;
1318
}
1419

20+
/**
21+
* Sets the list of slots used by this button.
22+
*
23+
* @param slots The list of slots to set.
24+
*/
1525
public void setSlots(List<Integer> slots) {
1626
this.slots = slots;
1727
}
18-
28+
1929
public int getPage() {
2030
return page;
2131
}
2232

33+
/**
34+
* Sets the page number for this button.
35+
* This page number is used to determine which page of an inventory this button should be displayed on.
36+
*
37+
* @param page The page number to set.
38+
*/
2339
public void setPage(int page) {
2440
this.page = page;
2541
}

API/src/main/java/fr/maxlego08/menu/api/checker/InventoryLoadRequirement.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ public void removeRequirement(InventoryRequirementType inventoryRequirementType,
5353
}
5454

5555
public boolean canLoad() {
56-
return this.requirements.values().stream().allMatch(List::isEmpty);
56+
for (List<String> names : this.requirements.values()) {
57+
if (!names.isEmpty()) {
58+
return false;
59+
}
60+
}
61+
return true;
5762
}
5863

5964
public Map<InventoryRequirementType, List<String>> getRequirements() {

API/src/main/java/fr/maxlego08/menu/api/configuration/Config.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.Arrays;
1515
import java.util.List;
1616
import java.util.Objects;
17-
import java.util.stream.Collectors;
1817

1918
@ConfigDialog(
2019
name = "zMenu Config",
@@ -332,17 +331,16 @@ public void load(FileConfiguration configuration) {
332331
antiDupeDiscordWebhookUrl = configuration.getString(ConfigPath.ANTI_DUPE_DISCORD_WEBHOOK_URL.getPath());
333332
antiDupeMessage = configuration.getString(ConfigPath.ANTI_DUPE_MESSAGE.getPath());
334333

335-
allClicksType = configuration.getStringList(ConfigPath.ALL_CLICKS_TYPE.getPath()).stream()
336-
.map(name -> {
337-
try {
338-
return ClickType.valueOf(name);
339-
} catch (IllegalArgumentException e) {
340-
Bukkit.getLogger().warning("[zMenu] Invalid click type in config: " + name);
341-
return null;
342-
}
343-
})
344-
.filter(Objects::nonNull)
345-
.collect(Collectors.toList());
334+
List<String> clickTypeStrings = configuration.getStringList(ConfigPath.ALL_CLICKS_TYPE.getPath());
335+
List<ClickType> clickTypes = new ArrayList<>(clickTypeStrings.size());
336+
for (String name : clickTypeStrings) {
337+
try {
338+
clickTypes.add(ClickType.valueOf(name));
339+
} catch (IllegalArgumentException e) {
340+
Bukkit.getLogger().warning("[zMenu] Invalid click type in config: " + name);
341+
}
342+
}
343+
allClicksType = clickTypes;
346344

347345
enableCacheItemStack = configuration.getBoolean(ConfigPath.ENABLE_CACHE_ITEM_STACK.getPath());
348346
enableCooldownClick = configuration.getBoolean(ConfigPath.ENABLE_COOLDOWN_CLICK.getPath());
@@ -386,9 +384,10 @@ public void save(FileConfiguration configuration, File file) {
386384
configuration.set(ConfigPath.ANTI_DUPE_DISCORD_WEBHOOK_URL.getPath(), antiDupeDiscordWebhookUrl);
387385
configuration.set(ConfigPath.ANTI_DUPE_MESSAGE.getPath(), antiDupeMessage);
388386

389-
List<String> clickTypeNames = allClicksType.stream()
390-
.map(Enum::name)
391-
.collect(Collectors.toList());
387+
List<String> clickTypeNames = new ArrayList<>(allClicksType.size());
388+
for (ClickType clickType : allClicksType) {
389+
clickTypeNames.add(clickType.name());
390+
}
392391
configuration.set(ConfigPath.ALL_CLICKS_TYPE.getPath(), clickTypeNames);
393392

394393
configuration.set(ConfigPath.ENABLE_CACHE_ITEM_STACK.getPath(), enableCacheItemStack);

API/src/main/java/fr/maxlego08/menu/api/enums/PlaceholderAction.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ public enum PlaceholderAction {
2828

2929
private final List<String> aliases;
3030

31-
PlaceholderAction() {
32-
this.aliases = new ArrayList<>();
33-
}
34-
3531
PlaceholderAction(String... aliases) {
3632
this.aliases = Arrays.asList(aliases);
3733
}
@@ -46,9 +42,14 @@ public static PlaceholderAction from(String string) {
4642
if (string == null) return null;
4743

4844
for (PlaceholderAction action : values()) {
49-
if (action.name().equalsIgnoreCase(string) || action.aliases.stream().anyMatch(e -> e.equalsIgnoreCase(string))) {
45+
if (action.name().equalsIgnoreCase(string)) {
5046
return action;
5147
}
48+
for (String alias : action.aliases) {
49+
if (alias.equalsIgnoreCase(string)) {
50+
return action;
51+
}
52+
}
5253
}
5354
System.err.println("Impossible to find the " + string + " action for placeholder");
5455
return null;

0 commit comments

Comments
 (0)