From b55cc822a9f9b617288cda38cb67df5eec7a092d Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 12 Nov 2018 11:35:49 -0800 Subject: [PATCH] Made generic some required items. FIREWORK_ROCKET, ENCHANTED_BOOK, WRITTEN_BOOK and FILLED_MAP will be checked generically instead of specifically because meta data can be different when created each time. https://github.com/BentoBoxWorld/addon-challenges/issues/27 --- .../addon/challenges/ChallengesAddon.java | 41 ++++++------- .../addon/challenges/panel/TryToComplete.java | 61 ++++++++++++++++--- 2 files changed, 71 insertions(+), 31 deletions(-) diff --git a/src/main/java/bentobox/addon/challenges/ChallengesAddon.java b/src/main/java/bentobox/addon/challenges/ChallengesAddon.java index 7a5ccfe..50aaca7 100644 --- a/src/main/java/bentobox/addon/challenges/ChallengesAddon.java +++ b/src/main/java/bentobox/addon/challenges/ChallengesAddon.java @@ -41,31 +41,30 @@ public class ChallengesAddon extends Addon { // Register commands - run one tick later to allow all addons to load // AcidIsland hook in - getServer().getScheduler().runTask(getPlugin(), () -> { - - this.getPlugin().getAddonsManager().getAddonByName("AcidIsland").ifPresent(a -> { - CompositeCommand acidIslandCmd = getPlugin().getCommandsManager().getCommand("ai"); - if (acidIslandCmd != null) { - new ChallengesCommand(this, acidIslandCmd); - CompositeCommand acidCmd = getPlugin().getCommandsManager().getCommand("acid"); - new Challenges(this, acidCmd); - hooked = true; - } - }); - this.getPlugin().getAddonsManager().getAddonByName("BSkyBlock").ifPresent(a -> { - // BSkyBlock hook in - CompositeCommand bsbIslandCmd = getPlugin().getCommandsManager().getCommand("island"); - if (bsbIslandCmd != null) { - new ChallengesCommand(this, bsbIslandCmd); - CompositeCommand bsbAdminCmd = getPlugin().getCommandsManager().getCommand("bsbadmin"); - new Challenges(this, bsbAdminCmd); - hooked = true; - } - }); + getPlugin().getAddonsManager().getAddonByName("AcidIsland").ifPresent(a -> { + CompositeCommand acidIslandCmd = getPlugin().getCommandsManager().getCommand("ai"); + if (acidIslandCmd != null) { + new ChallengesCommand(this, acidIslandCmd); + CompositeCommand acidCmd = getPlugin().getCommandsManager().getCommand("acid"); + new Challenges(this, acidCmd); + hooked = true; + } + }); + getPlugin().getAddonsManager().getAddonByName("BSkyBlock").ifPresent(a -> { + // BSkyBlock hook in + CompositeCommand bsbIslandCmd = getPlugin().getCommandsManager().getCommand("island"); + if (bsbIslandCmd != null) { + new ChallengesCommand(this, bsbIslandCmd); + CompositeCommand bsbAdminCmd = getPlugin().getCommandsManager().getCommand("bsbadmin"); + new Challenges(this, bsbAdminCmd); + hooked = true; + } }); // If the add-on never hooks in, then it is useless if (!hooked) { logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!"); + this.setState(State.DISABLED); + return; } // Try to find Level addon and if it does not exist, display a warning if (!getAddonByName("Level").isPresent()) { diff --git a/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java b/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java index da6fdea..a80e416 100644 --- a/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java +++ b/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java @@ -4,9 +4,13 @@ package bentobox.addon.challenges.panel; import java.util.ArrayList; +import java.util.Arrays; import java.util.EnumMap; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; import org.bukkit.Material; import org.bukkit.World; @@ -134,18 +138,55 @@ public class TryToComplete { // Run through inventory List required = new ArrayList<>(challenge.getRequiredItems()); for (ItemStack req : required) { - // I wonder how well this works - if (!user.getInventory().containsAtLeast(req, req.getAmount())) { - user.sendMessage("challenges.error.not-enough-items", "[items]", Util.prettifyText(req.getType().toString())); - return new ChallengeResult(); - } - } - // If remove items, then remove them - if (challenge.isTakeItems()) { - for (ItemStack items : required) { - user.getInventory().removeItem(items); + // Check for FIREWORK_ROCKET, ENCHANTED_BOOK, WRITTEN_BOOK, POTION and FILLED_MAP because these have unique meta when created + switch (req.getType()) { + case FIREWORK_ROCKET: + case ENCHANTED_BOOK: + case WRITTEN_BOOK: + case FILLED_MAP: + // Get how many items are in the inventory. Item stacks amounts need to be summed + int numInInventory = Arrays.stream(user.getInventory().getContents()).filter(Objects::nonNull).filter(i -> i.getType().equals(req.getType())).mapToInt(i -> i.getAmount()).sum(); + if (numInInventory < req.getAmount()) { + user.sendMessage("challenges.error.not-enough-items", "[items]", Util.prettifyText(req.getType().toString())); + return new ChallengeResult(); + } + // If remove items, then remove them + if (challenge.isTakeItems()) { + int amountToBeRemoved = req.getAmount(); + List itemsInInv = Arrays.stream(user.getInventory().getContents()).filter(Objects::nonNull).filter(i -> i.getType().equals(req.getType())).collect(Collectors.toList()); + for (ItemStack i : itemsInInv) { + if (amountToBeRemoved > 0) { + // Remove all of this item + HashMap remaining = user.getInventory().removeItem(i); + if (!remaining.isEmpty()) { + remaining.forEach((k,v) -> addon.logError("Could not remove items: " + v)); + } else { + amountToBeRemoved -= i.getAmount(); + } + } + } + + } + break; + default: + // General checking + if (!user.getInventory().containsAtLeast(req, req.getAmount())) { + user.sendMessage("challenges.error.not-enough-items", "[items]", Util.prettifyText(req.getType().toString())); + return new ChallengeResult(); + } + // If remove items, then remove them + if (challenge.isTakeItems()) { + for (ItemStack items : required) { + HashMap remaining = user.getInventory().removeItem(items); + if (!remaining.isEmpty()) { + remaining.forEach((k,v) -> addon.logError("Could not remove items: " + v)); + } + } + } } + } + return new ChallengeResult().setMeetsRequirements().setRepeat(manager.isChallengeComplete(user, challenge.getUniqueId(), world)); }