diff --git a/src/main/java/world/bentobox/challenges/ChallengesManager.java b/src/main/java/world/bentobox/challenges/ChallengesManager.java
index 9278ba1..9b6e40a 100644
--- a/src/main/java/world/bentobox/challenges/ChallengesManager.java
+++ b/src/main/java/world/bentobox/challenges/ChallengesManager.java
@@ -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
// ---------------------------------------------------------------------
diff --git a/src/main/java/world/bentobox/challenges/database/object/ChallengesPlayerData.java b/src/main/java/world/bentobox/challenges/database/object/ChallengesPlayerData.java
index afc84a0..9907099 100644
--- a/src/main/java/world/bentobox/challenges/database/object/ChallengesPlayerData.java
+++ b/src/main/java/world/bentobox/challenges/database/object/ChallengesPlayerData.java
@@ -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 true
if level is completed, otherwise false
+ */
+ public boolean isLevelDone(String uniqueId)
+ {
+ return !this.levelsDone.isEmpty() && this.levelsDone.contains(uniqueId);
+ }
+
+
/**
* @see Object#hashCode()
* @return object hashCode value.
diff --git a/src/main/java/world/bentobox/challenges/panel/TryToComplete.java b/src/main/java/world/bentobox/challenges/panel/TryToComplete.java
index 64ffb63..0a19811 100644
--- a/src/main/java/world/bentobox/challenges/panel/TryToComplete.java
+++ b/src/main/java/world/bentobox/challenges/panel/TryToComplete.java
@@ -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;
}