Fix for taking items even if challenge incomplete

https://github.com/BentoBoxWorld/addon-challenges/issues/27

Also added admin challenge reset command.
This commit is contained in:
tastybento 2018-11-18 19:39:14 -08:00
parent e5e022754b
commit ac80a7e7e4
8 changed files with 209 additions and 27 deletions

View File

@ -6,7 +6,7 @@
<groupId>us.tastybento</groupId> <groupId>us.tastybento</groupId>
<artifactId>Challenges</artifactId> <artifactId>Challenges</artifactId>
<version>0.2.1-SNAPSHOT</version> <version>0.2.2-SNAPSHOT</version>
<name>Challenges</name> <name>Challenges</name>
<description>Challenges is an add-on for BentoBox, an expandable Minecraft Bukkit plugin for island-type games like ASkyBlock or AcidIsland.</description> <description>Challenges is an add-on for BentoBox, an expandable Minecraft Bukkit plugin for island-type games like ASkyBlock or AcidIsland.</description>

View File

@ -398,6 +398,18 @@ public class ChallengesManager {
playerData.get(user.getUniqueId()).setChallengeDone(world, challengeUniqueId); playerData.get(user.getUniqueId()).setChallengeDone(world, challengeUniqueId);
} }
/**
* Reset the challenge to zero time / not done
* @param user - user
* @param challengeUniqueId - unique challenge id
* @param world - world to set
*/
public void setResetChallenge(User user, String challengeUniqueId, World world) {
addPlayer(user);
playerData.get(user.getUniqueId()).setChallengeTimes(world, challengeUniqueId, 0);
}
/** /**
* @param challengeList the challengeList to set * @param challengeList the challengeList to set
*/ */
@ -485,4 +497,6 @@ public class ChallengesManager {
Arrays.asList(string.split("\\|")).forEach(line -> result.addAll(Arrays.asList(WordUtils.wrap(line,25).split("\\n")))); Arrays.asList(string.split("\\|")).forEach(line -> result.addAll(Arrays.asList(WordUtils.wrap(line,25).split("\\n"))));
return result; return result;
} }
} }

View File

@ -27,6 +27,7 @@ public class Challenges extends CompositeCommand {
new ImportCommand(getAddon(), this); new ImportCommand(getAddon(), this);
new CompleteChallenge(getAddon(), this); new CompleteChallenge(getAddon(), this);
new ReloadChallenges(getAddon(), this); new ReloadChallenges(getAddon(), this);
new ResetChallenge(getAddon(), this);
//new ShowChallenges(getAddon(), this); //new ShowChallenges(getAddon(), this);
//new CreateChallenge(getAddon(), this); //new CreateChallenge(getAddon(), this);

View File

@ -0,0 +1,77 @@
package bentobox.addon.challenges.commands.admin;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import bentobox.addon.challenges.ChallengesAddon;
import bentobox.addon.challenges.ChallengesManager;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
public class ResetChallenge extends CompositeCommand {
private ChallengesManager manager;
/**
* Admin command to complete user challenges
* @param parent
*/
public ResetChallenge(Addon addon, CompositeCommand parent) {
super(addon, parent, "reset");
}
@Override
public void setup() {
this.setPermission("admin.challenges");
this.setParametersHelp("challenges.admin.reset.parameters");
this.setDescription("challenges.admin.reset.description");
manager = ((ChallengesAddon)getAddon()).getChallengesManager();
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (args.size() != 2) {
// Show help
showHelp(this, user);
return false;
}
// Get target player
UUID targetUUID = getPlayers().getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
if (!getPlugin().getIslands().hasIsland(getWorld(), targetUUID)) {
user.sendMessage("general.errors.player-has-no-island");
return false;
}
// Check for valid challenge name
if (!manager.isChallenge(getWorld(), args.get(1))) {
user.sendMessage("challenges.admin.complete.unknown-challenge");
return false;
}
// Complete challenge
User target = User.getInstance(targetUUID);
manager.setResetChallenge(target, args.get(1), getWorld());
user.sendMessage("general.success");
return true;
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (args.size() == 3) {
// Online players
return Optional.of(Util.tabLimit(new ArrayList<>(Util.getOnlinePlayerList(user)), lastArg));
} else if (args.size() == 4) {
// Challenges in this world
return Optional.of(Util.tabLimit(manager.getAllChallengesList(getWorld()), lastArg));
}
return Optional.empty();
}
}

View File

@ -21,7 +21,7 @@ import world.bentobox.bentobox.util.Util;
* *
*/ */
public class ChallengesPlayerData implements DataObject { public class ChallengesPlayerData implements DataObject {
@Expose @Expose
private String uniqueId = ""; private String uniqueId = "";
/** /**
@ -39,6 +39,7 @@ public class ChallengesPlayerData implements DataObject {
/** /**
* Mark a challenge as having been completed. Will increment the number of times and timestamp * Mark a challenge as having been completed. Will increment the number of times and timestamp
* @param world - world of challenge
* @param challengeName - unique challenge name * @param challengeName - unique challenge name
*/ */
public void setChallengeDone(World world, String challengeName) { public void setChallengeDone(World world, String challengeName) {
@ -48,6 +49,19 @@ public class ChallengesPlayerData implements DataObject {
challengesTimestamp.put(name, System.currentTimeMillis()); challengesTimestamp.put(name, System.currentTimeMillis());
} }
/**
* Set the number of times a challenge has been done
* @param world - world of challenge
* @param challengeName - unique challenge name
* @param times - the number of times to set
*
*/
public void setChallengeTimes(World world, String challengeName, int times) {
String name = Util.getWorld(world).getName() + challengeName;
challengeStatus.put(name, times);
challengesTimestamp.put(name, System.currentTimeMillis());
}
/** /**
* Check if a challenge has been done * Check if a challenge has been done
* @param challengeName - unique challenge name * @param challengeName - unique challenge name

View File

@ -97,7 +97,9 @@ public class ChallengesPanels {
.glow(completed) .glow(completed)
.clickHandler((panel, player, c, s) -> { .clickHandler((panel, player, c, s) -> {
if (!challenge.getChallengeType().equals(ChallengeType.ICON)) { if (!challenge.getChallengeType().equals(ChallengeType.ICON)) {
new TryToComplete(addon, player, manager, challenge, world, permPrefix, label); new TryToComplete(addon).user(player).manager(manager).challenge(challenge)
.world(world).permPrefix(permPrefix).label(label).build();
//new TryToComplete(addon, player, manager, challenge, world, permPrefix, label);
} }
return true; return true;
}) })

View File

@ -42,6 +42,85 @@ public class TryToComplete {
private User user; private User user;
private ChallengesManager manager; private ChallengesManager manager;
private Challenges challenge; private Challenges challenge;
private String label;
public TryToComplete label(String label) {
this.label = label;
return this;
}
public TryToComplete user(User user) {
this.user = user;
return this;
}
public TryToComplete manager(ChallengesManager manager) {
this.manager = manager;
return this;
}
public TryToComplete challenge(Challenges challenge) {
this.challenge = challenge;
return this;
}
public TryToComplete world(World world) {
this.world = world;
return this;
}
public TryToComplete permPrefix(String prefix) {
this.permPrefix = prefix;
return this;
}
public TryToComplete(ChallengesAddon addon) {
this.addon = addon;
}
public ChallengeResult build() {
// Check if can complete challenge
ChallengeResult result = checkIfCanCompleteChallenge();
if (!result.meetsRequirements) {
return result;
}
if (!result.repeat) {
// Give rewards
for (ItemStack reward : challenge.getRewardItems()) {
user.getInventory().addItem(reward).forEach((k,v) -> user.getWorld().dropItem(user.getLocation(), v));
}
// Give money
challenge.getRewardMoney();
// Give exp
user.getPlayer().giveExp(challenge.getRewardExp());
// Run commands
runCommands(challenge.getRewardCommands());
user.sendMessage("challenges.you-completed", "[challenge]", challenge.getFriendlyName());
if (addon.getConfig().getBoolean("broadcastmessages", false)) {
for (Player p : addon.getServer().getOnlinePlayers()) {
User.getInstance(p).sendMessage("challenges.name-has-completed",
"[name]", user.getName(), "[challenge]", challenge.getFriendlyName());
}
}
} else {
// Give rewards
for (ItemStack reward : challenge.getRepeatItemReward()) {
user.getInventory().addItem(reward).forEach((k,v) -> user.getWorld().dropItem(user.getLocation(), v));
}
// Give money
challenge.getRepeatMoneyReward();
// Give exp
user.getPlayer().giveExp(challenge.getRepeatExpReward());
// Run commands
runCommands(challenge.getRepeatRewardCommands());
user.sendMessage("challenges.you-repeated", "[challenge]", challenge.getFriendlyName());
}
// Mark as complete
manager.setChallengeComplete(user, challenge.getUniqueId(), world);
user.closeInventory();
user.getPlayer().performCommand(label + " " + ChallengesCommand.CHALLENGE_COMMAND + " " + challenge.getLevel());
return result;
}
/** /**
* @param addon * @param addon
@ -150,23 +229,6 @@ public class TryToComplete {
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 (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; break;
default: default:
// General checking // General checking
@ -174,19 +236,28 @@ public class TryToComplete {
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 (challenge.isTakeItems()) {
for (ItemStack items : required) { }
HashMap<Integer, ItemStack> remaining = user.getInventory().removeItem(items); // If remove items, then remove them
if (challenge.isTakeItems()) {
for (ItemStack req : required) {
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()) { if (!remaining.isEmpty()) {
remaining.forEach((k,v) -> addon.logError("Could not remove items: " + v)); remaining.forEach((k,v) -> addon.logError("Could not remove items: " + v));
} else {
amountToBeRemoved -= i.getAmount();
} }
} }
} }
} }
} }
// Return the result
return new ChallengeResult().setMeetsRequirements().setRepeat(manager.isChallengeComplete(user, challenge.getUniqueId(), world)); return new ChallengeResult().setMeetsRequirements().setRepeat(manager.isChallengeComplete(user, challenge.getUniqueId(), world));
} }

View File

@ -74,9 +74,12 @@ challenges:
overwriting: "Overwriting '[challenge]'" overwriting: "Overwriting '[challenge]'"
imported: "Imported '[challenge]'" imported: "Imported '[challenge]'"
complete: complete:
description: "Marks challenge complete" description: "Mark challenge complete"
parameters: "<player> <unique challenge name>" parameters: "<player> <unique challenge name>"
unknown-challenge: "&cUnknown challenge" unknown-challenge: "&cUnknown challenge"
reset:
description: "Reset challenge to 0 times / incomplete"
parameters: "<player> <unique challenge name>"
create: create:
description: "&6Collect:" description: "&6Collect:"
description-item-color: "&B" description-item-color: "&B"