WIP for #338 enable viewing of challenges by admins.

This commit is contained in:
tastybento 2024-04-25 16:13:27 -07:00
parent ec6b950304
commit ace31c292e
8 changed files with 436 additions and 376 deletions

View File

@ -2,10 +2,12 @@ package world.bentobox.challenges.commands.admin;
import java.util.List; import java.util.List;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.panel.admin.AdminPanel; import world.bentobox.challenges.panel.admin.AdminPanel;
import world.bentobox.challenges.panel.user.ChallengesPanel;
public class ChallengesAdminCommand extends CompositeCommand public class ChallengesAdminCommand extends CompositeCommand
@ -52,9 +54,11 @@ public class ChallengesAdminCommand extends CompositeCommand
@Override @Override
public boolean execute(User user, String label, List<String> args) public boolean execute(User user, String label, List<String> args)
{ {
BentoBox.getInstance().logDebug("Args size = " + args.size());
// Open up the admin challenges GUI // Open up the admin challenges GUI
if (user.isPlayer()) if (user.isPlayer())
{ {
if (args.isEmpty()) {
AdminPanel.open(this.getAddon(), AdminPanel.open(this.getAddon(),
this.getWorld(), this.getWorld(),
user, user,
@ -62,6 +66,24 @@ public class ChallengesAdminCommand extends CompositeCommand
this.getPermissionPrefix()); 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; return false;
} }

View File

@ -42,7 +42,7 @@ public abstract class CommonPagedPanel<T> extends CommonPanel
User user, User user,
World world, String topLabel, String permissionPrefix) World world, String topLabel, String permissionPrefix)
{ {
super(addon, user, world, topLabel, permissionPrefix); super(addon, user, user, world, topLabel, permissionPrefix);
} }

View File

@ -38,28 +38,80 @@ import world.bentobox.challenges.utils.Utils;
* This class contains common methods for all panels. * This class contains common methods for all panels.
*/ */
public abstract class CommonPanel { 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. * This is default constructor for all classes that extends CommonPanel.
* *
* @param addon ChallengesAddon instance. * @param addon ChallengesAddon instance.
* @param user User who opens panel. * @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.addon = addon;
this.world = world; this.world = world;
this.manager = addon.getChallengesManager(); this.manager = addon.getChallengesManager();
this.user = user; this.user = user;
this.viewer = viewer;
this.topLabel = topLabel; this.topLabel = topLabel;
this.permissionPrefix = permissionPrefix; this.permissionPrefix = permissionPrefix;
this.parentPanel = null; this.parentPanel = null;
this.returnButton = new PanelItemBuilder().name(this.user.getTranslation(Constants.BUTTON + "quit.name")) this.returnButton = new PanelItemBuilder().name(this.viewer.getTranslation(Constants.BUTTON + "quit.name"))
.description(this.user.getTranslationOrNothing(Constants.BUTTON + "quit.description")).description("") .description(this.viewer.getTranslationOrNothing(Constants.BUTTON + "quit.description")).description("")
.description(this.user.getTranslationOrNothing(Constants.TIPS + "click-to-quit")) .description(this.viewer.getTranslationOrNothing(Constants.TIPS + "click-to-quit"))
.icon(Material.OAK_DOOR).clickHandler((panel, user1, clickType, i) -> { .icon(Material.OAK_DOOR).clickHandler((panel, user1, clickType, i) -> {
this.user.closeInventory(); this.viewer.closeInventory();
return true; return true;
}).build(); }).build();
} }
@ -73,6 +125,7 @@ public abstract class CommonPanel {
this.addon = parentPanel.addon; this.addon = parentPanel.addon;
this.manager = parentPanel.manager; this.manager = parentPanel.manager;
this.user = parentPanel.user; this.user = parentPanel.user;
this.viewer = parentPanel.viewer;
this.world = parentPanel.world; this.world = parentPanel.world;
this.topLabel = parentPanel.topLabel; this.topLabel = parentPanel.topLabel;
@ -80,9 +133,9 @@ public abstract class CommonPanel {
this.parentPanel = parentPanel; this.parentPanel = parentPanel;
this.returnButton = new PanelItemBuilder().name(this.user.getTranslation(Constants.BUTTON + "return.name")) this.returnButton = new PanelItemBuilder().name(this.viewer.getTranslation(Constants.BUTTON + "return.name"))
.description(this.user.getTranslationOrNothing(Constants.BUTTON + "return.description")).description("") .description(this.viewer.getTranslationOrNothing(Constants.BUTTON + "return.description")).description("")
.description(this.user.getTranslationOrNothing(Constants.TIPS + "click-to-return")) .description(this.viewer.getTranslationOrNothing(Constants.TIPS + "click-to-return"))
.icon(Material.OAK_DOOR).clickHandler((panel, user1, clickType, i) -> { .icon(Material.OAK_DOOR).clickHandler((panel, user1, clickType, i) -> {
this.parentPanel.build(); this.parentPanel.build();
return true; return true;
@ -131,7 +184,7 @@ public abstract class CommonPanel {
final String reference = Constants.DESCRIPTIONS + "challenge."; final String reference = Constants.DESCRIPTIONS + "challenge.";
// Get description from custom translations // Get description from custom translations
String description = this.user String description = this.viewer
.getTranslationOrNothing("challenges.challenges." + challenge.getUniqueId() + ".description"); .getTranslationOrNothing("challenges.challenges." + challenge.getUniqueId() + ".description");
if (description.isEmpty()) { if (description.isEmpty()) {
@ -148,11 +201,11 @@ public abstract class CommonPanel {
String requirements = isCompletedAll ? "" : this.generateRequirements(challenge, target); String requirements = isCompletedAll ? "" : this.generateRequirements(challenge, target);
// Get rewards in single string // Get rewards in single string
String rewards = isCompletedAll ? "" : this.generateRewards(challenge, isCompletedOnce); 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); String coolDown = isCompletedAll || challenge.getTimeout() <= 0 ? "" : this.generateCoolDown(challenge, target);
if (!description.replaceAll("(?m)^[ \\t]*\\r?\\n", "").isEmpty()) { 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); "[rewards]", rewards, "[status]", status, "[cooldown]", coolDown);
// remove empty lines from the generated text. // remove empty lines from the generated text.
@ -169,7 +222,7 @@ public abstract class CommonPanel {
return collect; return collect;
} else { } 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]", description, "[requirements]", requirements, "[rewards]", rewards, "[status]", status, "[cooldown]",
coolDown); coolDown);
@ -196,16 +249,16 @@ public abstract class CommonPanel {
long missing = this.manager.getLastCompletionDate(this.user, this.world, challenge) + challenge.getTimeout() long missing = this.manager.getLastCompletionDate(this.user, this.world, challenge) + challenge.getTimeout()
- System.currentTimeMillis(); - System.currentTimeMillis();
coolDown = this.user.getTranslation(reference + "wait-time", "[time]", coolDown = this.viewer.getTranslation(reference + "wait-time", "[time]",
Utils.parseDuration(Duration.ofMillis(missing), this.user)); Utils.parseDuration(Duration.ofMillis(missing), this.viewer));
} else { } else {
coolDown = ""; coolDown = "";
} }
String timeout = this.user.getTranslation(reference + "timeout", "[time]", String timeout = this.viewer.getTranslation(reference + "timeout", "[time]",
Utils.parseDuration(Duration.ofMillis(challenge.getTimeout()), this.user)); 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 @@ public abstract class CommonPanel {
// If challenge can be completed everywhere, do not display requirement. // If challenge can be completed everywhere, do not display requirement.
environment = ""; environment = "";
} else if (challenge.getEnvironment().size() == 1) { } else if (challenge.getEnvironment().size() == 1) {
environment = this.user.getTranslationOrNothing(reference + "environment-single", environment = this.viewer.getTranslationOrNothing(reference + "environment-single",
Constants.PARAMETER_ENVIRONMENT, Constants.PARAMETER_ENVIRONMENT,
Utils.prettifyObject(challenge.getEnvironment().iterator().next(), this.user)); Utils.prettifyObject(challenge.getEnvironment().iterator().next(), this.viewer));
} else { } else {
StringBuilder builder = new StringBuilder(); 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 -> { challenge.getEnvironment().stream().sorted().forEach(en -> {
builder.append("\n"); builder.append("\n");
builder.append(this.user.getTranslationOrNothing(reference + "environment-single", builder.append(this.viewer.getTranslationOrNothing(reference + "environment-single",
Constants.PARAMETER_ENVIRONMENT, Utils.prettifyObject(en, this.user))); Constants.PARAMETER_ENVIRONMENT, Utils.prettifyObject(en, this.viewer)));
}); });
environment = builder.toString(); environment = builder.toString();
@ -248,13 +301,13 @@ public abstract class CommonPanel {
StringBuilder permissionBuilder = new StringBuilder(); StringBuilder permissionBuilder = new StringBuilder();
if (missingPermissions.size() == 1) { 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))); Constants.PARAMETER_PERMISSION, missingPermissions.get(0)));
} else if (!missingPermissions.isEmpty()) { } else if (!missingPermissions.isEmpty()) {
permissionBuilder.append(this.user.getTranslationOrNothing(reference + "permissions-title")); permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "permissions-title"));
missingPermissions.forEach(permission -> { missingPermissions.forEach(permission -> {
permissionBuilder.append("\n"); permissionBuilder.append("\n");
permissionBuilder.append(this.user.getTranslationOrNothing(reference + "permissions-list", permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "permissions-list",
Constants.PARAMETER_PERMISSION, permission)); Constants.PARAMETER_PERMISSION, permission));
}); });
} }
@ -271,7 +324,7 @@ public abstract class CommonPanel {
case STATISTIC_TYPE -> this.generateStatisticChallenge(challenge.getRequirements()); 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); "[type-requirement]", typeRequirement, "[permissions]", permissions);
} }
@ -288,17 +341,17 @@ public abstract class CommonPanel {
if (!requirement.getRequiredBlocks().isEmpty()) { if (!requirement.getRequiredBlocks().isEmpty()) {
StringBuilder builder = new StringBuilder(); 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 -> { requirement.getRequiredBlocks().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
builder.append("\n"); builder.append("\n");
if (entry.getValue() > 1) { 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, Constants.PARAMETER_NUMBER, String.valueOf(entry.getValue()), Constants.PARAMETER_MATERIAL,
Utils.prettifyObject(entry.getKey(), this.user))); Utils.prettifyObject(entry.getKey(), this.viewer)));
} else { } else {
builder.append(this.user.getTranslationOrNothing(reference + "block-value", builder.append(this.viewer.getTranslationOrNothing(reference + "block-value",
Constants.PARAMETER_MATERIAL, Utils.prettifyObject(entry.getKey(), this.user))); Constants.PARAMETER_MATERIAL, Utils.prettifyObject(entry.getKey(), this.viewer)));
} }
}); });
@ -311,17 +364,17 @@ public abstract class CommonPanel {
if (!requirement.getRequiredEntities().isEmpty()) { if (!requirement.getRequiredEntities().isEmpty()) {
StringBuilder builder = new StringBuilder(); 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 -> { requirement.getRequiredEntities().entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
builder.append("\n"); builder.append("\n");
if (entry.getValue() > 1) { 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, Constants.PARAMETER_NUMBER, String.valueOf(entry.getValue()), Constants.PARAMETER_ENTITY,
Utils.prettifyObject(entry.getKey(), this.user))); Utils.prettifyObject(entry.getKey(), this.viewer)));
} else { } else {
builder.append(this.user.getTranslationOrNothing(reference + "entity-value", builder.append(this.viewer.getTranslationOrNothing(reference + "entity-value",
Constants.PARAMETER_ENTITY, Utils.prettifyObject(entry.getKey(), this.user))); Constants.PARAMETER_ENTITY, Utils.prettifyObject(entry.getKey(), this.viewer)));
} }
}); });
@ -330,17 +383,17 @@ public abstract class CommonPanel {
entities = ""; 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.valueOf(requirement.getSearchRadius()));
String warningBlocks = requirement.isRemoveBlocks() String warningBlocks = requirement.isRemoveBlocks()
? this.user.getTranslationOrNothing(reference + "warning-block") ? this.viewer.getTranslationOrNothing(reference + "warning-block")
: ""; : "";
String warningEntities = requirement.isRemoveEntities() 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); "[warning-block]", warningBlocks, "[warning-entity]", warningEntities, "[search-radius]", searchRadius);
} }
@ -357,18 +410,18 @@ public abstract class CommonPanel {
if (!requirement.getRequiredItems().isEmpty()) { if (!requirement.getRequiredItems().isEmpty()) {
StringBuilder builder = new StringBuilder(); 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() Utils.groupEqualItems(requirement.getRequiredItems(), requirement.getIgnoreMetaData()).stream()
.sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> { .sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> {
builder.append("\n"); builder.append("\n");
if (itemStack.getAmount() > 1) { 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]", String.valueOf(itemStack.getAmount()), "[item]",
Utils.prettifyObject(itemStack, this.user))); Utils.prettifyObject(itemStack, this.viewer)));
} else { } else {
builder.append(this.user.getTranslationOrNothing(reference + "item-value", "[item]", builder.append(this.viewer.getTranslationOrNothing(reference + "item-value", "[item]",
Utils.prettifyObject(itemStack, this.user))); Utils.prettifyObject(itemStack, this.viewer)));
} }
}); });
@ -377,9 +430,9 @@ public abstract class CommonPanel {
items = ""; 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 @@ public abstract class CommonPanel {
final String reference = Constants.DESCRIPTIONS + "challenge.requirements.other."; final String reference = Constants.DESCRIPTIONS + "challenge.requirements.other.";
String experience = requirement.getRequiredExperience() <= 0 ? "" String experience = requirement.getRequiredExperience() <= 0 ? ""
: this.user.getTranslationOrNothing(reference + "experience", "[number]", : this.viewer.getTranslationOrNothing(reference + "experience", "[number]",
String.valueOf(requirement.getRequiredExperience())); String.valueOf(requirement.getRequiredExperience()));
String experienceWarning = requirement.getRequiredExperience() > 0 && requirement.isTakeExperience() 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 ? "" 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.valueOf(requirement.getRequiredMoney()));
String moneyWarning = this.addon.isEconomyProvided() && requirement.getRequiredMoney() > 0 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 ? "" String level = !this.addon.isLevelProvided() || requirement.getRequiredIslandLevel() <= 0 ? ""
: this.user.getTranslationOrNothing(reference + "level", "[number]", : this.viewer.getTranslationOrNothing(reference + "level", "[number]",
String.valueOf(requirement.getRequiredIslandLevel())); 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); experienceWarning, "[money]", money, "[money-warning]", moneyWarning, "[level]", level);
} }
@ -431,40 +484,40 @@ public abstract class CommonPanel {
} }
switch (requirement.getStatistic().getType()) { switch (requirement.getStatistic().getType()) {
case UNTYPED -> statistic = this.user.getTranslationOrNothing(reference + "statistic", "[statistic]", case UNTYPED -> statistic = this.viewer.getTranslationOrNothing(reference + "statistic", "[statistic]",
Utils.prettifyObject(requirement.getStatistic(), this.user), "[number]", Utils.prettifyObject(requirement.getStatistic(), this.viewer), "[number]",
String.valueOf(requirement.getAmount())); String.valueOf(requirement.getAmount()));
case ITEM, BLOCK -> { case ITEM, BLOCK -> {
if (requirement.getAmount() > 1) { if (requirement.getAmount() > 1) {
statistic = this.user.getTranslationOrNothing(reference + "multiple-target", "[statistic]", statistic = this.viewer.getTranslationOrNothing(reference + "multiple-target", "[statistic]",
Utils.prettifyObject(requirement.getStatistic(), this.user), "[number]", Utils.prettifyObject(requirement.getStatistic(), this.viewer), "[number]",
String.valueOf(requirement.getAmount()), "[target]", String.valueOf(requirement.getAmount()), "[target]",
Utils.prettifyObject(requirement.getMaterial(), this.user)); Utils.prettifyObject(requirement.getMaterial(), this.viewer));
} else { } else {
statistic = this.user.getTranslationOrNothing(reference + "single-target", "[statistic]", statistic = this.viewer.getTranslationOrNothing(reference + "single-target", "[statistic]",
Utils.prettifyObject(requirement.getStatistic(), this.user), "[target]", Utils.prettifyObject(requirement.getStatistic(), this.viewer), "[target]",
Utils.prettifyObject(requirement.getMaterial(), this.user)); Utils.prettifyObject(requirement.getMaterial(), this.viewer));
} }
} }
case ENTITY -> { case ENTITY -> {
if (requirement.getAmount() > 1) { if (requirement.getAmount() > 1) {
statistic = this.user.getTranslationOrNothing(reference + "multiple-target", "[statistic]", statistic = this.viewer.getTranslationOrNothing(reference + "multiple-target", "[statistic]",
Utils.prettifyObject(requirement.getStatistic(), this.user), "[number]", Utils.prettifyObject(requirement.getStatistic(), this.viewer), "[number]",
String.valueOf(requirement.getAmount()), "[target]", String.valueOf(requirement.getAmount()), "[target]",
Utils.prettifyObject(requirement.getEntity(), this.user)); Utils.prettifyObject(requirement.getEntity(), this.viewer));
} else { } else {
statistic = this.user.getTranslationOrNothing(reference + "single-target", "[statistic]", statistic = this.viewer.getTranslationOrNothing(reference + "single-target", "[statistic]",
Utils.prettifyObject(requirement.getStatistic(), this.user), "[target]", Utils.prettifyObject(requirement.getStatistic(), this.viewer), "[target]",
Utils.prettifyObject(requirement.getEntity(), this.user)); Utils.prettifyObject(requirement.getEntity(), this.viewer));
} }
} }
default -> statistic = ""; 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 @@ public abstract class CommonPanel {
if (completedAll) { if (completedAll) {
if (maxCompletions > 1) { 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)); String.valueOf(maxCompletions));
} else { } else {
return this.user.getTranslationOrNothing(reference + "completed"); return this.viewer.getTranslationOrNothing(reference + "completed");
} }
} else if (completedOnce) { } else if (completedOnce) {
if (maxCompletions > 0) { 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)); String.valueOf(maxCompletions), Constants.PARAMETER_NUMBER, String.valueOf(completionCount));
} else { } else {
return this.user.getTranslationOrNothing(reference + "completed-times", Constants.PARAMETER_NUMBER, return this.viewer.getTranslationOrNothing(reference + "completed-times", Constants.PARAMETER_NUMBER,
String.valueOf(completionCount)); String.valueOf(completionCount));
} }
} else { } else {
@ -530,18 +583,18 @@ public abstract class CommonPanel {
if (!challenge.getRepeatItemReward().isEmpty()) { if (!challenge.getRepeatItemReward().isEmpty()) {
StringBuilder builder = new StringBuilder(); 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() Utils.groupEqualItems(challenge.getRepeatItemReward(), challenge.getIgnoreRewardMetaData()).stream()
.sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> { .sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> {
builder.append("\n"); builder.append("\n");
if (itemStack.getAmount() > 1) { 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]", String.valueOf(itemStack.getAmount()), "[item]",
Utils.prettifyObject(itemStack, this.user))); Utils.prettifyObject(itemStack, this.viewer)));
} else { } else {
builder.append(this.user.getTranslationOrNothing(reference + "item-value", "[item]", builder.append(this.viewer.getTranslationOrNothing(reference + "item-value", "[item]",
Utils.prettifyObject(itemStack, this.user))); Utils.prettifyObject(itemStack, this.viewer)));
} }
}); });
@ -551,11 +604,11 @@ public abstract class CommonPanel {
} }
String experience = challenge.getRepeatExperienceReward() <= 0 ? "" String experience = challenge.getRepeatExperienceReward() <= 0 ? ""
: this.user.getTranslationOrNothing(reference + "experience", "[number]", : this.viewer.getTranslationOrNothing(reference + "experience", "[number]",
String.valueOf(challenge.getRepeatExperienceReward())); String.valueOf(challenge.getRepeatExperienceReward()));
String money = !this.addon.isEconomyProvided() || challenge.getRepeatMoneyReward() <= 0 ? "" 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())) addon.getPlugin().getVault().map(v -> v.format(challenge.getRepeatMoneyReward()))
.orElse(String.valueOf(challenge.getRepeatMoneyReward()))); .orElse(String.valueOf(challenge.getRepeatMoneyReward())));
@ -565,12 +618,12 @@ public abstract class CommonPanel {
StringBuilder permissionBuilder = new StringBuilder(); StringBuilder permissionBuilder = new StringBuilder();
if (!challenge.getRepeatRewardCommands().isEmpty()) { if (!challenge.getRepeatRewardCommands().isEmpty()) {
permissionBuilder.append(this.user.getTranslationOrNothing(reference + "commands-title")); permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "commands-title"));
challenge.getRepeatRewardCommands().forEach(command -> { challenge.getRepeatRewardCommands().forEach(command -> {
permissionBuilder.append("\n"); permissionBuilder.append("\n");
permissionBuilder permissionBuilder
.append(this.user.getTranslationOrNothing(reference + "command", "[command]", command)); .append(this.viewer.getTranslationOrNothing(reference + "command", "[command]", command));
}); });
} }
@ -585,14 +638,14 @@ public abstract class CommonPanel {
return ""; return "";
} }
String rewardText = this.user String rewardText = this.viewer
.getTranslationOrNothing("challenges.challenges." + challenge.getUniqueId() + ".repeat-reward-text"); .getTranslationOrNothing("challenges.challenges." + challenge.getUniqueId() + ".repeat-reward-text");
if (rewardText.isEmpty()) { if (rewardText.isEmpty()) {
rewardText = Util.translateColorCodes(String.join("\n", challenge.getRepeatRewardText())); 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); "[experience]", experience, "[money]", money, "[commands]", commands);
} }
@ -609,18 +662,18 @@ public abstract class CommonPanel {
if (!challenge.getRewardItems().isEmpty()) { if (!challenge.getRewardItems().isEmpty()) {
StringBuilder builder = new StringBuilder(); 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() Utils.groupEqualItems(challenge.getRewardItems(), challenge.getIgnoreRewardMetaData()).stream()
.sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> { .sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> {
builder.append("\n"); builder.append("\n");
if (itemStack.getAmount() > 1) { 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]", String.valueOf(itemStack.getAmount()), "[item]",
Utils.prettifyObject(itemStack, this.user))); Utils.prettifyObject(itemStack, this.viewer)));
} else { } else {
builder.append(this.user.getTranslationOrNothing(reference + "item-value", "[item]", builder.append(this.viewer.getTranslationOrNothing(reference + "item-value", "[item]",
Utils.prettifyObject(itemStack, this.user))); Utils.prettifyObject(itemStack, this.viewer)));
} }
}); });
@ -630,11 +683,11 @@ public abstract class CommonPanel {
} }
String experience = challenge.getRewardExperience() <= 0 ? "" String experience = challenge.getRewardExperience() <= 0 ? ""
: this.user.getTranslationOrNothing(reference + "experience", "[number]", : this.viewer.getTranslationOrNothing(reference + "experience", "[number]",
String.valueOf(challenge.getRewardExperience())); String.valueOf(challenge.getRewardExperience()));
String money = !this.addon.isEconomyProvided() || challenge.getRewardMoney() <= 0 ? "" 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())) addon.getPlugin().getVault().map(v -> v.format(challenge.getRewardMoney()))
.orElse(String.valueOf(challenge.getRewardMoney()))); .orElse(String.valueOf(challenge.getRewardMoney())));
@ -644,12 +697,12 @@ public abstract class CommonPanel {
StringBuilder permissionBuilder = new StringBuilder(); StringBuilder permissionBuilder = new StringBuilder();
if (!challenge.getRewardCommands().isEmpty()) { if (!challenge.getRewardCommands().isEmpty()) {
permissionBuilder.append(this.user.getTranslationOrNothing(reference + "commands-title")); permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "commands-title"));
challenge.getRewardCommands().forEach(command -> { challenge.getRewardCommands().forEach(command -> {
permissionBuilder.append("\n"); permissionBuilder.append("\n");
permissionBuilder permissionBuilder
.append(this.user.getTranslationOrNothing(reference + "command", "[command]", command)); .append(this.viewer.getTranslationOrNothing(reference + "command", "[command]", command));
}); });
} }
@ -664,14 +717,14 @@ public abstract class CommonPanel {
return ""; return "";
} }
String rewardText = this.user String rewardText = this.viewer
.getTranslationOrNothing("challenges.challenges." + challenge.getUniqueId() + ".reward-text"); .getTranslationOrNothing("challenges.challenges." + challenge.getUniqueId() + ".reward-text");
if (rewardText.isEmpty()) { if (rewardText.isEmpty()) {
rewardText = Util.translateColorCodes(String.join("\n", challenge.getRewardText())); 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); "[experience]", experience, "[money]", money, "[commands]", commands);
} }
@ -690,12 +743,12 @@ public abstract class CommonPanel {
String status = ""; String status = "";
// Get requirements in single string // Get requirements in single string
String waiver = this.manager.isLastLevel(level, this.world) ? "" String waiver = this.manager.isLastLevel(level, this.world) ? ""
: this.user.getTranslationOrNothing(reference + "waiver", "[number]", : this.viewer.getTranslationOrNothing(reference + "waiver", "[number]",
String.valueOf(level.getWaiverAmount())); String.valueOf(level.getWaiverAmount()));
// Get rewards in single string // Get rewards in single string
String rewards = this.generateReward(level); 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, Util.translateColorCodes(level.getUnlockMessage()), "[waiver]", waiver, "[rewards]", rewards,
"[status]", status); "[status]", status);
@ -724,19 +777,19 @@ public abstract class CommonPanel {
// Get requirements in single string // Get requirements in single string
String waiver = this.manager.isLastLevel(level, this.world) || !levelStatus.isUnlocked() String waiver = this.manager.isLastLevel(level, this.world) || !levelStatus.isUnlocked()
|| levelStatus.isComplete() ? "" || levelStatus.isComplete() ? ""
: this.user.getTranslationOrNothing(reference + "waiver", "[number]", : this.viewer.getTranslationOrNothing(reference + "waiver", "[number]",
String.valueOf(level.getWaiverAmount())); String.valueOf(level.getWaiverAmount()));
// Get rewards in single string // Get rewards in single string
String rewards = !levelStatus.isUnlocked() ? "" : this.generateReward(level); String rewards = !levelStatus.isUnlocked() ? "" : this.generateReward(level);
String description = this.user String description = this.viewer
.getTranslationOrNothing("challenges.levels." + level.getUniqueId() + ".description"); .getTranslationOrNothing("challenges.levels." + level.getUniqueId() + ".description");
if (description.isEmpty()) { if (description.isEmpty()) {
description = Util.translateColorCodes(String.join("\n", level.getUnlockMessage())); 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); "[rewards]", rewards, "[status]", status);
// Remove empty lines and returns as a list. // Remove empty lines and returns as a list.
@ -755,11 +808,11 @@ public abstract class CommonPanel {
final String reference = Constants.DESCRIPTIONS + "level.status."; final String reference = Constants.DESCRIPTIONS + "level.status.";
if (!levelStatus.isUnlocked()) { if (!levelStatus.isUnlocked()) {
return this.user.getTranslationOrNothing(reference + "locked") + "\n" return this.viewer.getTranslationOrNothing(reference + "locked") + "\n"
+ this.user.getTranslationOrNothing(reference + "missing-challenges", "[number]", + this.viewer.getTranslationOrNothing(reference + "missing-challenges", "[number]",
String.valueOf(levelStatus.getNumberOfChallengesStillToDo())); String.valueOf(levelStatus.getNumberOfChallengesStillToDo()));
} else if (levelStatus.isComplete()) { } else if (levelStatus.isComplete()) {
return this.user.getTranslationOrNothing(reference + "completed"); return this.viewer.getTranslationOrNothing(reference + "completed");
} else { } else {
ChallengeLevel level = levelStatus.getLevel(); ChallengeLevel level = levelStatus.getLevel();
List<Challenge> challengeList = this.addon.getChallengesManager().getLevelChallenges(level); List<Challenge> challengeList = this.addon.getChallengesManager().getLevelChallenges(level);
@ -768,7 +821,7 @@ public abstract class CommonPanel {
int doneChallenges = (int) challengeList.stream().filter(challenge -> this.addon.getChallengesManager() int doneChallenges = (int) challengeList.stream().filter(challenge -> this.addon.getChallengesManager()
.isChallengeComplete(user.getUniqueId(), world, challenge)).count(); .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())); String.valueOf(doneChallenges), "[max]", String.valueOf(challengeList.size()));
} }
} }
@ -786,18 +839,18 @@ public abstract class CommonPanel {
if (!level.getRewardItems().isEmpty()) { if (!level.getRewardItems().isEmpty()) {
StringBuilder builder = new StringBuilder(); 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() Utils.groupEqualItems(level.getRewardItems(), level.getIgnoreRewardMetaData()).stream()
.sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> { .sorted(Comparator.comparing(ItemStack::getType)).forEach(itemStack -> {
builder.append("\n"); builder.append("\n");
if (itemStack.getAmount() > 1) { 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]", String.valueOf(itemStack.getAmount()), "[item]",
Utils.prettifyObject(itemStack, this.user))); Utils.prettifyObject(itemStack, this.viewer)));
} else { } else {
builder.append(this.user.getTranslationOrNothing(reference + "item-value", "[item]", builder.append(this.viewer.getTranslationOrNothing(reference + "item-value", "[item]",
Utils.prettifyObject(itemStack, this.user))); Utils.prettifyObject(itemStack, this.viewer)));
} }
}); });
@ -807,11 +860,11 @@ public abstract class CommonPanel {
} }
String experience = level.getRewardExperience() <= 0 ? "" String experience = level.getRewardExperience() <= 0 ? ""
: this.user.getTranslationOrNothing(reference + "experience", "[number]", : this.viewer.getTranslationOrNothing(reference + "experience", "[number]",
String.valueOf(level.getRewardExperience())); String.valueOf(level.getRewardExperience()));
String money = !this.addon.isEconomyProvided() || level.getRewardMoney() <= 0 ? "" 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.valueOf(level.getRewardMoney()));
String commands; String commands;
@ -820,12 +873,12 @@ public abstract class CommonPanel {
StringBuilder permissionBuilder = new StringBuilder(); StringBuilder permissionBuilder = new StringBuilder();
if (!level.getRewardCommands().isEmpty()) { if (!level.getRewardCommands().isEmpty()) {
permissionBuilder.append(this.user.getTranslationOrNothing(reference + "commands-title")); permissionBuilder.append(this.viewer.getTranslationOrNothing(reference + "commands-title"));
level.getRewardCommands().forEach(command -> { level.getRewardCommands().forEach(command -> {
permissionBuilder.append("\n"); permissionBuilder.append("\n");
permissionBuilder permissionBuilder
.append(this.user.getTranslationOrNothing(reference + "command", "[command]", command)); .append(this.viewer.getTranslationOrNothing(reference + "command", "[command]", command));
}); });
} }
@ -840,59 +893,15 @@ public abstract class CommonPanel {
return ""; return "";
} }
String rewardText = this.user String rewardText = this.viewer
.getTranslationOrNothing("challenges.levels." + level.getUniqueId() + ".reward-text"); .getTranslationOrNothing("challenges.levels." + level.getUniqueId() + ".reward-text");
if (rewardText.isEmpty()) { if (rewardText.isEmpty()) {
rewardText = Util.translateColorCodes(String.join("\n", level.getRewardText())); 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); "[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;
} }

View File

@ -44,10 +44,11 @@ public class AdminPanel extends CommonPanel
private AdminPanel(ChallengesAddon addon, private AdminPanel(ChallengesAddon addon,
World world, World world,
User user, User user,
User viewer,
String topLabel, String topLabel,
String permissionPrefix) String permissionPrefix)
{ {
super(addon, user, world, topLabel, permissionPrefix); super(addon, user, viewer, world, topLabel, permissionPrefix);
} }
@ -66,7 +67,7 @@ public class AdminPanel extends CommonPanel
String topLabel, String topLabel,
String permissionPrefix) String permissionPrefix)
{ {
new AdminPanel(addon, world, user, topLabel, permissionPrefix).build(); new AdminPanel(addon, world, user, user, topLabel, permissionPrefix).build();
} }

View File

@ -61,7 +61,7 @@ public class EditChallengePanel extends CommonPanel {
*/ */
private EditChallengePanel(ChallengesAddon addon, User user, World world, String topLabel, String permissionPrefix, private EditChallengePanel(ChallengesAddon addon, User user, World world, String topLabel, String permissionPrefix,
Challenge challenge) { Challenge challenge) {
super(addon, user, world, topLabel, permissionPrefix); super(addon, user, user, world, topLabel, permissionPrefix);
this.challenge = challenge; this.challenge = challenge;
this.currentMenuType = MenuType.PROPERTIES; this.currentMenuType = MenuType.PROPERTIES;
} }

View File

@ -51,7 +51,7 @@ public class EditSettingsPanel extends CommonPanel
String topLabel, String topLabel,
String permissionPrefix) String permissionPrefix)
{ {
super(addon, user, world, topLabel, permissionPrefix); super(addon, user, user, world, topLabel, permissionPrefix);
this.settings = this.addon.getChallengesSettings(); this.settings = this.addon.getChallengesSettings();
} }

View File

@ -18,11 +18,13 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; 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.PanelItem;
import world.bentobox.bentobox.api.panels.TemplatedPanel; import world.bentobox.bentobox.api.panels.TemplatedPanel;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.panels.builders.TemplatedPanelBuilder; import world.bentobox.bentobox.api.panels.builders.TemplatedPanelBuilder;
import world.bentobox.bentobox.api.panels.reader.ItemTemplateRecord; 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.api.user.User;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesAddon;
@ -43,10 +45,11 @@ public class ChallengesPanel extends CommonPanel
private ChallengesPanel(ChallengesAddon addon, private ChallengesPanel(ChallengesAddon addon,
World world, World world,
User user, User user,
User viewer,
String topLabel, String topLabel,
String permissionPrefix) String permissionPrefix)
{ {
super(addon, user, world, topLabel, permissionPrefix); super(addon, user, viewer, world, topLabel, permissionPrefix);
this.updateLevelList(); this.updateLevelList();
this.containsChallenges = this.manager.hasAnyChallengeData(this.world); this.containsChallenges = this.manager.hasAnyChallengeData(this.world);
} }
@ -67,7 +70,27 @@ public class ChallengesPanel extends CommonPanel
String topLabel, String topLabel,
String permissionPrefix) 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 @@ public class ChallengesPanel extends CommonPanel
if (!this.containsChallenges) if (!this.containsChallenges)
{ {
this.addon.logError("There are no challenges set up!"); 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; return;
} }
@ -91,8 +114,8 @@ public class ChallengesPanel extends CommonPanel
// Set main template. // Set main template.
panelBuilder.template("main_panel", new File(this.addon.getDataFolder(), "panels")); panelBuilder.template("main_panel", new File(this.addon.getDataFolder(), "panels"));
panelBuilder.user(this.user); panelBuilder.user(this.viewer);
panelBuilder.world(this.user.getWorld()); panelBuilder.world(this.viewer.getWorld());
// Register button builders // Register button builders
panelBuilder.registerTypeBuilder("CHALLENGE", this::createChallengeButton); panelBuilder.registerTypeBuilder("CHALLENGE", this::createChallengeButton);
@ -257,7 +280,7 @@ public class ChallengesPanel extends CommonPanel
// Template specific title is always more important than challenge name. // Template specific title is always more important than challenge name.
if (template.title() != null && !template.title().isBlank()) if (template.title() != null && !template.title().isBlank())
{ {
builder.name(this.user.getTranslation(this.world, template.title(), builder.name(this.viewer.getTranslation(this.world, template.title(),
Constants.PARAMETER_CHALLENGE, challenge.getFriendlyName())); Constants.PARAMETER_CHALLENGE, challenge.getFriendlyName()));
} }
else else
@ -268,7 +291,7 @@ public class ChallengesPanel extends CommonPanel
if (template.description() != null && !template.description().isBlank()) if (template.description() != null && !template.description().isBlank())
{ {
// TODO: adding parameters could be useful. // 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 else
{ {
@ -308,7 +331,35 @@ public class ChallengesPanel extends CommonPanel
toList(); toList();
// Add Click handler // Add Click handler
builder.clickHandler((panel, user, clickType, i) -> { if (user.equals(viewer)) {
builder.clickHandler((panel, user, clickType, i) -> clickHandler(panel, user, clickType, i, actions, template, challenge));
// Collect tooltips.
List<String> 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<ActionRecords> actions, ItemTemplateRecord template, @NonNull Challenge challenge) {
for (ItemTemplateRecord.ActionRecords action : actions) for (ItemTemplateRecord.ActionRecords action : actions)
{ {
if (clickType == action.clickType() || clickType.equals(ClickType.UNKNOWN)) if (clickType == action.clickType() || clickType.equals(ClickType.UNKNOWN))
@ -398,29 +449,6 @@ public class ChallengesPanel extends CommonPanel
} }
return true; return true;
});
// Collect tooltips.
List<String> 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();
} }
@ -499,7 +527,7 @@ public class ChallengesPanel extends CommonPanel
if (template.title() != null && !template.title().isBlank()) if (template.title() != null && !template.title().isBlank())
{ {
builder.name(this.user.getTranslation(this.world, template.title(), builder.name(this.viewer.getTranslation(this.world, template.title(),
Constants.PARAMETER_LEVEL, level.getLevel().getFriendlyName())); Constants.PARAMETER_LEVEL, level.getLevel().getFriendlyName()));
} }
else else
@ -510,7 +538,7 @@ public class ChallengesPanel extends CommonPanel
if (template.description() != null && !template.description().isBlank()) if (template.description() != null && !template.description().isBlank())
{ {
// TODO: adding parameters could be useful. // 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 else
{ {
@ -535,7 +563,7 @@ public class ChallengesPanel extends CommonPanel
List<String> tooltips = template.actions().stream(). List<String> tooltips = template.actions().stream().
filter(action -> action.tooltip() != null). filter(action -> action.tooltip() != null).
filter(action -> level != this.lastSelectedLevel && level.isUnlocked()). filter(action -> level != this.lastSelectedLevel && level.isUnlocked()).
map(action -> this.user.getTranslation(this.world, action.tooltip())). map(action -> this.viewer.getTranslation(this.world, action.tooltip())).
filter(text -> !text.isBlank()). filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
@ -576,12 +604,12 @@ public class ChallengesPanel extends CommonPanel
if (template.title() != null) 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) if (template.description() != null)
{ {
builder.description(this.user.getTranslation(this.world, template.description())); builder.description(this.viewer.getTranslation(this.world, template.description()));
} }
// Add ClickHandler // Add ClickHandler
@ -601,7 +629,7 @@ public class ChallengesPanel extends CommonPanel
List<String> tooltips = template.actions().stream(). List<String> tooltips = template.actions().stream().
filter(action -> action.tooltip() != null). filter(action -> action.tooltip() != null).
filter(action -> this.lastSelectedLevel == null). filter(action -> this.lastSelectedLevel == null).
map(action -> this.user.getTranslation(this.world, action.tooltip())). map(action -> this.viewer.getTranslation(this.world, action.tooltip())).
filter(text -> !text.isBlank()). filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
@ -672,12 +700,12 @@ public class ChallengesPanel extends CommonPanel
if (template.title() != null) 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) if (template.description() != null)
{ {
builder.description(this.user.getTranslation(this.world, template.description(), builder.description(this.viewer.getTranslation(this.world, template.description(),
Constants.PARAMETER_NUMBER, String.valueOf(nextPageIndex))); Constants.PARAMETER_NUMBER, String.valueOf(nextPageIndex)));
} }
@ -700,7 +728,7 @@ public class ChallengesPanel extends CommonPanel
// Collect tooltips. // Collect tooltips.
List<String> tooltips = template.actions().stream(). List<String> tooltips = template.actions().stream().
filter(action -> action.tooltip() != null). filter(action -> action.tooltip() != null).
map(action -> this.user.getTranslation(this.world, action.tooltip())). map(action -> this.viewer.getTranslation(this.world, action.tooltip())).
filter(text -> !text.isBlank()). filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
@ -765,12 +793,12 @@ public class ChallengesPanel extends CommonPanel
if (template.title() != null) 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) if (template.description() != null)
{ {
builder.description(this.user.getTranslation(this.world, template.description(), builder.description(this.viewer.getTranslation(this.world, template.description(),
Constants.PARAMETER_NUMBER, String.valueOf(previousPageIndex))); Constants.PARAMETER_NUMBER, String.valueOf(previousPageIndex)));
} }
@ -793,7 +821,7 @@ public class ChallengesPanel extends CommonPanel
// Collect tooltips. // Collect tooltips.
List<String> tooltips = template.actions().stream(). List<String> tooltips = template.actions().stream().
filter(action -> action.tooltip() != null). filter(action -> action.tooltip() != null).
map(action -> this.user.getTranslation(this.world, action.tooltip())). map(action -> this.viewer.getTranslation(this.world, action.tooltip())).
filter(text -> !text.isBlank()). filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size()))); collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
@ -809,9 +837,9 @@ public class ChallengesPanel extends CommonPanel
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Variables // Section: Variables
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
/** /**
* This boolean indicates if in the world there exist challenges for displaying in GUI. * This boolean indicates if in the world there exist challenges for displaying in GUI.

View File

@ -40,7 +40,7 @@ public class GameModePanel extends CommonPanel
List<GameModeAddon> addonList, List<GameModeAddon> addonList,
boolean adminMode) boolean adminMode)
{ {
super(addon, user, world, null, null); super(addon, user, user, world, null, null);
this.addonList = addonList; this.addonList = addonList;
this.adminMode = adminMode; this.adminMode = adminMode;
} }