2019-06-16 20:21:25 +02:00
|
|
|
package world.bentobox.challenges.commands.admin;
|
|
|
|
|
|
|
|
|
2019-10-10 07:35:25 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.UUID;
|
2019-06-16 20:21:25 +02:00
|
|
|
|
|
|
|
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;
|
2021-09-19 20:04:09 +02:00
|
|
|
import world.bentobox.challenges.utils.Constants;
|
2019-08-01 12:19:15 +02:00
|
|
|
import world.bentobox.challenges.utils.Utils;
|
2019-06-16 20:21:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-12-01 10:30:02 +01:00
|
|
|
* This command allows completing challenges without a gui.
|
2019-06-16 20:21:25 +02:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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())
|
|
|
|
{
|
2023-03-31 10:12:08 +02:00
|
|
|
Utils.sendMessage(user, this.getWorld(), Constants.ERRORS + "no-name");
|
2019-06-16 20:21:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.addon.logError("Missing parameters");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (args.size() < 2)
|
|
|
|
{
|
|
|
|
if (user.isPlayer())
|
|
|
|
{
|
2023-03-31 10:12:08 +02:00
|
|
|
Utils.sendMessage(user, this.getWorld(), Constants.ERRORS + "missing-arguments");
|
2019-06-16 20:21:25 +02:00
|
|
|
}
|
|
|
|
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())
|
|
|
|
{
|
2023-03-31 10:12:08 +02:00
|
|
|
Utils.sendMessage(user,
|
|
|
|
this.getWorld(),
|
|
|
|
"general.errors.unknown-player",
|
2019-06-16 20:21:25 +02:00
|
|
|
TextVariables.NAME,
|
2023-03-31 10:12:08 +02:00
|
|
|
args.get(0));
|
2019-06-16 20:21:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-26 06:55:22 +02:00
|
|
|
this.addon.logError("Unknown player name " + args.get(0));
|
2019-06-16 20:21:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add world name back at the start
|
2019-08-01 12:19:15 +02:00
|
|
|
String challengeName = Utils.getGameMode(this.getWorld()) + "_" + args.get(1);
|
2019-06-16 20:21:25 +02:00
|
|
|
Challenge challenge = this.addon.getChallengesManager().getChallenge(challengeName);
|
2021-12-01 10:30:02 +01:00
|
|
|
User target = User.getInstance(targetUUID);
|
2019-06-16 20:21:25 +02:00
|
|
|
|
2022-04-17 00:54:49 +02:00
|
|
|
if (challenge != null)
|
2019-06-16 20:21:25 +02:00
|
|
|
{
|
|
|
|
if (!this.addon.getChallengesManager().isChallengeComplete(targetUUID, this.getWorld(), challenge))
|
|
|
|
{
|
|
|
|
this.addon.getChallengesManager().setChallengeComplete(
|
|
|
|
targetUUID, this.getWorld(), challenge, user.getUniqueId());
|
|
|
|
|
2021-12-01 10:30:02 +01:00
|
|
|
|
2019-06-16 20:21:25 +02:00
|
|
|
if (user.isPlayer())
|
|
|
|
{
|
2023-03-31 10:12:08 +02:00
|
|
|
Utils.sendMessage(user,
|
|
|
|
this.getWorld(),
|
|
|
|
Constants.MESSAGES + "completed",
|
2021-09-19 20:04:09 +02:00
|
|
|
Constants.PARAMETER_NAME, challenge.getFriendlyName(),
|
2023-03-31 10:12:08 +02:00
|
|
|
Constants.PARAMETER_PLAYER, target.getName());
|
2019-06-16 20:21:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.addon.log("Challenge " + challenge.getFriendlyName() + " completed for player " +
|
2021-12-01 10:30:02 +01:00
|
|
|
target.getName());
|
2019-06-16 20:21:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (user.isPlayer())
|
|
|
|
{
|
2023-03-31 10:12:08 +02:00
|
|
|
Utils.sendMessage(user,
|
|
|
|
this.getWorld(),
|
|
|
|
Constants.MESSAGES + "already-completed");
|
2019-06-16 20:21:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.addon.log("Challenge was already completed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (user.isPlayer())
|
|
|
|
{
|
2023-03-31 10:12:08 +02:00
|
|
|
Utils.sendMessage(user,
|
|
|
|
this.getWorld(),
|
|
|
|
Constants.ERRORS + "unknown-challenge");
|
2019-06-16 20:21:25 +02:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2021-12-01 10:30:02 +01:00
|
|
|
case 3 ->
|
2019-06-16 20:21:25 +02:00
|
|
|
// Create suggestions with all challenges that is available for users.
|
|
|
|
returnList.addAll(Util.getOnlinePlayerList(user));
|
2021-12-01 10:30:02 +01:00
|
|
|
case 4 ->
|
2019-06-16 20:21:25 +02:00
|
|
|
// 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().
|
2022-04-17 00:54:49 +02:00
|
|
|
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).toList());
|
2021-12-01 10:30:02 +01:00
|
|
|
default ->
|
2022-04-17 00:54:49 +02:00
|
|
|
returnList.add("help");
|
2019-06-16 20:21:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Optional.of(Util.tabLimit(returnList, lastString));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Variables
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variable that holds challenge addon. Single casting.
|
|
|
|
*/
|
2021-09-19 14:36:52 +02:00
|
|
|
private final ChallengesAddon addon;
|
2019-06-16 20:21:25 +02:00
|
|
|
}
|