From ace31c292e94af64cf81c0f44eff5d1c516d1922 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 25 Apr 2024 16:13:27 -0700 Subject: [PATCH] WIP for #338 enable viewing of challenges by admins. --- .../admin/ChallengesAdminCommand.java | 40 +- .../challenges/panel/CommonPagedPanel.java | 2 +- .../challenges/panel/CommonPanel.java | 327 ++++++------- .../challenges/panel/admin/AdminPanel.java | 5 +- .../panel/admin/EditChallengePanel.java | 2 +- .../panel/admin/EditSettingsPanel.java | 2 +- .../panel/user/ChallengesPanel.java | 428 ++++++++++-------- .../challenges/panel/user/GameModePanel.java | 2 +- 8 files changed, 434 insertions(+), 374 deletions(-) diff --git a/src/main/java/world/bentobox/challenges/commands/admin/ChallengesAdminCommand.java b/src/main/java/world/bentobox/challenges/commands/admin/ChallengesAdminCommand.java index 57f9efcf..b28d371c 100644 --- a/src/main/java/world/bentobox/challenges/commands/admin/ChallengesAdminCommand.java +++ b/src/main/java/world/bentobox/challenges/commands/admin/ChallengesAdminCommand.java @@ -2,10 +2,12 @@ import java.util.List; +import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.panel.admin.AdminPanel; +import world.bentobox.challenges.panel.user.ChallengesPanel; public class ChallengesAdminCommand extends CompositeCommand @@ -19,9 +21,9 @@ public class ChallengesAdminCommand extends CompositeCommand public ChallengesAdminCommand(ChallengesAddon addon, CompositeCommand parent) { super(addon, - parent, - addon.getChallengesSettings().getAdminMainCommand().split(" ")[0], - addon.getChallengesSettings().getAdminMainCommand().split(" ")); + parent, + addon.getChallengesSettings().getAdminMainCommand().split(" ")[0], + addon.getChallengesSettings().getAdminMainCommand().split(" ")); } @@ -52,16 +54,36 @@ public void setup() @Override public boolean execute(User user, String label, List args) { + BentoBox.getInstance().logDebug("Args size = " + args.size()); // Open up the admin challenges GUI if (user.isPlayer()) { - AdminPanel.open(this.getAddon(), - this.getWorld(), - user, - this.getTopLabel(), - this.getPermissionPrefix()); + if (args.isEmpty()) { + AdminPanel.open(this.getAddon(), + this.getWorld(), + user, + this.getTopLabel(), + this.getPermissionPrefix()); - return true; + return true; + } else if (args.size() == 1) { + User target = getPlayers().getUser(args.get(0).trim()); + if (target == null) { + user.sendMessage("general.errors.unknown-player"); + return false; + } + ChallengesPanel.view(this.getAddon(), + this.getWorld(), + target, + user, + this.getTopLabel(), + this.getPermissionPrefix()); + + return true; + } + // Show help + showHelp(this, user); + return false; } return false; } diff --git a/src/main/java/world/bentobox/challenges/panel/CommonPagedPanel.java b/src/main/java/world/bentobox/challenges/panel/CommonPagedPanel.java index 947c10e5..1f16ce6a 100644 --- a/src/main/java/world/bentobox/challenges/panel/CommonPagedPanel.java +++ b/src/main/java/world/bentobox/challenges/panel/CommonPagedPanel.java @@ -42,7 +42,7 @@ protected CommonPagedPanel(ChallengesAddon addon, User user, World world, String topLabel, String permissionPrefix) { - super(addon, user, world, topLabel, permissionPrefix); + super(addon, user, user, world, topLabel, permissionPrefix); } diff --git a/src/main/java/world/bentobox/challenges/panel/CommonPanel.java b/src/main/java/world/bentobox/challenges/panel/CommonPanel.java index 6709d617..7d94bf03 100644 --- a/src/main/java/world/bentobox/challenges/panel/CommonPanel.java +++ b/src/main/java/world/bentobox/challenges/panel/CommonPanel.java @@ -38,28 +38,80 @@ * This class contains common methods for all panels. */ public abstract class CommonPanel { + + // --------------------------------------------------------------------- + // Section: Variables + // --------------------------------------------------------------------- + + /** + * This variable stores parent gui. + */ + @Nullable + protected final CommonPanel parentPanel; + + /** + * Variable stores Challenges addon. + */ + protected final ChallengesAddon addon; + + /** + * Variable stores Challenges addon manager. + */ + protected final ChallengesManager manager; + + /** + * Variable stores world in which panel is referred to. + */ + protected final World world; + + /** + * Variable stores user who created this panel. + */ + protected final User user; + + /** + * Variable stores top label of command from which panel was called. + */ + protected final String topLabel; + + /** + * Variable stores permission prefix of command from which panel was called. + */ + protected final String permissionPrefix; + + /** + * This object holds PanelItem that allows to return to previous panel. + */ + protected PanelItem returnButton; + + /** + * The panel viewer + */ + protected User viewer; + /** * This is default constructor for all classes that extends CommonPanel. * * @param addon ChallengesAddon instance. * @param user User who opens panel. */ - protected CommonPanel(ChallengesAddon addon, User user, World world, String topLabel, String permissionPrefix) { + protected CommonPanel(ChallengesAddon addon, User user, User viewer, World world, String topLabel, String permissionPrefix) { this.addon = addon; this.world = world; this.manager = addon.getChallengesManager(); this.user = user; + this.viewer = viewer; this.topLabel = topLabel; this.permissionPrefix = permissionPrefix; this.parentPanel = null; - this.returnButton = new PanelItemBuilder().name(this.user.getTranslation(Constants.BUTTON + "quit.name")) - .description(this.user.getTranslationOrNothing(Constants.BUTTON + "quit.description")).description("") - .description(this.user.getTranslationOrNothing(Constants.TIPS + "click-to-quit")) + this.returnButton = new PanelItemBuilder().name(this.viewer.getTranslation(Constants.BUTTON + "quit.name")) + .description(this.viewer.getTranslationOrNothing(Constants.BUTTON + "quit.description")).description("") + .description(this.viewer.getTranslationOrNothing(Constants.TIPS + "click-to-quit")) .icon(Material.OAK_DOOR).clickHandler((panel, user1, clickType, i) -> { - this.user.closeInventory(); + this.viewer.closeInventory(); return true; }).build(); } @@ -73,6 +125,7 @@ protected CommonPanel(@NonNull CommonPanel parentPanel) { this.addon = parentPanel.addon; this.manager = parentPanel.manager; this.user = parentPanel.user; + this.viewer = parentPanel.viewer; this.world = parentPanel.world; this.topLabel = parentPanel.topLabel; @@ -80,9 +133,9 @@ protected CommonPanel(@NonNull CommonPanel parentPanel) { this.parentPanel = parentPanel; - this.returnButton = new PanelItemBuilder().name(this.user.getTranslation(Constants.BUTTON + "return.name")) - .description(this.user.getTranslationOrNothing(Constants.BUTTON + "return.description")).description("") - .description(this.user.getTranslationOrNothing(Constants.TIPS + "click-to-return")) + this.returnButton = new PanelItemBuilder().name(this.viewer.getTranslation(Constants.BUTTON + "return.name")) + .description(this.viewer.getTranslationOrNothing(Constants.BUTTON + "return.description")).description("") + .description(this.viewer.getTranslationOrNothing(Constants.TIPS + "click-to-return")) .icon(Material.OAK_DOOR).clickHandler((panel, user1, clickType, i) -> { this.parentPanel.build(); return true; @@ -131,7 +184,7 @@ protected List generateChallengeDescription(Challenge challenge, @Nullab final String reference = Constants.DESCRIPTIONS + "challenge."; // Get description from custom translations - String description = this.user + String description = this.viewer .getTranslationOrNothing("challenges.challenges." + challenge.getUniqueId() + ".description"); if (description.isEmpty()) { @@ -148,11 +201,11 @@ protected List generateChallengeDescription(Challenge challenge, @Nullab String requirements = isCompletedAll ? "" : this.generateRequirements(challenge, target); // Get rewards in single string String rewards = isCompletedAll ? "" : this.generateRewards(challenge, isCompletedOnce); - // Get coolDown in singe string + // Get coolDown in single string String coolDown = isCompletedAll || challenge.getTimeout() <= 0 ? "" : this.generateCoolDown(challenge, target); if (!description.replaceAll("(?m)^[ \\t]*\\r?\\n", "").isEmpty()) { - String returnString = this.user.getTranslationOrNothing(reference + "lore", "[requirements]", requirements, + String returnString = this.viewer.getTranslationOrNothing(reference + "lore", "[requirements]", requirements, "[rewards]", rewards, "[status]", status, "[cooldown]", coolDown); // remove empty lines from the generated text. @@ -169,7 +222,7 @@ protected List generateChallengeDescription(Challenge challenge, @Nullab return collect; } else { - String returnString = this.user.getTranslationOrNothing(reference + "lore", Constants.PARAMETER_DESCRIPTION, + String returnString = this.viewer.getTranslationOrNothing(reference + "lore", Constants.PARAMETER_DESCRIPTION, description, "[requirements]", requirements, "[rewards]", rewards, "[status]", status, "[cooldown]", coolDown); @@ -196,16 +249,16 @@ private String generateCoolDown(Challenge challenge, @Nullable User target) { long missing = this.manager.getLastCompletionDate(this.user, this.world, challenge) + challenge.getTimeout() - System.currentTimeMillis(); - coolDown = this.user.getTranslation(reference + "wait-time", "[time]", - Utils.parseDuration(Duration.ofMillis(missing), this.user)); + coolDown = this.viewer.getTranslation(reference + "wait-time", "[time]", + Utils.parseDuration(Duration.ofMillis(missing), this.viewer)); } else { coolDown = ""; } - String timeout = this.user.getTranslation(reference + "timeout", "[time]", - Utils.parseDuration(Duration.ofMillis(challenge.getTimeout()), this.user)); + String timeout = this.viewer.getTranslation(reference + "timeout", "[time]", + Utils.parseDuration(Duration.ofMillis(challenge.getTimeout()), this.viewer)); - return this.user.getTranslation(reference + "lore", "[timeout]", timeout, "[wait-time]", coolDown); + return this.viewer.getTranslation(reference + "lore", "[timeout]", timeout, "[wait-time]", coolDown); } /** @@ -223,16 +276,16 @@ private String generateRequirements(Challenge challenge, @Nullable User target) // If challenge can be completed everywhere, do not display requirement. environment = ""; } else if (challenge.getEnvironment().size() == 1) { - environment = this.user.getTranslationOrNothing(reference + "environment-single", + environment = this.viewer.getTranslationOrNothing(reference + "environment-single", Constants.PARAMETER_ENVIRONMENT, - Utils.prettifyObject(challenge.getEnvironment().iterator().next(), this.user)); + Utils.prettifyObject(challenge.getEnvironment().iterator().next(), this.viewer)); } else { StringBuilder builder = new StringBuilder(); - builder.append(this.user.getTranslationOrNothing(reference + "environment-title")); + builder.append(this.viewer.getTranslationOrNothing(reference + "environment-title")); challenge.getEnvironment().stream().sorted().forEach(en -> { builder.append("\n"); - builder.append(this.user.getTranslationOrNothing(reference + "environment-single", - Constants.PARAMETER_ENVIRONMENT, Utils.prettifyObject(en, this.user))); + builder.append(this.viewer.getTranslationOrNothing(reference + "environment-single", + Constants.PARAMETER_ENVIRONMENT, Utils.prettifyObject(en, this.viewer))); }); environment = builder.toString(); @@ -248,13 +301,13 @@ private String generateRequirements(Challenge challenge, @Nullable User target) StringBuilder permissionBuilder = new StringBuilder(); if (missingPermissions.size() == 1) { - permissionBuilder.append(this.user.getTranslationOrNothing(reference + "permission-single", + permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "permission-single", Constants.PARAMETER_PERMISSION, missingPermissions.get(0))); } else if (!missingPermissions.isEmpty()) { - permissionBuilder.append(this.user.getTranslationOrNothing(reference + "permissions-title")); + permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "permissions-title")); missingPermissions.forEach(permission -> { permissionBuilder.append("\n"); - permissionBuilder.append(this.user.getTranslationOrNothing(reference + "permissions-list", + permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "permissions-list", Constants.PARAMETER_PERMISSION, permission)); }); } @@ -271,7 +324,7 @@ private String generateRequirements(Challenge challenge, @Nullable User target) case STATISTIC_TYPE -> this.generateStatisticChallenge(challenge.getRequirements()); }; - return this.user.getTranslationOrNothing(reference + "lore", Constants.PARAMETER_ENVIRONMENT, environment, + return this.viewer.getTranslationOrNothing(reference + "lore", Constants.PARAMETER_ENVIRONMENT, environment, "[type-requirement]", typeRequirement, "[permissions]", permissions); } @@ -288,17 +341,17 @@ private String generateIslandChallenge(IslandRequirements requirement) { if (!requirement.getRequiredBlocks().isEmpty()) { StringBuilder builder = new StringBuilder(); - builder.append(this.user.getTranslationOrNothing(reference + "blocks-title")); + builder.append(this.viewer.getTranslationOrNothing(reference + "blocks-title")); requirement.getRequiredBlocks().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> { builder.append("\n"); if (entry.getValue() > 1) { - builder.append(this.user.getTranslationOrNothing(reference + "blocks-value", + builder.append(this.viewer.getTranslationOrNothing(reference + "blocks-value", Constants.PARAMETER_NUMBER, String.valueOf(entry.getValue()), Constants.PARAMETER_MATERIAL, - Utils.prettifyObject(entry.getKey(), this.user))); + Utils.prettifyObject(entry.getKey(), this.viewer))); } else { - builder.append(this.user.getTranslationOrNothing(reference + "block-value", - Constants.PARAMETER_MATERIAL, Utils.prettifyObject(entry.getKey(), this.user))); + builder.append(this.viewer.getTranslationOrNothing(reference + "block-value", + Constants.PARAMETER_MATERIAL, Utils.prettifyObject(entry.getKey(), this.viewer))); } }); @@ -311,17 +364,17 @@ private String generateIslandChallenge(IslandRequirements requirement) { if (!requirement.getRequiredEntities().isEmpty()) { StringBuilder builder = new StringBuilder(); - builder.append(this.user.getTranslationOrNothing(reference + "entities-title")); + builder.append(this.viewer.getTranslationOrNothing(reference + "entities-title")); requirement.getRequiredEntities().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> { builder.append("\n"); if (entry.getValue() > 1) { - builder.append(this.user.getTranslationOrNothing(reference + "entities-value", + builder.append(this.viewer.getTranslationOrNothing(reference + "entities-value", Constants.PARAMETER_NUMBER, String.valueOf(entry.getValue()), Constants.PARAMETER_ENTITY, - Utils.prettifyObject(entry.getKey(), this.user))); + Utils.prettifyObject(entry.getKey(), this.viewer))); } else { - builder.append(this.user.getTranslationOrNothing(reference + "entity-value", - Constants.PARAMETER_ENTITY, Utils.prettifyObject(entry.getKey(), this.user))); + builder.append(this.viewer.getTranslationOrNothing(reference + "entity-value", + Constants.PARAMETER_ENTITY, Utils.prettifyObject(entry.getKey(), this.viewer))); } }); @@ -330,17 +383,17 @@ private String generateIslandChallenge(IslandRequirements requirement) { entities = ""; } - String searchRadius = this.user.getTranslationOrNothing(reference + "search-radius", Constants.PARAMETER_NUMBER, + String searchRadius = this.viewer.getTranslationOrNothing(reference + "search-radius", Constants.PARAMETER_NUMBER, String.valueOf(requirement.getSearchRadius())); String warningBlocks = requirement.isRemoveBlocks() - ? this.user.getTranslationOrNothing(reference + "warning-block") + ? this.viewer.getTranslationOrNothing(reference + "warning-block") : ""; String warningEntities = requirement.isRemoveEntities() - ? this.user.getTranslationOrNothing(reference + "warning-entity") + ? this.viewer.getTranslationOrNothing(reference + "warning-entity") : ""; - return this.user.getTranslationOrNothing(reference + "lore", "[blocks]", blocks, "[entities]", entities, + return this.viewer.getTranslationOrNothing(reference + "lore", "[blocks]", blocks, "[entities]", entities, "[warning-block]", warningBlocks, "[warning-entity]", warningEntities, "[search-radius]", searchRadius); } @@ -357,18 +410,18 @@ private String generateInventoryChallenge(InventoryRequirements requirement) { if (!requirement.getRequiredItems().isEmpty()) { StringBuilder builder = new StringBuilder(); - builder.append(this.user.getTranslationOrNothing(reference + "item-title")); + builder.append(this.viewer.getTranslationOrNothing(reference + "item-title")); Utils.groupEqualItems(requirement.getRequiredItems(), requirement.getIgnoreMetaData()).stream() .sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> { builder.append("\n"); if (itemStack.getAmount() > 1) { - builder.append(this.user.getTranslationOrNothing(reference + "items-value", "[number]", + builder.append(this.viewer.getTranslationOrNothing(reference + "items-value", "[number]", String.valueOf(itemStack.getAmount()), "[item]", - Utils.prettifyObject(itemStack, this.user))); + Utils.prettifyObject(itemStack, this.viewer))); } else { - builder.append(this.user.getTranslationOrNothing(reference + "item-value", "[item]", - Utils.prettifyObject(itemStack, this.user))); + builder.append(this.viewer.getTranslationOrNothing(reference + "item-value", "[item]", + Utils.prettifyObject(itemStack, this.viewer))); } }); @@ -377,9 +430,9 @@ private String generateInventoryChallenge(InventoryRequirements requirement) { items = ""; } - String warning = requirement.isTakeItems() ? this.user.getTranslationOrNothing(reference + "warning") : ""; + String warning = requirement.isTakeItems() ? this.viewer.getTranslationOrNothing(reference + "warning") : ""; - return this.user.getTranslationOrNothing(reference + "lore", "[items]", items, "[warning]", warning); + return this.viewer.getTranslationOrNothing(reference + "lore", "[items]", items, "[warning]", warning); } /** @@ -392,25 +445,25 @@ private String generateOtherChallenge(OtherRequirements requirement) { final String reference = Constants.DESCRIPTIONS + "challenge.requirements.other."; String experience = requirement.getRequiredExperience() <= 0 ? "" - : this.user.getTranslationOrNothing(reference + "experience", "[number]", + : this.viewer.getTranslationOrNothing(reference + "experience", "[number]", String.valueOf(requirement.getRequiredExperience())); String experienceWarning = requirement.getRequiredExperience() > 0 && requirement.isTakeExperience() - ? this.user.getTranslationOrNothing(reference + "experience-warning") + ? this.viewer.getTranslationOrNothing(reference + "experience-warning") : ""; String money = !this.addon.isEconomyProvided() || requirement.getRequiredMoney() <= 0 ? "" - : this.user.getTranslationOrNothing(reference + "money", "[number]", + : this.viewer.getTranslationOrNothing(reference + "money", "[number]", String.valueOf(requirement.getRequiredMoney())); String moneyWarning = this.addon.isEconomyProvided() && requirement.getRequiredMoney() > 0 - && requirement.isTakeMoney() ? this.user.getTranslationOrNothing(reference + "money-warning") : ""; + && requirement.isTakeMoney() ? this.viewer.getTranslationOrNothing(reference + "money-warning") : ""; String level = !this.addon.isLevelProvided() || requirement.getRequiredIslandLevel() <= 0 ? "" - : this.user.getTranslationOrNothing(reference + "level", "[number]", + : this.viewer.getTranslationOrNothing(reference + "level", "[number]", String.valueOf(requirement.getRequiredIslandLevel())); - return this.user.getTranslationOrNothing(reference + "lore", "[experience]", experience, "[experience-warning]", + return this.viewer.getTranslationOrNothing(reference + "lore", "[experience]", experience, "[experience-warning]", experienceWarning, "[money]", money, "[money-warning]", moneyWarning, "[level]", level); } @@ -431,40 +484,40 @@ private String generateStatisticChallenge(StatisticRequirements requirement) { } switch (requirement.getStatistic().getType()) { - case UNTYPED -> statistic = this.user.getTranslationOrNothing(reference + "statistic", "[statistic]", - Utils.prettifyObject(requirement.getStatistic(), this.user), "[number]", + case UNTYPED -> statistic = this.viewer.getTranslationOrNothing(reference + "statistic", "[statistic]", + Utils.prettifyObject(requirement.getStatistic(), this.viewer), "[number]", String.valueOf(requirement.getAmount())); case ITEM, BLOCK -> { if (requirement.getAmount() > 1) { - statistic = this.user.getTranslationOrNothing(reference + "multiple-target", "[statistic]", - Utils.prettifyObject(requirement.getStatistic(), this.user), "[number]", + statistic = this.viewer.getTranslationOrNothing(reference + "multiple-target", "[statistic]", + Utils.prettifyObject(requirement.getStatistic(), this.viewer), "[number]", String.valueOf(requirement.getAmount()), "[target]", - Utils.prettifyObject(requirement.getMaterial(), this.user)); + Utils.prettifyObject(requirement.getMaterial(), this.viewer)); } else { - statistic = this.user.getTranslationOrNothing(reference + "single-target", "[statistic]", - Utils.prettifyObject(requirement.getStatistic(), this.user), "[target]", - Utils.prettifyObject(requirement.getMaterial(), this.user)); + statistic = this.viewer.getTranslationOrNothing(reference + "single-target", "[statistic]", + Utils.prettifyObject(requirement.getStatistic(), this.viewer), "[target]", + Utils.prettifyObject(requirement.getMaterial(), this.viewer)); } } case ENTITY -> { if (requirement.getAmount() > 1) { - statistic = this.user.getTranslationOrNothing(reference + "multiple-target", "[statistic]", - Utils.prettifyObject(requirement.getStatistic(), this.user), "[number]", + statistic = this.viewer.getTranslationOrNothing(reference + "multiple-target", "[statistic]", + Utils.prettifyObject(requirement.getStatistic(), this.viewer), "[number]", String.valueOf(requirement.getAmount()), "[target]", - Utils.prettifyObject(requirement.getEntity(), this.user)); + Utils.prettifyObject(requirement.getEntity(), this.viewer)); } else { - statistic = this.user.getTranslationOrNothing(reference + "single-target", "[statistic]", - Utils.prettifyObject(requirement.getStatistic(), this.user), "[target]", - Utils.prettifyObject(requirement.getEntity(), this.user)); + statistic = this.viewer.getTranslationOrNothing(reference + "single-target", "[statistic]", + Utils.prettifyObject(requirement.getStatistic(), this.viewer), "[target]", + Utils.prettifyObject(requirement.getEntity(), this.viewer)); } } default -> statistic = ""; } - String warning = requirement.isReduceStatistic() ? this.user.getTranslationOrNothing(reference + "warning") + String warning = requirement.isReduceStatistic() ? this.viewer.getTranslationOrNothing(reference + "warning") : ""; - return this.user.getTranslationOrNothing(reference + "lore", "[statistic]", statistic, "[warning]", warning); + return this.viewer.getTranslationOrNothing(reference + "lore", "[statistic]", statistic, "[warning]", warning); } /** @@ -483,17 +536,17 @@ private String generateChallengeStatus(boolean completedOnce, boolean completedA if (completedAll) { if (maxCompletions > 1) { - return this.user.getTranslationOrNothing(reference + "completed-times-reached", Constants.PARAMETER_MAX, + return this.viewer.getTranslationOrNothing(reference + "completed-times-reached", Constants.PARAMETER_MAX, String.valueOf(maxCompletions)); } else { - return this.user.getTranslationOrNothing(reference + "completed"); + return this.viewer.getTranslationOrNothing(reference + "completed"); } } else if (completedOnce) { if (maxCompletions > 0) { - return this.user.getTranslationOrNothing(reference + "completed-times-of", Constants.PARAMETER_MAX, + return this.viewer.getTranslationOrNothing(reference + "completed-times-of", Constants.PARAMETER_MAX, String.valueOf(maxCompletions), Constants.PARAMETER_NUMBER, String.valueOf(completionCount)); } else { - return this.user.getTranslationOrNothing(reference + "completed-times", Constants.PARAMETER_NUMBER, + return this.viewer.getTranslationOrNothing(reference + "completed-times", Constants.PARAMETER_NUMBER, String.valueOf(completionCount)); } } else { @@ -530,18 +583,18 @@ private String generateRepeatReward(Challenge challenge) { if (!challenge.getRepeatItemReward().isEmpty()) { StringBuilder builder = new StringBuilder(); - builder.append(this.user.getTranslationOrNothing(reference + "item-title")); + builder.append(this.viewer.getTranslationOrNothing(reference + "item-title")); Utils.groupEqualItems(challenge.getRepeatItemReward(), challenge.getIgnoreRewardMetaData()).stream() .sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> { builder.append("\n"); if (itemStack.getAmount() > 1) { - builder.append(this.user.getTranslationOrNothing(reference + "items-value", "[number]", + builder.append(this.viewer.getTranslationOrNothing(reference + "items-value", "[number]", String.valueOf(itemStack.getAmount()), "[item]", - Utils.prettifyObject(itemStack, this.user))); + Utils.prettifyObject(itemStack, this.viewer))); } else { - builder.append(this.user.getTranslationOrNothing(reference + "item-value", "[item]", - Utils.prettifyObject(itemStack, this.user))); + builder.append(this.viewer.getTranslationOrNothing(reference + "item-value", "[item]", + Utils.prettifyObject(itemStack, this.viewer))); } }); @@ -551,11 +604,11 @@ private String generateRepeatReward(Challenge challenge) { } String experience = challenge.getRepeatExperienceReward() <= 0 ? "" - : this.user.getTranslationOrNothing(reference + "experience", "[number]", + : this.viewer.getTranslationOrNothing(reference + "experience", "[number]", String.valueOf(challenge.getRepeatExperienceReward())); String money = !this.addon.isEconomyProvided() || challenge.getRepeatMoneyReward() <= 0 ? "" - : this.user.getTranslationOrNothing(reference + "money", "[number]", + : this.viewer.getTranslationOrNothing(reference + "money", "[number]", addon.getPlugin().getVault().map(v -> v.format(challenge.getRepeatMoneyReward())) .orElse(String.valueOf(challenge.getRepeatMoneyReward()))); @@ -565,12 +618,12 @@ private String generateRepeatReward(Challenge challenge) { StringBuilder permissionBuilder = new StringBuilder(); if (!challenge.getRepeatRewardCommands().isEmpty()) { - permissionBuilder.append(this.user.getTranslationOrNothing(reference + "commands-title")); + permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "commands-title")); challenge.getRepeatRewardCommands().forEach(command -> { permissionBuilder.append("\n"); permissionBuilder - .append(this.user.getTranslationOrNothing(reference + "command", "[command]", command)); + .append(this.viewer.getTranslationOrNothing(reference + "command", "[command]", command)); }); } @@ -585,14 +638,14 @@ private String generateRepeatReward(Challenge challenge) { return ""; } - String rewardText = this.user + String rewardText = this.viewer .getTranslationOrNothing("challenges.challenges." + challenge.getUniqueId() + ".repeat-reward-text"); if (rewardText.isEmpty()) { rewardText = Util.translateColorCodes(String.join("\n", challenge.getRepeatRewardText())); } - return this.user.getTranslationOrNothing(reference + "lore", "[text]", rewardText, "[items]", items, + return this.viewer.getTranslationOrNothing(reference + "lore", "[text]", rewardText, "[items]", items, "[experience]", experience, "[money]", money, "[commands]", commands); } @@ -609,18 +662,18 @@ private String generateReward(Challenge challenge) { if (!challenge.getRewardItems().isEmpty()) { StringBuilder builder = new StringBuilder(); - builder.append(this.user.getTranslationOrNothing(reference + "item-title")); + builder.append(this.viewer.getTranslationOrNothing(reference + "item-title")); Utils.groupEqualItems(challenge.getRewardItems(), challenge.getIgnoreRewardMetaData()).stream() .sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> { builder.append("\n"); if (itemStack.getAmount() > 1) { - builder.append(this.user.getTranslationOrNothing(reference + "items-value", "[number]", + builder.append(this.viewer.getTranslationOrNothing(reference + "items-value", "[number]", String.valueOf(itemStack.getAmount()), "[item]", - Utils.prettifyObject(itemStack, this.user))); + Utils.prettifyObject(itemStack, this.viewer))); } else { - builder.append(this.user.getTranslationOrNothing(reference + "item-value", "[item]", - Utils.prettifyObject(itemStack, this.user))); + builder.append(this.viewer.getTranslationOrNothing(reference + "item-value", "[item]", + Utils.prettifyObject(itemStack, this.viewer))); } }); @@ -630,11 +683,11 @@ private String generateReward(Challenge challenge) { } String experience = challenge.getRewardExperience() <= 0 ? "" - : this.user.getTranslationOrNothing(reference + "experience", "[number]", + : this.viewer.getTranslationOrNothing(reference + "experience", "[number]", String.valueOf(challenge.getRewardExperience())); String money = !this.addon.isEconomyProvided() || challenge.getRewardMoney() <= 0 ? "" - : this.user.getTranslationOrNothing(reference + "money", "[number]", + : this.viewer.getTranslationOrNothing(reference + "money", "[number]", addon.getPlugin().getVault().map(v -> v.format(challenge.getRewardMoney())) .orElse(String.valueOf(challenge.getRewardMoney()))); @@ -644,12 +697,12 @@ private String generateReward(Challenge challenge) { StringBuilder permissionBuilder = new StringBuilder(); if (!challenge.getRewardCommands().isEmpty()) { - permissionBuilder.append(this.user.getTranslationOrNothing(reference + "commands-title")); + permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "commands-title")); challenge.getRewardCommands().forEach(command -> { permissionBuilder.append("\n"); permissionBuilder - .append(this.user.getTranslationOrNothing(reference + "command", "[command]", command)); + .append(this.viewer.getTranslationOrNothing(reference + "command", "[command]", command)); }); } @@ -664,14 +717,14 @@ private String generateReward(Challenge challenge) { return ""; } - String rewardText = this.user + String rewardText = this.viewer .getTranslationOrNothing("challenges.challenges." + challenge.getUniqueId() + ".reward-text"); if (rewardText.isEmpty()) { rewardText = Util.translateColorCodes(String.join("\n", challenge.getRewardText())); } - return this.user.getTranslationOrNothing(reference + "lore", "[text]", rewardText, "[items]", items, + return this.viewer.getTranslationOrNothing(reference + "lore", "[text]", rewardText, "[items]", items, "[experience]", experience, "[money]", money, "[commands]", commands); } @@ -690,12 +743,12 @@ protected List generateLevelDescription(ChallengeLevel level) { String status = ""; // Get requirements in single string String waiver = this.manager.isLastLevel(level, this.world) ? "" - : this.user.getTranslationOrNothing(reference + "waiver", "[number]", + : this.viewer.getTranslationOrNothing(reference + "waiver", "[number]", String.valueOf(level.getWaiverAmount())); // Get rewards in single string String rewards = this.generateReward(level); - String returnString = this.user.getTranslation(reference + "lore", "[text]", + String returnString = this.viewer.getTranslation(reference + "lore", "[text]", Util.translateColorCodes(level.getUnlockMessage()), "[waiver]", waiver, "[rewards]", rewards, "[status]", status); @@ -724,19 +777,19 @@ protected List generateLevelDescription(LevelStatus levelStatus, User us // Get requirements in single string String waiver = this.manager.isLastLevel(level, this.world) || !levelStatus.isUnlocked() || levelStatus.isComplete() ? "" - : this.user.getTranslationOrNothing(reference + "waiver", "[number]", + : this.viewer.getTranslationOrNothing(reference + "waiver", "[number]", String.valueOf(level.getWaiverAmount())); // Get rewards in single string String rewards = !levelStatus.isUnlocked() ? "" : this.generateReward(level); - String description = this.user + String description = this.viewer .getTranslationOrNothing("challenges.levels." + level.getUniqueId() + ".description"); if (description.isEmpty()) { description = Util.translateColorCodes(String.join("\n", level.getUnlockMessage())); } - String returnString = this.user.getTranslation(reference + "lore", "[text]", description, "[waiver]", waiver, + String returnString = this.viewer.getTranslation(reference + "lore", "[text]", description, "[waiver]", waiver, "[rewards]", rewards, "[status]", status); // Remove empty lines and returns as a list. @@ -755,11 +808,11 @@ private String generateLevelStatus(LevelStatus levelStatus) { final String reference = Constants.DESCRIPTIONS + "level.status."; if (!levelStatus.isUnlocked()) { - return this.user.getTranslationOrNothing(reference + "locked") + "\n" - + this.user.getTranslationOrNothing(reference + "missing-challenges", "[number]", + return this.viewer.getTranslationOrNothing(reference + "locked") + "\n" + + this.viewer.getTranslationOrNothing(reference + "missing-challenges", "[number]", String.valueOf(levelStatus.getNumberOfChallengesStillToDo())); } else if (levelStatus.isComplete()) { - return this.user.getTranslationOrNothing(reference + "completed"); + return this.viewer.getTranslationOrNothing(reference + "completed"); } else { ChallengeLevel level = levelStatus.getLevel(); List challengeList = this.addon.getChallengesManager().getLevelChallenges(level); @@ -768,7 +821,7 @@ private String generateLevelStatus(LevelStatus levelStatus) { int doneChallenges = (int) challengeList.stream().filter(challenge -> this.addon.getChallengesManager() .isChallengeComplete(user.getUniqueId(), world, challenge)).count(); - return this.user.getTranslation(reference + "completed-challenges-of", "[number]", + return this.viewer.getTranslation(reference + "completed-challenges-of", "[number]", String.valueOf(doneChallenges), "[max]", String.valueOf(challengeList.size())); } } @@ -786,18 +839,18 @@ private String generateReward(ChallengeLevel level) { if (!level.getRewardItems().isEmpty()) { StringBuilder builder = new StringBuilder(); - builder.append(this.user.getTranslationOrNothing(reference + "item-title")); + builder.append(this.viewer.getTranslationOrNothing(reference + "item-title")); Utils.groupEqualItems(level.getRewardItems(), level.getIgnoreRewardMetaData()).stream() .sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> { builder.append("\n"); if (itemStack.getAmount() > 1) { - builder.append(this.user.getTranslationOrNothing(reference + "items-value", "[number]", + builder.append(this.viewer.getTranslationOrNothing(reference + "items-value", "[number]", String.valueOf(itemStack.getAmount()), "[item]", - Utils.prettifyObject(itemStack, this.user))); + Utils.prettifyObject(itemStack, this.viewer))); } else { - builder.append(this.user.getTranslationOrNothing(reference + "item-value", "[item]", - Utils.prettifyObject(itemStack, this.user))); + builder.append(this.viewer.getTranslationOrNothing(reference + "item-value", "[item]", + Utils.prettifyObject(itemStack, this.viewer))); } }); @@ -807,11 +860,11 @@ private String generateReward(ChallengeLevel level) { } String experience = level.getRewardExperience() <= 0 ? "" - : this.user.getTranslationOrNothing(reference + "experience", "[number]", + : this.viewer.getTranslationOrNothing(reference + "experience", "[number]", String.valueOf(level.getRewardExperience())); String money = !this.addon.isEconomyProvided() || level.getRewardMoney() <= 0 ? "" - : this.user.getTranslationOrNothing(reference + "money", "[number]", + : this.viewer.getTranslationOrNothing(reference + "money", "[number]", String.valueOf(level.getRewardMoney())); String commands; @@ -820,12 +873,12 @@ private String generateReward(ChallengeLevel level) { StringBuilder permissionBuilder = new StringBuilder(); if (!level.getRewardCommands().isEmpty()) { - permissionBuilder.append(this.user.getTranslationOrNothing(reference + "commands-title")); + permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "commands-title")); level.getRewardCommands().forEach(command -> { permissionBuilder.append("\n"); permissionBuilder - .append(this.user.getTranslationOrNothing(reference + "command", "[command]", command)); + .append(this.viewer.getTranslationOrNothing(reference + "command", "[command]", command)); }); } @@ -840,59 +893,15 @@ private String generateReward(ChallengeLevel level) { return ""; } - String rewardText = this.user + String rewardText = this.viewer .getTranslationOrNothing("challenges.levels." + level.getUniqueId() + ".reward-text"); if (rewardText.isEmpty()) { rewardText = Util.translateColorCodes(String.join("\n", level.getRewardText())); } - return this.user.getTranslationOrNothing(reference + "lore", "[text]", rewardText, "[items]", items, + return this.viewer.getTranslationOrNothing(reference + "lore", "[text]", rewardText, "[items]", items, "[experience]", experience, "[money]", money, "[commands]", commands); } - // --------------------------------------------------------------------- - // Section: Variables - // --------------------------------------------------------------------- - - /** - * This variable stores parent gui. - */ - @Nullable - protected final CommonPanel parentPanel; - - /** - * Variable stores Challenges addon. - */ - protected final ChallengesAddon addon; - - /** - * Variable stores Challenges addon manager. - */ - protected final ChallengesManager manager; - - /** - * Variable stores world in which panel is referred to. - */ - protected final World world; - - /** - * Variable stores user who created this panel. - */ - protected final User user; - - /** - * Variable stores top label of command from which panel was called. - */ - protected final String topLabel; - - /** - * Variable stores permission prefix of command from which panel was called. - */ - protected final String permissionPrefix; - - /** - * This object holds PanelItem that allows to return to previous panel. - */ - protected PanelItem returnButton; } diff --git a/src/main/java/world/bentobox/challenges/panel/admin/AdminPanel.java b/src/main/java/world/bentobox/challenges/panel/admin/AdminPanel.java index 5a1dd21e..d3afe44b 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/AdminPanel.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/AdminPanel.java @@ -44,10 +44,11 @@ public class AdminPanel extends CommonPanel private AdminPanel(ChallengesAddon addon, World world, User user, + User viewer, String topLabel, String permissionPrefix) { - super(addon, user, world, topLabel, permissionPrefix); + super(addon, user, viewer, world, topLabel, permissionPrefix); } @@ -66,7 +67,7 @@ public static void open(ChallengesAddon addon, String topLabel, String permissionPrefix) { - new AdminPanel(addon, world, user, topLabel, permissionPrefix).build(); + new AdminPanel(addon, world, user, user, topLabel, permissionPrefix).build(); } diff --git a/src/main/java/world/bentobox/challenges/panel/admin/EditChallengePanel.java b/src/main/java/world/bentobox/challenges/panel/admin/EditChallengePanel.java index 147f8f7b..0e807f73 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/EditChallengePanel.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/EditChallengePanel.java @@ -61,7 +61,7 @@ public class EditChallengePanel extends CommonPanel { */ private EditChallengePanel(ChallengesAddon addon, User user, World world, String topLabel, String permissionPrefix, Challenge challenge) { - super(addon, user, world, topLabel, permissionPrefix); + super(addon, user, user, world, topLabel, permissionPrefix); this.challenge = challenge; this.currentMenuType = MenuType.PROPERTIES; } diff --git a/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsPanel.java b/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsPanel.java index 840a8761..4e991bd3 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsPanel.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsPanel.java @@ -51,7 +51,7 @@ private EditSettingsPanel(ChallengesAddon addon, String topLabel, String permissionPrefix) { - super(addon, user, world, topLabel, permissionPrefix); + super(addon, user, user, world, topLabel, permissionPrefix); this.settings = this.addon.getChallengesSettings(); } diff --git a/src/main/java/world/bentobox/challenges/panel/user/ChallengesPanel.java b/src/main/java/world/bentobox/challenges/panel/user/ChallengesPanel.java index 4095dc57..69920df1 100644 --- a/src/main/java/world/bentobox/challenges/panel/user/ChallengesPanel.java +++ b/src/main/java/world/bentobox/challenges/panel/user/ChallengesPanel.java @@ -18,11 +18,13 @@ import java.util.Optional; import java.util.stream.Collectors; +import world.bentobox.bentobox.api.panels.Panel; import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.TemplatedPanel; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.panels.builders.TemplatedPanelBuilder; import world.bentobox.bentobox.api.panels.reader.ItemTemplateRecord; +import world.bentobox.bentobox.api.panels.reader.ItemTemplateRecord.ActionRecords; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.util.Util; import world.bentobox.challenges.ChallengesAddon; @@ -41,12 +43,13 @@ public class ChallengesPanel extends CommonPanel { private ChallengesPanel(ChallengesAddon addon, - World world, - User user, - String topLabel, - String permissionPrefix) + World world, + User user, + User viewer, + String topLabel, + String permissionPrefix) { - super(addon, user, world, topLabel, permissionPrefix); + super(addon, user, viewer, world, topLabel, permissionPrefix); this.updateLevelList(); this.containsChallenges = this.manager.hasAnyChallengeData(this.world); } @@ -62,12 +65,32 @@ private ChallengesPanel(ChallengesAddon addon, * @param permissionPrefix the permission prefix */ public static void open(ChallengesAddon addon, - World world, - User user, - String topLabel, - String permissionPrefix) + World world, + User user, + String topLabel, + String permissionPrefix) { - new ChallengesPanel(addon, world, user, topLabel, permissionPrefix).build(); + new ChallengesPanel(addon, world, user, user, topLabel, permissionPrefix).build(); + } + + /** + * View the Challenges GUI. + * + * @param addon the addon + * @param world the world + * @param user the user + * @param viewer the viewer + * @param topLabel the top label + * @param permissionPrefix the permission prefix + */ + public static void view(ChallengesAddon addon, + World world, + User user, + User viewer, + String topLabel, + String permissionPrefix) + { + new ChallengesPanel(addon, world, user, viewer, topLabel, permissionPrefix).build(); } @@ -77,7 +100,7 @@ protected void build() if (!this.containsChallenges) { this.addon.logError("There are no challenges set up!"); - Utils.sendMessage(this.user, this.world, Constants.ERRORS + "no-challenges"); + Utils.sendMessage(this.viewer, this.world, Constants.ERRORS + "no-challenges"); return; } @@ -91,8 +114,8 @@ protected void build() // Set main template. panelBuilder.template("main_panel", new File(this.addon.getDataFolder(), "panels")); - panelBuilder.user(this.user); - panelBuilder.world(this.user.getWorld()); + panelBuilder.user(this.viewer); + panelBuilder.world(this.viewer.getWorld()); // Register button builders panelBuilder.registerTypeBuilder("CHALLENGE", this::createChallengeButton); @@ -115,7 +138,7 @@ private void updateFreeChallengeList() if (this.addon.getChallengesSettings().isRemoveCompleteOneTimeChallenges()) { this.freeChallengeList.removeIf(challenge -> !challenge.isRepeatable() && - this.manager.isChallengeComplete(this.user, this.world, challenge)); + this.manager.isChallengeComplete(this.user, this.world, challenge)); } // Remove all undeployed challenges if VisibilityMode is set to Hidden. @@ -135,7 +158,7 @@ private void updateChallengeList() if (this.addon.getChallengesSettings().isRemoveCompleteOneTimeChallenges()) { this.challengeList.removeIf(challenge -> !challenge.isRepeatable() && - this.manager.isChallengeComplete(this.user, this.world, challenge)); + this.manager.isChallengeComplete(this.user, this.world, challenge)); } // Remove all undeployed challenges if VisibilityMode is set to Hidden. @@ -179,7 +202,7 @@ private void updateLevelList() private boolean updateLevelListSilent() { Optional firstLockedLevel = - this.levelList.stream().filter(levelStatus -> !levelStatus.isUnlocked()).findFirst(); + this.levelList.stream().filter(levelStatus -> !levelStatus.isUnlocked()).findFirst(); if (firstLockedLevel.isPresent()) { @@ -188,10 +211,10 @@ private boolean updateLevelListSilent() // Find a new first locked level. Optional newLockedLevel = - this.levelList.stream().filter(levelStatus -> !levelStatus.isUnlocked()).findFirst(); + this.levelList.stream().filter(levelStatus -> !levelStatus.isUnlocked()).findFirst(); return newLockedLevel.isEmpty() || - firstLockedLevel.get().getLevel() != newLockedLevel.get().getLevel(); + firstLockedLevel.get().getLevel() != newLockedLevel.get().getLevel(); } else { @@ -219,9 +242,9 @@ private PanelItem createChallengeButton(ItemTemplateRecord template, TemplatedPa // Find a challenge with given Id; levelChallenge = this.challengeList.stream(). - filter(challenge -> challenge.getUniqueId().equals(id)). - findFirst(). - orElse(null); + filter(challenge -> challenge.getUniqueId().equals(id)). + findFirst(). + orElse(null); if (levelChallenge == null) { @@ -257,8 +280,8 @@ private PanelItem createChallengeButton(ItemTemplateRecord template, @NonNull Ch // Template specific title is always more important than challenge name. if (template.title() != null && !template.title().isBlank()) { - builder.name(this.user.getTranslation(this.world, template.title(), - Constants.PARAMETER_CHALLENGE, challenge.getFriendlyName())); + builder.name(this.viewer.getTranslation(this.world, template.title(), + Constants.PARAMETER_CHALLENGE, challenge.getFriendlyName())); } else { @@ -268,7 +291,7 @@ private PanelItem createChallengeButton(ItemTemplateRecord template, @NonNull Ch if (template.description() != null && !template.description().isBlank()) { // TODO: adding parameters could be useful. - builder.description(this.user.getTranslation(this.world, template.description())); + builder.description(this.viewer.getTranslation(this.world, template.description())); } else { @@ -279,148 +302,153 @@ private PanelItem createChallengeButton(ItemTemplateRecord template, @NonNull Ch // If challenge is completed all possible times, remove action. List actions = template.actions().stream(). - filter(action -> challenge.isRepeatable() || "COMPLETE".equalsIgnoreCase(action.actionType())). - filter(action -> - { - boolean isCompletedOnce = - this.manager.isChallengeComplete(this.user.getUniqueId(), this.world, challenge); - - if (!isCompletedOnce) - { - // Is not completed once, then it must appear. - return true; - } - else if (challenge.isRepeatable() && challenge.getMaxTimes() <= 0) - { - // Challenge is unlimited. Must appear in the list. - return true; - } - else + filter(action -> challenge.isRepeatable() || "COMPLETE".equalsIgnoreCase(action.actionType())). + filter(action -> { - // Challenge still have some opened slots. + boolean isCompletedOnce = + this.manager.isChallengeComplete(this.user.getUniqueId(), this.world, challenge); - long doneTimes = challenge.isRepeatable() ? - this.manager.getChallengeTimes(this.user, this.world, challenge) : 1; + if (!isCompletedOnce) + { + // Is not completed once, then it must appear. + return true; + } + else if (challenge.isRepeatable() && challenge.getMaxTimes() <= 0) + { + // Challenge is unlimited. Must appear in the list. + return true; + } + else + { + // Challenge still have some opened slots. - return challenge.isRepeatable() && doneTimes < challenge.getMaxTimes(); - } - }). - toList(); + long doneTimes = challenge.isRepeatable() ? + this.manager.getChallengeTimes(this.user, this.world, challenge) : 1; + + return challenge.isRepeatable() && doneTimes < challenge.getMaxTimes(); + } + }). + toList(); // Add Click handler - builder.clickHandler((panel, user, clickType, i) -> { - for (ItemTemplateRecord.ActionRecords action : actions) + if (user.equals(viewer)) { + builder.clickHandler((panel, user, clickType, i) -> clickHandler(panel, user, clickType, i, actions, template, challenge)); + + // Collect tooltips. + List tooltips = actions.stream(). + filter(action -> action.tooltip() != null). + map(action -> this.viewer.getTranslation(this.world, action.tooltip())). + filter(text -> !text.isBlank()). + collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); + + // Add tooltips. + if (!tooltips.isEmpty()) + { + // Empty line and tooltips. + builder.description(""); + builder.description(tooltips); + } + } + + // Glow the icon. + builder.glow(this.addon.getChallengesSettings().isAddCompletedGlow() && + this.manager.isChallengeComplete(this.user, this.world, challenge)); + + // Click Handlers are managed by custom addon buttons. + return builder.build(); + } + + + private boolean clickHandler(Panel panel, User user, ClickType clickType, int i, List actions, ItemTemplateRecord template, @NonNull Challenge challenge) { + for (ItemTemplateRecord.ActionRecords action : actions) + { + if (clickType == action.clickType() || clickType.equals(ClickType.UNKNOWN)) { - if (clickType == action.clickType() || clickType.equals(ClickType.UNKNOWN)) + switch (action.actionType().toUpperCase()) { - switch (action.actionType().toUpperCase()) + case "COMPLETE": + if (TryToComplete.complete(this.addon, + this.user, + challenge, + this.world, + this.topLabel, + this.permissionPrefix)) + { + if (this.updateLevelListSilent()) + { + // Need to rebuild all because completing a challenge + // may unlock a new level. #187 + this.build(); + } + else + { + // There was no unlocked levels. + panel.getInventory().setItem(i, + this.createChallengeButton(template, challenge).getItem()); + } + } + else if (challenge.isRepeatable() && challenge.getTimeout() > 0) + { + // Update timeout after clicking. + panel.getInventory().setItem(i, + this.createChallengeButton(template, challenge).getItem()); + } + break; + case "COMPLETE_MAX": + if (challenge.isRepeatable()) { - case "COMPLETE": - if (TryToComplete.complete(this.addon, + if (TryToComplete.complete(this.addon, this.user, challenge, this.world, this.topLabel, - this.permissionPrefix)) + this.permissionPrefix, + Integer.MAX_VALUE)) + { + if (this.updateLevelListSilent()) { - if (this.updateLevelListSilent()) - { - // Need to rebuild all because completing a challenge - // may unlock a new level. #187 - this.build(); - } - else - { - // There was no unlocked levels. - panel.getInventory().setItem(i, - this.createChallengeButton(template, challenge).getItem()); - } + // Need to rebuild all because completing a challenge + // may unlock a new level. #187 + this.build(); } - else if (challenge.isRepeatable() && challenge.getTimeout() > 0) + else { - // Update timeout after clicking. + // There was no unlocked levels. panel.getInventory().setItem(i, - this.createChallengeButton(template, challenge).getItem()); + this.createChallengeButton(template, challenge).getItem()); } - break; - case "COMPLETE_MAX": - if (challenge.isRepeatable()) - { - if (TryToComplete.complete(this.addon, + } + else if (challenge.getTimeout() > 0) + { + // Update timeout after clicking. + panel.getInventory().setItem(i, + this.createChallengeButton(template, challenge).getItem()); + } + } + break; + case "MULTIPLE_PANEL": + if (challenge.isRepeatable()) + { + MultiplePanel.open(this.addon, this.user, value -> + { + TryToComplete.complete(this.addon, this.user, challenge, this.world, this.topLabel, this.permissionPrefix, - Integer.MAX_VALUE)) - { - if (this.updateLevelListSilent()) - { - // Need to rebuild all because completing a challenge - // may unlock a new level. #187 - this.build(); - } - else - { - // There was no unlocked levels. - panel.getInventory().setItem(i, - this.createChallengeButton(template, challenge).getItem()); - } - } - else if (challenge.getTimeout() > 0) - { - // Update timeout after clicking. - panel.getInventory().setItem(i, - this.createChallengeButton(template, challenge).getItem()); - } - } - break; - case "MULTIPLE_PANEL": - if (challenge.isRepeatable()) - { - MultiplePanel.open(this.addon, this.user, value -> - { - TryToComplete.complete(this.addon, - this.user, - challenge, - this.world, - this.topLabel, - this.permissionPrefix, - value); - - this.updateLevelListSilent(); - this.build(); - }); - } - break; + value); + + this.updateLevelListSilent(); + this.build(); + }); } + break; } } - - return true; - }); - - // Collect tooltips. - List tooltips = actions.stream(). - filter(action -> action.tooltip() != null). - map(action -> this.user.getTranslation(this.world, action.tooltip())). - filter(text -> !text.isBlank()). - collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); - - // Add tooltips. - if (!tooltips.isEmpty()) - { - // Empty line and tooltips. - builder.description(""); - builder.description(tooltips); } - // Glow the icon. - builder.glow(this.addon.getChallengesSettings().isAddCompletedGlow() && - this.manager.isChallengeComplete(this.user, this.world, challenge)); - - // Click Handlers are managed by custom addon buttons. - return builder.build(); + return true; } @@ -442,9 +470,9 @@ private PanelItem createLevelButton(ItemTemplateRecord template, TemplatedPanel. // Find a challenge with given Id; level = this.levelList.stream(). - filter(levelStatus -> levelStatus.getLevel().getUniqueId().equals(id)). - findFirst(). - orElse(null); + filter(levelStatus -> levelStatus.getLevel().getUniqueId().equals(id)). + findFirst(). + orElse(null); if (level == null) { @@ -499,8 +527,8 @@ else if (level.getLevel().getLockedIcon() != null) if (template.title() != null && !template.title().isBlank()) { - builder.name(this.user.getTranslation(this.world, template.title(), - Constants.PARAMETER_LEVEL, level.getLevel().getFriendlyName())); + builder.name(this.viewer.getTranslation(this.world, template.title(), + Constants.PARAMETER_LEVEL, level.getLevel().getFriendlyName())); } else { @@ -510,7 +538,7 @@ else if (level.getLevel().getLockedIcon() != null) if (template.description() != null && !template.description().isBlank()) { // TODO: adding parameters could be useful. - builder.description(this.user.getTranslation(this.world, template.description())); + builder.description(this.viewer.getTranslation(this.world, template.description())); } else { @@ -533,11 +561,11 @@ else if (level.getLevel().getLockedIcon() != null) // Collect tooltips. List tooltips = template.actions().stream(). - filter(action -> action.tooltip() != null). - filter(action -> level != this.lastSelectedLevel && level.isUnlocked()). - map(action -> this.user.getTranslation(this.world, action.tooltip())). - filter(text -> !text.isBlank()). - collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); + filter(action -> action.tooltip() != null). + filter(action -> level != this.lastSelectedLevel && level.isUnlocked()). + map(action -> this.viewer.getTranslation(this.world, action.tooltip())). + filter(text -> !text.isBlank()). + collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); // Add tooltips. if (!tooltips.isEmpty()) @@ -549,7 +577,7 @@ else if (level.getLevel().getLockedIcon() != null) // Glow the icon. builder.glow(level == this.lastSelectedLevel || - level.isUnlocked() && + level.isUnlocked() && this.addon.getChallengesSettings().isAddCompletedGlow() && this.manager.isLevelCompleted(this.user, this.world, level.getLevel())); @@ -576,12 +604,12 @@ private PanelItem createFreeChallengesButton(@NonNull ItemTemplateRecord templat if (template.title() != null) { - builder.name(this.user.getTranslation(this.world, template.title())); + builder.name(this.viewer.getTranslation(this.world, template.title())); } if (template.description() != null) { - builder.description(this.user.getTranslation(this.world, template.description())); + builder.description(this.viewer.getTranslation(this.world, template.description())); } // Add ClickHandler @@ -599,11 +627,11 @@ private PanelItem createFreeChallengesButton(@NonNull ItemTemplateRecord templat // Collect tooltips. List tooltips = template.actions().stream(). - filter(action -> action.tooltip() != null). - filter(action -> this.lastSelectedLevel == null). - map(action -> this.user.getTranslation(this.world, action.tooltip())). - filter(text -> !text.isBlank()). - collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); + filter(action -> action.tooltip() != null). + filter(action -> this.lastSelectedLevel == null). + map(action -> this.viewer.getTranslation(this.world, action.tooltip())). + filter(text -> !text.isBlank()). + collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); // Add tooltips. if (!tooltips.isEmpty()) @@ -626,34 +654,34 @@ private PanelItem createNextButton(@NonNull ItemTemplateRecord template, Templat switch (target) { - case "CHALLENGE" -> { - int size = this.challengeList.size(); + case "CHALLENGE" -> { + int size = this.challengeList.size(); - if (size <= slot.amountMap().getOrDefault("CHALLENGE", 1) || + if (size <= slot.amountMap().getOrDefault("CHALLENGE", 1) || 1.0 * size / slot.amountMap().getOrDefault("CHALLENGE", 1) <= this.challengeIndex + 1) - { - // There are no next elements - return null; - } - - nextPageIndex = this.challengeIndex + 2; + { + // There are no next elements + return null; } - case "LEVEL" -> { - int size = this.levelList.size(); - if (size <= slot.amountMap().getOrDefault("LEVEL", 1) || - 1.0 * size / slot.amountMap().getOrDefault("LEVEL", 1) <= this.levelIndex + 1) - { - // There are no next elements - return null; - } + nextPageIndex = this.challengeIndex + 2; + } + case "LEVEL" -> { + int size = this.levelList.size(); - nextPageIndex = this.levelIndex + 2; - } - default -> { - // If not assigned to any type, return null. + if (size <= slot.amountMap().getOrDefault("LEVEL", 1) || + 1.0 * size / slot.amountMap().getOrDefault("LEVEL", 1) <= this.levelIndex + 1) + { + // There are no next elements return null; } + + nextPageIndex = this.levelIndex + 2; + } + default -> { + // If not assigned to any type, return null. + return null; + } } PanelItemBuilder builder = new PanelItemBuilder(); @@ -672,13 +700,13 @@ private PanelItem createNextButton(@NonNull ItemTemplateRecord template, Templat if (template.title() != null) { - builder.name(this.user.getTranslation(this.world, template.title())); + builder.name(this.viewer.getTranslation(this.world, template.title())); } if (template.description() != null) { - builder.description(this.user.getTranslation(this.world, template.description(), - Constants.PARAMETER_NUMBER, String.valueOf(nextPageIndex))); + builder.description(this.viewer.getTranslation(this.world, template.description(), + Constants.PARAMETER_NUMBER, String.valueOf(nextPageIndex))); } // Add ClickHandler @@ -687,8 +715,8 @@ private PanelItem createNextButton(@NonNull ItemTemplateRecord template, Templat // Next button ignores click type currently. switch (target) { - case "CHALLENGE" -> this.challengeIndex++; - case "LEVEL" -> this.levelIndex++; + case "CHALLENGE" -> this.challengeIndex++; + case "LEVEL" -> this.levelIndex++; } this.build(); @@ -699,10 +727,10 @@ private PanelItem createNextButton(@NonNull ItemTemplateRecord template, Templat // Collect tooltips. List tooltips = template.actions().stream(). - filter(action -> action.tooltip() != null). - map(action -> this.user.getTranslation(this.world, action.tooltip())). - filter(text -> !text.isBlank()). - collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); + filter(action -> action.tooltip() != null). + map(action -> this.viewer.getTranslation(this.world, action.tooltip())). + filter(text -> !text.isBlank()). + collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); // Add tooltips. if (!tooltips.isEmpty()) @@ -765,13 +793,13 @@ else if ("LEVEL".equals(target)) if (template.title() != null) { - builder.name(this.user.getTranslation(this.world, template.title())); + builder.name(this.viewer.getTranslation(this.world, template.title())); } if (template.description() != null) { - builder.description(this.user.getTranslation(this.world, template.description(), - Constants.PARAMETER_NUMBER, String.valueOf(previousPageIndex))); + builder.description(this.viewer.getTranslation(this.world, template.description(), + Constants.PARAMETER_NUMBER, String.valueOf(previousPageIndex))); } // Add ClickHandler @@ -780,8 +808,8 @@ else if ("LEVEL".equals(target)) // Next button ignores click type currently. switch (target) { - case "CHALLENGE" -> this.challengeIndex--; - case "LEVEL" -> this.levelIndex--; + case "CHALLENGE" -> this.challengeIndex--; + case "LEVEL" -> this.levelIndex--; } this.build(); @@ -792,10 +820,10 @@ else if ("LEVEL".equals(target)) // Collect tooltips. List tooltips = template.actions().stream(). - filter(action -> action.tooltip() != null). - map(action -> this.user.getTranslation(this.world, action.tooltip())). - filter(text -> !text.isBlank()). - collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); + filter(action -> action.tooltip() != null). + map(action -> this.viewer.getTranslation(this.world, action.tooltip())). + filter(text -> !text.isBlank()). + collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); // Add tooltips. if (!tooltips.isEmpty()) @@ -809,9 +837,9 @@ else if ("LEVEL".equals(target)) } -// --------------------------------------------------------------------- -// Section: Variables -// --------------------------------------------------------------------- + // --------------------------------------------------------------------- + // Section: Variables + // --------------------------------------------------------------------- /** * This boolean indicates if in the world there exist challenges for displaying in GUI. diff --git a/src/main/java/world/bentobox/challenges/panel/user/GameModePanel.java b/src/main/java/world/bentobox/challenges/panel/user/GameModePanel.java index c8ed4778..fd835353 100644 --- a/src/main/java/world/bentobox/challenges/panel/user/GameModePanel.java +++ b/src/main/java/world/bentobox/challenges/panel/user/GameModePanel.java @@ -40,7 +40,7 @@ private GameModePanel(ChallengesAddon addon, List addonList, boolean adminMode) { - super(addon, user, world, null, null); + super(addon, user, user, world, null, null); this.addonList = addonList; this.adminMode = adminMode; }