Fix NPE when opening ChallengesGUI (#116).

Add code that removes challenges level from all its challenges, if level is deleted.
Add protection code that shows admins that something is not correct if challenge level is not in database.
This commit is contained in:
BONNe1704 2019-05-09 11:22:01 +03:00
parent dd56842dca
commit 6c10fcdca9
4 changed files with 32 additions and 5 deletions

View File

@ -1462,6 +1462,20 @@ public class ChallengesManager
if (this.levelCacheData.containsKey(challengeLevel.getUniqueId())) if (this.levelCacheData.containsKey(challengeLevel.getUniqueId()))
{ {
this.levelCacheData.remove(challengeLevel.getUniqueId()); this.levelCacheData.remove(challengeLevel.getUniqueId());
// Remove challenge level from challenges object.
if (!challengeLevel.getChallenges().isEmpty())
{
challengeLevel.getChallenges().forEach(challengeID -> {
Challenge challenge = this.getChallenge(challengeID);
if (challenge != null)
{
challenge.setLevel(ChallengesManager.FREE);
}
});
}
this.levelDatabase.deleteObject(challengeLevel); this.levelDatabase.deleteObject(challengeLevel);
} }
} }

View File

@ -298,8 +298,19 @@ public abstract class CommonGUI
{ {
case 'l': case 'l':
{ {
result.add(this.user.getTranslation("challenges.gui.challenge-description.level", ChallengeLevel level = manager.getLevel(challenge);
"[level]", manager.getLevel(challenge).getFriendlyName()));
if (level == null)
{
result.add(this.user.getTranslation("challenges.errors.missing-level",
"[level]", challenge.getLevel()));
}
else
{
result.add(this.user.getTranslation("challenges.gui.challenge-description.level",
"[level]", level.getFriendlyName()));
}
break; break;
} }
case 's': case 's':

View File

@ -359,12 +359,13 @@ public class TryToComplete
// Mark as complete // Mark as complete
this.manager.setChallengeComplete(this.user, this.world, this.challenge, result.getFactor()); this.manager.setChallengeComplete(this.user, this.world, this.challenge, result.getFactor());
// Check level completion. // Check level completion for non-free challenges
if (!result.wasCompleted()) if (!result.wasCompleted() &&
!this.challenge.getLevel().equals(ChallengesManager.FREE))
{ {
ChallengeLevel level = this.manager.getLevel(this.challenge); ChallengeLevel level = this.manager.getLevel(this.challenge);
if (!this.manager.isLevelCompleted(this.user, this.world, level)) if (level != null && !this.manager.isLevelCompleted(this.user, this.world, level))
{ {
if (this.manager.validateLevelCompletion(this.user, this.world, level)) if (this.manager.validateLevelCompletion(this.user, this.world, level))
{ {

View File

@ -364,6 +364,7 @@ challenges:
defaults-file-exist: '&cdefault.json already exists. Use overwrite mode to replace it!' defaults-file-exist: '&cdefault.json already exists. Use overwrite mode to replace it!'
defaults-file-error: '&cThere was an error while creating default.json file! Check console!' defaults-file-error: '&cThere was an error while creating default.json file! Check console!'
no-challenges: '&cChallenges are not implemented in current world!' no-challenges: '&cChallenges are not implemented in current world!'
missing-level: '&cChallenge Level [level] is not defined in database. It may case some errors!'
protection: protection:
flags: flags:
CHALLENGES_ISLAND_PROTECTION: CHALLENGES_ISLAND_PROTECTION: