Skip to content

Commit

Permalink
Implement Challenge Complete method. (#15)
Browse files Browse the repository at this point in the history
Method currently will be available only via /[gamemode] challenges complete <challenge_id>.
  • Loading branch information
BONNe committed May 1, 2019
1 parent a2f0765 commit c702dd4
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public boolean execute(User user, String label, List<String> args)
public void setup()
{
this.setPermission(CHALLENGE_COMMAND);
this.setParametersHelp("challenges.commands.user.parameters");
this.setDescription("challenges.commands.user.description");
this.setParametersHelp("challenges.commands.user.main.parameters");
this.setDescription("challenges.commands.user.main.description");

new CompleteChallengeCommand(this.getAddon(), this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public void setup()
{
this.setOnlyPlayer(true);
this.setPermission("challenges");
this.setParametersHelp("challenges.commands.user.parameters");
this.setDescription("challenges.commands.user.description");
this.setParametersHelp("challenges.commands.user.main.parameters");
this.setDescription("challenges.commands.user.main.description");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
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;


/**
* 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;

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.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
String challengeName = Util.getWorld(this.getWorld()).getName() + "_" + args.get(0);
Challenge challenge = this.addon.getChallengesManager().getChallenge(challengeName);

if (challenge != null)
{
return TryToComplete.complete(this.addon,
user,
challenge,
this.getWorld(),
this.getTopLabel(),
this.getPermissionPrefix());
}
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.

this.challenges.forEach(challenge -> {
returnList.addAll(Util.tabLimit(Collections.singletonList(challenge), lastString));
});

break;
// TODO: not implemented YET
// case 4:
// // Suggest a number of completions.
// if (lastString.isEmpty() || lastString.matches("[0-9]*"))
// {
// returnList.addAll(Util.tabLimit(Collections.singletonList("<number>"), lastString));
// }
//
// break;
default:
{
returnList.addAll(Util.tabLimit(Collections.singletonList("help"), lastString));
break;
}
}

return Optional.of(returnList);
}


// ---------------------------------------------------------------------
// 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;
}
8 changes: 6 additions & 2 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ challenges:
description: 'This method allows to export existing challenges into default.json file.'
parameters: '[overwrite] - allows to overwrite existing file.'
user:
description: 'This method opens Challenges GUI.'
parameters: ''
main:
description: 'This method opens Challenges GUI.'
parameters: ''
complete:
description: 'This method allows to complete challenge without GUI.'
parameters: '<challenge_id>'
gui:
title:
admin:
Expand Down

0 comments on commit c702dd4

Please sign in to comment.