Implement ChallengeLevel completion rewards.

Add methods isLevelDone() and addCompletedLevel() to ChallengesPlayerData object.
Add isLevelCompleted(), validateLevelCompletion() and setLevelComplete() to ChallengesManager.

Add check in TryToComplete after completing challenges first time.
This commit is contained in:
BONNe 2019-01-25 00:54:21 +02:00
parent 2a4b892ed7
commit 089f09dd45
3 changed files with 111 additions and 4 deletions

View File

@ -574,6 +574,49 @@ public class ChallengesManager
}
/**
* This method returns if given user has been already completed given level.
* @param level Level that must be checked.
* @param user User who need to be checked.
* @return true, if level is already completed.
*/
public boolean isLevelCompleted(User user, ChallengeLevel level)
{
this.addPlayer(user);
return this.playerCacheData.get(user.getUniqueId()).isLevelDone(level.getUniqueId());
}
/**
* This method checks all level challenges and checks if all challenges are done.
* @param level Level that must be checked.
* @param user User who need to be checked.
* @return true, if all challenges are done, otherwise false.
*/
public boolean validateLevelCompletion(User user, ChallengeLevel level)
{
this.addPlayer(user);
ChallengesPlayerData playerData = this.playerCacheData.get(user.getUniqueId());
long doneChallengeCount = level.getChallenges().stream().filter(playerData::isChallengeDone).count();
return level.getChallenges().size() == doneChallengeCount;
}
/**
* This method sets given level as completed.
* @param level Level that must be completed.
* @param user User who complete level.
*/
public void setLevelComplete(User user, ChallengeLevel level)
{
this.addPlayer(user);
this.playerCacheData.get(user.getUniqueId()).addCompletedLevel(level.getUniqueId());
// Save
this.savePlayer(user.getUniqueId());
}
// ---------------------------------------------------------------------
// Section: Challenges related methods
// ---------------------------------------------------------------------

View File

@ -1,14 +1,13 @@
package world.bentobox.challenges.database.object;
import com.google.gson.annotations.Expose;
import org.bukkit.World;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.bukkit.World;
import com.google.gson.annotations.Expose;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.util.Util;
@ -235,6 +234,27 @@ public class ChallengesPlayerData implements DataObject
}
/**
* This method adds given level id to completed level set.
* @param uniqueId from ChallengeLevel object.
*/
public void addCompletedLevel(String uniqueId)
{
this.levelsDone.add(uniqueId);
}
/**
* This method returns if given level is done.
* @param uniqueId of ChallengeLevel object.
* @return <code>true</code> if level is completed, otherwise <code>false</code>
*/
public boolean isLevelDone(String uniqueId)
{
return !this.levelsDone.isEmpty() && this.levelsDone.contains(uniqueId);
}
/**
* @see Object#hashCode()
* @return object hashCode value.

View File

@ -21,6 +21,7 @@ import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.ChallengesManager;
import world.bentobox.challenges.database.object.Challenge;
import world.bentobox.challenges.database.object.Challenge.ChallengeType;
import world.bentobox.challenges.database.object.ChallengeLevel;
/**
@ -243,6 +244,49 @@ public class TryToComplete
// Mark as complete
this.manager.setChallengeComplete(this.user, this.challenge);
if (!result.repeat)
{
ChallengeLevel level = this.manager.getLevel(this.challenge);
if (!this.manager.isLevelCompleted(this.user, level))
{
if (this.manager.validateLevelCompletion(this.user, level))
{
// Item rewards
for (ItemStack reward : level.getRewardItems())
{
this.user.getInventory().addItem(reward).forEach((k, v) ->
this.user.getWorld().dropItem(this.user.getLocation(), v));
}
// Money Reward
if (this.addon.isEconomyProvided())
{
this.addon.getEconomyProvider().deposit(this.user, level.getRewardMoney());
}
// Experience Reward
this.user.getPlayer().giveExp(level.getRewardExperience());
// Run commands
this.runCommands(level.getRewardCommands());
this.user.sendMessage("challenges.you-completed-level", "[level]", level.getFriendlyName());
if (this.addon.getChallengesSettings().isBroadcastMessages())
{
for (Player p : this.addon.getServer().getOnlinePlayers())
{
User.getInstance(p).sendMessage("challenges.name-has-completed-level",
"[name]", this.user.getName(), "[level]", level.getFriendlyName());
}
}
this.manager.setLevelComplete(this.user, level);
}
}
}
return result;
}