Skip to content

Commit

Permalink
Create EMPTY_RESULT variable to avoid so many new ChallengeResult obj…
Browse files Browse the repository at this point in the history
…ect initializations.
  • Loading branch information
BONNe committed Jan 24, 2019
1 parent e0f3820 commit 2a4b892
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/main/java/world/bentobox/challenges/panel/TryToComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public class TryToComplete
*/
private Challenge challenge;

/**
* Variable that will be used to avoid multiple empty object generation.
*/
private final ChallengeResult EMPTY_RESULT = new ChallengeResult();

// ---------------------------------------------------------------------
// Section: Builder
Expand Down Expand Up @@ -249,57 +253,69 @@ public ChallengeResult build()
*/
private ChallengeResult checkIfCanCompleteChallenge()
{
ChallengeResult result;

ChallengeType type = this.challenge.getChallengeType();

// Check the world
if (!this.challenge.getUniqueId().startsWith(Util.getWorld(this.world).getName()))
{
this.user.sendMessage("general.errors.wrong-world");
result = EMPTY_RESULT;
}
// Check if user has unlocked challenges level.
else if (!this.challenge.getLevel().equals(ChallengesManager.FREE) &&
!this.manager.isLevelUnlocked(this.user, this.world, this.manager.getLevel(this.challenge.getLevel())))
{
this.user.sendMessage("challenges.errors.challenge-level-not-available");
result = EMPTY_RESULT;
}
// Check max times
else if (this.challenge.isRepeatable() && this.challenge.getMaxTimes() > 0 &&
this.manager.getChallengeTimes(this.user, this.challenge) >= this.challenge.getMaxTimes())
{
this.user.sendMessage("challenges.not-repeatable");
result = EMPTY_RESULT;
}
// Check repeatability
else if (this.manager.isChallengeComplete(this.user, this.challenge)
&& (!this.challenge.isRepeatable() || type.equals(ChallengeType.ISLAND)))
{
this.user.sendMessage("challenges.not-repeatable");
result = EMPTY_RESULT;
}
// Check environment
else if (!this.challenge.getEnvironment().isEmpty() &&
!this.challenge.getEnvironment().contains(this.user.getWorld().getEnvironment()))
{
this.user.sendMessage("challenges.errors.wrong-environment");
result = EMPTY_RESULT;
}
// Check permission
else if (!this.checkPermissions())
{
this.user.sendMessage("general.errors.no-permission");
result = EMPTY_RESULT;
}
else if (type.equals(ChallengeType.INVENTORY))
{
return this.checkInventory();
result = this.checkInventory();
}
else if (type.equals(ChallengeType.ISLAND))
{
return this.checkSurrounding();
result = this.checkSurrounding();
}
else if (type.equals(ChallengeType.OTHER))
{
return this.checkOthers();
result = this.checkOthers();
}
else
{
result = EMPTY_RESULT;
}

// Everything fails till this point.
return new ChallengeResult();
return result;
}


Expand Down Expand Up @@ -407,7 +423,7 @@ private ChallengeResult checkInventory()
this.user.sendMessage("challenges.error.not-enough-items",
"[items]",
Util.prettifyText(req.getType().toString()));
return new ChallengeResult();
return EMPTY_RESULT;
}
break;
default:
Expand All @@ -417,7 +433,7 @@ private ChallengeResult checkInventory()
this.user.sendMessage("challenges.error.not-enough-items",
"[items]",
Util.prettifyText(req.getType().toString()));
return new ChallengeResult();
return EMPTY_RESULT;
}
}
}
Expand Down Expand Up @@ -500,7 +516,7 @@ private ChallengeResult checkSurrounding()
{
// Player is not on island
this.user.sendMessage("challenges.error.not-on-island");
result = new ChallengeResult();
result = EMPTY_RESULT;
}
else
{
Expand Down Expand Up @@ -569,7 +585,7 @@ private ChallengeResult searchForBlocks(Map<Material, Integer> map, int searchRa
"[amount]", String.valueOf(v),
"[item]", Util.prettifyText(k.toString())));

return new ChallengeResult();
return EMPTY_RESULT;
}


Expand Down Expand Up @@ -598,7 +614,7 @@ private ChallengeResult searchForEntities(Map<EntityType, Integer> map, int sear
"[amount]", String.valueOf(amount),
"[item]", Util.prettifyText(reqEnt.toString())));

return new ChallengeResult();
return EMPTY_RESULT;
}


Expand Down Expand Up @@ -703,7 +719,7 @@ else if (!this.addon.isLevelProvided() ||
return new ChallengeResult().setMeetsRequirements();
}

return new ChallengeResult();
return EMPTY_RESULT;
}


Expand Down

0 comments on commit 2a4b892

Please sign in to comment.