From 4527216dc2992417c6475e068f7d8632b1f66bb9 Mon Sep 17 00:00:00 2001 From: BONNe Date: Sun, 16 Jun 2019 21:21:25 +0300 Subject: [PATCH] Add ability for admins to complete challenge without gui. (#136) --- .../challenges/commands/admin/Challenges.java | 3 + .../commands/admin/CompleteCommand.java | 208 ++++++++++++++++++ src/main/resources/locales/en-US.yml | 6 + src/main/resources/locales/lv-LV.yml | 6 + 4 files changed, 223 insertions(+) create mode 100644 src/main/java/world/bentobox/challenges/commands/admin/CompleteCommand.java 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 49c732c..529b035 100644 --- a/src/main/java/world/bentobox/challenges/commands/admin/Challenges.java +++ b/src/main/java/world/bentobox/challenges/commands/admin/Challenges.java @@ -37,6 +37,9 @@ public class Challenges extends CompositeCommand new ImportCommand(getAddon(), this); // Defaults processing command new DefaultsCommand(this.getAddon(), this); + + // Defaults processing command + new CompleteCommand(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 new file mode 100644 index 0000000..c64dc79 --- /dev/null +++ b/src/main/java/world/bentobox/challenges/commands/admin/CompleteCommand.java @@ -0,0 +1,208 @@ +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 complete challenges without a gui. + */ +public class CompleteCommand extends CompositeCommand +{ + /** + * Default constructor for Composite Command. + * @param addon Challenges addon. + * @param cmd Parent Command. + */ + public CompleteCommand(Addon addon, CompositeCommand cmd) + { + 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()); + } + } + + + /** + * {@inheritDoc} + */ + @Override + public void setup() + { + this.setPermission("complete"); + this.setParametersHelp("challenges.commands.admin.complete.parameters"); + this.setDescription("challenges.commands.admin.complete.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 + 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().setChallengeComplete( + targetUUID, this.getWorld(), challenge, user.getUniqueId()); + + if (user.isPlayer()) + { + user.sendMessage("challenges.messages.admin.completed", + "[name]", challenge.getFriendlyName(), + "[player]", User.getInstance(targetUUID).getName()); + } + else + { + this.addon.log("Challenge " + challenge.getFriendlyName() + " completed for player " + + User.getInstance(targetUUID).getName()); + } + } + else + { + if (user.isPlayer()) + { + user.sendMessage("challenges.messages.admin.already-completed"); + } + else + { + this.addon.log("Challenge was already completed"); + } + } + + 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. + this.challenges.forEach(challenge -> { + returnList.addAll(Collections.singletonList(challenge)); + }); + + 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; + + /** + * This list contains all challenge IDs without a world name. + */ + private List challenges; +} diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 3583c4c..71904a1 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -30,6 +30,9 @@ challenges: defaults-generate: description: 'This method allows to export existing challenges into default.json file.' parameters: '[overwrite] - allows to overwrite existing file.' + complete: + description: 'This command allows to complete challenge for player without GUI.' + parameters: ' ' user: main: description: 'This method opens Challenges GUI.' @@ -330,6 +333,8 @@ 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]!' + already-completed: '&2This challenge was already completed!' 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!' @@ -376,6 +381,7 @@ challenges: no-challenges: '&cChallenges are not implemented in current world!' no-challenges-admin: '&cChallenges are not implemented in current world! You should use &5/[label] challenges &cto adding them!' missing-level: '&cChallenge Level [level] is not defined in database. It may case some errors!' + missing-arguments: '&cCommand is missing arguments.' protection: flags: CHALLENGES_ISLAND_PROTECTION: diff --git a/src/main/resources/locales/lv-LV.yml b/src/main/resources/locales/lv-LV.yml index 2aef42d..cc80c3b 100755 --- a/src/main/resources/locales/lv-LV.yml +++ b/src/main/resources/locales/lv-LV.yml @@ -30,6 +30,9 @@ challenges: defaults-generate: description: 'Šī metode izveidos failu defaults.json, kas saturēs šīs pasaules uzdevumus un līmeņus.|Parametrs overwrite ļauj pārrakstīt pāri esošajam failam.' parameters: '[overwrite]' + complete: + description: 'Šī komanda ļauj pabeigt uzdevumu spēlētājam.' + parameters: ' ' user: main: description: 'Šī metode atver Uzdevumu logu.' @@ -328,6 +331,8 @@ challenges: admin: challenge-created: '[challenge]&r izveidots!' 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!' 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!' @@ -374,6 +379,7 @@ challenges: no-challenges: '&cŠajā pasaulē nav izveidoti uzdevumi!' no-challenges-admin: '&cŠajā pasaulē nav izveidoti uzdevumi! Izmanot komandu &5/[label] challenges&c, lai tos pievienotu!' missing-level: '&cLīmenis [level] nav definēts datubāzē. Tas var radīt problēmas!' + missing-arguments: '&cKomandai trūkst parametri.' protection: flags: CHALLENGES_ISLAND_PROTECTION: