2019-05-01 23:31:36 +02:00
|
|
|
package world.bentobox.challenges.commands;
|
|
|
|
|
|
|
|
|
|
|
|
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.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;
|
2019-08-01 12:19:15 +02:00
|
|
|
import world.bentobox.challenges.utils.Utils;
|
2019-05-01 23:31:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This command allows to complete challenges without a gui.
|
|
|
|
*/
|
|
|
|
public class CompleteChallengeCommand extends CompositeCommand
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Default constructor for Composite Command.
|
|
|
|
* @param addon Challenges addon.
|
|
|
|
* @param cmd Parent Command.
|
|
|
|
*/
|
|
|
|
public CompleteChallengeCommand(Addon addon, CompositeCommand cmd)
|
|
|
|
{
|
|
|
|
super(addon, cmd, "complete");
|
|
|
|
this.addon = (ChallengesAddon) addon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setup()
|
|
|
|
{
|
|
|
|
this.setOnlyPlayer(true);
|
|
|
|
this.setPermission("complete");
|
|
|
|
this.setParametersHelp("challenges.commands.user.complete.parameters");
|
|
|
|
this.setDescription("challenges.commands.user.complete.description");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean execute(User user, String label, List<String> args)
|
|
|
|
{
|
|
|
|
if (args.isEmpty())
|
|
|
|
{
|
|
|
|
user.sendMessage("challenges.errors.no-name");
|
|
|
|
this.showHelp(this, user);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (!args.get(0).isEmpty())
|
|
|
|
{
|
|
|
|
// Add world name back at the start
|
2019-08-01 12:19:15 +02:00
|
|
|
String challengeName = Utils.getGameMode(this.getWorld()) + "_" + args.get(0);
|
2019-05-01 23:31:36 +02:00
|
|
|
Challenge challenge = this.addon.getChallengesManager().getChallenge(challengeName);
|
|
|
|
|
|
|
|
if (challenge != null)
|
|
|
|
{
|
2019-08-28 15:55:18 +02:00
|
|
|
int count = args.size() == 2 ? Integer.valueOf(args.get(1)) : 1;
|
|
|
|
|
|
|
|
boolean canMultipleTimes =
|
|
|
|
user.hasPermission(this.getPermission() + ".multiple");
|
|
|
|
|
|
|
|
if (!canMultipleTimes && count > 1)
|
|
|
|
{
|
|
|
|
user.sendMessage("challenges.error.no-multiple-permission");
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:31:36 +02:00
|
|
|
return TryToComplete.complete(this.addon,
|
|
|
|
user,
|
|
|
|
challenge,
|
|
|
|
this.getWorld(),
|
|
|
|
this.getTopLabel(),
|
2019-06-16 19:43:07 +02:00
|
|
|
this.getPermissionPrefix(),
|
|
|
|
count);
|
2019-05-01 23:31:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
user.sendMessage("challenges.errors.unknown-challenge");
|
|
|
|
this.showHelp(this, user);
|
|
|
|
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.
|
2019-06-23 15:51:49 +02:00
|
|
|
returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream().
|
2019-08-01 12:19:15 +02:00
|
|
|
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).
|
2019-06-23 15:51:49 +02:00
|
|
|
collect(Collectors.toList()));
|
2019-05-01 23:31:36 +02:00
|
|
|
|
|
|
|
break;
|
2019-05-06 11:10:02 +02:00
|
|
|
case 4:
|
|
|
|
// Suggest a number of completions.
|
|
|
|
if (lastString.isEmpty() || lastString.matches("[0-9]*"))
|
|
|
|
{
|
2019-06-23 15:51:49 +02:00
|
|
|
returnList.add("<number>");
|
2019-05-06 11:10:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2019-05-01 23:31:36 +02:00
|
|
|
default:
|
|
|
|
{
|
2019-06-23 15:51:49 +02:00
|
|
|
returnList.add("help");
|
2019-05-01 23:31:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 15:51:49 +02:00
|
|
|
return Optional.of(Util.tabLimit(returnList, lastString));
|
2019-05-01 23:31:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Variables
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variable that holds challenge addon. Single casting.
|
|
|
|
*/
|
|
|
|
private ChallengesAddon addon;
|
|
|
|
}
|