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
// 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()) {

View File

@ -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<ItemStack> 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<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));
}