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
This commit is contained in:
tastybento 2018-11-12 11:35:49 -08:00
parent e64bb169a0
commit b55cc822a9
2 changed files with 71 additions and 31 deletions

View File

@ -41,9 +41,7 @@ public class ChallengesAddon extends Addon {
// Register commands - run one tick later to allow all addons to load // Register commands - run one tick later to allow all addons to load
// AcidIsland hook in // AcidIsland hook in
getServer().getScheduler().runTask(getPlugin(), () -> { getPlugin().getAddonsManager().getAddonByName("AcidIsland").ifPresent(a -> {
this.getPlugin().getAddonsManager().getAddonByName("AcidIsland").ifPresent(a -> {
CompositeCommand acidIslandCmd = getPlugin().getCommandsManager().getCommand("ai"); CompositeCommand acidIslandCmd = getPlugin().getCommandsManager().getCommand("ai");
if (acidIslandCmd != null) { if (acidIslandCmd != null) {
new ChallengesCommand(this, acidIslandCmd); new ChallengesCommand(this, acidIslandCmd);
@ -52,7 +50,7 @@ public class ChallengesAddon extends Addon {
hooked = true; hooked = true;
} }
}); });
this.getPlugin().getAddonsManager().getAddonByName("BSkyBlock").ifPresent(a -> { getPlugin().getAddonsManager().getAddonByName("BSkyBlock").ifPresent(a -> {
// BSkyBlock hook in // BSkyBlock hook in
CompositeCommand bsbIslandCmd = getPlugin().getCommandsManager().getCommand("island"); CompositeCommand bsbIslandCmd = getPlugin().getCommandsManager().getCommand("island");
if (bsbIslandCmd != null) { if (bsbIslandCmd != null) {
@ -62,10 +60,11 @@ public class ChallengesAddon extends Addon {
hooked = true; hooked = true;
} }
}); });
});
// If the add-on never hooks in, then it is useless // If the add-on never hooks in, then it is useless
if (!hooked) { if (!hooked) {
logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!"); 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 // Try to find Level addon and if it does not exist, display a warning
if (!getAddonByName("Level").isPresent()) { if (!getAddonByName("Level").isPresent()) {

View File

@ -4,9 +4,13 @@
package bentobox.addon.challenges.panel; package bentobox.addon.challenges.panel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -134,18 +138,55 @@ public class TryToComplete {
// Run through inventory // Run through inventory
List<ItemStack> required = new ArrayList<>(challenge.getRequiredItems()); List<ItemStack> required = new ArrayList<>(challenge.getRequiredItems());
for (ItemStack req : required) { for (ItemStack req : required) {
// I wonder how well this works // 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<ItemStack> 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<Integer, ItemStack> 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())) { if (!user.getInventory().containsAtLeast(req, req.getAmount())) {
user.sendMessage("challenges.error.not-enough-items", "[items]", Util.prettifyText(req.getType().toString())); user.sendMessage("challenges.error.not-enough-items", "[items]", Util.prettifyText(req.getType().toString()));
return new ChallengeResult(); return new ChallengeResult();
} }
}
// If remove items, then remove them // If remove items, then remove them
if (challenge.isTakeItems()) { if (challenge.isTakeItems()) {
for (ItemStack items : required) { for (ItemStack items : required) {
user.getInventory().removeItem(items); HashMap<Integer, ItemStack> 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)); return new ChallengeResult().setMeetsRequirements().setRepeat(manager.isChallengeComplete(user, challenge.getUniqueId(), world));
} }