Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions API/src/main/java/fr/maxlego08/menu/api/button/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,15 @@ public void onRender(Player player, InventoryEngine inventoryEngine) {

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

int[] slots = this.getSlots().stream().map(slot -> {
List<Integer> slotList = new ArrayList<>(this.getSlots());
int[] slots = new int[slotList.size()];
for (int i = 0; i < slotList.size(); i++) {
int slot = slotList.get(i);
if (!this.isPermanent) {
return slot - ((this.getPage() - 1) * inventorySize);
slot -= ((this.getPage() - 1) * inventorySize);
}
return slot;
}).mapToInt(Integer::intValue).toArray();
slots[i] = slot;
}
inventoryEngine.displayFinalButton(this, slots);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,153 @@ public abstract class PermissibleButton extends PerformButton {
private Button elseButton;
private Button parentButton;

/**
* Retrieves the button displayed when the current button does not have the required permissions.
*
* @return the button displayed when the current button does not have the required permissions
*/
public Button getElseButton() {
return this.elseButton;
}

/**
* Sets the button displayed when the current button does not have the required permissions.
*
* @param elseButton the button displayed when the current button does not have the required permissions
*/
public void setElseButton(Button elseButton) {
this.elseButton = elseButton;
}

/**
* Returns true if this button has any permissions (either AND or OR).
*
* @return true if this button has any permissions, false otherwise
*/
public boolean hasPermission() {
return !this.permissions.isEmpty() || !this.orPermissions.isEmpty();
}

/**
* Returns true if this button has an alternative button set using the "elseButton" method.
*
* @return true if this button has an alternative button set using the "elseButton" method, false otherwise
*/
public boolean hasElseButton() {
return this.elseButton != null;
}

/**
* Checks if the player has permission to interact with this button.
* Permissions are checked in the following order:
* <ol>
* <li>OR permissions (if any of the OR permissions are true, the method returns true)</li>
* <li>AND permissions (if all of the AND permissions are true, the method returns true)</li>
* </ol>
* If none of the above conditions are true, the method returns false.
*
* @param player the player to check
* @param inventoryEngine the inventory engine
* @param placeholders the placeholders
* @return true if the player has permission, false otherwise
*/
public boolean checkPermission(Player player, InventoryEngine inventoryEngine, Placeholders placeholders) {

if (!this.orPermissions.isEmpty()) {
return this.orPermissions.stream().anyMatch(p -> p.hasPermission(player, null, inventoryEngine, placeholders));
for (PermissionPermissible permission : this.orPermissions) {
if (permission.hasPermission(player, null, inventoryEngine, placeholders)) {
return true;
}
}
return false;
}

if (!this.permissions.isEmpty()) {
return this.permissions.stream().allMatch(p -> p.hasPermission(player, null, inventoryEngine, placeholders));
for (PermissionPermissible permission : this.permissions) {
if (!permission.hasPermission(player, null, inventoryEngine, placeholders)) {
return false;
}
}
return true;
}

return true;
}

/**
* Retrieves the parent button of this button.
* The parent button is the button that is closest to this button in the
* button hierarchy.
*
* @return the parent button of this button, or null if this button does not have a parent
*/
public Button getParentButton() {
return this.parentButton;
}

/**
* Sets the parent button of this button.
* The parent button is the button that is closest to this button in the
* button hierarchy.
*
* @param parentButton the parent button of this button
*/
public void setParentButton(Button parentButton) {
this.parentButton = parentButton;
}

/**
* Retrieves the master parent button of this button.
* The master parent button is the highest button in the button hierarchy that is not null.
* If this button does not have a parent button, then this button is returned as the master parent button.
*
* @return the master parent button of this button
*/
public Button getMasterParentButton() {
Button button = this.getParentButton();
return button == null ? (Button) this : button.getMasterParentButton();
}

/**
* Retrieves the list of permissions that must be met for this button to be visible.
* This list is used in conjunction with the list of permissions returned by
* {@link #getPermissions()}, and is used to specify alternative permissions
* that can be used to satisfy the visibility requirement of this button.
*
* @return the list of alternative permissions that can be used to satisfy the visibility requirement of this button
*/
public List<PermissionPermissible> getOrPermission() {
return this.orPermissions;
}

/**
* Retrieves the list of permissions that must be met for this button to be visible.
*
* @return the list of permissions that must be met for this button to be visible
*/
public List<PermissionPermissible> getPermissions() {
return this.permissions;
}

/**
* Sets the list of permissions that must be met for this button to be visible.
* This list is used to specify the permissions that must be met for this button to be visible.
* If this list is empty, then this button is always visible.
*
* @param permissions the list of permissions that must be met for this button to be visible
*/
public void setPermissions(List<PermissionPermissible> permissions) {
this.permissions = permissions;
}

/**
* Sets the list of permissions that can be used to satisfy the visibility requirement of this button.
* This list is used in conjunction with the list of permissions returned by
* {@link #getPermissions()}, and is used to specify alternative permissions that can be used to satisfy the visibility requirement of this button.
* 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.
*
* @param orPermissions the list of permissions that can be used to satisfy the visibility requirement of this button
*/
public void setOrPermissions(List<PermissionPermissible> orPermissions) {
this.orPermissions = orPermissions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,78 @@ public abstract class PlaceholderButton extends PermissibleButton {

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

/**
* Retrieves the list of placeholders that must be met for this button to be visible.
*
* @return the list of placeholders that must be met for this button to be visible
*/
public List<PlaceholderPermissible> getPlaceholders() {
return this.placeholders;
}

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

/**
* Returns true if this button has any placeholders, false otherwise.
*
* @return true if this button has any placeholders, false otherwise
*/
public boolean hasPlaceHolder() {
return this.placeholders != null && !this.placeholders.isEmpty();
}

/**
* Returns true if this button has any placeholders or permissions, false otherwise.
* This method will first check if the button has any placeholders, and if so, it will return true.
* If not, it will then check if the button has any permissions using the method from the superclass.
*
* @return true if this button has any placeholders or permissions, false otherwise
*/
@Override
public boolean hasPermission() {
return this.hasPlaceHolder() || super.hasPermission();
}

/**
* Checks if the player has permission to interact with this button.
* Permissions are checked in the following order:
* <ol>
* <li>OR permissions (if any of the OR permissions are true, the method returns true)</li>
* <li>AND permissions (if all of the AND permissions are true, the method returns true)</li>
* <li>Placeholders (if all of the placeholders are true, the method returns true)</li>
* </ol>
* If none of the above conditions are true, the method returns false.
*
* @param player the player to check
* @param inventoryEngine the inventory engine
* @param placeholders the placeholders
* @return true if the player has permission, false otherwise
*/
@Override
public boolean checkPermission(Player player, InventoryEngine inventoryEngine, Placeholders placeholders) {
// First check if player has permission
if (!super.checkPermission(player, inventoryEngine, placeholders)) {
return false;
}

if (this.placeholders.isEmpty()) return true;

// Then we will check if the player to all valid placeholders
return this.placeholders.stream().allMatch(placeholder -> placeholder.hasPermission(player, null, inventoryEngine, placeholders));
for (PlaceholderPermissible placeholder : this.placeholders) {
if (!placeholder.hasPermission(player, null, inventoryEngine, placeholders)) {
return false;
}
}
return true;
}
}
18 changes: 17 additions & 1 deletion API/src/main/java/fr/maxlego08/menu/api/button/SlotButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,34 @@ public abstract class SlotButton {
protected List<Integer> slots;
protected int page;

/**
* Returns the list of slots used by this button.
*
* @return The list of slots used by this button.
*/
public Collection<Integer> getSlots() {
return this.slots;
}

/**
* Sets the list of slots used by this button.
*
* @param slots The list of slots to set.
*/
public void setSlots(List<Integer> slots) {
this.slots = slots;
}

public int getPage() {
return page;
}

/**
* Sets the page number for this button.
* This page number is used to determine which page of an inventory this button should be displayed on.
*
* @param page The page number to set.
*/
public void setPage(int page) {
this.page = page;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ public void removeRequirement(InventoryRequirementType inventoryRequirementType,
}

public boolean canLoad() {
return this.requirements.values().stream().allMatch(List::isEmpty);
for (List<String> names : this.requirements.values()) {
if (!names.isEmpty()) {
return false;
}
}
return true;
}

public Map<InventoryRequirementType, List<String>> getRequirements() {
Expand Down
29 changes: 14 additions & 15 deletions API/src/main/java/fr/maxlego08/menu/api/configuration/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@ConfigDialog(
name = "zMenu Config",
Expand Down Expand Up @@ -332,17 +331,16 @@ public void load(FileConfiguration configuration) {
antiDupeDiscordWebhookUrl = configuration.getString(ConfigPath.ANTI_DUPE_DISCORD_WEBHOOK_URL.getPath());
antiDupeMessage = configuration.getString(ConfigPath.ANTI_DUPE_MESSAGE.getPath());

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

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

List<String> clickTypeNames = allClicksType.stream()
.map(Enum::name)
.collect(Collectors.toList());
List<String> clickTypeNames = new ArrayList<>(allClicksType.size());
for (ClickType clickType : allClicksType) {
clickTypeNames.add(clickType.name());
}
configuration.set(ConfigPath.ALL_CLICKS_TYPE.getPath(), clickTypeNames);

configuration.set(ConfigPath.ENABLE_CACHE_ITEM_STACK.getPath(), enableCacheItemStack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public enum PlaceholderAction {

private final List<String> aliases;

PlaceholderAction() {
this.aliases = new ArrayList<>();
}

PlaceholderAction(String... aliases) {
this.aliases = Arrays.asList(aliases);
}
Expand All @@ -46,9 +42,14 @@ public static PlaceholderAction from(String string) {
if (string == null) return null;

for (PlaceholderAction action : values()) {
if (action.name().equalsIgnoreCase(string) || action.aliases.stream().anyMatch(e -> e.equalsIgnoreCase(string))) {
if (action.name().equalsIgnoreCase(string)) {
return action;
}
for (String alias : action.aliases) {
if (alias.equalsIgnoreCase(string)) {
return action;
}
}
}
System.err.println("Impossible to find the " + string + " action for placeholder");
return null;
Expand Down
Loading
Loading