From 43fcde5781f6dfab7164de35a3117a48a6ce8685 Mon Sep 17 00:00:00 2001 From: BONNe Date: Sun, 13 Mar 2022 14:26:56 +0200 Subject: [PATCH] Fixes some small issues with TopLevelPanel Add Utils class that contains some useful things. --- .../bentobox/level/panels/TopLevelPanel.java | 59 +++----------- .../java/world/bentobox/level/util/Utils.java | 76 +++++++++++++++++++ 2 files changed, 85 insertions(+), 50 deletions(-) create mode 100644 src/main/java/world/bentobox/level/util/Utils.java diff --git a/src/main/java/world/bentobox/level/panels/TopLevelPanel.java b/src/main/java/world/bentobox/level/panels/TopLevelPanel.java index c9f5741..23c313a 100644 --- a/src/main/java/world/bentobox/level/panels/TopLevelPanel.java +++ b/src/main/java/world/bentobox/level/panels/TopLevelPanel.java @@ -8,10 +8,9 @@ package world.bentobox.level.panels; import org.bukkit.Material; import org.bukkit.World; -import org.bukkit.permissions.PermissionAttachmentInfo; +import org.bukkit.event.inventory.ClickType; import java.io.File; import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import world.bentobox.bentobox.api.panels.PanelItem; @@ -23,6 +22,7 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.level.Level; +import world.bentobox.level.util.Utils; /** @@ -194,10 +194,13 @@ public class TopLevelPanel { for (ItemTemplateRecord.ActionRecords action : activeActions) { - if (clickType == action.clickType() && "VIEW".equalsIgnoreCase(action.actionType())) + if ((clickType == action.clickType() || action.clickType() == ClickType.UNKNOWN) && + "VIEW".equalsIgnoreCase(action.actionType())) { this.user.closeInventory(); // Open Detailed GUI. + + DetailsPanel.openPanel(this.addon, this.world, this.user); } } @@ -279,7 +282,8 @@ public class TopLevelPanel User owner = island.getOwner() == null ? null : User.getInstance(island.getOwner()); // Get permission or island icon - String permissionIcon = TopLevelPanel.getPermissionValue(owner, this.iconPermission); + String permissionIcon = owner == null ? null : + Utils.getPermissionValue(owner, this.iconPermission, null); Material material; @@ -375,7 +379,7 @@ public class TopLevelPanel "[number]", String.valueOf(index)); String levelText = this.user.getTranslation(reference + "level", - "[number]", String.valueOf(islandTopRecord.level())); + "[number]", this.addon.getManager().formatLevel(islandTopRecord.level())); // Template specific description is always more important than custom one. if (template.description() != null && !template.description().isBlank()) @@ -445,51 +449,6 @@ public class TopLevelPanel } - /** - * This method gets string value of given permission prefix. If user does not have given permission or it have all - * (*), then return default value. - * - * @param user User who's permission should be checked. - * @param permissionPrefix Prefix that need to be found. - * @return String value that follows permissionPrefix. - */ - private static String getPermissionValue(User user, String permissionPrefix) - { - if (user != null && user.isPlayer()) - { - if (permissionPrefix.endsWith(".")) - { - permissionPrefix = permissionPrefix.substring(0, permissionPrefix.length() - 1); - } - - String permPrefix = permissionPrefix + "."; - - List permissions = user.getEffectivePermissions().stream(). - map(PermissionAttachmentInfo::getPermission). - filter(permission -> permission.startsWith(permPrefix)). - collect(Collectors.toList()); - - for (String permission : permissions) - { - if (permission.contains(permPrefix + "*")) - { - // * means all. So continue to search more specific. - continue; - } - - String[] parts = permission.split(permPrefix); - - if (parts.length > 1) - { - return parts[1]; - } - } - } - - return null; - } - - // --------------------------------------------------------------------- // Section: Record // --------------------------------------------------------------------- diff --git a/src/main/java/world/bentobox/level/util/Utils.java b/src/main/java/world/bentobox/level/util/Utils.java new file mode 100644 index 0000000..2c6cd71 --- /dev/null +++ b/src/main/java/world/bentobox/level/util/Utils.java @@ -0,0 +1,76 @@ +// +// Created by BONNe +// Copyright - 2021 +// + + +package world.bentobox.level.util; + + +import org.bukkit.permissions.PermissionAttachmentInfo; +import java.util.List; +import java.util.stream.Collectors; + +import world.bentobox.bentobox.api.user.User; + + +public class Utils +{ + /** + * This method sends a message to the user with appended "prefix" text before message. + * @param user User who receives message. + * @param translationText Translation text of the message. + * @param parameters Parameters for the translation text. + */ + public static void sendMessage(User user, String translationText, String... parameters) + { + user.sendMessage(user.getTranslation( "level.conversations.prefix") + + user.getTranslation( translationText, parameters)); + } + + + /** + * This method gets string value of given permission prefix. If user does not have given permission or it have all + * (*), then return default value. + * + * @param user User who's permission should be checked. + * @param permissionPrefix Prefix that need to be found. + * @param defaultValue Default value that will be returned if permission not found. + * @return String value that follows permissionPrefix. + */ + public static String getPermissionValue(User user, String permissionPrefix, String defaultValue) + { + if (user.isPlayer()) + { + if (permissionPrefix.endsWith(".")) + { + permissionPrefix = permissionPrefix.substring(0, permissionPrefix.length() - 1); + } + + String permPrefix = permissionPrefix + "."; + + List permissions = user.getEffectivePermissions().stream(). + map(PermissionAttachmentInfo::getPermission). + filter(permission -> permission.startsWith(permPrefix)). + collect(Collectors.toList()); + + for (String permission : permissions) + { + if (permission.contains(permPrefix + "*")) + { + // * means all. So continue to search more specific. + continue; + } + + String[] parts = permission.split(permPrefix); + + if (parts.length > 1) + { + return parts[1]; + } + } + } + + return defaultValue; + } +}