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:
BONNe 2021-09-24 12:26:44 +03:00
parent adf4e7c58b
commit aa0336d62a

View File

@ -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();
});
}