Add ability for admins to complete challenge without gui. (#136)

This commit is contained in:
BONNe 2019-06-16 21:21:25 +03:00
parent ca5ff504f0
commit 4527216dc2
4 changed files with 223 additions and 0 deletions

View File

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

View File

@ -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<String> 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<List<String>> tabComplete(User user, String alias, List<String> args)
{
String lastString = args.get(args.size() - 1);
final List<String> 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<String> challenges;
}

View File

@ -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: '<player> <challenge_id>'
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:

View File

@ -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: '<player> <challenge_id>'
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: