From 5e0f0510d93ccd4c875cd7353973a10a70d3fa28 Mon Sep 17 00:00:00 2001 From: BONNe Date: Sun, 23 Jun 2019 16:51:49 +0300 Subject: [PATCH] Add admin reset command, that allows to reset player challenges (#141) Fix some bugs in complete command that did not display challenge list. Add new lang parameters in en-US and lv-LV. --- .../commands/CompleteChallengeCommand.java | 26 +-- .../challenges/commands/admin/Challenges.java | 5 +- .../commands/admin/CompleteCommand.java | 19 +- .../commands/admin/ResetCommand.java | 215 ++++++++++++++++++ src/main/resources/locales/en-US.yml | 8 +- src/main/resources/locales/lv-LV.yml | 6 + 6 files changed, 241 insertions(+), 38 deletions(-) create mode 100644 src/main/java/world/bentobox/challenges/commands/admin/ResetCommand.java diff --git a/src/main/java/world/bentobox/challenges/commands/CompleteChallengeCommand.java b/src/main/java/world/bentobox/challenges/commands/CompleteChallengeCommand.java index b114c51..4c2abd9 100644 --- a/src/main/java/world/bentobox/challenges/commands/CompleteChallengeCommand.java +++ b/src/main/java/world/bentobox/challenges/commands/CompleteChallengeCommand.java @@ -27,14 +27,6 @@ public class CompleteChallengeCommand extends CompositeCommand { super(addon, cmd, "complete"); this.addon = (ChallengesAddon) addon; - - if (this.addon.getChallengesManager().hasAnyChallengeData(this.getWorld())) - { - // Strip world name from all challenges - this.challenges = this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream(). - map(challenge -> challenge.replaceFirst(Util.getWorld(this.getWorld()).getName() + "_", "")). - collect(Collectors.toList()); - } } @@ -109,28 +101,27 @@ public class CompleteChallengeCommand extends CompositeCommand { case 3: // Create suggestions with all challenges that is available for users. - - this.challenges.forEach(challenge -> { - returnList.addAll(Util.tabLimit(Collections.singletonList(challenge), lastString)); - }); + returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream(). + map(challenge -> challenge.replaceFirst(Util.getWorld(this.getWorld()).getName() + "_", "")). + collect(Collectors.toList())); break; case 4: // Suggest a number of completions. if (lastString.isEmpty() || lastString.matches("[0-9]*")) { - returnList.addAll(Util.tabLimit(Collections.singletonList(""), lastString)); + returnList.add(""); } break; default: { - returnList.addAll(Util.tabLimit(Collections.singletonList("help"), lastString)); + returnList.add("help"); break; } } - return Optional.of(returnList); + return Optional.of(Util.tabLimit(returnList, lastString)); } @@ -142,9 +133,4 @@ public class CompleteChallengeCommand extends CompositeCommand * Variable that holds challenge addon. Single casting. */ private ChallengesAddon addon; - - /** - * This list contains all challenge IDs without a world name. - */ - private List challenges; } diff --git a/src/main/java/world/bentobox/challenges/commands/admin/Challenges.java b/src/main/java/world/bentobox/challenges/commands/admin/Challenges.java index 529b035..52ac0ff 100644 --- a/src/main/java/world/bentobox/challenges/commands/admin/Challenges.java +++ b/src/main/java/world/bentobox/challenges/commands/admin/Challenges.java @@ -38,8 +38,11 @@ public class Challenges extends CompositeCommand // Defaults processing command new DefaultsCommand(this.getAddon(), this); - // Defaults processing command + // Complete challenge command new CompleteCommand(this.getAddon(), this); + + // Reset challenge command + new ResetCommand(this.getAddon(), this); } diff --git a/src/main/java/world/bentobox/challenges/commands/admin/CompleteCommand.java b/src/main/java/world/bentobox/challenges/commands/admin/CompleteCommand.java index c64dc79..323b002 100644 --- a/src/main/java/world/bentobox/challenges/commands/admin/CompleteCommand.java +++ b/src/main/java/world/bentobox/challenges/commands/admin/CompleteCommand.java @@ -28,14 +28,6 @@ public class CompleteCommand extends CompositeCommand { super(addon, cmd, "complete"); this.addon = (ChallengesAddon) addon; - - if (this.addon.getChallengesManager().hasAnyChallengeData(this.getWorld())) - { - // Strip world name from all challenges - this.challenges = this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream(). - map(challenge -> challenge.replaceFirst(Util.getWorld(this.getWorld()).getName() + "_", "")). - collect(Collectors.toList()); - } } @@ -176,9 +168,9 @@ public class CompleteCommand extends CompositeCommand break; case 4: // Create suggestions with all challenges that is available for users. - this.challenges.forEach(challenge -> { - returnList.addAll(Collections.singletonList(challenge)); - }); + returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream(). + map(challenge -> challenge.replaceFirst(Util.getWorld(this.getWorld()).getName() + "_", "")). + collect(Collectors.toList())); break; default: @@ -200,9 +192,4 @@ public class CompleteCommand extends CompositeCommand * Variable that holds challenge addon. Single casting. */ private ChallengesAddon addon; - - /** - * This list contains all challenge IDs without a world name. - */ - private List challenges; } diff --git a/src/main/java/world/bentobox/challenges/commands/admin/ResetCommand.java b/src/main/java/world/bentobox/challenges/commands/admin/ResetCommand.java new file mode 100644 index 0000000..325e6d3 --- /dev/null +++ b/src/main/java/world/bentobox/challenges/commands/admin/ResetCommand.java @@ -0,0 +1,215 @@ +package world.bentobox.challenges.commands.admin; + + +import java.util.*; +import java.util.stream.Collectors; + +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; +import world.bentobox.challenges.ChallengesAddon; +import world.bentobox.challenges.database.object.Challenge; +import world.bentobox.challenges.tasks.TryToComplete; + + +/** + * This command allows to reset challenges without a gui. + */ +public class ResetCommand extends CompositeCommand +{ + /** + * Default constructor for Composite Command. + * @param addon Challenges addon. + * @param cmd Parent Command. + */ + public ResetCommand(Addon addon, CompositeCommand cmd) + { + super(addon, cmd, "reset"); + this.addon = (ChallengesAddon) addon; + } + + + /** + * {@inheritDoc} + */ + @Override + public void setup() + { + this.setPermission("reset"); + this.setParametersHelp("challenges.commands.admin.reset.parameters"); + this.setDescription("challenges.commands.admin.reset.description"); + } + + + /** + * {@inheritDoc} + */ + @Override + public boolean execute(User user, String label, List args) + { + if (args.isEmpty()) + { + if (user.isPlayer()) + { + user.sendMessage("challenges.errors.no-name"); + } + else + { + this.addon.logError("Missing parameters"); + } + } + else if (args.size() < 2) + { + if (user.isPlayer()) + { + user.sendMessage("challenges.errors.missing-arguments"); + } + else + { + this.addon.logError("Missing parameters"); + } + } + else if (!args.get(1).isEmpty()) + { + UUID targetUUID = this.getPlayers().getUUID(args.get(0)); + + if (targetUUID == null) + { + if (user.isPlayer()) + { + user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0)); + } + else + { + this.addon.logError("Unknonw player name " + args.get(0)); + } + + return false; + } + + // Add world name back at the start + + if (args.get(1).equals("all")) + { + this.addon.getChallengesManager().resetAllChallenges(targetUUID, this.getWorld(), user.getUniqueId()); + + if (user.isPlayer()) + { + user.sendMessage("challenges.messages.admin.reset-all", + "[player]", User.getInstance(targetUUID).getName()); + } + else + { + this.addon.log("All challenges for user " + + User.getInstance(targetUUID).getName() + " was reset!"); + } + + return true; + } + else + { + String challengeName = Util.getWorld(this.getWorld()).getName() + "_" + args.get(1); + Challenge challenge = this.addon.getChallengesManager().getChallenge(challengeName); + + if (challenge != null) + { + if (this.addon.getChallengesManager().isChallengeComplete(targetUUID, this.getWorld(), challenge)) + { + this.addon.getChallengesManager().resetChallenge(targetUUID, this.getWorld(), challenge, user.getUniqueId()); + + if (user.isPlayer()) + { + user.sendMessage("challenges.messages.admin.reset", + "[name]", challenge.getFriendlyName(), + "[player]", User.getInstance(targetUUID).getName()); + } + else + { + this.addon.log("Challenge " + challenge.getFriendlyName() + " was reset for player " + + User.getInstance(targetUUID).getName()); + } + } + else + { + if (user.isPlayer()) + { + user.sendMessage("challenges.messages.admin.not-completed"); + } + else + { + this.addon.log("Challenge is not completed yet"); + } + } + + return true; + } + else + { + if (user.isPlayer()) + { + user.sendMessage("challenges.errors.unknown-challenge"); + } + else + { + this.addon.logError("Unknown challenge " + args.get(1)); + } + + return false; + } + } + } + + this.showHelp(this, user); + return false; + } + + + /** + * {@inheritDoc} + */ + @Override + public Optional> tabComplete(User user, String alias, List args) + { + String lastString = args.get(args.size() - 1); + + final List returnList = new ArrayList<>(); + final int size = args.size(); + + switch (size) + { + case 3: + // Create suggestions with all challenges that is available for users. + + returnList.addAll(Util.getOnlinePlayerList(user)); + break; + case 4: + // Create suggestions with all challenges that is available for users. + returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream(). + map(challenge -> challenge.replaceFirst(Util.getWorld(this.getWorld()).getName() + "_", "")). + collect(Collectors.toList())); + + returnList.add("all"); + + break; + default: + { + returnList.addAll(Collections.singletonList("help")); + break; + } + } + + return Optional.of(Util.tabLimit(returnList, lastString)); + } + + +// --------------------------------------------------------------------- +// Section: Variables +// --------------------------------------------------------------------- + + /** + * Variable that holds challenge addon. Single casting. + */ + private ChallengesAddon addon; +} diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 6fd1052..dc5659f 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -33,6 +33,9 @@ challenges: complete: description: 'This command allows to complete challenge for player without GUI.' parameters: ' ' + reset: + description: 'This command allows to reset challenge for player without GUI. If "challenge_id" is set to "all", then it will reset all challenges.' + parameters: ' ' user: main: description: 'This method opens Challenges GUI.' @@ -333,8 +336,11 @@ challenges: you-added: 'You added one [thing] to the challenge' challenge-created: '[challenge]&r created!' complete-wipe: '&cHope you have backups, as you just empty all Challenges Addon databases!' - completed: '&2You complted challenge [name] for [player]!' + completed: '&2You completed challenge [name] for [player]!' already-completed: '&2This challenge was already completed!' + reset: '&2You reset challenge [name] for [player]!' + reset-all: '&2All [player] challenges were reset!' + not-completed: '&2This challenge is not completed yet!' you-completed-challenge: '&2You completed the [value] &r&2challenge!' you-repeated-challenge: '&2You repeated the [value] &r&2challenge!' you-repeated-challenge-multiple: '&2You repeated the [value] &r&2challenge [count] times!' diff --git a/src/main/resources/locales/lv-LV.yml b/src/main/resources/locales/lv-LV.yml index 2b54010..011487e 100755 --- a/src/main/resources/locales/lv-LV.yml +++ b/src/main/resources/locales/lv-LV.yml @@ -33,6 +33,9 @@ challenges: complete: description: 'Šī komanda ļauj pabeigt uzdevumu spēlētājam.' parameters: ' ' + reset: + description: 'Šī komanda ļauj atiestatīt uzdevumu spēlētājam. Ja "challenge_id" aizstāj ar "all", tad tiek atiestatīti visi uzdevumi.' + parameters: ' ' user: main: description: 'Šī metode atver Uzdevumu logu.' @@ -333,6 +336,9 @@ challenges: complete-wipe: '&cCerams, ka tev ir saglabātas rezerves kopijas, jo tu tikko iztīrīji visas šī papildinājuma datubāzes!' completed: '&2Tu pabeidzi uzdevumu [name] [player] vietā!' already-completed: '&2Šīs uzdevums jau bija izpildīts!' + reset: '&2Tu atiestatīji uzdevumu [name] priekš [player]!' + reset-all: '&2Visi [player] uzdevumi ir atiesatīti!' + not-completed: '&2Šis uzdevums vēl nav izpildīts!' you-completed-challenge: '&2Tu izpildīji [value] &r&2uzdevumu!' you-repeated-challenge: '&2Tu atkārtoji [value] &r&2uzdevumu!' you-repeated-challenge-multiple: '&2Tu atkārtoji [value] &r&2uzdevumu [count] reizes!'