From c2051d23e589879818b75b7056a60c2df3a1c2ec Mon Sep 17 00:00:00 2001 From: BONNe1704 Date: Mon, 17 Dec 2018 15:00:22 +0200 Subject: [PATCH] Implement Money support for challenges. #25 If VaultHook is found, then add money to user account. Add check on Required XP and Required Money. Enable Money withdraw if isTakeMoney() is true. --- .../addon/challenges/panel/TryToComplete.java | 80 +++++++++++++++++-- src/main/resources/locales/en-US.yml | 4 +- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java b/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java index 6020001..8e2e257 100644 --- a/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java +++ b/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java @@ -10,6 +10,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.stream.Collectors; import org.bukkit.Material; @@ -26,6 +27,7 @@ import bentobox.addon.challenges.database.object.Challenges; import bentobox.addon.challenges.database.object.Challenges.ChallengeType; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.hooks.VaultHook; import world.bentobox.bentobox.util.Util; import world.bentobox.level.Level; @@ -89,8 +91,11 @@ public class TryToComplete { for (ItemStack reward : challenge.getRewardItems()) { user.getInventory().addItem(reward).forEach((k,v) -> user.getWorld().dropItem(user.getLocation(), v)); } + // Give money - challenge.getRewardMoney(); + this.addon.getPlugin().getVault().ifPresent( + vaultHook -> vaultHook.deposit(this.user, this.challenge.getRewardMoney())); + // Give exp user.getPlayer().giveExp(challenge.getRewardExp()); // Run commands @@ -107,8 +112,11 @@ public class TryToComplete { for (ItemStack reward : challenge.getRepeatItemReward()) { user.getInventory().addItem(reward).forEach((k,v) -> user.getWorld().dropItem(user.getLocation(), v)); } - // Give money - challenge.getRepeatMoneyReward(); + + // Give money + this.addon.getPlugin().getVault().ifPresent( + vaultHook -> vaultHook.deposit(this.user, this.challenge.getRepeatMoneyReward())); + // Give exp user.getPlayer().giveExp(challenge.getRepeatExpReward()); // Run commands @@ -148,8 +156,11 @@ public class TryToComplete { for (ItemStack reward : challenge.getRewardItems()) { user.getInventory().addItem(reward).forEach((k,v) -> user.getWorld().dropItem(user.getLocation(), v)); } - // Give money - challenge.getRewardMoney(); + + // Give money + this.addon.getPlugin().getVault().ifPresent( + vaultHook -> vaultHook.deposit(this.user, this.challenge.getRewardMoney())); + // Give exp user.getPlayer().giveExp(challenge.getRewardExp()); // Run commands @@ -166,8 +177,11 @@ public class TryToComplete { for (ItemStack reward : challenge.getRepeatItemReward()) { user.getInventory().addItem(reward).forEach((k,v) -> user.getWorld().dropItem(user.getLocation(), v)); } + // Give money - challenge.getRepeatMoneyReward(); + this.addon.getPlugin().getVault().ifPresent( + vaultHook -> vaultHook.deposit(this.user, this.challenge.getRepeatMoneyReward())); + // Give exp user.getPlayer().giveExp(challenge.getRepeatExpReward()); // Run commands @@ -206,6 +220,26 @@ public class TryToComplete { user.sendMessage("challenges.not-repeatable"); return new ChallengeResult(); } + + // Check money + Optional vaultHook = this.addon.getPlugin().getVault(); + + if (vaultHook.isPresent()) + { + if (!vaultHook.get().has(this.user, this.challenge.getReqMoney())) + { + this.user.sendMessage("challenges.not-enough-money"); + return new ChallengeResult(); + } + } + + // Check exp + if (this.user.getPlayer().getTotalExperience() < this.challenge.getReqExp()) + { + this.user.sendMessage("challenges.not-enough-exp"); + return new ChallengeResult(); + } + switch (challenge.getChallengeType()) { case INVENTORY: return checkInventory(); @@ -249,10 +283,33 @@ public class TryToComplete { removeItems(required); } + + // process money removal + this.removeMoney(); + // Return the result return new ChallengeResult().setMeetsRequirements().setRepeat(manager.isChallengeComplete(user, challenge.getUniqueId(), world)); } + + /** + * This method withdraw user money, if challenge Required Money is larger then 0, and + * it is set to removal. + * This works only if vaultHook is enabled. + */ + private void removeMoney() + { + Optional vaultHook = this.addon.getPlugin().getVault(); + + if (vaultHook.isPresent() && + this.challenge.isTakeMoney() && + this.challenge.getReqMoney() > 0) + { + vaultHook.get().withdraw(this.user, this.challenge.getReqMoney()); + } + } + + /** * Removes items from a user's inventory * @param required - a list of item stacks to be removed @@ -290,7 +347,9 @@ public class TryToComplete { long level = addon.getAddonByName("Level") .map(l -> ((Level)l).getIslandLevel(world, user.getUniqueId())).orElse(0L); if (level >= challenge.getReqIslandlevel()) { - return new ChallengeResult().setMeetsRequirements(); + // process money removal + this.removeMoney(); + return new ChallengeResult().setMeetsRequirements(); } else { user.sendMessage("challenges.error.island-level", TextVariables.NUMBER, String.valueOf(challenge.getReqIslandlevel())); return new ChallengeResult(); @@ -309,6 +368,13 @@ public class TryToComplete { // Search for items only if entities found result = searchForBlocks(challenge.getRequiredBlocks(), challenge.getSearchRadius()); } + + if (result.meetsRequirements && this.challenge.isTakeMoney()) + { + // process money removal + this.removeMoney(); + } + return result; } diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index c4138f4..cfbd361 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -98,4 +98,6 @@ challenges: rewards: "&FReward(s)" to-complete: "Complete [challengesToDo] more [thisLevel] challenges to unlock this level!" you-completed: "You completed the [challenge] challenge!" - you-repeated: "You repeated the [challenge] challenge!" \ No newline at end of file + you-repeated: "You repeated the [challenge] challenge!" + not-enough-money: "It is necessary to have [money] on your account to complete the challenge." + not-enough-exp: "It is necessary to have [xp] EXP to complete challenge." \ No newline at end of file