Skip to content

Commit

Permalink
Fixes #187
Browse files Browse the repository at this point in the history
Add a new method that updates unlocked level list without changing active level. This method returns if last unlocked level was changed, and in that case it triggers whole gui rebuilding.
  • Loading branch information
BONNe committed Sep 24, 2021
1 parent adf4e7c commit aa0336d
Showing 1 changed file with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import world.bentobox.bentobox.api.panels.PanelItem;
Expand Down Expand Up @@ -149,6 +150,9 @@ private void updateChallengeList()
}


/**
* Updates level status list and selects last unlocked level.
*/
private void updateLevelList()
{
this.levelList = this.manager.getAllChallengeLevelStatus(this.user, this.world);
Expand All @@ -167,6 +171,35 @@ private void updateLevelList()
}


/**
* Updates level status list and returns if any new level has been unlocked.
* @return {code true} if a new level was unlocked, {@code false} otherwise.
*/
private boolean updateLevelListSilent()
{
Optional<LevelStatus> firstLockedLevel =
this.levelList.stream().filter(levelStatus -> !levelStatus.isUnlocked()).findFirst();

if (firstLockedLevel.isPresent())
{
// If there still exist any locked level, update level status list.
this.levelList = this.manager.getAllChallengeLevelStatus(this.user, this.world);

// Find a new first locked level.
Optional<LevelStatus> newLockedLevel =
this.levelList.stream().filter(levelStatus -> !levelStatus.isUnlocked()).findFirst();

return newLockedLevel.isEmpty() ||
firstLockedLevel.get().getLevel() != newLockedLevel.get().getLevel();
}
else
{
// If locked level is not present, return false.
return false;
}
}


@Nullable
private PanelItem createChallengeButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot)
{
Expand Down Expand Up @@ -257,8 +290,18 @@ private PanelItem createChallengeButton(ItemTemplateRecord template, @NonNull Ch
this.topLabel,
this.permissionPrefix))
{
panel.getInventory().setItem(i,
this.createChallengeButton(template, challenge).getItem());
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());
}
}
break;
case "COMPLETE_MAX":
Expand All @@ -272,8 +315,18 @@ private PanelItem createChallengeButton(ItemTemplateRecord template, @NonNull Ch
this.permissionPrefix,
Integer.MAX_VALUE))
{
panel.getInventory().setItem(i,
this.createChallengeButton(template, challenge).getItem());
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());
}
}
}
break;
Expand All @@ -290,6 +343,7 @@ private PanelItem createChallengeButton(ItemTemplateRecord template, @NonNull Ch
this.permissionPrefix,
value);

this.updateLevelListSilent();
this.build();
});
}
Expand Down

0 comments on commit aa0336d

Please sign in to comment.