Skip to content

Commit

Permalink
Rewrite action display logic. (#308)
Browse files Browse the repository at this point in the history
There were no filters for action displaying. All actions were active from the start, even if they are not possible.
I added action filter that will remove impossible actions:
    - COMPLETE_MAX and MULTIPLE_PANEL for non-repeatable challenge,
    - already completely finished challenge.
Fixes #307
  • Loading branch information
BONNe committed Nov 21, 2022
1 parent 31bd923 commit 57a4c08
Showing 1 changed file with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


import org.bukkit.World;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -274,11 +275,43 @@ private PanelItem createChallengeButton(ItemTemplateRecord template, @NonNull Ch
builder.description(this.generateChallengeDescription(challenge, this.user));
}

// If challenge is not repeatable, remove all other actions beside "COMPLETE".
// If challenge is completed all possible times, remove action.

List<ItemTemplateRecord.ActionRecords> 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
{
// Challenge still have some opened slots.

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 : template.actions())
for (ItemTemplateRecord.ActionRecords action : actions)
{
if (clickType == action.clickType())
if (clickType == action.clickType() || clickType.equals(ClickType.UNKNOWN))
{
switch (action.actionType().toUpperCase())
{
Expand Down Expand Up @@ -368,9 +401,8 @@ else if (challenge.getTimeout() > 0)
});

// Collect tooltips.
List<String> tooltips = template.actions().stream().
List<String> tooltips = actions.stream().
filter(action -> action.tooltip() != null).
filter(action -> challenge.isRepeatable() || "COMPLETE".equalsIgnoreCase(action.actionType())).
map(action -> this.user.getTranslation(this.world, action.tooltip())).
filter(text -> !text.isBlank()).
collect(Collectors.toCollection(() -> new ArrayList<>(template.actions().size())));
Expand Down

0 comments on commit 57a4c08

Please sign in to comment.