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,31 +41,30 @@ 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 -> {
CompositeCommand acidIslandCmd = getPlugin().getCommandsManager().getCommand("ai");
this.getPlugin().getAddonsManager().getAddonByName("AcidIsland").ifPresent(a -> { if (acidIslandCmd != null) {
CompositeCommand acidIslandCmd = getPlugin().getCommandsManager().getCommand("ai"); new ChallengesCommand(this, acidIslandCmd);
if (acidIslandCmd != null) { CompositeCommand acidCmd = getPlugin().getCommandsManager().getCommand("acid");
new ChallengesCommand(this, acidIslandCmd); new Challenges(this, acidCmd);
CompositeCommand acidCmd = getPlugin().getCommandsManager().getCommand("acid"); hooked = true;
new Challenges(this, acidCmd); }
hooked = true; });
} getPlugin().getAddonsManager().getAddonByName("BSkyBlock").ifPresent(a -> {
}); // BSkyBlock hook in
this.getPlugin().getAddonsManager().getAddonByName("BSkyBlock").ifPresent(a -> { CompositeCommand bsbIslandCmd = getPlugin().getCommandsManager().getCommand("island");
// BSkyBlock hook in if (bsbIslandCmd != null) {
CompositeCommand bsbIslandCmd = getPlugin().getCommandsManager().getCommand("island"); new ChallengesCommand(this, bsbIslandCmd);
if (bsbIslandCmd != null) { CompositeCommand bsbAdminCmd = getPlugin().getCommandsManager().getCommand("bsbadmin");
new ChallengesCommand(this, bsbIslandCmd); new Challenges(this, bsbAdminCmd);
CompositeCommand bsbAdminCmd = getPlugin().getCommandsManager().getCommand("bsbadmin"); hooked = true;
new Challenges(this, bsbAdminCmd); }
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
if (!user.getInventory().containsAtLeast(req, req.getAmount())) { switch (req.getType()) {
user.sendMessage("challenges.error.not-enough-items", "[items]", Util.prettifyText(req.getType().toString())); case FIREWORK_ROCKET:
return new ChallengeResult(); case ENCHANTED_BOOK:
} case WRITTEN_BOOK:
} case FILLED_MAP:
// If remove items, then remove them // Get how many items are in the inventory. Item stacks amounts need to be summed
if (challenge.isTakeItems()) { int numInInventory = Arrays.stream(user.getInventory().getContents()).filter(Objects::nonNull).filter(i -> i.getType().equals(req.getType())).mapToInt(i -> i.getAmount()).sum();
for (ItemStack items : required) { if (numInInventory < req.getAmount()) {
user.getInventory().removeItem(items); 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())) {
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<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));
} }