mirror of
https://github.com/BentoBoxWorld/Challenges.git
synced 2024-11-28 13:36:06 +01:00
Fixes #187
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.
This commit is contained in:
parent
adf4e7c58b
commit
aa0336d62a
@ -14,6 +14,7 @@ import org.eclipse.jdt.annotation.Nullable;
|
||||
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;
|
||||
@ -149,6 +150,9 @@ public class ChallengesPanel extends CommonPanel
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Updates level status list and selects last unlocked level.
|
||||
*/
|
||||
private void updateLevelList()
|
||||
{
|
||||
this.levelList = this.manager.getAllChallengeLevelStatus(this.user, this.world);
|
||||
@ -167,6 +171,35 @@ public class ChallengesPanel extends CommonPanel
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
@ -257,9 +290,19 @@ public class ChallengesPanel extends CommonPanel
|
||||
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());
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "COMPLETE_MAX":
|
||||
if (challenge.isRepeatable())
|
||||
@ -272,10 +315,20 @@ public class ChallengesPanel extends CommonPanel
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "MULTIPLE_PANEL":
|
||||
if (challenge.isRepeatable())
|
||||
@ -290,6 +343,7 @@ public class ChallengesPanel extends CommonPanel
|
||||
this.permissionPrefix,
|
||||
value);
|
||||
|
||||
this.updateLevelListSilent();
|
||||
this.build();
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user